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
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.ModelConfiguration.UnitTests
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Entity.ModelConfiguration.Configuration.Mapping;
using System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation;
using System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive;
using System.Data.Entity.ModelConfiguration.Configuration.Types;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.ModelConfiguration.Edm;
using System.Data.Entity.ModelConfiguration.Utilities;
using System.Data.Entity.Resources;
using System.Data.Entity.Spatial;
using System.Data.Entity.Utilities;
using System.Linq;
using System.Reflection;
using Moq;
using Xunit;
using BinaryPropertyConfiguration = System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.BinaryPropertyConfiguration
;
using DateTimePropertyConfiguration =
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.DateTimePropertyConfiguration;
using DecimalPropertyConfiguration =
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.DecimalPropertyConfiguration;
using StringPropertyConfiguration = System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.StringPropertyConfiguration
;
public sealed class DbModelBuilderTests
{
[Fact]
public void Can_set_default_schema()
{
var modelConfiguration = new ModelConfiguration();
var modelBuilder = new DbModelBuilder(modelConfiguration);
modelBuilder.HasDefaultSchema("foo");
Assert.Equal("foo", modelConfiguration.DefaultSchema);
}
[Fact]
public void Ctor_should_throw_when_version_out_of_range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DbModelBuilder((DbModelBuilderVersion)(-1)));
}
[Fact]
public void Conventions_should_return_non_null_configuration_object()
{
Assert.NotNull(new DbModelBuilder().Conventions);
}
[Fact]
public void Ignore_should_add_to_list_of_ignored_types()
{
var modelConfiguration = new ModelConfiguration();
new DbModelBuilder(modelConfiguration).Ignore<object>();
Assert.True(modelConfiguration.IsIgnoredType(typeof(object)));
}
[Fact]
public void Build_should_validate_and_throw_with_invalid_model()
{
var modelConfiguration = new ModelConfiguration();
modelConfiguration.Entity(new MockType(), true);
Assert.Throws<ModelValidationException>(
() => new DbModelBuilder(modelConfiguration).Build(ProviderRegistry.Sql2008_ProviderInfo));
}
[Fact]
public void Configurations_should_return_non_null_registrar()
{
Assert.NotNull(new DbModelBuilder().Configurations);
}
[Fact]
public void Build_should_throw_when_entity_type_is_not_mappable()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<object>();
Assert.Equal(
Strings.InvalidEntityType(typeof(object)),
Assert.Throws<InvalidOperationException>(() => modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo)).Message);
}
[Fact]
public void Build_should_throw_when_complex_type_is_not_mappable()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.ComplexType<object>();
Assert.Equal(
Strings.CodeFirstInvalidComplexType(typeof(object)),
Assert.Throws<InvalidOperationException>(() => modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo)).Message);
}
[Fact]
public void Build_should_not_throw_when_complex_type_ignored_then_configured()
{
var modelConfiguration = new ModelConfiguration();
var mockType = new MockType();
modelConfiguration.Ignore(mockType);
modelConfiguration.ComplexType(mockType);
var modelBuilder = new DbModelBuilder();
var databaseMapping = modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo).DatabaseMapping;
Assert.Equal(0, databaseMapping.Model.ComplexTypes.Count());
}
[Fact]
public void Build_should_map_types()
{
var modelConfiguration = new ModelConfiguration();
var mockType = new MockType("T").Property<int>("Id");
modelConfiguration.Entity(mockType).Key(mockType.GetProperty("Id"));
modelConfiguration.ComplexType(new MockType("C"));
var modelBuilder = new DbModelBuilder(modelConfiguration);
var databaseMapping = modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo).DatabaseMapping;
Assert.NotNull(databaseMapping);
Assert.Equal(1, databaseMapping.Model.EntityTypes.Count());
Assert.Equal(1, databaseMapping.Model.ComplexTypes.Count());
}
[Fact]
public void Build_should_apply_model_configuration()
{
var modelConfiguration = new ModelConfiguration();
var mockType = new MockType("T").Property<int>("Id");
modelConfiguration.Entity(mockType)
.Property(new PropertyPath(mockType.GetProperty("Id")))
.ConcurrencyMode = ConcurrencyMode.Fixed;
var databaseMapping = new DbModelBuilder(modelConfiguration).Build(ProviderRegistry.Sql2008_ProviderInfo).DatabaseMapping;
Assert.NotNull(databaseMapping);
Assert.Equal(
ConcurrencyMode.Fixed,
databaseMapping.Model.EntityTypes.Single().DeclaredProperties.Single().ConcurrencyMode);
}
[Fact]
public void Mapping_a_single_abstract_type_should_not_throw()
{
var modelConfiguration = new ModelConfiguration();
var mockType = new MockType("T").TypeAttributes(TypeAttributes.Abstract).Property<int>("Id");
modelConfiguration.Entity(mockType).Key(mockType.GetProperty("Id"));
var modelBuilder = new DbModelBuilder();
var databaseMapping = modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo);
Assert.NotNull(databaseMapping);
}
#region Model builder cloning tests
[Fact]
public void Cloning_the_model_builder_clones_contained_types()
{
var builder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
builder.ModelConfiguration.Entity(new MockType(), true);
Assert.Same(builder.ModelConfiguration, builder.ModelConfiguration);
Assert.Same(builder.Conventions, builder.Conventions);
var clone = builder.Clone();
Assert.Equal(DbModelBuilderVersion.V4_1, clone.Version);
Assert.NotSame(builder.ModelConfiguration, clone.ModelConfiguration);
Assert.NotSame(builder.Conventions, clone.Conventions);
Assert.Equal(1, clone.ModelConfiguration.Entities.Count());
Assert.True(clone.Conventions.Conventions.Count() > 0);
}
[Fact]
public void DbModelBuilder_has_expected_number_of_fields()
{
VerifyFieldCount<DbModelBuilder>(4);
}
private void VerifyFieldCount<T>(int expectedCount)
{
const BindingFlags bindingFlags =
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
var actualCount = typeof(T).GetFields(bindingFlags).Count();
if (expectedCount != actualCount)
{
Assert.True(
false,
String.Format(
"The number of fields on {0} was expected to be {1} but is {2}. If a field has been added then make sure it is being properly copied by Clone and then update this test.",
typeof(T).Name, expectedCount, actualCount));
}
}
[Fact]
public void Cloning_the_conventions_configuration_clones_the_list_of_conventions_but_not_the_conventions_themselves()
{
var convention1 = new Mock<IConvention>().Object;
var conventions = new ConventionsConfiguration(
new List<IConvention>
{
convention1
});
Assert.Same(conventions.Conventions, conventions.Conventions);
var clone = conventions.Clone();
var convention2 = new Mock<IConvention>().Object;
conventions.Add(convention2);
Assert.NotSame(conventions.Conventions, clone.Conventions);
Assert.True(clone.Conventions.Contains(convention1));
Assert.False(clone.Conventions.Contains(convention2));
}
[Fact]
public void ConventionsConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ConventionsConfiguration>(1);
}
[Fact]
public void Cloning_the_model_configuration_clones_type_configurations_and_ignored_types()
{
var configuration = new ModelConfiguration();
var entityType1 = new MockType("E1");
var complexType1 = new MockType("C1");
var ignoredType1 = new MockType("I1");
configuration.Add(new EntityTypeConfiguration(entityType1));
configuration.Add(new ComplexTypeConfiguration(complexType1));
configuration.Ignore(ignoredType1);
configuration.DefaultSchema = "Foo";
configuration.ModelNamespace = "Bar";
var clone = configuration.Clone();
Assert.True(clone.Entities.Contains(entityType1));
Assert.True(clone.ComplexTypes.Contains(complexType1));
Assert.True(clone.IsIgnoredType(ignoredType1));
Assert.Equal("Foo", clone.DefaultSchema);
Assert.Equal("Bar", clone.ModelNamespace);
var entityType2 = new MockType("E2");
var complexType2 = new MockType("C2");
var ignoredType2 = new MockType("I2");
configuration.Add(new EntityTypeConfiguration(entityType2));
configuration.Add(new ComplexTypeConfiguration(complexType2));
configuration.Ignore(ignoredType2);
Assert.False(clone.Entities.Contains(entityType2));
Assert.False(clone.ComplexTypes.Contains(complexType2));
Assert.False(clone.IsIgnoredType(ignoredType2));
}
[Fact]
public void ModelConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ModelConfiguration>(5);
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_scalar_properties()
{
var configuration = new EntityTypeConfiguration(typeof(object));
configuration.IsReplaceable = true;
configuration.ToTable("Table");
configuration.IsExplicitEntity = true;
configuration.EntitySetName = "ESN";
var clone = configuration.Clone();
Assert.True(clone.IsReplaceable);
Assert.True(clone.IsTableNameConfigured);
Assert.True(clone.IsExplicitEntity);
Assert.Equal("ESN", clone.EntitySetName);
Assert.Same(typeof(object), clone.ClrType);
}
[Fact]
public void EntityTypeConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<EntityTypeConfiguration>(12);
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_key_information()
{
var configuration = new EntityTypeConfiguration(typeof(object));
var mockPropertyInfo1 = new MockPropertyInfo(typeof(int), "P1");
configuration.Property(new PropertyPath(mockPropertyInfo1)).ColumnOrder = 0;
var mockPropertyInfo2 = new MockPropertyInfo(typeof(int), "P2");
configuration.Property(new PropertyPath(mockPropertyInfo2)).ColumnOrder = 1;
// This will set _isKeyConfigured to true
configuration.Key(
new List<PropertyInfo>
{
mockPropertyInfo1
});
var clone = configuration.Clone();
VerifyKeyProperty(clone, "P1", mockPropertyInfo1, mockPropertyInfo2);
// This should have no effect because _isKeyConfigured is set to true
clone.Key(mockPropertyInfo2);
VerifyKeyProperty(clone, "P1", mockPropertyInfo1, mockPropertyInfo2);
// This should change the key on the original, but not on the clone.
configuration.Key(
new List<PropertyInfo>
{
mockPropertyInfo2
});
VerifyKeyProperty(configuration, "P2", mockPropertyInfo1, mockPropertyInfo2);
VerifyKeyProperty(clone, "P1", mockPropertyInfo1, mockPropertyInfo2);
}
private void VerifyKeyProperty(EntityTypeConfiguration configuration, string expectedKeyName, params PropertyInfo[] props)
{
var entityType = CreateEntityTypeWithProperties(props);
configuration.Configure(entityType, new EdmModel(DataSpace.CSpace));
Assert.Equal(1, entityType.DeclaredKeyProperties.Count);
Assert.Equal(expectedKeyName, entityType.DeclaredKeyProperties.Single().Name);
}
private EntityType CreateEntityTypeWithProperties(params PropertyInfo[] props)
{
var entityType = new EntityType
{
Name = "E"
};
foreach (var prop in props)
{
var property = EdmProperty.Primitive(prop.Name, PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
entityType.AddMember(property);
entityType.GetDeclaredPrimitiveProperties().SingleOrDefault(p => p.Name == prop.Name).SetClrPropertyInfo(prop);
}
return entityType;
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_navigation_property_information()
{
var configuration = new EntityTypeConfiguration(typeof(object));
var mockNavProp1 = new MockPropertyInfo(typeof(object), "Nav1");
var navConfig1 = configuration.Navigation(mockNavProp1);
var clone = configuration.Clone();
Assert.True(clone.ConfiguredProperties.Contains(mockNavProp1));
Assert.NotSame(navConfig1, clone.Navigation(mockNavProp1));
var mockNavProp2 = new MockPropertyInfo(typeof(object), "Nav2");
configuration.Navigation(mockNavProp2);
Assert.False(clone.ConfiguredProperties.Contains(mockNavProp2));
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_primitive_property_information()
{
var configuration = new EntityTypeConfiguration(typeof(object));
var mockProperty1 = new MockPropertyInfo(typeof(int), "P1");
var propConfig1 = configuration.Property(new PropertyPath(mockProperty1));
var clone = configuration.Clone();
Assert.True(clone.ConfiguredProperties.Contains(mockProperty1));
Assert.NotSame(propConfig1, clone.Property(new PropertyPath(mockProperty1)));
var mockProperty2 = new MockPropertyInfo(typeof(int), "P2");
configuration.Property(new PropertyPath(mockProperty2));
Assert.False(clone.ConfiguredProperties.Contains(mockProperty2));
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_ignored_properties()
{
var configuration = new EntityTypeConfiguration(typeof(object));
var mockProperty1 = new MockPropertyInfo(typeof(int), "P1");
configuration.Ignore(mockProperty1);
var clone = configuration.Clone();
Assert.True(clone.IgnoredProperties.Contains(mockProperty1));
var mockProperty2 = new MockPropertyInfo(typeof(int), "P2");
configuration.Ignore(mockProperty2);
Assert.False(clone.IgnoredProperties.Contains(mockProperty2));
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_mapping_information()
{
var configuration = new EntityTypeConfiguration(typeof(object));
configuration.ToTable("Table");
var clone = configuration.Clone();
Assert.Equal("Table", clone.GetTableName().Name);
configuration.ToTable("AnotherTable");
Assert.Equal("Table", clone.GetTableName().Name);
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_subtype_mapping_information()
{
var configuration = new EntityTypeConfiguration(typeof(object));
var mappingConfiguration = new EntityMappingConfiguration();
configuration.AddSubTypeMappingConfiguration(typeof(object), mappingConfiguration);
var clone = configuration.Clone();
Assert.NotSame(configuration.SubTypeMappingConfigurations, clone.SubTypeMappingConfigurations);
Assert.True(clone.SubTypeMappingConfigurations.ContainsKey(typeof(object)));
Assert.NotSame(mappingConfiguration, clone.SubTypeMappingConfigurations[typeof(object)]);
configuration.AddSubTypeMappingConfiguration(typeof(int), new EntityMappingConfiguration());
Assert.False(clone.SubTypeMappingConfigurations.ContainsKey(typeof(int)));
}
[Fact]
public void Cloning_an_entity_configuration_clones_its_modification_function_information()
{
var configuration = new EntityTypeConfiguration(typeof(object));
configuration.MapToFunctions();
var clone = configuration.Clone();
Assert.True(clone.IsMappedToFunctions);
}
[Fact]
public void Cloning_a_complex_type_configuration_clones_its_primitive_property_information()
{
var configuration = new ComplexTypeConfiguration(typeof(object));
var mockProperty1 = new MockPropertyInfo(typeof(int), "P1");
var propConfig1 = configuration.Property(new PropertyPath(mockProperty1));
var clone = configuration.Clone();
Assert.True(clone.ConfiguredProperties.Contains(mockProperty1));
Assert.NotSame(propConfig1, clone.Property(new PropertyPath(mockProperty1)));
var mockProperty2 = new MockPropertyInfo(typeof(int), "P2");
configuration.Property(new PropertyPath(mockProperty2));
Assert.False(clone.ConfiguredProperties.Contains(mockProperty2));
}
[Fact]
public void ComplexTypeConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ComplexTypeConfiguration>(0);
}
[Fact]
public void StructuralTypeConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<StructuralTypeConfiguration>(3);
}
[Fact]
public void Cloning_a_complex_type_configuration_clones_its_ignored_properties()
{
var configuration = new ComplexTypeConfiguration(typeof(object));
var mockProperty1 = new MockPropertyInfo(typeof(int), "P1");
configuration.Ignore(mockProperty1);
var clone = configuration.Clone();
Assert.True(clone.IgnoredProperties.Contains(mockProperty1));
var mockProperty2 = new MockPropertyInfo(typeof(int), "P2");
configuration.Ignore(mockProperty2);
Assert.False(clone.IgnoredProperties.Contains(mockProperty2));
}
[Fact]
public void Cloning_a_complex_type_configuration_clones_its_scalar_properties()
{
var configuration = new ComplexTypeConfiguration(typeof(object));
var clone = configuration.Clone();
Assert.Same(typeof(object), clone.ClrType);
}
[Fact]
public void Cloning_a_primitive_property_configuration_clones_its_property_information()
{
Cloning_a_primitive_property_configuration_clones_its_property_information_implementation(
new PrimitivePropertyConfiguration());
}
[Fact]
public void PrimitivePropertyConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<PrimitivePropertyConfiguration>(8);
}
[Fact]
public void Cloning_a_binary_property_configuration_clones_its_property_information()
{
var configuration = new BinaryPropertyConfiguration();
configuration.IsRowVersion = true;
var clone = (BinaryPropertyConfiguration)
Cloning_a_length_property_configuration_clones_its_property_information(configuration);
Assert.True(clone.IsRowVersion.Value);
}
[Fact]
public void BinaryPropertyConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<BinaryPropertyConfiguration>(1);
}
[Fact]
public void Cloning_a_decimal_property_configuration_clones_its_property_information()
{
var configuration = new DecimalPropertyConfiguration();
configuration.Precision = 100;
configuration.Scale = 101;
var clone = (DecimalPropertyConfiguration)
Cloning_a_primitive_property_configuration_clones_its_property_information_implementation(configuration);
Assert.Equal(100, clone.Precision.Value);
Assert.Equal(101, clone.Scale.Value);
}
[Fact]
public void DecimalPropertyConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<DecimalPropertyConfiguration>(2);
}
[Fact]
public void Cloning_a_date_time_property_configuration_clones_its_property_information()
{
var configuration = new DateTimePropertyConfiguration();
configuration.Precision = 100;
var clone = (DateTimePropertyConfiguration)
Cloning_a_primitive_property_configuration_clones_its_property_information_implementation(configuration);
Assert.Equal(100, clone.Precision.Value);
}
[Fact]
public void DateTimePropertyConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<DateTimePropertyConfiguration>(1);
}
[Fact]
public void Cloning_a_string_property_configuration_clones_its_property_information()
{
var configuration = new StringPropertyConfiguration();
configuration.IsUnicode = true;
var clone = (StringPropertyConfiguration)
Cloning_a_length_property_configuration_clones_its_property_information(configuration);
Assert.True(clone.IsUnicode.Value);
}
[Fact]
public void StringPropertyConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<StringPropertyConfiguration>(1);
}
[Fact]
public void LengthPropertyConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<LengthPropertyConfiguration>(3);
}
private LengthPropertyConfiguration Cloning_a_length_property_configuration_clones_its_property_information(
LengthPropertyConfiguration configuration)
{
configuration.IsFixedLength = true;
configuration.IsMaxLength = true;
configuration.MaxLength = 77;
var clone = (LengthPropertyConfiguration)
Cloning_a_primitive_property_configuration_clones_its_property_information_implementation(configuration);
Assert.True(clone.IsFixedLength.Value);
Assert.True(clone.IsMaxLength.Value);
Assert.Equal(77, clone.MaxLength);
return clone;
}
private PrimitivePropertyConfiguration Cloning_a_primitive_property_configuration_clones_its_property_information_implementation(
PrimitivePropertyConfiguration configuration)
{
configuration.IsNullable = true;
configuration.ConcurrencyMode = ConcurrencyMode.Fixed;
configuration.DatabaseGeneratedOption = DatabaseGeneratedOption.Identity;
configuration.ColumnType = "ColumnType";
configuration.ColumnName = "ColumnName";
configuration.ColumnOrder = 1;
configuration.OverridableConfigurationParts = OverridableConfigurationParts.OverridableInCSpace;
var clone = configuration.Clone();
Assert.True(clone.IsNullable.Value);
Assert.Equal(ConcurrencyMode.Fixed, clone.ConcurrencyMode);
Assert.Equal(DatabaseGeneratedOption.Identity, clone.DatabaseGeneratedOption);
Assert.Equal("ColumnType", clone.ColumnType);
Assert.Equal("ColumnName", clone.ColumnName);
Assert.Equal(1, clone.ColumnOrder);
Assert.Equal(OverridableConfigurationParts.OverridableInCSpace, clone.OverridableConfigurationParts);
return clone;
}
[Fact]
public void Cloning_a_navigation_property_configuration_clones_its_property_information()
{
var navProp = new MockPropertyInfo(typeof(int), "P1");
var configuration = new NavigationPropertyConfiguration(navProp);
configuration.RelationshipMultiplicity = RelationshipMultiplicity.Many;
var inverseNavProp = new MockPropertyInfo(typeof(int), "P2");
configuration.InverseNavigationProperty = inverseNavProp;
configuration.InverseEndKind = RelationshipMultiplicity.ZeroOrOne;
configuration.DeleteAction = OperationAction.Restrict;
configuration.IsNavigationPropertyDeclaringTypePrincipal = true;
var clone = configuration.Clone();
Assert.Equal(navProp, clone.NavigationProperty);
Assert.Equal(RelationshipMultiplicity.Many, clone.RelationshipMultiplicity);
Assert.Equal(inverseNavProp, clone.InverseNavigationProperty);
Assert.Equal(RelationshipMultiplicity.ZeroOrOne, clone.InverseEndKind);
Assert.Equal(OperationAction.Restrict, clone.DeleteAction);
Assert.True(clone.IsNavigationPropertyDeclaringTypePrincipal.Value);
Assert.Null(clone.Constraint);
Assert.Null(clone.AssociationMappingConfiguration);
}
[Fact]
public void NavigationPropertyConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<NavigationPropertyConfiguration>(8);
}
[Fact]
public void Cloning_a_navigation_property_configuration_clones_its_constraint_information()
{
var navProp = new MockPropertyInfo(typeof(int), "P1");
var configuration = new NavigationPropertyConfiguration(navProp);
configuration.Constraint =
new ForeignKeyConstraintConfiguration(
new List<PropertyInfo>
{
new MockPropertyInfo(typeof(int), "P1")
});
var clone = configuration.Clone();
Assert.NotSame(configuration.Constraint, clone.Constraint);
Assert.Equal(configuration.Constraint, clone.Constraint);
}
[Fact]
public void Cloning_a_navigation_property_configuration_clones_its_association_mapping_configuration()
{
var navProp = new MockPropertyInfo(typeof(int), "P1");
var configuration = new NavigationPropertyConfiguration(navProp);
var mappingConfiguration = new ForeignKeyAssociationMappingConfiguration();
mappingConfiguration.MapKey("C1");
configuration.AssociationMappingConfiguration = mappingConfiguration;
var clone = configuration.Clone();
Assert.NotSame(configuration.AssociationMappingConfiguration, clone.AssociationMappingConfiguration);
Assert.Equal(configuration.AssociationMappingConfiguration, clone.AssociationMappingConfiguration);
}
[Fact]
public void Cloning_a_foreign_key_constraint_clones_its_property_information()
{
var configuration =
new ForeignKeyConstraintConfiguration(
new List<PropertyInfo>
{
new MockPropertyInfo(typeof(int), "P1")
});
var clone = (ForeignKeyConstraintConfiguration)configuration.Clone();
Assert.True(clone.IsFullySpecified);
Assert.True(clone.ToProperties.Any(p => p.Name == "P1"));
}
[Fact]
public void ForeignKeyConstraintConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ForeignKeyConstraintConfiguration>(2);
}
[Fact]
public void Cloning_an_indpendent_constraint_just_returns_the_singleton_instance()
{
var configuration = IndependentConstraintConfiguration.Instance;
var clone = (IndependentConstraintConfiguration)configuration.Clone();
Assert.Same(IndependentConstraintConfiguration.Instance, clone);
}
[Fact]
public void IndependentConstraintConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<IndependentConstraintConfiguration>(0);
}
[Fact]
public void ConstraintConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ConstraintConfiguration>(0);
}
[Fact]
public void Cloning_an_foreign_key_mapping_configuration_clones_its_table_and_column_information()
{
var configuration = new ForeignKeyAssociationMappingConfiguration();
configuration.MapKey("C1");
configuration.ToTable("T", "S");
var clone = (ForeignKeyAssociationMappingConfiguration)configuration.Clone();
Assert.Equal(configuration, clone);
configuration.MapKey("C2");
Assert.NotEqual(configuration, clone);
}
[Fact]
public void ForeignKeyAssociationMappingConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ForeignKeyAssociationMappingConfiguration>(2);
}
[Fact]
public void Cloning_a_many_to_many_foreign_key_mapping_configuration_clones_its_table_and_column_information()
{
var configuration = new ManyToManyAssociationMappingConfiguration();
configuration.MapLeftKey("C1");
configuration.MapRightKey("C2");
configuration.ToTable("T", "S");
var clone = (ManyToManyAssociationMappingConfiguration)configuration.Clone();
Assert.Equal(configuration, clone);
configuration.MapLeftKey("C3");
Assert.NotEqual(configuration, clone);
}
[Fact]
public void ManyToManyAssociationMappingConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ManyToManyAssociationMappingConfiguration>(3);
}
[Fact]
public void AssociationMappingConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<AssociationMappingConfiguration>(0);
}
[Fact]
public void Cloning_an_entity_mapping_configuration_clones_its_table_property_and_condition_information()
{
var configuration = new EntityMappingConfiguration();
var propertyInfo1 = new MockPropertyInfo(typeof(int), "P1");
configuration.Properties = new List<PropertyPath>
{
new PropertyPath(propertyInfo1)
};
configuration.TableName = new DatabaseName("T", "S");
configuration.AddValueCondition(new ValueConditionConfiguration(configuration, "D"));
configuration.AddNullabilityCondition(new NotNullConditionConfiguration(configuration, new PropertyPath(propertyInfo1)));
configuration.MapInheritedProperties = true;
var clone = configuration.Clone();
Assert.True(clone.Properties.Any(p => p[0].Name == "P1"));
Assert.Equal("T", clone.TableName.Name);
Assert.Equal("S", clone.TableName.Schema);
Assert.True(clone.ValueConditions.Any(c => c.Discriminator == "D"));
Assert.True(clone.NullabilityConditions.Any(c => c.PropertyPath[0].Name == "P1"));
configuration.AddValueCondition(new ValueConditionConfiguration(configuration, "D2"));
configuration.AddNullabilityCondition(
new NotNullConditionConfiguration(
configuration,
new PropertyPath(new MockPropertyInfo(typeof(int), "P2"))));
Assert.False(clone.ValueConditions.Any(c => c.Discriminator == "D2"));
Assert.False(clone.NullabilityConditions.Any(c => c.PropertyPath[0].Name == "P2"));
Assert.True(clone.MapInheritedProperties);
}
[Fact]
public void EntityMappingConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<EntityMappingConfiguration>(5);
}
[Fact]
public void Cloning_an_entity_mapping_configuration_works_when_no_properties_have_been_set()
{
var configuration = new EntityMappingConfiguration();
var clone = configuration.Clone();
Assert.Null(clone.Properties);
}
[Fact]
public void Cloning_a_value_condition_clones_its_discriminator_and_value()
{
var entityConfiguration = new EntityMappingConfiguration();
var configuration = new ValueConditionConfiguration(entityConfiguration, "D");
configuration.Value = "V";
var clone = configuration.Clone(entityConfiguration);
Assert.Equal("V", (string)clone.Value);
Assert.Equal("D", clone.Discriminator);
}
[Fact]
public void ValueConditionConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<ValueConditionConfiguration>(4);
}
[Fact]
public void Cloning_a_null_condition_clones_its_property_info()
{
var entityConfiguration = new EntityMappingConfiguration();
var configuration = new NotNullConditionConfiguration(
entityConfiguration,
new PropertyPath(new MockPropertyInfo(typeof(int), "P")));
var clone = configuration.Clone(entityConfiguration);
Assert.Equal("P", clone.PropertyPath[0].Name);
}
[Fact]
public void NotNullConditionConfiguration_has_expected_number_of_fields()
{
VerifyFieldCount<NotNullConditionConfiguration>(2);
}
#endregion
[Fact]
public void Entities_returns_configuration_object()
{
Assert.NotNull(new DbModelBuilder().Entities());
}
[Fact]
public void Entities_with_type_returns_configuration_object()
{
Assert.NotNull(new DbModelBuilder().Entities<object>());
}
[Fact]
public void Properties_returns_configuration_object()
{
Assert.NotNull(new DbModelBuilder().Properties());
}
[Fact]
public void Properties_with_type_returns_configuration_object()
{
var decimalProperty = new MockPropertyInfo(typeof(decimal), "Property1");
var nullableDecimalProperty = new MockPropertyInfo(typeof(decimal?), "Property2");
var nonDecimalProperty = new MockPropertyInfo(typeof(string), "Property3");
var config = new DbModelBuilder().Properties<decimal>();
Assert.NotNull(config);
Assert.Equal(1, config.Predicates.Count());
var predicate = config.Predicates.Single();
Assert.True(predicate(decimalProperty));
Assert.True(predicate(nullableDecimalProperty));
Assert.False(predicate(nonDecimalProperty));
}
[Fact]
public void Properties_with_type_throws_when_not_primitive()
{
var modelBuilder = new DbModelBuilder();
// Ensure non-struct primitive types are allowed
modelBuilder.Properties<byte[]>();
modelBuilder.Properties<DbGeography>();
modelBuilder.Properties<DbGeometry>();
modelBuilder.Properties<string>();
modelBuilder.Properties<DateTimeKind>();
var ex = Assert.Throws<InvalidOperationException>(
() => modelBuilder.Properties<object>());
Assert.Equal(
Strings.ModelBuilder_PropertyFilterTypeMustBePrimitive(typeof(object)),
ex.Message);
}
}
}
|