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
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace FunctionalTests
{
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.ModelConfiguration.Edm;
using System.Linq;
using FunctionalTests.Fixtures;
using Xunit;
public class EnumsScenarioTests : TestBase
{
[Fact]
public void Simple_enum_mapping_scenario()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_Product>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<Enum_Product>(p => p.CategoryId).IsTrue(t => t.IsEnumType);
databaseMapping.Assert<Enum_Product>().HasColumn("CategoryId");
var enumType = databaseMapping.Model.GetEnumType("CategoryId");
Assert.NotNull(enumType);
Assert.Equal(7, enumType.Members.Count);
Assert.False(enumType.IsFlags);
Assert.Equal(PrimitiveTypeKind.Int32, enumType.UnderlyingType.PrimitiveTypeKind);
}
[Fact]
public void Short_enum_mapping_with_flags_attribute()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_Flags>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
var enumType = databaseMapping.Model.GetEnumType("WithFlags");
Assert.NotNull(enumType);
Assert.Equal(4, enumType.Members.Count);
Assert.True(enumType.IsFlags);
Assert.Equal(PrimitiveTypeKind.Int16, enumType.UnderlyingType.PrimitiveTypeKind);
}
[Fact]
public void Build_model_for_a_single_type_with_a_enum_key()
{
var modelBuilder = new AdventureWorksModelBuilder();
modelBuilder.Entity<Enum_Product_PK>();
var databaseMapping = BuildMapping(modelBuilder);
Assert.Equal(1, databaseMapping.EntityContainerMappings.Single().EntitySetMappings.Count());
}
[Fact]
public void Enum_mapped_as_by_convention_fk_in_association()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_Fk_Product>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<Enum_Fk_Product>().HasForeignKeyColumn("CategoryId");
}
[Fact]
public void Enum_mapped_as_by_convention_IA_in_association()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_IA_Principal>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<Enum_IA_Dependent>().HasForeignKeyColumn("Principal_Id");
}
[Fact]
public void Annotated_nullable_enum_inside_complex_type_is_mapped()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_ComplexEntity>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<Enum_ComplexType>(c => c.Enum).IsTrue(t => t.IsEnumType);
databaseMapping.Assert<Enum_ComplexType>(c => c.Enum).IsTrue(t => t.Nullable);
databaseMapping.Assert<Enum_ComplexType>(c => c.Enum).DbEqual("col_enum", c => c.Name);
}
[Fact]
public void Fluent_api_configured_enum_property_gets_correct_facets()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_Product>().Property(p => p.CategoryId).IsOptional();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<Enum_Product>(p => p.CategoryId).IsTrue(p => p.Nullable);
}
[Fact]
public void Should_be_able_to_use_v4_1_model_builder_without_enums()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.Entity<Enum_Product>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
Assert.Equal(2.0, databaseMapping.Model.Version);
Assert.Equal(2.0, databaseMapping.Database.Version);
}
[Fact]
public void Empty_enum_in_model_does_not_fail_validation()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_Empty>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<Enum_Empty>(p => p.Empty).IsFalse(e => e.EnumType.Members.Any());
}
[Fact]
public void Unsigned_enum_in_model_should_fail_validation()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<Enum_Unsigned>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
}
[Fact]
public void Explicitly_mapping_an_enum_property_using_4_1_throws()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.Entity<Enum_Product>().Property(e => e.CategoryId);
Assert.Throws<NotSupportedException>(
() =>
BuildMapping(modelBuilder))
.ValidateMessage("UnsupportedUseOfV3Type", "Enum_Product", "CategoryId");
}
[Fact]
public void Explicitly_mapping_a_nullable_enum_property_using_4_1_throws()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.Entity<Enum_Nullable>().Property(e => e.CategoryId);
Assert.Throws<NotSupportedException>(
() =>
BuildMapping(modelBuilder))
.ValidateMessage("UnsupportedUseOfV3Type", "Enum_Nullable", "CategoryId");
}
[Fact]
public void Explicitly_mapping_an_enum_property_on_a_complex_type_using_4_1_throws()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.ComplexType<Enum_ComplexType>().Property(c => c.Enum);
Assert.Throws<NotSupportedException>(
() =>
BuildMapping(modelBuilder))
.ValidateMessage("UnsupportedUseOfV3Type", "Enum_ComplexType", "Enum");
}
[Fact]
public void Explicitly_using_an_enum_property_as_a_key_with_4_1_throws()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.Entity<Enum_Product>().HasKey(e => e.CategoryId);
Assert.Throws<NotSupportedException>(
() =>
BuildMapping(modelBuilder))
.ValidateMessage("UnsupportedUseOfV3Type", "Enum_Product", "CategoryId");
}
[Fact]
public void Explicitly_using_an_enum_property_as_part_of_a_composite_key_with_4_1_throws()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.Entity<Enum_Product>().HasKey(
e => new
{
e.Id,
e.CategoryId
});
Assert.Throws<NotSupportedException>(
() =>
BuildMapping(modelBuilder))
.ValidateMessage("UnsupportedUseOfV3Type", "Enum_Product", "CategoryId");
}
[Fact]
public void Explicitly_mapping_an_enum_property_with_Map_using_4_1_throws()
{
var modelBuilder = new DbModelBuilder(DbModelBuilderVersion.V4_1);
modelBuilder.Entity<Enum_Product>().Map(
mapping =>
{
mapping.ToTable("Table1");
mapping.Properties(e => e.CategoryId);
});
Assert.Throws<InvalidOperationException>(
() =>
BuildMapping(modelBuilder))
.ValidateMessage("EntityMappingConfiguration_CannotMapIgnoredProperty", "Enum_Product", "CategoryId");
}
}
namespace Fixtures
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
public enum CategoryId
{
Beverages = 1,
Condiments,
Confections,
Dairy_Products,
Grains_Cereals,
Meat_Poultry,
Produce
}
public enum Empty
{
}
public class Enum_Empty
{
public int Id { get; set; }
public Empty Empty { get; set; }
}
public enum Unsigned : uint
{
Member
}
public class Enum_Unsigned
{
public int Id { get; set; }
public Unsigned Unsigned { get; set; }
}
public class Enum_Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public short UnitsInStock { get; set; }
public CategoryId CategoryId { get; set; }
}
public class Enum_Nullable
{
public int Id { get; set; }
public CategoryId? CategoryId { get; set; }
}
[Flags]
public enum WithFlags : short
{
Beverages = 0,
Condiments = 1,
Confections = 2,
Dairy_Products = 4
}
public class Enum_Flags
{
public int Id { get; set; }
public WithFlags FlagsEnum { get; set; }
}
public class Enum_Product_PK
{
public CategoryId Id { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public short UnitsInStock { get; set; }
}
public class Enum_Fk_Product
{
public int Id { get; set; }
public CategoryId CategoryId { get; set; }
public Enum_Fk_Category Category { get; set; }
}
public class Enum_Fk_Category
{
public CategoryId Id { get; set; }
public ICollection<Enum_Fk_Product> Products { get; set; }
}
public class Enum_IA_Principal
{
public CategoryId Id { get; set; }
public ICollection<Enum_IA_Dependent> Dependents { get; set; }
}
public class Enum_IA_Dependent
{
public int Id { get; set; }
public Enum_IA_Principal Principal { get; set; }
}
public class Enum_ComplexEntity
{
public int Id { get; set; }
public Enum_ComplexType ComplexType { get; set; }
}
[ComplexType]
public class Enum_ComplexType
{
[Column("col_enum")]
public WithFlags? Enum { get; set; }
}
}
}
|