-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBaseFixture.cs
More file actions
76 lines (65 loc) · 2.67 KB
/
BaseFixture.cs
File metadata and controls
76 lines (65 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Data.Common;
using AutoMapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NorthwindCRUD.Helpers;
using NorthwindCRUD.Services;
namespace NorthwindCRUD.Tests
{
public class BaseFixture
{
// null! indicates that the member is initialized in other code
private DbConnection? connection = null!;
public DataHelper DataHelper { get; set; } = null!;
public DataHelper DataHelper2 { get; set; } = null!;
[TestInitialize]
public void Initialize()
{
var mappingConfigs = new MapperConfiguration(cfg =>
{
cfg.AddProfile<MappingProfiles>();
});
var mapper = mappingConfigs.CreateMapper();
var pagingService = new PagingService(mapper);
DataContext context = GetInMemoryDatabaseContext();
DataContext context2 = GetInMemoryDatabaseContext();
Assert.AreNotEqual(context.GetHashCode(), context2.GetHashCode(), "Contexts instances should be different.");
Assert.AreEqual(context.Database.GetDbConnection(), context2.Database.GetDbConnection(), "Contexts instances should have the same database connection.");
DataHelper = new DataHelper(context, pagingService, mapper);
DataHelper2 = new DataHelper(context2, pagingService, mapper);
}
protected DataContext GetInMemoryDatabaseContext()
{
if (connection == null)
{
connection = CreateDbConnection();
var context = CreateInMemoryDatabaseContext(connection);
context.Database.EnsureCreated();
// DBSeeder.Seed(context);
return context;
}
else
{
// Create a new Context on already initialized DB connection
return CreateInMemoryDatabaseContext(connection);
}
}
protected DbConnection CreateDbConnection()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
return connection;
}
protected DataContext CreateInMemoryDatabaseContext(DbConnection connection)
{
var options = new DbContextOptionsBuilder<DataContext>()
.UseSqlite(connection)
.EnableSensitiveDataLogging()
// Uncomment the following line for detailed sql EF logs.
// .EnableDetailedErrors().LogTo(Console.WriteLine, LogLevel.Debug)
.Options;
return new DataContext(options);
}
}
}