-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
307 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
test/SpatialFocus.EntityFrameworkCore.Extensions.Test/Entities/Product.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test/SpatialFocus.EntityFrameworkCore.Extensions.Test/Entities/ProductCategory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/SpatialFocus.EntityFrameworkCore.Extensions.Test/Entities/ProductTag.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
test/SpatialFocus.EntityFrameworkCore.Extensions.Test/Entities/SpecialOccasion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
test/SpatialFocus.EntityFrameworkCore.Extensions.Test/NamingOptionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
test/SpatialFocus.EntityFrameworkCore.Extensions.Test/ProductContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...tityFrameworkCore.Extensions.Test/SpatialFocus.EntityFrameworkCore.Extensions.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |