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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace ProductivityApiUnitTests
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Internal;
using System.Data.Entity.Internal.Linq;
using System.Data.Entity.Resources;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Moq;
using Xunit;
/// <summary>
/// Unit tests for data binding and DbSet.Local.
/// </summary>
public class DatabindingTests : TestBase
{
#region Test subjects
private class ListElement
{
public ListElement()
{
}
public ListElement(int i)
{
Int = i;
NullableInt = i;
String = i.ToString();
XNode = new XText(i.ToString());
Random = new Random();
ByteArray = new[] { (byte)i, (byte)i, (byte)i, (byte)i };
}
public static implicit operator ListElement(int i)
{
return new ListElement(i);
}
public int Int { get; set; }
public int? NullableInt { get; set; }
public string String { get; set; }
public XNode XNode { get; set; }
public Random Random { get; set; }
public byte[] ByteArray { get; set; }
public static PropertyDescriptor Property(string name)
{
return TypeDescriptor.GetProperties(typeof(ListElement))[name];
}
}
private class DerivedListElement : ListElement
{
public DerivedListElement()
{
}
public DerivedListElement(int i)
: base(i)
{
}
}
private class ListElementComparer : IEqualityComparer<ListElement>
{
public bool Equals(ListElement x, ListElement y)
{
return x.Int == y.Int;
}
public int GetHashCode(ListElement obj)
{
return obj.Int;
}
}
#endregion
#region SortableBindingList tests
private void SortTest(string property, ListSortDirection direction)
{
var list = new List<ListElement>
{
3,
1,
4,
1,
5,
9
};
var sortedList = direction == ListSortDirection.Ascending
? new List<ListElement>
{
1,
1,
3,
4,
5,
9
}
: new List<ListElement>
{
9,
5,
4,
3,
1,
1
};
var bindingList = new SortableBindingList<ListElement>(list);
((IBindingList)bindingList).ApplySort(ListElement.Property(property), direction);
Assert.True(list.SequenceEqual(sortedList, new ListElementComparer()));
}
[Fact]
public void SortableBindingList_can_sort_ascending_using_IComparable_on_value_type()
{
SortTest("Int", ListSortDirection.Ascending);
}
[Fact]
public void SortableBindingList_can_sort_ascending_using_IComparable_on_nullable_value_type()
{
SortTest("NullableInt", ListSortDirection.Ascending);
}
[Fact]
public void SortableBindingList_can_sort_ascending_using_IComparable_on_reference_type()
{
SortTest("String", ListSortDirection.Ascending);
}
[Fact]
public void SortableBindingList_can_sort_descending_using_IComparable_on_value_type()
{
SortTest("Int", ListSortDirection.Descending);
}
[Fact]
public void SortableBindingList_can_sort_descending_using_IComparable_on_nullable_value_type()
{
SortTest("NullableInt", ListSortDirection.Descending);
}
[Fact]
public void SortableBindingList_can_sort_descending_using_IComparable_on_reference_type()
{
SortTest("String", ListSortDirection.Descending);
}
[Fact]
public void SortableBindingList_can_sort_ascending_for_XNode_using_ToString()
{
SortTest("XNode", ListSortDirection.Ascending);
}
[Fact]
public void SortableBindingList_can_sort_descending_for_XNode_using_ToString()
{
SortTest("XNode", ListSortDirection.Descending);
}
[Fact]
public void SortableBindingList_does_not_sort_for_non_XNode_that_does_not_implement_IComparable()
{
var list = new List<ListElement>
{
3,
1,
4,
1,
5,
9
};
var unsortedList = new List<ListElement>
{
3,
1,
4,
1,
5,
9
};
var bindingList = new SortableBindingList<ListElement>(list);
((IBindingList)bindingList).ApplySort(ListElement.Property("Random"), ListSortDirection.Ascending);
Assert.True(list.SequenceEqual(unsortedList, new ListElementComparer()));
}
[Fact]
public void SortableBindingList_does_not_sort_for_byte_arrays()
{
var list = new List<ListElement>
{
3,
1,
4,
1,
5,
9
};
var unsortedList = new List<ListElement>
{
3,
1,
4,
1,
5,
9
};
var bindingList = new SortableBindingList<ListElement>(list);
((IBindingList)bindingList).ApplySort(ListElement.Property("ByteArray"), ListSortDirection.Descending);
Assert.True(list.SequenceEqual(unsortedList, new ListElementComparer()));
}
[Fact]
public void SortableBindingList_can_sort_when_list_contains_derived_objects()
{
var list = new List<ListElement>
{
new DerivedListElement(3),
new DerivedListElement(1),
new DerivedListElement(4)
};
var sortedList = new List<ListElement>
{
new DerivedListElement(1),
new DerivedListElement(3),
new DerivedListElement(4)
};
var bindingList = new SortableBindingList<ListElement>(list);
((IBindingList)bindingList).ApplySort(ListElement.Property("Int"), ListSortDirection.Ascending);
Assert.True(list.SequenceEqual(sortedList, new ListElementComparer()));
}
[Fact]
public void SortableBindingList_can_sort_when_list_is_of_derived_type()
{
var list = new List<DerivedListElement>
{
new DerivedListElement(3),
new DerivedListElement(1),
new DerivedListElement(4)
};
var sortedList = new List<DerivedListElement>
{
new DerivedListElement(1),
new DerivedListElement(3),
new DerivedListElement(4)
};
var bindingList = new SortableBindingList<DerivedListElement>(list);
((IBindingList)bindingList).ApplySort(ListElement.Property("Int"), ListSortDirection.Ascending);
Assert.True(list.SequenceEqual(sortedList, new ListElementComparer()));
}
#endregion
#region ObservableBackedBindingList tests
[Fact]
public void Items_added_to_ObservableCollection_are_added_to_binding_list()
{
var oc = new ObservableCollection<ListElement>();
var obbl = new ObservableBackedBindingList<ListElement>(oc);
var item = new ListElement(1);
oc.Add(item);
Assert.True(obbl.Contains(item));
}
[Fact]
public void Items_removed_from_ObservableCollection_are_removed_from_binding_list()
{
var item = new ListElement(4);
var oc = new ObservableCollection<ListElement>
{
3,
1,
item,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
oc.Remove(item);
Assert.False(obbl.Contains(item));
Assert.Equal(5, obbl.Count);
}
[Fact]
public void Items_replaced_in_the_ObservableCollection_are_replaced_in_the_binding_list()
{
var item = new ListElement(4);
var oc = new ObservableCollection<ListElement>
{
3,
1,
item,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
var newItem = new ListElement(-4);
oc[2] = newItem;
Assert.False(obbl.Contains(item));
Assert.True(obbl.Contains(newItem));
Assert.Equal(6, obbl.Count);
}
[Fact]
public void Items_cleared_in_the_ObservableCollection_are_cleared_in_the_binding_list()
{
var oc = new ObservableCollection<ListElement>
{
3,
1,
4,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
oc.Clear();
Assert.Equal(0, obbl.Count);
}
[Fact]
public void Adding_duplicate_item_to_the_ObservableCollection_adds_duplicate_to_the_binding_list()
{
var item = new ListElement(4);
var oc = new ObservableCollection<ListElement>
{
3,
1,
item,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
oc.Add(item);
Assert.Equal(7, obbl.Count);
Assert.Equal(2, obbl.Count(i => ReferenceEquals(i, item)));
}
[Fact]
public void Items_added_to_the_binding_list_are_added_to_the_ObservableCollection()
{
var oc = new ObservableCollection<ListElement>();
var obbl = new ObservableBackedBindingList<ListElement>(oc);
var item = new ListElement(7);
obbl.Add(item);
Assert.True(oc.Contains(item));
}
[Fact]
public void Items_added_to_the_binding_list_with_AddNew_are_added_to_the_ObservableCollection()
{
var oc = new ObservableCollection<ListElement>();
var obbl = new ObservableBackedBindingList<ListElement>(oc);
var item = obbl.AddNew();
obbl.EndNew(0);
Assert.True(oc.Contains(item));
}
[Fact]
public void Items_canceled_during_AddNew_are_not_added_to_the_ObservableCollection()
{
var oc = new ObservableCollection<ListElement>();
var obbl = new ObservableBackedBindingList<ListElement>(oc);
var item = obbl.AddNew();
obbl.CancelNew(0);
Assert.False(oc.Contains(item));
}
[Fact]
public void Items_inserted_into_the_binding_list_are_added_to_the_ObservableCollection()
{
var oc = new ObservableCollection<ListElement>();
var obbl = new ObservableBackedBindingList<ListElement>(oc);
var item = new ListElement(7);
obbl.Insert(0, item);
Assert.True(oc.Contains(item));
}
[Fact]
public void Items_set_in_the_binding_list_are_replaced_in_the_ObservableCollection()
{
var item = new ListElement(4);
var oc = new ObservableCollection<ListElement>
{
3,
1,
item,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
var newItem = new ListElement(7);
obbl[2] = newItem;
Assert.True(oc.Contains(newItem));
Assert.False(oc.Contains(item));
}
[Fact]
public void Items_removed_from_the_binding_list_are_removed_from_the_ObservableCollection()
{
var item = new ListElement(4);
var oc = new ObservableCollection<ListElement>
{
3,
1,
item,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
obbl.Remove(item);
Assert.False(oc.Contains(item));
}
[Fact]
public void Items_removed_by_index_from_the_binding_list_are_removed_from_the_ObservableCollection()
{
var item = new ListElement(4);
var oc = new ObservableCollection<ListElement>
{
3,
1,
item,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
obbl.RemoveAt(2);
Assert.False(oc.Contains(item));
}
[Fact]
public void Items_cleared_from_the_binding_list_are_cleared_from_the_ObservableCollection()
{
var oc = new ObservableCollection<ListElement>
{
3,
1,
4,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
obbl.Clear();
Assert.Equal(0, oc.Count);
}
[Fact]
public void Adding_duplicate_item_to_the_binding_list_adds_duplicate_to_the_ObservableCollection()
{
var item = new ListElement(4);
var oc = new ObservableCollection<ListElement>
{
3,
1,
item,
1,
5,
9
};
var obbl = new ObservableBackedBindingList<ListElement>(oc);
obbl.Add(item);
Assert.Equal(7, oc.Count);
Assert.Equal(2, oc.Count(i => ReferenceEquals(i, item)));
}
[Fact]
public void Attempt_to_AddNew_for_abstract_type_works_if_AddingNew_event_is_used_to_create_new_object()
{
var obbl = new ObservableBackedBindingList<XNode>(new ObservableCollection<XNode>());
var item = new XText("Some Value");
obbl.AddingNew += (s, e) => e.NewObject = item;
obbl.AddNew();
obbl.EndNew(0);
Assert.True(obbl.Contains(item));
}
[Fact]
public void Attempt_to_AddNew_for_abstract_type_throws_if_AddingNew_event_is_not_used()
{
var obbl = new ObservableBackedBindingList<XNode>(new ObservableCollection<XNode>());
const BindingFlags bindingAttr = BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
Assert.Equal(
GenerateException(() => Activator.CreateInstance(typeof(XNode), bindingAttr, null, null, null)).Message,
Assert.Throws<MissingMethodException>(() => obbl.AddNew()).Message);
}
[Fact]
public void Attempt_to_AddNew_for_type_without_parameterless_constructor_works_if_AddingNew_event_is_used_to_create_new_object()
{
var obbl = new ObservableBackedBindingList<XText>(new ObservableCollection<XText>());
var item = new XText("Some Value");
obbl.AddingNew += (s, e) => e.NewObject = item;
obbl.AddNew();
obbl.EndNew(0);
Assert.True(obbl.Contains(item));
}
[Fact]
public void Attempt_to_AddNew_for_type_without_parameterless_constructor_throws_if_AddingNew_event_is_not_used()
{
var obbl = new ObservableBackedBindingList<XText>(new ObservableCollection<XText>());
const BindingFlags bindingAttr = BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
Assert.Equal(
GenerateException(() => Activator.CreateInstance(typeof(XText), bindingAttr, null, null, null)).Message,
Assert.Throws<MissingMethodException>(() => obbl.AddNew()).Message);
}
#endregion
#region DbLocalView tests
private Mock<InternalContextForMock> CreateMockedInternalContext(
Mock<IDbSet<FakeEntity>> mockDbSet, IList<FakeEntity> entities = null)
{
entities = entities ?? new List<FakeEntity>();
var mockInternalContext = new Mock<InternalContextForMock>();
mockInternalContext.Setup(i => i.GetLocalEntities<FakeEntity>()).Returns(entities);
mockInternalContext.Setup(i => i.Set<FakeEntity>()).Returns(mockDbSet.Object);
mockInternalContext.Setup(i => i.EntityInContextAndNotDeleted(It.Is<FakeEntity>(e => entities.Contains(e))));
return mockInternalContext;
}
private DbLocalView<FakeEntity> CreateLocalView(Mock<IDbSet<FakeEntity>> mockDbSet, IList<FakeEntity> entities = null)
{
return new DbLocalView<FakeEntity>(CreateMockedInternalContext(mockDbSet, entities).Object);
}
[Fact]
public void DbLocalView_is_initialized_with_entities_from_the_context()
{
var entities = new List<FakeEntity>
{
new FakeEntity(),
new FakeEntity()
};
var localView = CreateLocalView(new Mock<IDbSet<FakeEntity>>(), entities);
Assert.Equal(2, localView.Count);
Assert.True(localView.Contains(entities[0]));
Assert.True(localView.Contains(entities[1]));
}
[Fact]
public void Adding_entity_to_DbLocalView_adds_entity_to_DbSet()
{
var mockDbSet = new Mock<IDbSet<FakeEntity>>();
var localView = CreateLocalView(mockDbSet);
var entity = new FakeEntity();
localView.Add(entity);
mockDbSet.Verify(s => s.Add(entity), Times.Once());
}
[Fact]
public void Removing_entity_from_DbLocalView_removes_entity_from_DbSet()
{
var entity = new FakeEntity();
var mockDbSet = new Mock<IDbSet<FakeEntity>>();
var localView = CreateLocalView(
mockDbSet, new List<FakeEntity>
{
entity
});
localView.Remove(entity);
mockDbSet.Verify(s => s.Remove(entity), Times.Once());
}
[Fact]
public void Replacing_entity_in_DbLocalView_adds_an_entity_and_removes_an_entity_from_DbSet()
{
var entity = new FakeEntity();
var mockDbSet = new Mock<IDbSet<FakeEntity>>();
var localView = CreateLocalView(
mockDbSet, new List<FakeEntity>
{
entity
});
var newEntity = new FakeEntity();
localView[0] = newEntity;
mockDbSet.Verify(s => s.Remove(entity), Times.Once());
mockDbSet.Verify(s => s.Add(newEntity), Times.Once());
}
[Fact]
public void Moving_an_entity_in_DbLocalView_is_ignored()
{
var entities = new List<FakeEntity>
{
new FakeEntity(),
new FakeEntity()
};
var mockDbSet = new Mock<IDbSet<FakeEntity>>();
var localView = CreateLocalView(mockDbSet, entities);
localView.Move(0, 1);
mockDbSet.Verify(s => s.Remove(It.IsAny<FakeEntity>()), Times.Never());
mockDbSet.Verify(s => s.Add(It.IsAny<FakeEntity>()), Times.Never());
}
[Fact]
public void Adding_entity_to_DbLocalView_that_is_already_in_state_manager_is_ignored()
{
var entity = new FakeEntity();
var mockDbSet = new Mock<IDbSet<FakeEntity>>();
var localView = CreateLocalView(
mockDbSet, new List<FakeEntity>
{
entity
});
localView.Add(entity);
mockDbSet.Verify(s => s.Add(It.IsAny<FakeEntity>()), Times.Never());
}
[Fact]
public void BindingList_obtained_from_DbLocalView_is_cached()
{
var localView = CreateLocalView(new Mock<IDbSet<FakeEntity>>());
var bindingList = localView.BindingList;
Assert.NotNull(bindingList);
var bindingListAgain = localView.BindingList;
Assert.Same(bindingList, bindingListAgain);
}
[Fact]
public void BindingList_obtaibed_from_DbLocalView_stays_in_sync_with_the_local_view()
{
var entities = new List<FakeEntity>
{
new FakeEntity(),
new FakeEntity()
};
var localView = CreateLocalView(new Mock<IDbSet<FakeEntity>>(), entities);
var bindingList = localView.BindingList;
Assert.Equal(2, bindingList.Count);
localView.Add(new FakeEntity());
Assert.Equal(3, bindingList.Count);
localView.Remove(entities[0]);
Assert.Equal(2, bindingList.Count);
}
[Fact]
public void DbLocalView_stays_in_sync_with_BindingList_obtaibed_from_it()
{
var entities = new List<FakeEntity>
{
new FakeEntity(),
new FakeEntity()
};
var localView = CreateLocalView(new Mock<IDbSet<FakeEntity>>(), entities);
var bindingList = localView.BindingList;
Assert.Equal(2, bindingList.Count);
bindingList.Add(new FakeEntity());
Assert.Equal(3, localView.Count);
bindingList.Remove(entities[0]);
Assert.Equal(2, localView.Count);
}
[Fact]
public void Clear_on_DbLocalView_removes_all_items_from_DbSet()
{
var entities = new List<FakeEntity>
{
new FakeEntity(),
new FakeEntity()
};
var mockDbSet = new Mock<IDbSet<FakeEntity>>();
var localView = CreateLocalView(mockDbSet, entities);
localView.Clear();
Assert.Equal(0, localView.Count);
mockDbSet.Verify(s => s.Remove(It.IsAny<FakeEntity>()), Times.Exactly(2));
}
[Fact]
public void Attempted_adds_of_duplicates_to_DbLocalView_are_ignored()
{
var entities = new List<FakeEntity>
{
new FakeEntity()
};
var mockDbSet = new Mock<IDbSet<FakeEntity>>();
var localView = CreateLocalView(mockDbSet, entities);
localView.Add(entities[0]);
Assert.Equal(1, localView.Count);
mockDbSet.Verify(s => s.Add(It.IsAny<FakeEntity>()), Times.Never());
}
[Fact]
public void State_manager_Remove_event_causes_entity_to_be_removed_from_DbLocalView()
{
var entity = new FakeEntity();
var mockInternalContext = CreateMockedInternalContext(
new Mock<IDbSet<FakeEntity>>(), new List<FakeEntity>
{
entity
});
CollectionChangeEventHandler stateManagerChanged = null;
mockInternalContext.Setup(i => i.RegisterObjectStateManagerChangedEvent(It.IsAny<CollectionChangeEventHandler>())).
Callback<CollectionChangeEventHandler>(h => stateManagerChanged = h);
var localView = new DbLocalView<FakeEntity>(mockInternalContext.Object);
stateManagerChanged.Invoke(null, new CollectionChangeEventArgs(CollectionChangeAction.Remove, entity));
Assert.False(localView.Contains(entity));
}
[Fact]
public void State_manager_Remove_event_for_entity_not_in_DbLocalView_is_ignored()
{
var mockInternalContext = CreateMockedInternalContext(new Mock<IDbSet<FakeEntity>>());
CollectionChangeEventHandler stateManagerChanged = null;
mockInternalContext.Setup(i => i.RegisterObjectStateManagerChangedEvent(It.IsAny<CollectionChangeEventHandler>())).
Callback<CollectionChangeEventHandler>(h => stateManagerChanged = h);
new DbLocalView<FakeEntity>(mockInternalContext.Object);
stateManagerChanged.Invoke(null, new CollectionChangeEventArgs(CollectionChangeAction.Remove, new FakeEntity()));
}
[Fact]
public void State_manager_Remove_event_for_entity_of_wrong_type_for_DbLocalView_is_ignored()
{
var mockInternalContext = CreateMockedInternalContext(new Mock<IDbSet<FakeEntity>>());
CollectionChangeEventHandler stateManagerChanged = null;
mockInternalContext.Setup(i => i.RegisterObjectStateManagerChangedEvent(It.IsAny<CollectionChangeEventHandler>())).
Callback<CollectionChangeEventHandler>(h => stateManagerChanged = h);
new DbLocalView<FakeEntity>(mockInternalContext.Object);
stateManagerChanged.Invoke(null, new CollectionChangeEventArgs(CollectionChangeAction.Remove, "Wrong Type"));
}
[Fact]
public void State_manager_Add_event_causes_entity_to_be_added_to_DbLocalView()
{
var mockInternalContext = CreateMockedInternalContext(new Mock<IDbSet<FakeEntity>>());
CollectionChangeEventHandler stateManagerChanged = null;
mockInternalContext.Setup(i => i.RegisterObjectStateManagerChangedEvent(It.IsAny<CollectionChangeEventHandler>())).
Callback<CollectionChangeEventHandler>(h => stateManagerChanged = h);
var localView = new DbLocalView<FakeEntity>(mockInternalContext.Object);
var entity = new FakeEntity();
stateManagerChanged.Invoke(null, new CollectionChangeEventArgs(CollectionChangeAction.Add, entity));
Assert.True(localView.Contains(entity));
}
[Fact]
public void State_manager_Add_event_for_entity_already_in_DbLocalView_is_ignored()
{
var entity = new FakeEntity();
var mockInternalContext = CreateMockedInternalContext(
new Mock<IDbSet<FakeEntity>>(), new List<FakeEntity>
{
entity
});
CollectionChangeEventHandler stateManagerChanged = null;
mockInternalContext.Setup(i => i.RegisterObjectStateManagerChangedEvent(It.IsAny<CollectionChangeEventHandler>())).
Callback<CollectionChangeEventHandler>(h => stateManagerChanged = h);
var localView = new DbLocalView<FakeEntity>(mockInternalContext.Object);
stateManagerChanged.Invoke(null, new CollectionChangeEventArgs(CollectionChangeAction.Add, entity));
Assert.Equal(1, localView.Count);
}
[Fact]
public void State_manager_Add_event_for_entity_of_wrong_type_for_DbLocalView_is_ignored()
{
var mockInternalContext = CreateMockedInternalContext(new Mock<IDbSet<FakeEntity>>());
CollectionChangeEventHandler stateManagerChanged = null;
mockInternalContext.Setup(i => i.RegisterObjectStateManagerChangedEvent(It.IsAny<CollectionChangeEventHandler>())).
Callback<CollectionChangeEventHandler>(h => stateManagerChanged = h);
var localView = new DbLocalView<FakeEntity>(mockInternalContext.Object);
stateManagerChanged.Invoke(null, new CollectionChangeEventArgs(CollectionChangeAction.Add, "Wrong Type"));
Assert.Equal(0, localView.Count);
}
#endregion
#region ToBindingList tests
[Fact]
public void ToBindingList_throws_when_given_null_ObservableCollection()
{
Assert.Equal(
"source",
Assert.Throws<ArgumentNullException>(() => ObservableCollectionExtensions.ToBindingList<FakeEntity>(null)).ParamName);
}
[Fact]
public void ToBindingList_returns_the_cached_BindingList_when_called_with_DbLocalView()
{
var localView = CreateLocalView(new Mock<IDbSet<FakeEntity>>());
var bindingList = localView.ToBindingList();
Assert.NotNull(bindingList);
var bindingListAgain = localView.ToBindingList();
Assert.Same(bindingList, bindingListAgain);
}
[Fact]
public void ToBindingList_returns_a_new_binding_list_each_time_when_called_on_non_DbLocalView_ObervableCollections()
{
var oc = new ObservableCollection<FakeEntity>();
var bindingList = oc.ToBindingList();
Assert.NotNull(bindingList);
var bindingListAgain = oc.ToBindingList();
Assert.NotNull(bindingListAgain);
Assert.NotSame(bindingList, bindingListAgain);
}
#endregion
#region ObservableListSource tests
[Fact]
public void ObservableListSource_exposes_ObervableCollection_parameterless_constructor()
{
var ols = new ObservableListSource<FakeEntity>();
Assert.Equal(0, ols.Count);
}
[Fact]
public void ObservableListSource_exposes_ObervableCollection_IEnumerable_constructor()
{
IEnumerable<FakeEntity> entities = new[] { new FakeEntity(), new FakeEntity() };
var ols = new ObservableListSource<FakeEntity>(entities);
Assert.Equal(2, ols.Count);
}
[Fact]
public void ObservableListSource_exposes_ObervableCollection_List_constructor()
{
var entities = new List<FakeEntity>
{
new FakeEntity(),
new FakeEntity()
};
var ols = new ObservableListSource<FakeEntity>(entities);
Assert.Equal(2, ols.Count);
}
[Fact]
public void ObservableListSource_ContainsListCollection_returns_false()
{
Assert.False(((IListSource)new ObservableListSource<FakeEntity>()).ContainsListCollection);
}
[Fact]
public void ObservableListSource_GetList_returns_BindingList_attached_to_the_ObservableCollection()
{
var ols = new ObservableListSource<FakeEntity>
{
new FakeEntity(),
new FakeEntity()
};
var bindingList = ((IListSource)ols).GetList();
Assert.Equal(2, bindingList.Count);
ols.Add(new FakeEntity());
Assert.Equal(3, bindingList.Count);
ols.Remove(ols[0]);
Assert.Equal(2, bindingList.Count);
bindingList.Add(new FakeEntity());
Assert.Equal(3, ols.Count);
bindingList.RemoveAt(0);
Assert.Equal(2, ols.Count);
}
[Fact]
public void The_BindingList_returned_from_ObservableListSource_GetList_is_cached()
{
var ols = new ObservableListSource<FakeEntity>();
var bindingList = ((IListSource)ols).GetList();
Assert.Same(bindingList, ((IListSource)ols).GetList());
}
#endregion
#region DbQuery as IListSource tests
[Fact]
public void DbQuery_ContainsListCollection_returns_false()
{
var fakeQuery = new DbQuery<FakeEntity>(new Mock<IInternalQuery<FakeEntity>>().Object);
Assert.False(((IListSource)fakeQuery).ContainsListCollection);
}
[Fact]
public void Non_generic_DbQuery_ContainsListCollection_returns_false()
{
var fakeQuery = new InternalDbQuery<FakeEntity>(new Mock<IInternalQuery<FakeEntity>>().Object);
Assert.False(((IListSource)fakeQuery).ContainsListCollection);
}
[Fact]
public void DbQuery_GetList_throws_indicating_that_binding_to_queries_is_not_allowed()
{
var fakeQuery = new DbQuery<FakeEntity>(new Mock<IInternalQuery<FakeEntity>>().Object);
Assert.Equal(
Strings.DbQuery_BindingToDbQueryNotSupported,
Assert.Throws<NotSupportedException>(() => ((IListSource)fakeQuery).GetList()).Message);
}
[Fact]
public void Non_generic_DbQuery_GetList_throws_indicating_that_binding_to_queries_is_not_allowed()
{
var fakeQuery = new InternalDbQuery<FakeEntity>(new Mock<IInternalQuery<FakeEntity>>().Object);
Assert.Equal(
Strings.DbQuery_BindingToDbQueryNotSupported,
Assert.Throws<NotSupportedException>(() => ((IListSource)fakeQuery).GetList()).Message);
}
#endregion
#region Load tests
[Fact]
public void Load_throws_when_given_null_query()
{
Assert.Equal("source", Assert.Throws<ArgumentNullException>(() => IQueryableExtensions.Load(null)).ParamName);
}
[Fact]
public void Load_enumerates_the_query()
{
var count = 0;
var mockEnumerator = new Mock<IEnumerator<FakeEntity>>();
var mockQuery = new Mock<IList<FakeEntity>>();
mockQuery.Setup(q => q.GetEnumerator()).Returns(mockEnumerator.Object);
mockEnumerator.Setup(e => e.MoveNext()).Returns(() => count++ < 5 ? true : false);
mockQuery.Object.AsQueryable().Load();
mockEnumerator.Verify(e => e.MoveNext(), Times.Exactly(6));
}
#endregion
}
}
|