Skip to content

Commit 308e9d4

Browse files
committed
Added unit tests for NamingOptions
1 parent f409e46 commit 308e9d4

8 files changed

+307
-3
lines changed

SpatialFocus.EntityFrameworkCore.Extensions.sln

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27703.2042
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29709.97
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpatialFocus.EntityFrameworkCore.Extensions", "src\SpatialFocus.EntityFrameworkCore.Extensions\SpatialFocus.EntityFrameworkCore.Extensions.csproj", "{768CB889-C6A3-475B-B8EE-72CE676DD5B8}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpatialFocus.EntityFrameworkCore.Extensions.Demo", "samples\SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo\SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.csproj", "{28AC20A6-FE86-427F-80D9-19D5D02D31FB}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo", "samples\SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo\SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.csproj", "{28AC20A6-FE86-427F-80D9-19D5D02D31FB}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{70FF5B08-F25B-44BE-B8FE-1F1884B5B3F0}"
1111
EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8092090A-24E1-45E6-84B3-AA060CC34619}"
1313
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2C1E7909-3D0A-462C-AEE1-21D583FBCFD2}"
15+
EndProject
16+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "global", "global", "{AA695DE3-C613-4023-97F5-BF3841C5E233}"
17+
ProjectSection(SolutionItems) = preProject
18+
.editorconfig = .editorconfig
19+
.gitattributes = .gitattributes
20+
.gitignore = .gitignore
21+
Directory.Build.props = Directory.Build.props
22+
Directory.Build.targets = Directory.Build.targets
23+
LICENSE = LICENSE
24+
README.md = README.md
25+
SpatialFocus.EntityFrameworkCore.Extensions.ruleset = SpatialFocus.EntityFrameworkCore.Extensions.ruleset
26+
SpatialFocus.EntityFrameworkCore.Extensions.sln.DotSettings = SpatialFocus.EntityFrameworkCore.Extensions.sln.DotSettings
27+
stylecop.json = stylecop.json
28+
EndProjectSection
29+
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpatialFocus.EntityFrameworkCore.Extensions.Test", "test\SpatialFocus.EntityFrameworkCore.Extensions.Test\SpatialFocus.EntityFrameworkCore.Extensions.Test.csproj", "{F40647CD-65D8-4FD0-8391-88F7CD8D976D}"
31+
EndProject
1432
Global
1533
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1634
Debug|Any CPU = Debug|Any CPU
@@ -25,13 +43,18 @@ Global
2543
{28AC20A6-FE86-427F-80D9-19D5D02D31FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
2644
{28AC20A6-FE86-427F-80D9-19D5D02D31FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
2745
{28AC20A6-FE86-427F-80D9-19D5D02D31FB}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Release|Any CPU.Build.0 = Release|Any CPU
2850
EndGlobalSection
2951
GlobalSection(SolutionProperties) = preSolution
3052
HideSolutionNode = FALSE
3153
EndGlobalSection
3254
GlobalSection(NestedProjects) = preSolution
3355
{768CB889-C6A3-475B-B8EE-72CE676DD5B8} = {70FF5B08-F25B-44BE-B8FE-1F1884B5B3F0}
3456
{28AC20A6-FE86-427F-80D9-19D5D02D31FB} = {8092090A-24E1-45E6-84B3-AA060CC34619}
57+
{F40647CD-65D8-4FD0-8391-88F7CD8D976D} = {2C1E7909-3D0A-462C-AEE1-21D583FBCFD2}
3558
EndGlobalSection
3659
GlobalSection(ExtensibilityGlobals) = postSolution
3760
SolutionGuid = {8D07B300-5F54-4D11-BA2E-668BF1671D72}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// <copyright file="Product.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
7+
{
8+
using System;
9+
10+
public class Product
11+
{
12+
public Product()
13+
{
14+
Created = DateTime.Now;
15+
}
16+
17+
public DateTime Created { get; set; }
18+
19+
public SpecialOccasion? IdealForSpecialOccasion { get; set; }
20+
21+
public string Name { get; set; }
22+
23+
public double Price { get; set; }
24+
25+
public ProductCategory ProductCategory { get; set; }
26+
27+
public int ProductId { get; set; }
28+
29+
public DateTime ReleaseDate { get; set; }
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// <copyright file="ProductCategory.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
7+
{
8+
public enum ProductCategory
9+
{
10+
Book = 1,
11+
12+
Bluray,
13+
14+
CD,
15+
16+
DVD,
17+
18+
Other,
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <copyright file="ProductTag.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
7+
{
8+
public class ProductTag
9+
{
10+
public int ProductTagId { get; set; }
11+
12+
public virtual Product Product { get; set; }
13+
14+
public string Name { get; set; }
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <copyright file="SpecialOccasion.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
7+
{
8+
using System.ComponentModel;
9+
10+
public enum SpecialOccasion
11+
{
12+
[Description("Your birth anniversary")]
13+
Birthday = 1,
14+
15+
[Description("Jesus' birth anniversary")]
16+
Christmas,
17+
18+
[Description("Jesus' resurrection anniversary")]
19+
Easter,
20+
21+
[Description("Florist holiday")]
22+
Valentines,
23+
24+
[Description("Marriage anniversary")]
25+
WeddingDay,
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// <copyright file="NamingOptionsTest.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.Test
7+
{
8+
using System;
9+
using System.Linq;
10+
using Microsoft.EntityFrameworkCore;
11+
using Microsoft.EntityFrameworkCore.Metadata;
12+
using Microsoft.EntityFrameworkCore.Storage;
13+
using SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities;
14+
using Xunit;
15+
16+
public class NamingOptionsTest
17+
{
18+
protected ProductContext GetContext(EnumLookupOptions? enumLookupOptions = null, NamingOptions? namingOptions = null)
19+
{
20+
DbContextOptions<ProductContext> options = new DbContextOptionsBuilder<ProductContext>()
21+
.UseInMemoryDatabase(Guid.NewGuid().ToString(), new InMemoryDatabaseRoot())
22+
.Options;
23+
24+
ProductContext context = new ProductContext(options, null, namingOptions);
25+
context.Database.EnsureCreated();
26+
27+
return context;
28+
}
29+
30+
[Fact]
31+
public void OverrideColumnNaming()
32+
{
33+
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideColumnNaming(NamingScheme.SnakeCase));
34+
35+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
36+
Assert.Equal("product_tag_id", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnName());
37+
}
38+
39+
[Fact]
40+
public void OverrideConstraintNaming()
41+
{
42+
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideConstraintNaming(NamingScheme.SnakeCase));
43+
44+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
45+
Assert.Equal("ProductTag", findEntityType.GetTableName());
46+
Assert.Equal("ProductTagId", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnName());
47+
Assert.True(findEntityType.GetKeys().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
48+
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
49+
Assert.True(findEntityType.GetIndexes().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
50+
}
51+
52+
[Fact]
53+
public void OverrideTableNaming()
54+
{
55+
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideTableNaming(NamingScheme.SnakeCase));
56+
57+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
58+
Assert.Equal("product_tag", findEntityType.GetTableName());
59+
}
60+
61+
[Fact]
62+
public void Pluralize()
63+
{
64+
ProductContext context = GetContext(namingOptions: new NamingOptions().Pluralize());
65+
66+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
67+
Assert.Equal("ProductTags", findEntityType.GetTableName());
68+
}
69+
70+
[Fact]
71+
public void PluralizeAndOverrideTableNaming()
72+
{
73+
ProductContext context = GetContext(namingOptions: new NamingOptions().Pluralize().OverrideTableNaming(NamingScheme.SnakeCase));
74+
75+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
76+
Assert.Equal("product_tags", findEntityType.GetTableName());
77+
}
78+
79+
[Fact]
80+
public void SetNamingScheme()
81+
{
82+
ProductContext context = GetContext(namingOptions: new NamingOptions().SetNamingScheme(NamingScheme.SnakeCase));
83+
84+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
85+
Assert.Equal("product_tag", findEntityType.GetTableName());
86+
Assert.Equal("product_tag_id", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnName());
87+
Assert.True(findEntityType.GetKeys().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
88+
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
89+
Assert.True(findEntityType.GetIndexes().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
90+
}
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// <copyright file="ProductContext.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions.Test
7+
{
8+
using System;
9+
using Microsoft.EntityFrameworkCore;
10+
using SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities;
11+
12+
public class ProductContext : DbContext
13+
{
14+
public ProductContext(DbContextOptions options, EnumLookupOptions enumLookupOptions, NamingOptions namingOptions)
15+
: base(options)
16+
{
17+
EnumLookupOptions = enumLookupOptions;
18+
NamingOptions = namingOptions;
19+
}
20+
21+
public DbSet<Product> Products { get; set; }
22+
23+
public DbSet<ProductTag> ProductTags { get; set; }
24+
25+
protected EnumLookupOptions EnumLookupOptions { get; }
26+
27+
protected NamingOptions NamingOptions { get; }
28+
29+
protected override void OnModelCreating(ModelBuilder modelBuilder)
30+
{
31+
base.OnModelCreating(modelBuilder);
32+
33+
modelBuilder.Entity<Product>()
34+
.HasData(
35+
new Product
36+
{
37+
ProductId = 1,
38+
ProductCategory = ProductCategory.Book,
39+
Name = "Robinson Crusoe",
40+
ReleaseDate = new DateTime(1719, 4, 25),
41+
Price = 14.99,
42+
},
43+
new Product
44+
{
45+
ProductId = 2,
46+
ProductCategory = ProductCategory.Bluray,
47+
Name = "Rogue One: A Star Wars Story",
48+
ReleaseDate = new DateTime(2017, 5, 4),
49+
Price = 11.99,
50+
},
51+
new Product
52+
{
53+
ProductId = 3,
54+
ProductCategory = ProductCategory.CD,
55+
Name = "Wham! - Last Christmas",
56+
ReleaseDate = new DateTime(1984, 12, 3),
57+
Price = 6.97,
58+
IdealForSpecialOccasion = SpecialOccasion.Christmas,
59+
});
60+
61+
modelBuilder.Entity<ProductTag>().HasIndex("ProductId", "Name").IsUnique();
62+
63+
if (EnumLookupOptions != null)
64+
{
65+
modelBuilder.ConfigureEnumLookup(EnumLookupOptions);
66+
}
67+
68+
if (NamingOptions != null)
69+
{
70+
modelBuilder.ConfigureNames(NamingOptions);
71+
}
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
12+
<PackageReference Include="xunit" Version="2.4.0" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
14+
<PackageReference Include="coverlet.collector" Version="1.0.1" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\src\SpatialFocus.EntityFrameworkCore.Extensions\SpatialFocus.EntityFrameworkCore.Extensions.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)