-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQueryTest.cs
More file actions
138 lines (122 loc) · 5.01 KB
/
QueryTest.cs
File metadata and controls
138 lines (122 loc) · 5.01 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
using DirectSQL.SqlLite;
namespace TestSqlLiteDatabase
{
[TestClass]
public class QueryTest
{
[TestMethod]
public void TestQuery()
{
SqlLiteDatabase db = new SqlLiteDatabase("Data Source=:memory:");
db.Process((connection, transaction) =>
{
CreateTableForTest(connection);
SqlLiteDatabase.ExecuteNonQuery(
"insert into TEST_TABLE(TEST_VAL1,TEST_VAL2) values('abcdef',123)",
connection,
transaction);
SqlLiteDatabase.ExecuteNonQuery(
"insert into TEST_TABLE(TEST_VAL1,TEST_VAL2) values('xyz',456)",
connection,
transaction);
SqlLiteDatabase.Query(
"select TEST_VAL1,TEST_VAL2 from TEST_TABLE order by 1",
connection,
transaction,
(result) => {
if (result.Next())
{
var resultValues = result.ResultValues;
Assert.AreEqual(resultValues.TEST_VAL1, "abcdef");
Assert.AreEqual(resultValues.TEST_VAL2, 123L);
//Same instance
Assert.AreSame(resultValues, result.ResultValues);
var resultTuples = result.ResultTuples;
Assert.AreEqual(resultTuples[0].name, "TEST_VAL1");
Assert.AreEqual(resultTuples[0].value, "abcdef");
Assert.AreEqual(resultTuples[1].name, "TEST_VAL2");
Assert.AreEqual(resultTuples[1].value, 123L);
//go to next row.
result.Next();
var resultValues2 = result.ResultValues;
Assert.AreEqual(resultValues2.TEST_VAL1, "xyz");
Assert.AreEqual(resultValues2.TEST_VAL2, 456L);
}
else
{
Assert.Fail();
}
}
);
SqlLiteDatabase.Query(
"select TEST_VAL1,TEST_VAL2 from TEST_TABLE where TEST_VAL1 = @val1",
new (String, object)[] {("@val1","abcdef")},
connection,
transaction,
(result) => {
if (result.Next())
{
var resultValues = result.ResultValues;
Assert.AreEqual(resultValues.TEST_VAL1, "abcdef");
Assert.AreEqual(resultValues.TEST_VAL2, 123L);
}
else
{
Assert.Fail();
}
}
);
SqlLiteDatabase.Query(
"select TEST_VAL1,TEST_VAL2 from TEST_TABLE where TEST_VAL1 = @val1",
new (String, object)[] {("@val1", "XXXXXXX") },
connection,
transaction,
(result) => {
Assert.IsFalse(result.Next());
}
);
var abcdef = SqlLiteDatabase.ExecuteScalar(
"select TEST_VAL1 from TEST_TABLE",
connection,
transaction
);
Assert.AreEqual(abcdef, "abcdef");
var abcdef2 = SqlLiteDatabase.ExecuteScalar(
"select TEST_VAL1 from TEST_TABLE where TEST_VAL1 = @val1",
new (String, object)[] { ("@val1", "abcdef") },
connection,
transaction
);
Assert.AreEqual(abcdef2, "abcdef");
var abcdef3 = SqlLiteDatabase.ExecuteScalar(
"select TEST_VAL1 from TEST_TABLE where TEST_VAL1 = @val1",
new SQLiteParameter[] {
SqlLiteDatabase.CreateParameter("@val1","abcdef",DbType.String)
},
connection,
transaction
);
Assert.AreEqual(abcdef3, "abcdef");
});
}
private static void CreateTableForTest(IDbConnection connection)
{
using( var command = connection.CreateCommand())
{
command.CommandText =
"create table " +
"TEST_TABLE(" +
"TEST_VAL1 text," +
"TEST_VAL2 integer" +
")";
command.ExecuteNonQuery();
}
}
}
}