-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteMappedQueryTests.cs
More file actions
109 lines (94 loc) · 3.22 KB
/
Copy pathSQLiteMappedQueryTests.cs
File metadata and controls
109 lines (94 loc) · 3.22 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
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using DBAClientX;
using Xunit;
namespace DBAClientX.Tests;
public sealed class SQLiteMappedQueryTests
{
private readonly record struct SampleRow(long Id, string? Name);
[Fact]
public async Task QueryReadOnlyAsync_WithEmptySql_Throws()
{
var sqlite = new SQLite();
var exception = await Assert.ThrowsAsync<ArgumentException>(() =>
sqlite.QueryReadOnlyAsync(":memory:", " "));
Assert.Equal("query", exception.ParamName);
}
[Fact]
public async Task QueryReadOnlyAsListAsync_SimpleSelect_ReturnsMappedRows()
{
string database = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.db");
try
{
var sqlite = new SQLite();
await sqlite.ExecuteNonQueryAsync(database, "CREATE TABLE t (id INTEGER NOT NULL, name TEXT NULL);");
await sqlite.ExecuteNonQueryAsync(database, "INSERT INTO t (id, name) VALUES (1, 'a'), (2, NULL);");
var rows = await sqlite.QueryReadOnlyAsListAsync(
database,
"SELECT id, name FROM t ORDER BY id;",
r => new SampleRow(
r.GetInt64(0),
r.IsDBNull(1) ? null : r.GetString(1)));
Assert.Equal(2, rows.Count);
Assert.Equal(new SampleRow(1, "a"), rows[0]);
Assert.Equal(new SampleRow(2, null), rows[1]);
}
finally
{
try
{
if (File.Exists(database))
{
File.Delete(database);
}
}
catch
{
// Ignore cleanup failures on locked temp files.
}
}
}
[Fact]
public async Task QueryReadOnlyAsListAsync_Canceled_Throws()
{
string database = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.db");
try
{
var sqlite = new SQLite();
await sqlite.ExecuteNonQueryAsync(database, "CREATE TABLE t (id INTEGER NOT NULL);");
await sqlite.ExecuteNonQueryAsync(database, "INSERT INTO t (id) VALUES (1);");
using var cts = new CancellationTokenSource();
cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
sqlite.QueryReadOnlyAsListAsync(
database,
"SELECT id FROM t;",
r => r.GetInt64(0),
cancellationToken: cts.Token));
}
finally
{
try
{
if (File.Exists(database))
{
File.Delete(database);
}
}
catch
{
// Ignore cleanup failures on locked temp files.
}
}
}
[Fact]
public async Task QueryReadOnlyAsListAsync_WithEmptySql_Throws()
{
var sqlite = new SQLite();
var exception = await Assert.ThrowsAsync<ArgumentException>(() =>
sqlite.QueryReadOnlyAsListAsync(":memory:", " ", r => r.GetInt64(0)));
Assert.Equal("query", exception.ParamName);
}
}