-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from NoxOrg/feature/introduce-sql-tests
Introduce sql server tests | introduce country code 2 configurator
- Loading branch information
Showing
20 changed files
with
252 additions
and
47 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
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
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
13 changes: 9 additions & 4 deletions
13
src/Nox.Types.EntityFramework/Exceptions/NoxEntityFrameworkException.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 |
---|---|---|
@@ -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) | ||
{ | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Nox.Types.EntityFramework/Types/CountryCode2/CountryCode2Configurator.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,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>(); | ||
} | ||
} |
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
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
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
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
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
File renamed without changes.
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
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
43 changes: 43 additions & 0 deletions
43
tests/Nox.Tests/DatabaseIntegrationTests/SqlServerIntegrationTests.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,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
69
tests/Nox.Tests/DatabaseIntegrationTests/SqlServerTestBase.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,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(); | ||
} | ||
} |
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
Oops, something went wrong.