Skip to content

Commit

Permalink
Add unit test files.
Browse files Browse the repository at this point in the history
  • Loading branch information
bit365 committed Nov 24, 2024
1 parent 7626268 commit 937a163
Show file tree
Hide file tree
Showing 17 changed files with 688 additions and 57 deletions.
6 changes: 3 additions & 3 deletions EfCore.NamingConverter.Sample/MyDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;

namespace EfCore.NamingConverter
namespace EfCore.NamingConverter.Sample
{
public class MyDbContext : DbContext
{
Expand All @@ -12,12 +12,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseNpgsql("Host=localhost;Database=testdb;Username=postgres;Password=postgres");
optionsBuilder.UseNpgsql("Host=localhost;Database=mydb;Username=postgres;Password=postgres");
}

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.AddNamingConventions(NamingPolicy.CamelCase);
configurationBuilder.AddNamingConventions(NamingPolicy.SnakeCaseLower);
}
}
}
2 changes: 1 addition & 1 deletion EfCore.NamingConverter.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EfCore.NamingConverter;
using EfCore.NamingConverter.Sample;

MyDbContext context = new();

Expand Down
93 changes: 93 additions & 0 deletions EfCore.NamingConverter.Tests/CamelCaseNamingPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
namespace EfCore.NamingConverter.Tests
{
public class CamelCaseNamingPolicyTests
{
[Fact]
public void ConvertName_ShouldConvertToCamelCase()
{
// Arrange
var policy = new CamelCaseNamingPolicy();
var input = "TestName";
var expected = "testName";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldHandleNullInput()
{
// Arrange
var policy = new CamelCaseNamingPolicy();

// Act
var result = policy.ConvertName(null);

// Assert
Assert.Null(result);
}

[Fact]
public void ConvertName_ShouldHandleEmptyString()
{
// Arrange
var policy = new CamelCaseNamingPolicy();
var input = string.Empty;
var expected = string.Empty;

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldHandleSingleWord()
{
// Arrange
var policy = new CamelCaseNamingPolicy();
var input = "Word";
var expected = "word";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldHandleMultipleWords()
{
// Arrange
var policy = new CamelCaseNamingPolicy();
var input = "MultipleWordsInString";
var expected = "multipleWordsInString";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldNotChangeAlreadyCamelCase()
{
// Arrange
var policy = new CamelCaseNamingPolicy();
var input = "alreadyCamelCase";
var expected = "alreadyCamelCase";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}
}
}
17 changes: 17 additions & 0 deletions EfCore.NamingConverter.Tests/ColumnNameConventionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace EfCore.NamingConverter.Tests
{
public class ColumnNameConventionTests
{
[Theory]
[InlineData(NamingPolicy.CamelCase, "TestName", "testName")]
[InlineData(NamingPolicy.SnakeCaseLower, "TestName", "test_name")]
[InlineData(NamingPolicy.SnakeCaseUpper, "TestName", "TEST_NAME")]
[InlineData(NamingPolicy.KebabCaseLower, "TestName", "test-name")]
[InlineData(NamingPolicy.KebabCaseUpper, "TestName", "TEST-NAME")]
public void ProcessPropertyAdded_ShouldConvertColumnName(NamingPolicy namingPolicy, string originalName, string expectedName)
{
// TODO: The test is not complete. You need to create a mock for IConventionPropertyBuilder and IConventionContext<IConventionPropertyBuilder>
}
}
}

51 changes: 51 additions & 0 deletions EfCore.NamingConverter.Tests/ConvertNamingPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace EfCore.NamingConverter.Tests
{
public class ConvertNamingPolicyTests
{
[Theory]
[InlineData(NamingPolicy.Unspecified, typeof(NoneCaseNamingPolicy))]
[InlineData(NamingPolicy.CamelCase, typeof(CamelCaseNamingPolicy))]
[InlineData(NamingPolicy.SnakeCaseLower, typeof(SnakeCaseLowerNamingPolicy))]
[InlineData(NamingPolicy.SnakeCaseUpper, typeof(SnakeCaseUpperNamingPolicy))]
[InlineData(NamingPolicy.KebabCaseLower, typeof(KebabCaseLowerNamingPolicy))]
[InlineData(NamingPolicy.KebabCaseUpper, typeof(KebabCaseUpperNamingPolicy))]
public void From_ShouldReturnCorrectPolicy(NamingPolicy namingPolicy, Type expectedType)
{
// Act
var result = ConvertNamingPolicy.From(namingPolicy);

// Assert
Assert.IsType(expectedType, result);
}

[Fact]
public void From_ShouldThrowArgumentOutOfRangeExceptionForInvalidPolicy()
{
// Arrange
var invalidPolicy = (NamingPolicy)999;

// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => ConvertNamingPolicy.From(invalidPolicy));
}

[Theory]
[InlineData("TestName", "testName", NamingPolicy.CamelCase)]
[InlineData("TestName", "test_name", NamingPolicy.SnakeCaseLower)]
[InlineData("TestName", "TEST_NAME", NamingPolicy.SnakeCaseUpper)]
[InlineData("TestName", "test-name", NamingPolicy.KebabCaseLower)]
[InlineData("TestName", "TEST-NAME", NamingPolicy.KebabCaseUpper)]
[InlineData("TestName", "TestName", NamingPolicy.Unspecified)]
public void ConvertName_ShouldConvertNameAccordingToPolicy(string input, string expected, NamingPolicy namingPolicy)
{
// Arrange
var policy = ConvertNamingPolicy.From(namingPolicy);

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<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" />
<PackageReference Include="Moq" Version="4.20.72" />
</ItemGroup>

<ItemGroup>
Expand Down
78 changes: 78 additions & 0 deletions EfCore.NamingConverter.Tests/KebabCaseLowerNamingPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace EfCore.NamingConverter.Tests
{
public class KebabCaseLowerNamingPolicyTests
{
[Fact]
public void ConvertName_ShouldConvertToKebabCaseLower()
{
// Arrange
var policy = new KebabCaseLowerNamingPolicy();
var input = "TestName";
var expected = "test-name";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldThrowOnNullInput()
{
// Arrange
var policy = new KebabCaseLowerNamingPolicy();

// Act
void action() => policy.ConvertName(null);

// Assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void ConvertName_ShouldHandleEmptyString()
{
// Arrange
var policy = new KebabCaseLowerNamingPolicy();
var input = string.Empty;
var expected = string.Empty;

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldHandleSingleWord()
{
// Arrange
var policy = new KebabCaseLowerNamingPolicy();
var input = "Word";
var expected = "word";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldHandleMultipleWords()
{
// Arrange
var policy = new KebabCaseLowerNamingPolicy();
var input = "MultipleWordsInString";
var expected = "multiple-words-in-string";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}
}
}
75 changes: 75 additions & 0 deletions EfCore.NamingConverter.Tests/KebabCaseUpperNamingPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace EfCore.NamingConverter.Tests
{
public class KebabCaseUpperNamingPolicyTests
{
[Fact]
public void ConvertName_ShouldConvertToKebabCaseUpper()
{
// Arrange
var policy = new KebabCaseUpperNamingPolicy();
var input = "TestName";
var expected = "TEST-NAME";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldThrowArgumentNullException()
{
// Arrange
var policy = new KebabCaseUpperNamingPolicy();

// Act & Assert
Assert.Throws<ArgumentNullException>(() => policy.ConvertName(null));
}

[Fact]
public void ConvertName_ShouldHandleEmptyString()
{
// Arrange
var policy = new KebabCaseUpperNamingPolicy();
var input = string.Empty;
var expected = string.Empty;

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldHandleSingleWord()
{
// Arrange
var policy = new KebabCaseUpperNamingPolicy();
var input = "Word";
var expected = "WORD";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ConvertName_ShouldHandleMultipleWords()
{
// Arrange
var policy = new KebabCaseUpperNamingPolicy();
var input = "MultipleWordsInString";
var expected = "MULTIPLE-WORDS-IN-STRING";

// Act
var result = policy.ConvertName(input);

// Assert
Assert.Equal(expected, result);
}
}
}
Loading

0 comments on commit 937a163

Please sign in to comment.