Skip to content

Commit

Permalink
Merge pull request #170 from NoxOrg/feature/introduce-sql-tests
Browse files Browse the repository at this point in the history
Introduce sql server tests | introduce country code 2 configurator
  • Loading branch information
rochar authored Jul 6, 2023
2 parents 8d2df06 + b7c3ff6 commit 8bea006
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public class SqlServerDatabaseProvider: NoxDatabaseConfigurator, INoxDatabasePro
private static readonly Dictionary<NoxType, INoxTypeDatabaseConfigurator> TypesConfiguration =
new()
{
{ NoxType.Text, new SqlServerTextDatabaseConfigurator() }, //Use SqlServer Implementation
{ NoxType.Number, new NumberDatabaseConfigurator() }, // use default implementation
{ NoxType.Money, new MoneyDatabaseConfigurator() } // use default implementation
// Use default implementation for all types
{ NoxType.Text, new SqlServerTextDatabaseConfigurator() },
{ NoxType.Number, new NumberDatabaseConfigurator() },
{ NoxType.Money, new MoneyDatabaseConfigurator() },
{ NoxType.CountryCode2, new CountryCode2Configurator() }
};

private string _connectionString = string.Empty;
Expand Down
3 changes: 2 additions & 1 deletion src/Nox.EntityFramework.Sqlite/SqliteDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public sealed class SqliteDatabaseProvider : NoxDatabaseConfigurator, INoxDataba
// Use default implementation for all types
{ NoxType.Text, new TextDatabaseConfigurator() },
{ NoxType.Number, new NumberDatabaseConfigurator() },
{ NoxType.Money, new MoneyDatabaseConfigurator() }
{ NoxType.Money, new MoneyDatabaseConfigurator() },
{ NoxType.CountryCode2, new CountryCode2Configurator() }
};

public SqliteDatabaseProvider() : base(TypesConfiguration)
Expand Down
21 changes: 10 additions & 11 deletions src/Nox.Generator/Nox.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<PlatformTarget>AnyCPU</PlatformTarget>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand All @@ -54,19 +55,17 @@
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PKGYamlDotNet)\lib\netstandard2.0\YamlDotNet.dll"
IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGHumanizer_Core)\lib\netstandard2.0\Humanizer.dll"
IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGFluentValidation)\lib\netstandard2.0\FluentValidation.dll"
IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker
Include="..\..\src\Nox.Solution\bin\$(Configuration)\netstandard2.0\Nox.Solution.dll"
IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="..\..\src\Nox.Types\bin\$(Configuration)\netstandard2.0\Nox.Types.dll"
IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGYamlDotNet)\lib\netstandard2.0\YamlDotNet.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGHumanizer_Core)\lib\netstandard2.0\Humanizer.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGFluentValidation)\lib\netstandard2.0\FluentValidation.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="..\..\src\Nox.Solution\bin\$(Configuration)\netstandard2.0\Nox.Solution.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="..\..\src\Nox.Types\bin\$(Configuration)\netstandard2.0\Nox.Types.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using System.Runtime.Serialization;

namespace Nox.Types.EntityFramework.Exceptions;

[Serializable]
public class NoxEntityFrameworkException : Exception
{
public NoxEntityFrameworkException()
{
}

public NoxEntityFrameworkException(string message)
: base(message)
public NoxEntityFrameworkException(string message) : base(message)
{
}

public NoxEntityFrameworkException(string message, Exception inner) : base(message, inner)
{
}

public NoxEntityFrameworkException(string message, Exception inner)
: base(message, inner)
protected NoxEntityFrameworkException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Nox.Solution;
using Nox.Types.EntityFramework.Abstractions;

namespace Nox.Types.EntityFramework.Types;

public class CountryCode2Configurator : INoxTypeDatabaseConfigurator
{
public void ConfigureEntityProperty(EntityTypeBuilder builder, NoxSimpleTypeDefinition property, bool isKey)
{
if (isKey)
{
builder.HasKey(property.Name);
}

builder
.Property(property.Name)
.IsRequired(isKey || property.IsRequired)
.IsUnicode(false)
.IsFixedLength(true)
.HasMaxLength(2)
.HasConversion<CountryCode2Converter>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class MoneyDatabaseConfigurator : INoxTypeDatabaseConfigurator

public void ConfigureEntityProperty(EntityTypeBuilder builder, NoxSimpleTypeDefinition property, bool isKey)
{
//Todo Default values from static property in the Nox.Type
// TODO: Default values from static property in the Nox.Type
var typeOptions = property.MoneyTypeOptions ?? new MoneyTypeOptions();

if (isKey)
Expand Down
8 changes: 8 additions & 0 deletions src/SampleWebApp/SampleWebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<Target Name="CleanSourceGeneratedFiles" BeforeTargets="BeforeBuild" DependsOnTargets="$(BeforeBuildDependsOn)">
<RemoveDir Directories="Generated" />
</Target>
Expand Down
16 changes: 8 additions & 8 deletions tests/Nox.Generator.Tests/Nox.Generator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.2" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
Expand All @@ -13,9 +21,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.6.0" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.8" />
</ItemGroup>

<ItemGroup>
Expand All @@ -32,7 +37,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Nox.EntityFramework.Sqlite\Nox.EntityFramework.Sqlite.csproj" />
<ProjectReference Include="..\..\src\Nox.Generator\Nox.Generator.csproj" />
<ProjectReference Include="..\..\src\Nox.Solution\Nox.Solution.csproj" />
<ProjectReference Include="..\..\src\Nox.Types\Nox.Types.csproj" />
Expand All @@ -43,7 +47,6 @@
</ItemGroup>

<ItemGroup>
<None Remove="Database\Design\test.solution.nox.yaml" />
<None Remove="yaml\application\generator.nox.yaml" />
<None Remove="yaml\domain\generator.nox.yaml" />
<None Update="files\yaml\application\generator.nox.yaml">
Expand Down Expand Up @@ -86,9 +89,6 @@

<ItemGroup>
<Compile Remove="files\Program.cs" />
<AdditionalFiles Include="Database\Design\test.solution.nox.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AdditionalFiles>
<None Include="files\Program.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
8 changes: 8 additions & 0 deletions tests/Nox.Solution.Tests/Nox.Solution.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="JsonSchema.Net.Generation" Version="3.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ domain:
- name: MoneyTestField
type: money

#- name: CountryCode2TestField
# type: countryCode2
- name: CountryCode2TestField
type: countryCode2

#- name: GeoCoordTestField
# type: latLong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Nox.Solution;
using System;
using Nox.Types.EntityFramework.Abstractions;
using TestDatabaseWebApp.Domain;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public partial class TestEntity : AuditableEntityBase
/// (Optional)
/// </summary>
public Money? MoneyTestField { get; set; } = null!;

/// <summary>
/// (Optional)
/// </summary>
public CountryCode2? CountryCode2TestField { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FluentAssertions;
using Nox.Types;
using TestDatabaseWebApp.Domain;

namespace Nox.Tests.DatabaseIntegrationTests;

public class SqlServerIntegrationTests : SqlServerTestBase
{
// TODO: uncomment when automated and included into pipeline
//[Fact]
public void GeneratedEntity_SqlServer_CanSaveAndReadFields_AllTypes()
{
var text = "TestTextValue";
var number = 123;
var money = 10;
var currencyCode = CurrencyCode.UAH;
var countryCode2 = "UA";

var newItem = new TestEntity()
{
Id = Text.From(countryCode2),
TextTestField = Text.From(text),
NumberTestField = Number.From(number),
MoneyTestField = Money.From(money, currencyCode),
CountryCode2TestField = CountryCode2.From(countryCode2)
};
DbContext.TestEntities.Add(newItem);
DbContext.SaveChanges();

// Force the recreation of DBContext and ensure we have fresh data from database
RecreateDbContext();

var testEntity = DbContext.TestEntities.First();

// TODO: make it work without .Value
testEntity.Id.Value.Should().Be(countryCode2);
testEntity.TextTestField.Value.Should().Be(text);
testEntity.NumberTestField.Value.Should().Be(number);
testEntity.MoneyTestField!.Value.Amount.Should().Be(money);
testEntity.MoneyTestField.Value.CurrencyCode.Should().Be(currencyCode);
testEntity.CountryCode2TestField!.Value.Should().Be(countryCode2);
}
}
69 changes: 69 additions & 0 deletions tests/Nox.Tests/DatabaseIntegrationTests/SqlServerTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Nox.EntityFramework.SqlServer;
using Nox.Solution;
using TestDatabaseWebApp.Infrastructure.Persistence;

namespace Nox.Tests.DatabaseIntegrationTests;

public abstract class SqlServerTestBase : IDisposable
{
// TODO: currently works in manually set up database in docker
// include database setup into repository (mybae use localdb or express)
private const string _databaseNameTemplate = @"test_database_{0}";
private const string _databasePassword = @"";
private static string _inMemoryConnectionString = @"Server=localhost;User Id=SA;Password=" + _databasePassword + ";TrustServerCertificate=True;";
private static string _databaseName = string.Empty;
private const string _testSolutionFile = @"./DatabaseIntegrationTests/Design/test.solution.nox.yaml";
private readonly SqlConnection _connection;

protected TestDatabaseWebAppDbContext DbContext;

protected SqlServerTestBase()
{
_connection = new SqlConnection(_inMemoryConnectionString);
_connection.Open();

_databaseName = string.Format(_databaseNameTemplate, DateTime.UtcNow.Ticks);
var databaseCreation = $"CREATE DATABASE {_databaseName}";
var createDatabaseCommand = new SqlCommand(databaseCreation, _connection);
createDatabaseCommand.ExecuteNonQuery();

_connection.Close();
_connection = new SqlConnection(_inMemoryConnectionString + $"Database={_databaseName};");
_connection.Open();

DbContext = CreateDbContext(_connection);
}

private static TestDatabaseWebAppDbContext CreateDbContext(SqlConnection connection)
{
var databaseConfigurator = new SqlServerDatabaseProvider();
var solution = new NoxSolutionBuilder()
.UseYamlFile(_testSolutionFile)
.Build();

var options = new DbContextOptionsBuilder<TestDatabaseWebAppDbContext>()
.UseSqlServer(connection)
.Options;

var dbContext = new TestDatabaseWebAppDbContext(options, solution, databaseConfigurator);
dbContext.Database.EnsureCreated();

return dbContext;
}

internal void RecreateDbContext()
{
var previousDbContext = DbContext;
DbContext = CreateDbContext(_connection);
previousDbContext.Dispose();
}

public void Dispose()
{
GC.SuppressFinalize(this);
DbContext?.Dispose();
_connection.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using FluentAssertions;
using Nox.Types;
using System.Linq;
using Nox.Generator.Tests.Database;
using TestDatabaseWebApp.Domain;
using Xunit;

namespace Nox.Generator.Test.DatabaseTests;
namespace Nox.Tests.DatabaseIntegrationTests;

public class SqliteIntegrationTests : SqliteTestBase
{
[Fact]
public void GeneratedEntity_CanSaveAndReadFields_AllTypes()
public void GeneratedEntity_Sqlite_CanSaveAndReadFields_AllTypes()
{
// TODO:
// array
Expand Down Expand Up @@ -48,13 +45,15 @@ public void GeneratedEntity_CanSaveAndReadFields_AllTypes()
var number = 123;
var money = 10;
var currencyCode = CurrencyCode.UAH;
var countryCode2 = "UA";

var newItem = new TestEntity()
{
Id = Text.From(text),
TextTestField = Text.From(text),
NumberTestField = Number.From(number),
MoneyTestField = Money.From(money, currencyCode),
CountryCode2TestField = CountryCode2.From(countryCode2)
};
DbContext.TestEntities.Add(newItem);
DbContext.SaveChanges();
Expand All @@ -68,7 +67,8 @@ public void GeneratedEntity_CanSaveAndReadFields_AllTypes()
testEntity.Id.Value.Should().Be(text);
testEntity.TextTestField.Value.Should().Be(text);
testEntity.NumberTestField.Value.Should().Be(number);
testEntity.MoneyTestField.Value.Amount.Should().Be(money);
testEntity.MoneyTestField!.Value.Amount.Should().Be(money);
testEntity.MoneyTestField.Value.CurrencyCode.Should().Be(currencyCode);
testEntity.CountryCode2TestField!.Value.Should().Be(countryCode2);
}
}
Loading

0 comments on commit 8bea006

Please sign in to comment.