forked from koush/sqlite-net
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinqTest.cs
187 lines (154 loc) · 3.93 KB
/
LinqTest.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Linq;
using System.Collections.Generic;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#else
using NUnit.Framework;
#endif
namespace SQLite.Tests
{
[TestFixture]
public class LinqTest
{
TestDb CreateDb ()
{
var db = new TestDb ();
db.CreateTable<Product> ();
db.CreateTable<Order> ();
db.CreateTable<OrderLine> ();
db.CreateTable<OrderHistory> ();
return db;
}
[Test]
public void FunctionParameter ()
{
var db = CreateDb ();
db.Insert (new Product {
Name = "A",
Price = 20,
});
db.Insert (new Product {
Name = "B",
Price = 10,
});
Func<decimal, List<Product>> GetProductsWithPriceAtLeast = delegate(decimal val) {
return (from p in db.Table<Product> () where p.Price > val select p).ToList ();
};
var r = GetProductsWithPriceAtLeast (15);
Assert.AreEqual (1, r.Count);
Assert.AreEqual ("A", r [0].Name);
}
[Test]
public void WhereGreaterThan ()
{
var db = CreateDb ();
db.Insert (new Product {
Name = "A",
Price = 20,
});
db.Insert (new Product {
Name = "B",
Price = 10,
});
Assert.AreEqual (2, db.Table<Product> ().Count ());
var r = (from p in db.Table<Product> () where p.Price > 15 select p).ToList ();
Assert.AreEqual (1, r.Count);
Assert.AreEqual ("A", r [0].Name);
}
[Test]
public void GetWithExpression ()
{
var db = CreateDb();
db.Insert (new Product {
Name = "A",
Price = 20,
});
db.Insert (new Product {
Name = "B",
Price = 10,
});
db.Insert(new Product
{
Name = "C",
Price = 5,
});
Assert.AreEqual (3, db.Table<Product> ().Count ());
var r = db.Get<Product>(x => x.Price == 10);
Assert.IsNotNull(r);
Assert.AreEqual ("B", r.Name);
}
[Test]
public void FindWithExpression ()
{
var db = CreateDb();
var r = db.Find<Product> (x => x.Price == 10);
Assert.IsNull (r);
}
[Test]
public void OrderByCast ()
{
var db = CreateDb();
db.Insert (new Product {
Name = "A",
TotalSales = 1,
});
db.Insert (new Product {
Name = "B",
TotalSales = 100,
});
var nocast = (from p in db.Table<Product> () orderby p.TotalSales descending select p).ToList ();
Assert.AreEqual (2, nocast.Count);
Assert.AreEqual ("B", nocast [0].Name);
var cast = (from p in db.Table<Product> () orderby (int)p.TotalSales descending select p).ToList ();
Assert.AreEqual (2, cast.Count);
Assert.AreEqual ("B", cast [0].Name);
}
public class Issue96_A
{
[ AutoIncrement, PrimaryKey]
public int ID { get; set; }
public string AddressLine { get; set; }
[Indexed]
public int? ClassB { get; set; }
[Indexed]
public int? ClassC { get; set; }
}
public class Issue96_B
{
[ AutoIncrement, PrimaryKey]
public int ID { get; set; }
public string CustomerName { get; set; }
}
public class Issue96_C
{
[ AutoIncrement, PrimaryKey]
public int ID { get; set; }
public string SupplierName { get; set; }
}
[Test]
public void Issue96_NullabelIntsInQueries ()
{
var db = CreateDb();
db.CreateTable<Issue96_A> ();
var id = 42;
db.Insert (new Issue96_A {
ClassB = id,
});
db.Insert (new Issue96_A {
ClassB = null,
});
db.Insert (new Issue96_A {
ClassB = null,
});
db.Insert (new Issue96_A {
ClassB = null,
});
Assert.AreEqual (1, db.Table<Issue96_A>().Where(p => p.ClassB == id).Count ());
Assert.AreEqual (3, db.Table<Issue96_A>().Where(p => p.ClassB == null).Count ());
}
}
}