Skip to content

Commit

Permalink
Merge pull request #24 from JohnnySenior/users/ZafarUrakov/exposers-c…
Browse files Browse the repository at this point in the history
…reate-efxceptions-context

EXPOSERS: Create EFxceptions Context
  • Loading branch information
cjdutoit authored Apr 29, 2024
2 parents 9403b00 + 851d9ec commit a8dcdb4
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 17 deletions.
13 changes: 0 additions & 13 deletions STX.EFxceptions.SqlServer.Tests.Acceptance/DeleteMe.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// --------------------------------------------------------------------------------

using System;
using STX.EFxceptions.Abstractions.Models.Exceptions;
using STX.EFxceptions.SqlServer.Tests.Acceptance.Models.Clients;

namespace STX.EFxceptions.SqlServer.Tests.Acceptance
{
public partial class EFxceptionsContextTests
{
[Fact]
public void ShouldSaveChangesSuccessfully()
{
// given
var client = new Client
{
Id = Guid.NewGuid()
};

// when
context.Clients.Add(client);
context.SaveChanges();

// then
context.Clients.Remove(client);
context.SaveChanges();
}

[Fact]
public void ShouldThrowDuplicateKeyExceptionOnSaveChanges()
{
// given
var client = new Client
{
Id = Guid.NewGuid()
};

// when . then
Assert.Throws<DuplicateKeyException>(() =>
{
try
{
for (int i = 0; i < 2; i++)
{
context.Clients.Add(client);
context.SaveChanges();
}
}
catch (ArgumentException argumentException)
{
throw new DuplicateKeyException(argumentException.Message);
}
finally
{
context.Clients.Remove(client);
context.SaveChanges();
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// --------------------------------------------------------------------------------


using Microsoft.EntityFrameworkCore;

namespace STX.EFxceptions.SqlServer.Tests.Acceptance
{
public partial class EFxceptionsContextTests
{
private readonly DbContextOptions<EFxceptionsContext> options;
private readonly MyEFxceptionsContext context;

public EFxceptionsContextTests()
{
options = new DbContextOptionsBuilder<EFxceptionsContext>()
.UseInMemoryDatabase(databaseName: "InMemoryDatabase")
.Options;

this.context = new MyEFxceptionsContext(options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;

namespace STX.EFxceptions.SqlServer.Tests.Acceptance.Models.Clients
{
public class Client
{
public Guid Id { get; set; }
}
}
23 changes: 23 additions & 0 deletions STX.EFxceptions.SqlServer.Tests.Acceptance/MyEFxceptionsContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.SqlServer.Tests.Acceptance.Models.Clients;

namespace STX.EFxceptions.SqlServer.Tests.Acceptance
{
public class MyEFxceptionsContext : EFxceptionsContext
{
public MyEFxceptionsContext(DbContextOptions<EFxceptionsContext> options)
: base(options)
{ }

public DbSet<Client> Clients { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
Expand All @@ -20,4 +21,8 @@
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\STX.EFxceptions.SqlServer\STX.EFxceptions.SqlServer.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions STX.EFxceptions.SqlServer/EFxceptionsContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ----------------------------------------------------------------------------------
// Copyright(c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using STX.EFxceptions.Abstractions.Brokers.DbErrorBroker;
using STX.EFxceptions.Abstractions.Services.EFxceptions;
using STX.EFxceptions.Core;
using STX.EFxceptions.SqlServer.Base.Brokers.DbErrorBroker;
using STX.EFxceptions.SqlServer.Base.Services.Foundations;

namespace STX.EFxceptions.SqlServer
{
public abstract class EFxceptionsContext : DbContextBase<SqlException>
{
public EFxceptionsContext(DbContextOptions<EFxceptionsContext> options)
: base(options)
{ }

protected EFxceptionsContext()
: base()
{ }

protected override IDbErrorBroker<SqlException> CreateErrorBroker() =>
new SqlServerErrorBroker();

protected override IEFxceptionService CreateEFxceptionService(
IDbErrorBroker<SqlException> errorBroker)
{
return new SqlServerEFxceptionService(errorBroker);
}
}
}
8 changes: 7 additions & 1 deletion STX.EFxceptions.SqlServer/STX.EFxceptions.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="STX.EFxceptions.Abstractions" Version="0.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
<PackageReference Include="STX.EFxceptions.Core" Version="0.1.6" />
<PackageReference Include="STX.EFxceptions.Abstractions" Version="0.1.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\STX.EFxceptions.SqlServer.Base\STX.EFxceptions.SqlServer.Base.csproj" />
</ItemGroup>

</Project>

0 comments on commit a8dcdb4

Please sign in to comment.