Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableLoadRowTest.cs
1 //
2 // DataTableLoadRowTest.cs - NUnit Test Cases for testing the
3 //                          DataTable's LoadRow method
4 // Author:
5 //      Sureshkumar T (tsureshkumar@novell.com)
6 //
7 // Copyright (c) 2004 Novell Inc., and the individuals listed
8 // on the ChangeLog entries.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30
31 using System;
32 using System.Data;
33 using System.Data.SqlClient;
34 using System.Text;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Data
39 {
40         [TestFixture]
41         public class DataTableLoadRowTest
42         {
43                 bool rowChanging;
44                 bool rowChanged;
45                 bool rowDeleting;
46                 bool rowDeleted;
47
48                 DataRow rowInAction_Changing;
49                 DataRowAction rowAction_Changing;
50                 DataRow rowInAction_Changed;
51                 DataRowAction rowAction_Changed;
52                 DataRow rowInAction_Deleting;
53                 DataRowAction rowAction_Deleting;
54                 DataRow rowInAction_Deleted;
55                 DataRowAction rowAction_Deleted;
56
57                 [Test]
58                 public void LoadRowTest ()
59                 {
60                         DataTable dt = new DataTable ();
61                         dt.Columns.Add ("id", typeof (int));
62                         dt.Columns.Add ("name", typeof (string));
63
64                         dt.Rows.Add (new object [] { 1, "mono 1" });
65                         dt.Rows.Add (new object [] { 2, "mono 2" });
66                         dt.Rows.Add (new object [] { 3, "mono 3" });
67
68                         dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
69                         dt.AcceptChanges ();
70
71                         dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.Upsert);
72                         Assert.AreEqual (4, dt.Rows.Count, "#1 has not added a new row");
73                 }
74
75                 [Test]
76                 public void LoadRowTestUpsert ()
77                 {
78                         DataTable dt = new DataTable ();
79                         dt.Columns.Add ("id", typeof (int));
80                         dt.Columns.Add ("name", typeof (string));
81
82                         dt.Rows.Add (new object [] { 1, "mono 1" });
83                         dt.Rows.Add (new object [] { 2, "mono 2" });
84                         dt.Rows.Add (new object [] { 3, "mono 3" });
85
86                         dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
87
88                         dt.AcceptChanges ();
89                         try {
90                                 SubscribeEvents (dt);
91
92                                 ResetEventFlags ();
93                                 dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.Upsert);
94                                 Assert.AreEqual (3, dt.Rows.Count, "#1 should not add a row");
95                                 Assert.AreEqual ("mono test", dt.Rows [1] [1], "#2 should change the current");
96                                 Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#3 should not change original");
97                                 Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#4 should change state");
98                                 Assert.IsTrue (rowChanging, "#ev1 row changing not called");
99                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev2 this row is not intended to change");
100                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev3 row action is not Change");
101                                 Assert.IsTrue (rowChanged, "#ev4 row changed not called");
102                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev5 this row is not intended to change");
103                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev6 row action is not Change");
104                 
105
106                                 // Row State tests
107                                 // current - modified ; result - modified
108                                 ResetEventFlags ();
109                                 dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert);
110                                 Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c1 should change the current");
111                                 Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#c2 should not change original");
112                                 Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c3 should not change state");
113                                 Assert.IsTrue (rowChanging, "#ev11 row changing not called");
114                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev12 this row is not intended to change");
115                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev13 row action is not Change");
116                                 Assert.IsTrue (rowChanged, "#ev14 row changed not called");
117                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev15 this row is not intended to change");
118                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev16 row action is not Change");
119                 
120
121                                 // current - Unchanged; result - Unchanged if no new value
122                                 dt.AcceptChanges ();
123                                 ResetEventFlags ();
124                                 dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert);
125                                 Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c4 should not change the current");
126                                 Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c5 should not change original");
127                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c6 should not change state");
128                                 Assert.IsTrue (rowChanging, "#ev21 row changing not called");
129                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev22 this row is not intended to change");
130                                 Assert.AreEqual (DataRowAction.Nothing, rowAction_Changing, "#ev13 row action is not Nothing");
131                                 Assert.IsTrue (rowChanged, "#ev24 row changed not called");
132                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev25 this row is not intended to change");
133                                 Assert.AreEqual (DataRowAction.Nothing, rowAction_Changed, "#ev26 row action is not Nothing");
134                 
135                                 // not the same value again
136                                 dt.RejectChanges (); 
137                                 ResetEventFlags ();
138                                 dt.LoadDataRow (new object [] { 2, "mono test 3" }, LoadOption.Upsert);
139                                 Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c7 should not change state");
140                                 Assert.IsTrue (rowChanging, "#ev31 row changing not called");
141                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev32 this row is not intended to change");
142                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev33 row action is not Change");
143                                 Assert.IsTrue (rowChanged, "#ev34 row changed not called");
144                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev35 this row is not intended to change");
145                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev36 row action is not Change");
146                 
147
148                                 // current - added; result - added
149                                 dt.Rows.Add (new object [] { 4, "mono 4" });
150                                 ResetEventFlags ();
151                                 dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.Upsert);
152                                 Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#c8 should change the current");
153                                 try {
154                                         object o = dt.Rows [3] [1, DataRowVersion.Original];
155                                         Assert.Fail ("#c9 should have thrown version not found exception");
156                                 } catch (VersionNotFoundException) { }
157                                 Assert.AreEqual (DataRowState.Added, dt.Rows [3].RowState, "#c10 should not change state");
158                                 Assert.IsTrue (rowChanging, "#ev41 row changing not called");
159                                 Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ev42 this row is not intended to change");
160                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev43 row action is not Change");
161                                 Assert.IsTrue (rowChanged, "#ev44 row changed not called");
162                                 Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ev45 this row is not intended to change");
163                                 Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev46 row action is not Change");
164                 
165
166                                 // current - none; result - added
167                                 ResetEventFlags ();
168                                 dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert);
169                                 Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#c11 should change the current");
170                                 try {
171                                         object o = dt.Rows [4] [1, DataRowVersion.Original];
172                                         Assert.Fail ("#c12 should have thrown version not found exception");
173                                 } catch (VersionNotFoundException) { }
174                                 Assert.AreEqual (DataRowState.Added, dt.Rows [4].RowState, "#c13 should change state");
175                                 Assert.IsTrue (rowChanging, "#ev51 row changing not called");
176                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ev52 this row is not intended to change");
177                                 Assert.AreEqual (DataRowAction.Add, rowAction_Changing, "#ev53 row action is not Change");
178                                 Assert.IsTrue (rowChanged, "#ev54 row changed not called");
179                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ev55 this row is not intended to change");
180                                 Assert.AreEqual (DataRowAction.Add, rowAction_Changed, "#ev56 row action is not Change");
181                 
182
183                                 // current - deleted; result - added a new row
184                                 ResetEventFlags ();
185                                 dt.AcceptChanges ();
186                                 dt.Rows [4].Delete ();
187                                 Assert.IsTrue (rowDeleting, "#ev57 row deleting");
188                                 Assert.IsTrue (rowDeleted, "#ev58 row deleted");
189                                 Assert.AreEqual (rowInAction_Deleting, dt.Rows[4], "#ev59 rowInAction_Deleting");
190                                 Assert.AreEqual (rowInAction_Deleted, dt.Rows[4], "#ev59 rowInAction_Deleted");
191                                 Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ev60 rowInAction_Deleting");
192                                 Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ev61 rowInAction_Deleted");
193                                 dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert);
194                                 Assert.AreEqual (6, dt.Rows.Count, "#c14 should not add a row");
195                                 Assert.AreEqual ("mono 5", dt.Rows [5] [1], "#c15 should change the current");
196                                 try {
197                                         object o = dt.Rows [5] [1, DataRowVersion.Original];
198                                         Assert.Fail ("#c16 expected version not found exception ");
199                                 } catch (VersionNotFoundException) { }
200                                 Assert.AreEqual (DataRowState.Added, dt.Rows [5].RowState, "#c17 should change state");
201                                 Assert.IsTrue (rowChanging, "#ev61 row changing not called");
202                                 Assert.AreEqual (dt.Rows [5], rowInAction_Changing, "#ev62 this row is not intended to change");
203                                 Assert.AreEqual (DataRowAction.Add, rowAction_Changing, "#ev63 row action is not Change");
204                                 Assert.IsTrue (rowChanged, "#ev64 row changed not called");
205                                 Assert.AreEqual (dt.Rows [5], rowInAction_Changed, "#ev65 this row is not intended to change");
206                                 Assert.AreEqual (DataRowAction.Add, rowAction_Changed, "#ev66 row action is not Change");
207                 
208                         } finally {
209                                 UnsubscribeEvents (dt);
210                         }
211                 }
212
213                 [Test]
214                 public void LoadRowTestOverwriteChanges ()
215                 {
216                         DataTable dt = new DataTable ();
217                         dt.Columns.Add ("id", typeof (int));
218                         dt.Columns.Add ("name", typeof (string));
219
220                         dt.Rows.Add (new object [] { 1, "mono 1" });
221                         dt.Rows.Add (new object [] { 2, "mono 2" });
222                         dt.Rows.Add (new object [] { 3, "mono 3" });
223
224                         dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
225                         dt.AcceptChanges ();
226
227                         dt.Rows [1] [1] = "overwrite";
228                         Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#1 has not changed the row state");
229
230                         try {
231                                 SubscribeEvents (dt);
232                                 ResetEventFlags ();
233                                 dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.OverwriteChanges);
234                                 Assert.AreEqual (3, dt.Rows.Count, "#2 has not added a new row");
235                                 Assert.AreEqual ("mono test", dt.Rows [1] [1], "#3 should change the current");
236                                 Assert.AreEqual ("mono test", dt.Rows [1] [1, DataRowVersion.Original], "#4 should change the original");
237                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#5 has not changed the row state");
238                                 Assert.IsTrue (rowChanging, "#ltoc11 row changing not called");
239                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltoc12 this row is not intended to change");
240                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc13 row action is not Change");
241                                 Assert.IsTrue (rowChanged, "#ltoc14 row changed not called");
242                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltoc15 this row is not intended to change");
243                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc16 row action is not Change");
244
245                                 DataRow r = dt.Rows [1];
246                                 r [1] = "test";
247                                 Assert.AreEqual ("test", dt.Rows [1] [1], "#6 should change the current");
248                                 Assert.AreEqual ("mono test", dt.Rows [1] [1, DataRowVersion.Original], "#7 should change the original");
249                                 //Assert.AreEqual ("ramesh", dt.Rows [1] [1, DataRowVersion.Proposed], "#8 should change the original");
250
251                                 // Row State tests
252                                 // current - modified ; result - modified
253                                 ResetEventFlags ();
254                                 dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.OverwriteChanges);
255                                 Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c1 should change the current");
256                                 Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c2 should change original");
257                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c3 should not change state");
258                                 Assert.IsTrue (rowChanging, "#ltoc21 row changing not called");
259                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltoc22 this row is not intended to change");
260                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc23 row action is not Change");
261                                 Assert.IsTrue (rowChanged, "#ltoc24 row changed not called");
262                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltoc25 this row is not intended to change");
263                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc26 row action is not Change");
264
265
266                                 // current - Unchanged; result - Unchanged
267                                 dt.AcceptChanges ();
268                                 ResetEventFlags ();
269                                 dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.OverwriteChanges);
270                                 Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c4 should change the current");
271                                 Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c5 should change original");
272                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c6 should not change state");
273                                 Assert.IsTrue (rowChanging, "#ltoc31 row changing not called");
274                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltoc32 this row is not intended to change");
275                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc33 row action is not Change");
276                                 Assert.IsTrue (rowChanged, "#ltoc34 row changed not called");
277                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltoc35 this row is not intended to change");
278                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc36 row action is not Change");
279
280                                 // current - added; result - added
281                                 dt.Rows.Add (new object [] { 4, "mono 4" });
282                                 ResetEventFlags ();
283                                 dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.OverwriteChanges);
284                                 Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#c8 should change the current");
285                                 Assert.AreEqual ("mono 4", dt.Rows [3] [1, DataRowVersion.Original], "#c9 should change the original");
286                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [3].RowState, "#c10 should not change state");
287                                 Assert.IsTrue (rowChanging, "#ltoc41 row changing not called");
288                                 Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ltoc42 this row is not intended to change");
289                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc43 row action is not Change");
290                                 Assert.IsTrue (rowChanged, "#ltoc44 row changed not called");
291                                 Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ltoc45 this row is not intended to change");
292                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc46 row action is not Change");
293
294
295                                 // current - new; result - added
296                                 ResetEventFlags ();
297                                 dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.OverwriteChanges);
298                                 Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#c11 should change the current");
299                                 Assert.AreEqual ("mono 5", dt.Rows [4] [1, DataRowVersion.Original], "#c12 should change original");
300                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [4].RowState, "#c13 should change state");
301                                 Assert.IsTrue (rowChanging, "#ltoc51 row changing not called");
302                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ltoc52 this row is not intended to change");
303                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc53 row action is not Change");
304                                 Assert.IsTrue (rowChanged, "#ltoc54 row changed not called");
305                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ltoc55 this row is not intended to change");
306                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc56 row action is not Change");
307
308
309                                 // current - deleted; result - added a new row
310                                 ResetEventFlags ();
311                                 dt.AcceptChanges ();
312                                 dt.Rows [4].Delete ();
313                                 Assert.IsTrue (rowDeleting, "#ltoc57 row deleting");
314                                 Assert.IsTrue (rowDeleted, "#ltoc58 row deleted");
315                                 Assert.AreEqual (rowInAction_Deleting, dt.Rows[4], "#ltoc59 rowInAction_Deleting");
316                                 Assert.AreEqual (rowInAction_Deleted, dt.Rows[4], "#ltoc60 rowInAction_Deleted");
317                                 Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ltoc60 rowInAction_Deleting");
318                                 Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ltoc61 rowInAction_Deleted");
319                                 dt.LoadDataRow (new object [] { 5, "mono 51" }, LoadOption.OverwriteChanges);
320                                 Assert.AreEqual (5, dt.Rows.Count, "#c14 should not add a row");
321                                 Assert.AreEqual ("mono 51", dt.Rows [4] [1], "#c15 should change the current");
322                                 Assert.AreEqual ("mono 51", dt.Rows [4] [1, DataRowVersion.Original], "#c16 should change the current");
323                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [4].RowState, "#c17 should change state");
324                                 Assert.IsTrue (rowChanging, "#ltoc61 row changing not called");
325                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ltoc62 this row is not intended to change");
326                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltoc63 row action is not Change");
327                                 Assert.IsTrue (rowChanged, "#ltoc64 row changed not called");
328                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ltoc65 this row is not intended to change");
329                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltoc66 row action is not Change");
330
331                         } finally {
332                                 UnsubscribeEvents (dt);
333                         }
334                 }
335
336                 [Test]
337                 public void LoadRowTestPreserveChanges ()
338                 {
339                         DataTable dt = new DataTable ();
340                         dt.Columns.Add ("id", typeof (int));
341                         dt.Columns.Add ("name", typeof (string));
342
343                         dt.Rows.Add (new object [] { 1, "mono 1" });
344                         dt.Rows.Add (new object [] { 2, "mono 2" });
345                         dt.Rows.Add (new object [] { 3, "mono 3" });
346
347                         dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
348                         dt.AcceptChanges ();
349                         try {
350                                 SubscribeEvents (dt);
351
352                                 // current - modified; new - modified
353                                 ResetEventFlags ();
354                                 dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.PreserveChanges);
355                                 Assert.AreEqual (3, dt.Rows.Count, "#1 should not add a new row");
356                                 Assert.AreEqual ("mono test", dt.Rows [1] [1], "#2 should change the current");
357                                 Assert.AreEqual ("mono test", dt.Rows [1] [1, DataRowVersion.Original], "#3 should change the original");
358                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#4 has not changed the row state");
359                                 Assert.IsTrue (rowChanging, "#ltpc11 row changing not called");
360                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltpc12 this row is not intended to change");
361                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltpc13 row action is not Change");
362                                 Assert.IsTrue (rowChanged, "#ltpc14 row changed not called");
363                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltpc15 this row is not intended to change");
364                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltpc16 row action is not Change");
365
366                                 // current - none; new - unchanged
367                                 ResetEventFlags ();
368                                 dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.PreserveChanges);
369                                 Assert.AreEqual (4, dt.Rows.Count,"#5 should add a new row");
370                                 Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#6 should change the current");
371                                 Assert.AreEqual ("mono 4", dt.Rows [3] [1, DataRowVersion.Original], "#7 should change the original");
372                                 Assert.AreEqual (DataRowState.Unchanged, dt.Rows [3].RowState, "#8 has not changed the row state");
373                                 Assert.IsTrue (rowChanging, "#ltpc21 row changing not called");
374                                 Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ltpc22 this row is not intended to change");
375                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltpc23 row action is not Change");
376                                 Assert.IsTrue (rowChanged, "#ltpc24 row changed not called");
377                                 Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ltpc25 this row is not intended to change");
378                                 Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltpc16 row action is not Change");
379
380
381                                 dt.RejectChanges ();
382                 
383                                 // current - added; new - modified
384                                 dt.Rows.Add (new object [] { 5, "mono 5" });
385                                 ResetEventFlags ();
386                                 dt.LoadDataRow (new object [] { 5, "mono test" }, LoadOption.PreserveChanges);
387                                 Assert.AreEqual (5, dt.Rows.Count, "#9 should not add a new row");
388                                 Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#10 should not change the current");
389                                 Assert.AreEqual ("mono test", dt.Rows [4] [1, DataRowVersion.Original], "#11 should change the original");
390                                 Assert.AreEqual (DataRowState.Modified, dt.Rows [4].RowState, "#12 has not changed the row state");
391                                 Assert.IsTrue (rowChanging, "#ltpc31 row changing not called");
392                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ltpc32 this row is not intended to change");
393                                 Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changing, "#ltpc33 row action is not Change");
394                                 Assert.IsTrue (rowChanged, "#ltpc34 row changed not called");
395                                 Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ltpc35 this row is not intended to change");
396                                 Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changed, "#ltpc36 row action is not Change");
397
398
399                                 dt.RejectChanges ();
400
401                                 // current - deleted ; new - deleted ChangeOriginal
402                                 ResetEventFlags ();
403                                 dt.Rows [1].Delete ();
404                                 Assert.IsTrue (rowDeleting, "#ltpc37 row deleting");
405                                 Assert.IsTrue (rowDeleted, "#ltpc38 row deleted");
406                                 Assert.AreEqual (rowInAction_Deleting, dt.Rows[1], "#ltpc39 rowInAction_Deleting");
407                                 Assert.AreEqual (rowInAction_Deleted, dt.Rows[1], "#ltpc40 rowInAction_Deleted");
408                                 Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ltpc60 rowInAction_Deleting");
409                                 Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ltpc61 rowInAction_Deleted");
410                                 dt.LoadDataRow (new object [] { 2, "mono deleted" }, LoadOption.PreserveChanges);
411                                 Assert.AreEqual (5, dt.Rows.Count, "#13 should not add a new row");
412                                 Assert.AreEqual ("mono deleted", dt.Rows [1] [1, DataRowVersion.Original], "#14 should change the original");
413                                 Assert.AreEqual (DataRowState.Deleted, dt.Rows [1].RowState, "#15 has not changed the row state");
414                                 Assert.IsTrue (rowChanging, "#ltpc41 row changing not called");
415                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltpc42 this row is not intended to change");
416                                 Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changing, "#ltoc43 row action is not Change");
417                                 Assert.IsTrue (rowChanged, "#ltpc44 row changed not called");
418                                 Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltpc45 this row is not intended to change");
419                                 Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changed, "#ltpc46 row action is not Change");
420
421
422                         } finally {
423                                 UnsubscribeEvents (dt);
424                         }
425                 }
426
427                 [Test]
428                 public void LoadRowDefaultValueTest ()
429                 {
430                         DataTable dt = new DataTable ();
431                         dt.Columns.Add ("id", typeof (int));
432                         dt.Columns.Add ("age", typeof (int));
433                         dt.Columns.Add ("name", typeof (string));
434
435                         dt.Columns [1].DefaultValue = 20;
436
437                         dt.Rows.Add (new object [] { 1, 15, "mono 1" });
438                         dt.Rows.Add (new object [] { 2, 25, "mono 2" });
439                         dt.Rows.Add (new object [] { 3, 35, "mono 3" });
440
441                         dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
442
443                         dt.AcceptChanges ();
444
445                         dt.LoadDataRow (new object [] { 2, null, "mono test" }, LoadOption.OverwriteChanges);
446                         Assert.AreEqual (3, dt.Rows.Count, "#1 should not have added a row");
447                         Assert.AreEqual (25, dt.Rows [1] [1], "#2 should be default value");
448                         Assert.AreEqual (25, dt.Rows [1] [1, DataRowVersion.Original], "#3 should be default value");
449
450                 }
451
452                 [Test]
453                 public void LoadRowAutoIncrementTest ()
454                 {
455                         DataTable dt = new DataTable ();
456                         dt.Columns.Add ("id", typeof (int));
457                         dt.Columns.Add ("age", typeof (int));
458                         dt.Columns.Add ("name", typeof (string));
459
460                         dt.Columns [0].AutoIncrementSeed = 10;
461                         dt.Columns [0].AutoIncrementStep = 5;
462                         dt.Columns [0].AutoIncrement = true;
463
464                         dt.Rows.Add (new object [] { null, 15, "mono 1" });
465                         dt.Rows.Add (new object [] { null, 25, "mono 2" });
466                         dt.Rows.Add (new object [] { null, 35, "mono 3" });
467
468                         dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] };
469
470                         dt.AcceptChanges ();
471
472                         dt.LoadDataRow (new object [] { null, 20, "mono test" }, LoadOption.OverwriteChanges);
473                         Assert.AreEqual (4, dt.Rows.Count, "#1 has not added a new row");
474                         Assert.AreEqual (25, dt.Rows [3] [0], "#2 current should be ai");
475                         Assert.AreEqual (25, dt.Rows [3] [0, DataRowVersion.Original], "#3 original should be ai");
476
477                         dt.LoadDataRow (new object [] { 25, 20, "mono test" }, LoadOption.Upsert);
478                         dt.LoadDataRow (new object [] { 25, 20, "mono test 2" }, LoadOption.Upsert);
479                         dt.LoadDataRow (new object [] { null, 20, "mono test aaa" }, LoadOption.Upsert);
480
481                         Assert.AreEqual (5, dt.Rows.Count, "#4 has not added a new row");
482                         Assert.AreEqual (25, dt.Rows [3] [0], "#5 current should be ai");
483                         Assert.AreEqual (25, dt.Rows [3] [0, DataRowVersion.Original], "#6 original should be ai");
484
485                         Assert.AreEqual (30, dt.Rows [4] [0], "#7 current should be ai");
486
487                 }
488
489                 public void SubscribeEvents (DataTable dt)
490                 {
491                         dt.RowChanging += new DataRowChangeEventHandler (dt_RowChanging);
492                         dt.RowChanged += new DataRowChangeEventHandler (dt_RowChanged);
493                         dt.RowDeleted += new DataRowChangeEventHandler (dt_RowDeleted);
494                         dt.RowDeleting += new DataRowChangeEventHandler (dt_RowDeleting);
495                         //dt.TableNewRow += new DataTableNewRowEventHandler (dt_TableNewRow);
496                 }
497
498         
499                 public void UnsubscribeEvents (DataTable dt)
500                 {
501                         dt.RowChanging -= new DataRowChangeEventHandler (dt_RowChanging);
502                         dt.RowChanged -= new DataRowChangeEventHandler (dt_RowChanged);
503                         dt.RowDeleted -= new DataRowChangeEventHandler (dt_RowDeleted);
504                         dt.RowDeleting -= new DataRowChangeEventHandler (dt_RowDeleting);
505                         //dt.TableNewRow -= new DataTableNewRowEventHandler (dt_TableNewRow);
506                 }
507
508                 public void ResetEventFlags ()
509                 {
510                         rowChanging = false;
511                         rowChanged = false;
512                         rowDeleting = false;
513                         rowDeleted = false;
514                         rowInAction_Changing = null;
515                         rowAction_Changing = DataRowAction.Nothing;
516                         rowInAction_Changed = null;
517                         rowAction_Changed = DataRowAction.Nothing;
518                         rowInAction_Deleting = null;
519                         rowAction_Deleting = DataRowAction.Nothing;
520                         rowInAction_Deleted = null;
521                         rowAction_Deleted = DataRowAction.Nothing;
522                 }
523
524                 void dt_RowDeleting (object sender, DataRowChangeEventArgs e)
525                 {
526                         rowDeleting = true;
527                         rowInAction_Deleting = e.Row;
528                         rowAction_Deleting = e.Action;
529                 }
530
531                 void dt_RowDeleted (object sender, DataRowChangeEventArgs e)
532                 {
533                         rowDeleted = true;
534                         rowInAction_Deleted = e.Row;
535                         rowAction_Deleted = e.Action;
536                 }
537
538                 void dt_RowChanged (object sender, DataRowChangeEventArgs e)
539                 {
540                         rowChanged = true;
541                         rowInAction_Changed = e.Row;
542                         rowAction_Changed = e.Action;
543                 }
544
545                 void dt_RowChanging (object sender, DataRowChangeEventArgs e)
546                 {
547                         rowChanging = true;
548                         rowInAction_Changing = e.Row;
549                         rowAction_Changing = e.Action;
550                 }
551
552         }
553 }
554