-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTestUtils.cs
More file actions
75 lines (68 loc) · 2.25 KB
/
TestUtils.cs
File metadata and controls
75 lines (68 loc) · 2.25 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
using System.Runtime.CompilerServices;
using PowerSync.Common.Client;
namespace PowerSync.Common.Tests.Utils;
public static class TestUtils
{
/// <summary>
/// Deep equivalence assertion with line number on failure.
/// </summary>
public static void DeepEquivalent(object? expected, object? actual, [CallerLineNumber] int lineNumber = 0)
{
try
{
Assert.Equivalent(expected, actual, strict: true);
}
catch (Exception ex)
{
throw new Exception($"Equivalence assertion failed at line {lineNumber}: {ex.Message}", ex);
}
}
public static async Task WaitForAsync(Func<bool> condition, TimeSpan? timeout = null)
{
timeout ??= TimeSpan.FromSeconds(5);
var start = DateTime.UtcNow;
while (DateTime.UtcNow - start < timeout)
{
if (condition())
return;
await Task.Delay(50);
}
throw new TimeoutException("Condition not met within timeout");
}
public static async Task WaitForAsync(Func<Task<bool>> condition, TimeSpan? timeout = null)
{
timeout ??= TimeSpan.FromSeconds(5);
var start = DateTime.UtcNow;
while (DateTime.UtcNow - start < timeout)
{
if (await condition())
return;
await Task.Delay(50);
}
throw new TimeoutException("Condition not met within timeout");
}
public static async Task<string> InsertRandomAsset(PowerSyncDatabase db)
{
var id = Guid.NewGuid().ToString();
await db.Execute(
"insert into assets(id, description, make) values (?, ?, ?)",
[id, "some desc", "some make"]
);
return id;
}
public static async Task<string[]> InsertRandomAssets(PowerSyncDatabase db, int assetCount)
{
var ids = Enumerable
.Range(0, assetCount)
.Select(_ => Guid.NewGuid().ToString())
.ToArray();
var parameters = ids
.Select<string, object?[]>(id => [id, "some desc", "some make"])
.ToArray();
await db.ExecuteBatch(
"insert into assets(id, description, make) values (?, ?, ?)",
parameters
);
return ids;
}
}