Skip to content

Commit

Permalink
Added unit tests for NamingOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dresel committed Jan 28, 2020
1 parent f409e46 commit 308e9d4
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 3 deletions.
29 changes: 26 additions & 3 deletions SpatialFocus.EntityFrameworkCore.Extensions.sln
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2042
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpatialFocus.EntityFrameworkCore.Extensions", "src\SpatialFocus.EntityFrameworkCore.Extensions\SpatialFocus.EntityFrameworkCore.Extensions.csproj", "{768CB889-C6A3-475B-B8EE-72CE676DD5B8}"
EndProject
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}"
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}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{70FF5B08-F25B-44BE-B8FE-1F1884B5B3F0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8092090A-24E1-45E6-84B3-AA060CC34619}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2C1E7909-3D0A-462C-AEE1-21D583FBCFD2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "global", "global", "{AA695DE3-C613-4023-97F5-BF3841C5E233}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
LICENSE = LICENSE
README.md = README.md
SpatialFocus.EntityFrameworkCore.Extensions.ruleset = SpatialFocus.EntityFrameworkCore.Extensions.ruleset
SpatialFocus.EntityFrameworkCore.Extensions.sln.DotSettings = SpatialFocus.EntityFrameworkCore.Extensions.sln.DotSettings
stylecop.json = stylecop.json
EndProjectSection
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,13 +43,18 @@ Global
{28AC20A6-FE86-427F-80D9-19D5D02D31FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28AC20A6-FE86-427F-80D9-19D5D02D31FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28AC20A6-FE86-427F-80D9-19D5D02D31FB}.Release|Any CPU.Build.0 = Release|Any CPU
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F40647CD-65D8-4FD0-8391-88F7CD8D976D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{768CB889-C6A3-475B-B8EE-72CE676DD5B8} = {70FF5B08-F25B-44BE-B8FE-1F1884B5B3F0}
{28AC20A6-FE86-427F-80D9-19D5D02D31FB} = {8092090A-24E1-45E6-84B3-AA060CC34619}
{F40647CD-65D8-4FD0-8391-88F7CD8D976D} = {2C1E7909-3D0A-462C-AEE1-21D583FBCFD2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D07B300-5F54-4D11-BA2E-668BF1671D72}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="Product.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
{
using System;

public class Product
{
public Product()
{
Created = DateTime.Now;
}

public DateTime Created { get; set; }

public SpecialOccasion? IdealForSpecialOccasion { get; set; }

public string Name { get; set; }

public double Price { get; set; }

public ProductCategory ProductCategory { get; set; }

public int ProductId { get; set; }

public DateTime ReleaseDate { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <copyright file="ProductCategory.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
{
public enum ProductCategory
{
Book = 1,

Bluray,

CD,

DVD,

Other,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <copyright file="ProductTag.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
{
public class ProductTag
{
public int ProductTagId { get; set; }

public virtual Product Product { get; set; }

public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <copyright file="SpecialOccasion.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities
{
using System.ComponentModel;

public enum SpecialOccasion
{
[Description("Your birth anniversary")]
Birthday = 1,

[Description("Jesus' birth anniversary")]
Christmas,

[Description("Jesus' resurrection anniversary")]
Easter,

[Description("Florist holiday")]
Valentines,

[Description("Marriage anniversary")]
WeddingDay,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// <copyright file="NamingOptionsTest.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.Test
{
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities;
using Xunit;

public class NamingOptionsTest
{
protected ProductContext GetContext(EnumLookupOptions? enumLookupOptions = null, NamingOptions? namingOptions = null)
{
DbContextOptions<ProductContext> options = new DbContextOptionsBuilder<ProductContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString(), new InMemoryDatabaseRoot())
.Options;

ProductContext context = new ProductContext(options, null, namingOptions);
context.Database.EnsureCreated();

return context;
}

[Fact]
public void OverrideColumnNaming()
{
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideColumnNaming(NamingScheme.SnakeCase));

IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
Assert.Equal("product_tag_id", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnName());
}

[Fact]
public void OverrideConstraintNaming()
{
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideConstraintNaming(NamingScheme.SnakeCase));

IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
Assert.Equal("ProductTag", findEntityType.GetTableName());
Assert.Equal("ProductTagId", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnName());
Assert.True(findEntityType.GetKeys().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
Assert.True(findEntityType.GetIndexes().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
}

[Fact]
public void OverrideTableNaming()
{
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideTableNaming(NamingScheme.SnakeCase));

IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
Assert.Equal("product_tag", findEntityType.GetTableName());
}

[Fact]
public void Pluralize()
{
ProductContext context = GetContext(namingOptions: new NamingOptions().Pluralize());

IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
Assert.Equal("ProductTags", findEntityType.GetTableName());
}

[Fact]
public void PluralizeAndOverrideTableNaming()
{
ProductContext context = GetContext(namingOptions: new NamingOptions().Pluralize().OverrideTableNaming(NamingScheme.SnakeCase));

IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
Assert.Equal("product_tags", findEntityType.GetTableName());
}

[Fact]
public void SetNamingScheme()
{
ProductContext context = GetContext(namingOptions: new NamingOptions().SetNamingScheme(NamingScheme.SnakeCase));

IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
Assert.Equal("product_tag", findEntityType.GetTableName());
Assert.Equal("product_tag_id", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnName());
Assert.True(findEntityType.GetKeys().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
Assert.True(findEntityType.GetIndexes().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// <copyright file="ProductContext.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions.Test
{
using System;
using Microsoft.EntityFrameworkCore;
using SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities;

public class ProductContext : DbContext
{
public ProductContext(DbContextOptions options, EnumLookupOptions enumLookupOptions, NamingOptions namingOptions)
: base(options)
{
EnumLookupOptions = enumLookupOptions;
NamingOptions = namingOptions;
}

public DbSet<Product> Products { get; set; }

public DbSet<ProductTag> ProductTags { get; set; }

protected EnumLookupOptions EnumLookupOptions { get; }

protected NamingOptions NamingOptions { get; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Product>()
.HasData(
new Product
{
ProductId = 1,
ProductCategory = ProductCategory.Book,
Name = "Robinson Crusoe",
ReleaseDate = new DateTime(1719, 4, 25),
Price = 14.99,
},
new Product
{
ProductId = 2,
ProductCategory = ProductCategory.Bluray,
Name = "Rogue One: A Star Wars Story",
ReleaseDate = new DateTime(2017, 5, 4),
Price = 11.99,
},
new Product
{
ProductId = 3,
ProductCategory = ProductCategory.CD,
Name = "Wham! - Last Christmas",
ReleaseDate = new DateTime(1984, 12, 3),
Price = 6.97,
IdealForSpecialOccasion = SpecialOccasion.Christmas,
});

modelBuilder.Entity<ProductTag>().HasIndex("ProductId", "Name").IsUnique();

if (EnumLookupOptions != null)
{
modelBuilder.ConfigureEnumLookup(EnumLookupOptions);
}

if (NamingOptions != null)
{
modelBuilder.ConfigureNames(NamingOptions);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SpatialFocus.EntityFrameworkCore.Extensions\SpatialFocus.EntityFrameworkCore.Extensions.csproj" />
</ItemGroup>

</Project>

0 comments on commit 308e9d4

Please sign in to comment.