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+ }
0 commit comments