-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryTest.cs
More file actions
163 lines (148 loc) · 5.13 KB
/
Copy pathInMemoryTest.cs
File metadata and controls
163 lines (148 loc) · 5.13 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
namespace NuExt.System.Data.SQLite.Tests;
public class Tests
{
private static SQLiteDbConnection CreateInMemoryConnection()
{
var csb = new SQLiteConnectionStringBuilder
{
DataSource = ":memory:"
};
return new SQLiteDbConnection(csb.ToString(), true);
}
private static void CreateSampleDataBase(SQLiteDbConnection conn)
{
conn.ExecuteInTransaction(trans =>
{
int result = conn.ExecuteNonQuery(@"
CREATE TABLE Sample (
Id INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
Description TEXT,
Int32Value INTEGER NOT NULL DEFAULT 0,
Int32NullableValue INTEGER,
BoolValue INTEGER NOT NULL DEFAULT 1);");
Debug.Assert(result == 0);
trans.Commit();
});
}
[SetUp]
public void Setup()
{
}
[Test]
public void TestIsDisposed()
{
var conn = CreateInMemoryConnection();
using (conn.SuspendAsserts())
{
conn.Dispose();
Assert.That(conn.IsDisposed, Is.True);
}
}
[Test]
public void TestIsOpen()
{
using var conn = CreateInMemoryConnection();
using(conn.SuspendAsserts())
{
conn.Open();
Assert.That(conn.IsOpen, Is.True);
conn.Close();
}
}
[Test]
public void TestInMemory()
{
using var conn = CreateInMemoryConnection();
using (conn.SuspendAsserts())
{
conn.Open();
CreateSampleDataBase(conn);
Assert.That(conn.IsOpen, Is.True);
var (adapter, table) = conn.SelectForUpdate("SELECT * FROM Sample", true);
Assert.That(table.PrimaryKey, Has.Length.EqualTo(0));
using (adapter)
{
for (int i = 0; i < 10; i++)
{
var row = table.NewRow();
row["Name"] = $"Name{i}";
row["Int32Value"] = i;
table.Rows.Add(row);
}
int num = conn.Update(adapter, table);
Assert.That(num, Is.EqualTo(10));
}
table.ClearAndDispose();
var numRead1 = conn.ExecuteReader("SELECT * FROM Sample", null);
Assert.That(numRead1, Is.EqualTo(10));
(adapter, table) = conn.SelectForUpdate("SELECT * FROM Sample", true);
Assert.That(table.Rows, Has.Count.EqualTo(10));
//table.PrimaryKey = new [] { table.Columns["ID"] };
using (adapter)
{
int expected = 0;
for (int i = 0; i < table.Rows.Count; i++)
{
var row = table.Rows[i];
if (i % 2 == 0)
{
row["Description"] = $"Description{i}";
row["Int32NullableValue"] = i;
row["BoolValue"] = 0;
expected++;
}
else if (i % 3 == 0)
{
row.Delete();
expected++;
}
}
int num = conn.Update(adapter, table);
using (Assert.EnterMultipleScope())
{
Assert.That(expected, Is.EqualTo(7));
Assert.That(num, Is.EqualTo(expected));
}
}
table.ClearAndDispose();
var listReader =
new List<(string Name, string? Description, int Int32Value, int? Int32NullableValue, bool BoolValue
)>();
var numRead2 = conn.ExecuteReader("SELECT * FROM Sample", (reader) =>
{
listReader.Add((
reader.GetString("Name"),
reader.GetNullableString("Description"),
reader.GetInt32("Int32Value"),
reader.GetNullableInt32("Int32NullableValue"),
reader.GetBoolean("BoolValue")
));
});
Assert.That(numRead2, Is.EqualTo(listReader.Count));
table = conn.Select("SELECT * FROM Sample");
Assert.That(table.Rows, Has.Count.EqualTo(8));
var listTable =
new List<(string Name, string? Description, int Int32Value, int? Int32NullableValue, bool BoolValue
)>();
foreach (DataRow row in table.Rows)
{
listTable.Add((
row.GetString("Name"),
row.GetNullableString("Description"),
row.GetInt32("Int32Value"),
row.GetNullableInt32("Int32NullableValue"),
row.GetBoolean("BoolValue")
));
}
Assert.That(listTable, Has.Count.EqualTo(listReader.Count));
Assert.That(listTable.SequenceEqual(listReader), Is.True);
var dr = table.FindRow("Description", o => "Description2".Equals(o));
Assert.That(dr, Is.Not.Null);
Assert.Pass();
}
}
}