Skip to content

Commit

Permalink
Add Unit Test Class
Browse files Browse the repository at this point in the history
  • Loading branch information
bit365 committed Nov 26, 2024
1 parent 8b439f1 commit 78d1511
Show file tree
Hide file tree
Showing 20 changed files with 350 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
namespace EfCore.NamingConverter.Tests.Conventions
using EfCore.NamingConverter.Conventions;
using EfCore.NamingConverter.Converters;
using EfCore.NamingConverter.Tests.TestCases;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace EfCore.NamingConverter.Tests.Conventions
{
public class ColumnNameConventionTest
{
// TODO: The test is not implemented yet
[Fact]
public void ProcessPropertyAdded_UseSnakeCaseLower_ShouldApplyColumnNameConvention()
{
var convention = new ColumnNameConvention(NameConverter.SnakeCaseLower);
var entityType = MockBuilder.BuildEntityType(b => b.Entity<SomeEntity>(), convention);
var property = entityType.GetProperty(nameof(SomeEntity.SomeProperty));

var identifier = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table).GetValueOrDefault();

var columnName = property.GetColumnName(identifier);

Assert.Equal("some_property", columnName);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
namespace EfCore.NamingConverter.Tests.Conventions
using EfCore.NamingConverter.Conventions;
using EfCore.NamingConverter.Converters;
using EfCore.NamingConverter.Tests.TestCases;
using Microsoft.EntityFrameworkCore;

namespace EfCore.NamingConverter.Tests.Conventions
{
public class ForeignKeyNameConventionTest
{
// TODO:The test is not implemented yet
[Fact]
public void ProcessForeignKeyAdded_UseSnakeCaseLower_ShouldReturnSnakeCaseLower()
{
var convention = new ForeignKeyNameConvention(NameConverter.SnakeCaseLower);
var model = MockBuilder.BuildModel(b => b.Entity<Blog>(), convention);
var entityType = model.FindEntityType(typeof(Post));

Assert.NotNull(entityType);

string? foreignKeyName = entityType.GetForeignKeys().Single().GetConstraintName();

Assert.NotNull(foreignKeyName);
Assert.Equal("fk_post_blog_blog_id", foreignKeyName);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
namespace EfCore.NamingConverter.Tests.Conventions
using EfCore.NamingConverter.Conventions;
using EfCore.NamingConverter.Converters;
using EfCore.NamingConverter.Tests.TestCases;
using Microsoft.EntityFrameworkCore;

namespace EfCore.NamingConverter.Tests.Conventions
{
public class IndexNameConventionTest
{
// TDDO: The test is not implemented yet
[Fact]
public void ProcessIndexAdded_UseSnakeCaseLower_ShouldReturnSnakeCaseLower()
{
var convention = new IndexNameConvention(NameConverter.SnakeCaseLower);

var entityType = MockBuilder.BuildEntityType(b => b.Entity<SomeEntity>().HasIndex(x => x.SomeProperty), convention);

string? indexName = entityType.GetIndexes().Single()?.GetDatabaseName();

Assert.NotNull(indexName);
Assert.Equal("ix_some_entity_some_property", indexName);
}
}
}
22 changes: 20 additions & 2 deletions EfCore.NamingConverter.Tests/Conventions/KeyNameConventionTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
namespace EfCore.NamingConverter.Tests.Conventions
using EfCore.NamingConverter.Conventions;
using EfCore.NamingConverter.Converters;
using EfCore.NamingConverter.Tests.TestCases;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace EfCore.NamingConverter.Tests.Conventions
{
public class KeyNameConventionTest
{
// TODO:The test is not implemented yet
[Fact]
public void ProcessKeyAdded_UseSnakeCaseLower_ShouldReturnSnakeCaseLower()
{
var convention = new KeyNameConvention(NameConverter.SnakeCaseLower);
var entityType = MockBuilder.BuildEntityType(b => b.Entity<SomeEntity>(), convention);
var key = entityType.FindPrimaryKey();

Assert.NotNull(key);

var keyName = key.GetName();

Assert.NotNull(keyName);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
namespace EfCore.NamingConverter.Tests.Conventions
using EfCore.NamingConverter.Conventions;
using EfCore.NamingConverter.Converters;
using EfCore.NamingConverter.Tests.TestCases;
using Microsoft.EntityFrameworkCore;

namespace EfCore.NamingConverter.Tests.Conventions
{
public class TableNameConventionTest
{
// TODO:The test is not implemented yet
[Fact]
public void ProcessEntityTypeAdded_UseSnakeCaseLower_ShouldApplyTableNameConvention()
{
// Arrange
var convention = new TableNameConvention(NameConverter.SnakeCaseLower);
var entityType = MockBuilder.BuildEntityType(b => b.Entity<SomeEntity>(), convention);
// Act
var tableName = entityType.GetTableName();
// Assert
Assert.Equal("some_entity", tableName);
}
}
}
45 changes: 43 additions & 2 deletions EfCore.NamingConverter.Tests/Conventions/ViewNameConventionTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
namespace EfCore.NamingConverter.Tests.Conventions
using EfCore.NamingConverter.Conventions;
using EfCore.NamingConverter.Converters;
using EfCore.NamingConverter.Tests.TestCases;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace EfCore.NamingConverter.Tests.Conventions
{
public class ViewNameConventionTest
{
// TODO:The test is not implemented yet

[Fact]
public void ProcessEntityTypeAdded_UseSnakeCaseLower_ShouldApplyViewNameConvention()
{
// Arrange
var convention = new ViewNameConvention(NameConverter.SnakeCaseLower);

var entityType = MockBuilder.BuildEntityType(b => b.Entity<SomeEntity>(e =>
{
e.ToView("some_entity_view");
}), convention);

// Act
var viewName = entityType.GetViewName();

// Assert
Assert.Equal("some_entity_view", viewName);
}

[Fact]
public void ProcessColumnTypeAdded_UseSnakeCaseLower_ShouldApplyViewNameColumnConvention()
{
var convention = new ColumnNameConvention(NameConverter.SnakeCaseLower);
var entityType = MockBuilder.BuildEntityType(b => b.Entity<SomeEntity>(e =>
{
e.ToView("SomeEntityView");
}), convention);

var property = entityType.GetProperty(nameof(SomeEntity.SomeProperty));

var identifier = StoreObjectIdentifier.Create(entityType, StoreObjectType.View).GetValueOrDefault();

var columnName = property.GetColumnName(identifier);

Assert.Equal("some_property", columnName);
}
}
}
24 changes: 21 additions & 3 deletions EfCore.NamingConverter.Tests/Converters/CamelCaseConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ namespace EfCore.NamingConverter.Tests.Converters
public class CamelCaseConverterTest
{
[Fact]
public void CanConvertNameToCamelCase()
public void ConvertName_ShouldReturnCamelCase()
{
var converter = new CamelCaseConverter();
// Arrange
var camelCaseConverter = new CamelCaseConverter();

Assert.Equal("camelCaseName", converter.ConvertName("CamelCaseName"));
// Act
var actual = camelCaseConverter.ConvertName("FullName");

// Assert
Assert.Equal("fullName", actual);
}

[Fact]
public void ConvertName_InputEmpty_ShouldReturnEmpty()
{
// Arrange
var camelCaseConverter = new CamelCaseConverter();

// Act
var actual = camelCaseConverter.ConvertName(string.Empty);

// Assert
Assert.Equal(string.Empty, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ namespace EfCore.NamingConverter.Tests.Converters
public class KebabCaseLowerConverterTest
{
[Fact]
public void CanConvertNameToKebabCaseLower()
public void ConvertName_ShouldReturnKebabCaseLower()
{
var converter = new KebabCaseLowerConverter();
// Arrange
var kebabCaseConverter = new KebabCaseLowerConverter();

Assert.Equal("kebab-case-lower", converter.ConvertName("KebabCaseLower"));
// Act
var actual = kebabCaseConverter.ConvertName("FullName");

// Assert
Assert.Equal("full-name", actual);
}

[Fact]
public void ConvertName_InputEmpty_ShouldReturnEmpty()
{
// Arrange
var kebabCaseConverter = new KebabCaseLowerConverter();

// Act
var actual = kebabCaseConverter.ConvertName(string.Empty);

// Assert
Assert.Equal(string.Empty, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ namespace EfCore.NamingConverter.Tests.Converters
public class KebabCaseUpperConverterTest
{
[Fact]
public void CanConvertNameToKebabCaseUpper()
public void ConvertName_ShouldReturnKebabCaseUpper()
{
var converter = new KebabCaseUpperConverter();
// Arrange
var kebabCaseConverter = new KebabCaseUpperConverter();

Assert.Equal("SNAKE-CASE-LOWER", converter.ConvertName("SnakeCaseLower"));
// Act
var actual = kebabCaseConverter.ConvertName("FullName");

// Assert
Assert.Equal("FULL-NAME", actual);
}

[Fact]
public void ConvertName_InputEmpty_ShouldReturnEmpty()
{
// Arrange
var kebabCaseConverter = new KebabCaseUpperConverter();

// Act
var actual = kebabCaseConverter.ConvertName(string.Empty);

// Assert
Assert.Equal(string.Empty, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class NameConverterTest
[InlineData(NamingPolicy.KebabCaseLower, "FullName", "full-name")]
[InlineData(NamingPolicy.KebabCaseUpper, "FullName", "FULL-NAME")]
[InlineData(NamingPolicy.Unspecified, "FullName", "FullName")]
public void CanConvertName(NamingPolicy namingPolicy, string input, string expected)
public void ConvertName_ShouldReturnConvertedName(NamingPolicy namingPolicy, string input, string expected)
{
var converter = NameConverter.From(namingPolicy);
Assert.Equal(expected, converter.ConvertName(input));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public MySeparatorConverter() : base(lowercase: true, separator: '_')
}

[Fact]
public void CanConvertNameToSnakeCaseLower()
public void ConvertName_ShouldReturnSnakeCaseLower()
{
var converter = new MySeparatorConverter();
Assert.Equal("snake_case_lower", converter.ConvertName("SnakeCaseLower"));
Assert.Equal("full_name", converter.ConvertName("FullName"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,29 @@ namespace EfCore.NamingConverter.Tests.Converters
public class SnakeCaseLowerConverterTest
{
[Fact]
public void CanConvertNameToSnakeCaseLower()
public void ConvertName_ShouldReturnSnakeCaseLower()
{
var converter = new SnakeCaseLowerConverter();
Assert.Equal("snake_case_lower", converter.ConvertName("SnakeCaseLower"));
// Arrange
var snakeCaseConverter = new SnakeCaseLowerConverter();

// Act
var actual = snakeCaseConverter.ConvertName("FullName");

// Assert
Assert.Equal("full_name", actual);
}

[Fact]
public void ConvertName_InputEmpty_ShouldReturnEmpty()
{
// Arrange
var snakeCaseConverter = new SnakeCaseLowerConverter();

// Act
var actual = snakeCaseConverter.ConvertName(string.Empty);

// Assert
Assert.Equal(string.Empty, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ namespace EfCore.NamingConverter.Tests.Converters
public class SnakeCaseUpperConverterTest
{
[Fact]
public void CanConvertNameToSnakeCaseUpper()
public void ConvertName_ShouldReturnSnakeCaseUpper()
{
var converter = new SnakeCaseUpperConverter();
// Arrange
var snakeCaseConverter = new SnakeCaseUpperConverter();

Assert.Equal("SNAKE_CASE_NAME", converter.ConvertName("SnakeCaseName"));
// Act
var actual = snakeCaseConverter.ConvertName("FullName");

// Assert
Assert.Equal("FULL_NAME", actual);
}

[Fact]
public void ConvertName_InputEmpty_ShouldReturnEmpty()
{
// Arrange
var snakeCaseConverter = new SnakeCaseUpperConverter();

// Act
var actual = snakeCaseConverter.ConvertName(string.Empty);

// Assert
Assert.Equal(string.Empty, actual);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Specification.Tests" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
Expand Down
Loading

0 comments on commit 78d1511

Please sign in to comment.