-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMDSQLiteAdapterTests.cs
More file actions
96 lines (87 loc) · 3.03 KB
/
MDSQLiteAdapterTests.cs
File metadata and controls
96 lines (87 loc) · 3.03 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace PowerSync.Common.Tests.MDSQLite;
using Microsoft.Data.Sqlite;
using PowerSync.Common.Client;
using PowerSync.Common.MDSQLite;
using PowerSync.Common.Tests.Utils;
using PowerSync.Common.Utils;
/// <summary>
/// dotnet test -v n --framework net8.0 --filter "MDSQLiteAdapterTests"
/// </summary>
[Collection("MDSQLiteAdapterTests")]
public class MDSQLiteAdapterTests
{
private class AssetResult
{
public string id { get; set; } = "";
public string description { get; set; } = "";
public string? make { get; set; }
}
[Fact]
public async Task DisablingCoreExtensionPreventsPowerSyncFromLoading()
{
var dbName = $"MDSQLiteAdapter-{Guid.NewGuid():N}.db";
var db = new PowerSyncDatabase(new PowerSyncDatabaseOptions
{
Database = new MDSQLiteDBOpenFactory(new MDSQLiteOpenFactoryOptions
{
DbFilename = dbName,
SqliteOptions = new MDSQLiteOptions
{
LoadPowerSyncExtension = false,
Extensions = [],
},
}),
Schema = TestSchema.AppSchema,
});
try
{
// Without the PowerSync extension, `powersync_init()` is not a registered function.
await Assert.ThrowsAsync<SqliteException>(async () => await db.Init());
}
finally
{
try { await db.Close(); } catch { /* expected — init failed */ }
DatabaseUtils.CleanDb(dbName);
}
}
[Fact]
public async Task LoadsCustomPowerSyncExtensionFromOverriddenPath()
{
var dbName = $"MDSQLiteAdapter-{Guid.NewGuid():N}.db";
var sourcePath = PowerSyncPathResolver.GetNativeLibraryPath(AppContext.BaseDirectory);
var customPath = Path.Combine(
Path.GetTempPath(),
$"powersync-ext-copy-{Guid.NewGuid():N}{Path.GetExtension(sourcePath)}"
);
File.Copy(sourcePath, customPath, overwrite: true);
var db = new PowerSyncDatabase(new PowerSyncDatabaseOptions
{
Database = new MDSQLiteDBOpenFactory(new MDSQLiteOpenFactoryOptions
{
DbFilename = dbName,
SqliteOptions = new MDSQLiteOptions
{
LoadPowerSyncExtension = false,
Extensions = [
new SqliteExtension { Path = customPath, EntryPoint = "sqlite3_powersync_init" },
],
},
}),
Schema = TestSchema.AppSchema,
});
try
{
await db.Init();
var id = await TestUtils.InsertRandomAsset(db);
var rows = await db.GetAll<AssetResult>("SELECT id, description, make FROM assets");
Assert.Single(rows);
Assert.Equal(id, rows[0].id);
}
finally
{
await db.Close();
DatabaseUtils.CleanDb(dbName);
try { File.Delete(customPath); } catch { /* best-effort cleanup */ }
}
}
}