Skip to content

Commit 9fcbdf6

Browse files
committed
added test project
1 parent 3a2c13b commit 9fcbdf6

File tree

8 files changed

+222
-12
lines changed

8 files changed

+222
-12
lines changed

FileContextCore-Tests/Data/Base.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Text;
5+
6+
namespace FileContextCore_Tests.Data
7+
{
8+
public class Base
9+
{
10+
public Base()
11+
{
12+
Id = Guid.NewGuid();
13+
CreatedOn = DateTime.Now;
14+
}
15+
16+
[Key]
17+
public Guid Id { get; set; }
18+
19+
public DateTime CreatedOn { get; set; }
20+
21+
public DateTime UpdatedOn { get; set; }
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace FileContextCore_Tests.Data
6+
{
7+
public class Entry : Base
8+
{
9+
public string Content { get; set; }
10+
11+
public int SomeInt { get; set; }
12+
13+
public double SomeDouble { get; set; }
14+
}
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using FileContextCore.Extensions;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace FileContextCore_Tests.Data
8+
{
9+
public class TestContext : DbContext
10+
{
11+
private readonly string _serializer = "json";
12+
private readonly string _filemanager = "default";
13+
14+
public TestContext(string serializer, string filemanager)
15+
{
16+
_serializer = serializer;
17+
_filemanager = filemanager;
18+
}
19+
20+
public TestContext(DbContextOptions options) : base(options)
21+
{
22+
23+
}
24+
25+
public DbSet<Entry> Entries { get; set; }
26+
27+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
28+
{
29+
optionsBuilder.UseFileContext(_serializer, _filemanager);
30+
}
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<RootNamespace>FileContextCore_Tests</RootNamespace>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Bogus" Version="27.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
14+
<PackageReference Include="xunit" Version="2.4.0" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\FileContextCore\FileContextCore.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Bogus;
5+
using FileContextCore_Tests.Data;
6+
7+
namespace FileContextCore_Tests.Helper
8+
{
9+
public static class ModelCreator
10+
{
11+
private static readonly Faker<Entry> entryFaker;
12+
13+
static ModelCreator()
14+
{
15+
entryFaker = new Faker<Entry>()
16+
.RuleFor(u => u.Content, (f, u) => f.Name.FirstName())
17+
.RuleFor(u => u.SomeInt, (f, u) => f.Random.Int())
18+
.RuleFor(u => u.SomeDouble, (f, u) => f.Random.Double());
19+
}
20+
21+
public static Entry GenerateEntry()
22+
{
23+
return entryFaker.Generate();
24+
}
25+
}
26+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using FileContextCore_Tests.Data;
6+
using FileContextCore_Tests.Helper;
7+
using Microsoft.EntityFrameworkCore.Internal;
8+
using Microsoft.EntityFrameworkCore.Storage;
9+
using Xunit;
10+
11+
namespace FileContextCore_Tests
12+
{
13+
public class TransactionTests
14+
{
15+
[Fact]
16+
public void TransactionSuccess()
17+
{
18+
TestContext db = new TestContext("json", "default");
19+
db.Entries.RemoveRange(db.Entries);
20+
db.SaveChanges();
21+
22+
Assert.Empty(db.Entries);
23+
24+
using (IDbContextTransaction transaction = db.Database.BeginTransaction())
25+
{
26+
try
27+
{
28+
db.Entries.Add(ModelCreator.GenerateEntry());
29+
db.Entries.Add(ModelCreator.GenerateEntry());
30+
db.Entries.Add(ModelCreator.GenerateEntry());
31+
db.SaveChanges();
32+
33+
db.Entries.Add(ModelCreator.GenerateEntry());
34+
db.Entries.Add(ModelCreator.GenerateEntry());
35+
db.Entries.Add(ModelCreator.GenerateEntry());
36+
db.SaveChanges();
37+
38+
transaction.Commit();
39+
}
40+
catch (Exception ex)
41+
{
42+
transaction.Rollback();
43+
}
44+
}
45+
46+
Assert.Equal(6, db.Entries.Count());
47+
}
48+
49+
[Fact]
50+
public void TransactionFailureRollback()
51+
{
52+
TestContext db = new TestContext("json", "default");
53+
db.Entries.RemoveRange(db.Entries);
54+
db.SaveChanges();
55+
56+
Assert.Empty(db.Entries);
57+
58+
using (IDbContextTransaction transaction = db.Database.BeginTransaction())
59+
{
60+
try
61+
{
62+
db.Entries.Add(ModelCreator.GenerateEntry());
63+
db.Entries.Add(ModelCreator.GenerateEntry());
64+
db.Entries.Add(ModelCreator.GenerateEntry());
65+
db.SaveChanges();
66+
67+
db.Entries.Add(ModelCreator.GenerateEntry());
68+
db.Entries.Add(ModelCreator.GenerateEntry());
69+
db.Entries.Add(ModelCreator.GenerateEntry());
70+
db.SaveChanges();
71+
72+
throw new Exception("Should perform rollback");
73+
74+
transaction.Commit();
75+
}
76+
catch (Exception ex)
77+
{
78+
transaction.Rollback();
79+
db.SaveChanges();
80+
}
81+
}
82+
83+
Assert.Equal(6, db.Entries.Count());
84+
}
85+
}
86+
}

FileContextCore.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2010
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28803.352
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileContextCore", "FileContextCore\FileContextCore.csproj", "{F8A3A7CB-1F80-4021-B3BB-88919CB1B005}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "Example\Example.csproj", "{B6D2852A-973B-47F7-A390-7A1A5203E5F4}"
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetOAuth", "AspNetOAuth\AspNetOAuth.csproj", "{A78088E6-B64B-4D98-96CE-2C30D82E3187}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileContextCore-Tests", "FileContextCore-Tests\FileContextCore-Tests.csproj", "{8E939885-B2A2-46DD-8255-94349EE01476}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{A78088E6-B64B-4D98-96CE-2C30D82E3187}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{A78088E6-B64B-4D98-96CE-2C30D82E3187}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{A78088E6-B64B-4D98-96CE-2C30D82E3187}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{8E939885-B2A2-46DD-8255-94349EE01476}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{8E939885-B2A2-46DD-8255-94349EE01476}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{8E939885-B2A2-46DD-8255-94349EE01476}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{8E939885-B2A2-46DD-8255-94349EE01476}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

FileContextCore/Extensions/Internal/FileContextLoggerExtensions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ static class FileContextLoggerExtensions
2424
public static void TransactionIgnoredWarning(
2525
[NotNull] this IDiagnosticsLogger<DbLoggerCategory.Database.Transaction> diagnostics)
2626
{
27-
EventDefinition definition = FileContextStrings.LogTransactionsNotSupported;
27+
//EventDefinition definition = FileContextStrings.LogTransactionsNotSupported;
2828

29-
definition.Log(diagnostics, WarningBehavior.Log);
29+
//definition.Log(diagnostics, WarningBehavior.Log);
3030

31-
if (diagnostics.DiagnosticSource.IsEnabled(definition.EventId.Name))
32-
{
33-
diagnostics.DiagnosticSource.Write(
34-
definition.EventId.Name,
35-
new EventData(
36-
definition,
37-
(d, p) => ((EventDefinition)d).GenerateMessage()));
38-
}
31+
//if (diagnostics.DiagnosticSource.IsEnabled(definition.EventId.Name))
32+
//{
33+
// diagnostics.DiagnosticSource.Write(
34+
// definition.EventId.Name,
35+
// new EventData(
36+
// definition,
37+
// (d, p) => ((EventDefinition)d).GenerateMessage()));
38+
//}
3939
}
4040

4141
/// <summary>

0 commit comments

Comments
 (0)