Skip to content

Commit 95f932e

Browse files
committed
Fix: Correctly working Tests
1 parent 40ce13e commit 95f932e

File tree

15 files changed

+408
-18
lines changed

15 files changed

+408
-18
lines changed

CodeOfChaos.Types.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Types.UnitOfWor
2626
EndProject
2727
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.CodeOfChaos.Types.UnitOfWork", "tests\Tests.CodeOfChaos.Types.UnitOfWork\Tests.CodeOfChaos.Types.UnitOfWork.csproj", "{58C5DC7B-083F-42B0-AE5E-A7F662C0EF39}"
2828
EndProject
29+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChoas.Types.UnitOfWork.Contracts", "src\CodeOfChoas.Types.UnitOfWork.Contracts\CodeOfChoas.Types.UnitOfWork.Contracts.csproj", "{9B998187-74B9-4B75-83DD-76CF822BAFB3}"
30+
EndProject
2931
Global
3032
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3133
Debug|Any CPU = Debug|Any CPU
@@ -72,6 +74,10 @@ Global
7274
{58C5DC7B-083F-42B0-AE5E-A7F662C0EF39}.Debug|Any CPU.Build.0 = Debug|Any CPU
7375
{58C5DC7B-083F-42B0-AE5E-A7F662C0EF39}.Release|Any CPU.ActiveCfg = Release|Any CPU
7476
{58C5DC7B-083F-42B0-AE5E-A7F662C0EF39}.Release|Any CPU.Build.0 = Release|Any CPU
77+
{9B998187-74B9-4B75-83DD-76CF822BAFB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
78+
{9B998187-74B9-4B75-83DD-76CF822BAFB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
79+
{9B998187-74B9-4B75-83DD-76CF822BAFB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
80+
{9B998187-74B9-4B75-83DD-76CF822BAFB3}.Release|Any CPU.Build.0 = Release|Any CPU
7581
EndGlobalSection
7682
GlobalSection(NestedProjects) = preSolution
7783
{26284571-0E09-4BAF-8C2B-DF87DCC1BA0B} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
@@ -84,5 +90,6 @@ Global
8490
{46E31210-89A5-4E2B-8C89-4757ED54E869} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
8591
{938BE8FB-AAB5-4D15-82FC-DFABC83DB2F1} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
8692
{58C5DC7B-083F-42B0-AE5E-A7F662C0EF39} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
93+
{9B998187-74B9-4B75-83DD-76CF822BAFB3} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
8794
EndGlobalSection
8895
EndGlobal

src/CodeOfChaos.Types.UnitOfWork/CodeOfChaos.Types.UnitOfWork.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<ItemGroup>
1515
<ProjectReference Include="..\CodeOfChaos.Types\CodeOfChaos.Types.csproj" />
16+
<ProjectReference Include="..\CodeOfChoas.Types.UnitOfWork.Contracts\CodeOfChoas.Types.UnitOfWork.Contracts.csproj" />
1617
</ItemGroup>
1718

1819
</Project>

src/CodeOfChaos.Types.UnitOfWork/UnitOfWork.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public virtual async ValueTask<bool> TryCreateTransactionAsync(CancellationToken
3838
if (_transaction != null) return false;
3939

4040
TDbContext dbContext = await _db.GetValueAsync(ct);
41-
if (dbContext.Database.CurrentTransaction != null) return false;
41+
if (dbContext.Database.CurrentTransaction != null) {
42+
// Something went wrong during saving before and the transaction wasn't set by the unit of work
43+
_transaction = dbContext.Database.CurrentTransaction;
44+
return true;
45+
}
4246

4347
_transaction = await dbContext.Database.BeginTransactionAsync(ct);
4448

@@ -49,7 +53,7 @@ public virtual async ValueTask<bool> TryRollbackTransactionAsync(CancellationTok
4953
if (_transaction == null) return false;
5054

5155
await _transaction.RollbackAsync(ct);
52-
_transaction.Dispose();
56+
await _transaction.DisposeAsync();
5357
_transaction = null;
5458

5559
return true;
@@ -92,11 +96,7 @@ public virtual TRepo GetRepository<TRepo>() where TRepo : class, IRepository {
9296
}
9397

9498
public virtual async ValueTask DisposeAsync() {
95-
if (_transaction != null) {
96-
await TryRollbackTransactionAsync();
97-
await _transaction.DisposeAsync();
98-
_transaction = null;
99-
}
99+
if (_transaction != null) await TryRollbackTransactionAsync();
100100

101101
if (!AttachedRepositories.IsEmpty) {
102102
// First detach all references to this unit of work
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<RootNamespace>CodeOfChaos.Types.UnitOfWork</RootNamespace>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="CodeOfChaos.Extensions.DependencyInjection" Version="0.32.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
14+
</ItemGroup>
15+
16+
</Project>

tests/Tests.CodeOfChaos.Types.TypedValueStore/TypedValueStoreTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public async Task GetOrAdd_ShouldReturnExistingValue() {
387387

388388
// Act
389389
// Try to get the value using GetOrAdd, if the key exists it should return the existing value
390-
var result = store.GetOrAdd(key, () => newValue);
390+
string? result = store.GetOrAdd(key, () => newValue);
391391

392392
// Assert
393393
await Assert.That(result).IsEqualTo(existingValue).Because("Expected the existing value to be returned.");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.EntityFrameworkCore;
5+
6+
namespace Tests.CodeOfChaos.Types.UnitOfWork.Assets;
7+
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class DefaultDbContext : DbContext {
12+
public DefaultDbContext() : base() {}
13+
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options) {}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.EntityFrameworkCore;
5+
6+
namespace Tests.CodeOfChaos.Types.UnitOfWork.Assets;
7+
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class MockDbContext : DbContext {
12+
public MockDbContext() : base() {}
13+
public MockDbContext(DbContextOptions<MockDbContext> options) : base(options) { }
14+
15+
}

0 commit comments

Comments
 (0)