1
1
using FluentAssertions ;
2
2
using Microsoft . EntityFrameworkCore ;
3
- using Microsoft . Extensions . DependencyInjection ;
4
- using Npgsql ;
5
3
using Shiny . Auditing ;
6
4
7
5
namespace Shiny . Extensions . EntityFramework . Tests ;
8
6
9
7
10
8
// TODO: check entityId & entityType
11
- public class AuditTests : IDisposable
9
+ public partial class EfTests
12
10
{
13
- readonly TestAuditInfoProvider auditProvider ;
14
- readonly IServiceProvider serviceProvider ;
15
11
16
- public AuditTests ( )
17
- {
18
- this . auditProvider = new ( ) ;
19
-
20
- var services = new ServiceCollection ( ) ;
21
- services . AddSingleton < IAuditInfoProvider > ( this . auditProvider ) ;
22
- services . AddScoped < AuditSaveChangesInterceptor > ( ) ;
23
- services . AddDbContext < TestDbContext > ( ( sp , opts ) =>
24
- {
25
- // .UseSqlite("Data Source=test.db")
26
- opts . UseNpgsql ( new NpgsqlDataSourceBuilder ( "User ID=sa;Password=Blargh911!;Host=localhost;Port=5432;Database=AuditUnitTests;Pooling=true;Connection Lifetime=30;" ) . Build ( ) ) ;
27
- var interceptor = sp . GetRequiredService < AuditSaveChangesInterceptor > ( ) ;
28
- opts . AddInterceptors ( interceptor ) ;
29
- } ) ;
30
- this . serviceProvider = services . BuildServiceProvider ( ) ;
31
-
32
- using var scope = this . serviceProvider . CreateScope ( ) ;
33
- var data = scope . ServiceProvider . GetRequiredService < TestDbContext > ( ) ;
34
- data . Database . EnsureDeleted ( ) ;
35
- data . Database . EnsureCreated ( ) ;
36
-
37
- // TODO: seed
38
- }
39
-
40
- async Task DoDb ( Func < TestDbContext , Task > task )
41
- {
42
- using var scope = this . serviceProvider . CreateScope ( ) ;
43
- var data = scope . ServiceProvider . GetRequiredService < TestDbContext > ( ) ;
44
- await task ( data ) ;
45
- }
46
-
47
-
48
12
[ Fact ]
49
13
public async Task AddAudits ( )
50
14
{
@@ -106,6 +70,15 @@ await this.DoDb(async data =>
106
70
}
107
71
108
72
73
+ void AssertAudit ( AuditEntry audit , DbOperation op )
74
+ {
75
+ audit . Operation . Should ( ) . Be ( op , "Invalid Operation" ) ;
76
+ audit . UserIdentifier . Should ( ) . Be ( "Test User" ) ;
77
+ audit . UserIpAddress . Should ( ) . Be ( "0.0.0.0" ) ;
78
+ audit . AppLocation . Should ( ) . Be ( "UNIT TESTS" ) ;
79
+ }
80
+
81
+
109
82
Task Seed ( ) => this . DoDb ( data =>
110
83
{
111
84
var manu = new Manufacturer { Name = "Cadillac" } ;
@@ -119,23 +92,4 @@ Task Seed() => this.DoDb(data =>
119
92
data . Add ( model ) ;
120
93
return data . SaveChangesAsync ( ) ;
121
94
} ) ;
122
-
123
-
124
- void AssertAudit ( AuditEntry audit , DbOperation op )
125
- {
126
- audit . Operation . Should ( ) . Be ( op , "Invalid Operation" ) ;
127
- audit . UserIdentifier . Should ( ) . Be ( "Test User" ) ;
128
- audit . UserIpAddress . Should ( ) . Be ( "0.0.0.0" ) ;
129
- audit . AppLocation . Should ( ) . Be ( "UNIT TESTS" ) ;
130
- }
131
-
132
- public void Dispose ( ) => ( this . serviceProvider as IDisposable ) ? . Dispose ( ) ;
133
- }
134
-
135
- public class TestAuditInfoProvider : IAuditInfoProvider
136
- {
137
- public string ? AppLocation { get ; set ; } = "UNIT TESTS" ;
138
- public string ? Tenant { get ; set ; } = "Test Tenant" ;
139
- public string ? UserIdentifier { get ; set ; } = "Test User" ;
140
- public string ? UserIpAddress { get ; set ; } = "0.0.0.0" ;
141
95
}
0 commit comments