-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_setup.cs
More file actions
74 lines (59 loc) · 1.84 KB
/
_setup.cs
File metadata and controls
74 lines (59 loc) · 1.84 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
using SQLitePCL;
using System.Runtime.CompilerServices;
namespace NoSQLite.Test;
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Initialize()
{
Randomizer.Seed = new Random(420690001);
}
}
public abstract class TestBase
{
protected string DbPath { get; private set; } = null!;
protected sqlite3 Db { get; private set; } = null!;
protected NoSQLiteConnection Connection { get; private set; } = null!;
protected JsonSerializerOptions? JsonOptions { get; }
private bool Delete { get; } = true;
protected TestBase(JsonSerializerOptions? jsonOptions = null)
{
JsonOptions = jsonOptions;
}
[Before(HookType.Test)]
public async Task BeforeAsync()
{
var dir = Path.Combine(Environment.CurrentDirectory, "databases");
var now = TimeProvider.System.GetTimestamp();
DbPath = Path.Combine(dir, $"{GetType().Name}_{now}.sqlite3");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
if (Delete && File.Exists(DbPath))
{
File.Delete(DbPath);
}
Batteries_V2.Init();
Db = sqlite3.Create(DbPath, useWal: true);
Connection = new NoSQLiteConnection(Db, JsonOptions);
await That(File.Exists(DbPath)).IsTrue();
}
public NoSQLiteTable GetTable([CallerMemberName] string? caller = null)
{
return Connection.GetTable($"{caller}");
}
[After(HookType.Test)]
public async Task AfterAsync()
{
Connection.Dispose();
Db.CloseAndDispose();
await That(Connection.Tables.Count).IsEqualTo(0);
await That(File.Exists($"{DbPath}-shm")).IsFalse();
await That(File.Exists($"{DbPath}-wal")).IsFalse();
if (Delete)
{
File.Delete(DbPath);
}
}
}