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