forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryTest.cs
More file actions
225 lines (200 loc) · 8.88 KB
/
QueryTest.cs
File metadata and controls
225 lines (200 loc) · 8.88 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Simple.Data.Mocking.Ado;
namespace Simple.Data.IntegrationTest
{
[TestFixture]
public class QueryTest : DatabaseIntegrationContext
{
protected override void SetSchema(MockSchemaProvider schemaProvider)
{
schemaProvider.SetTables(new[] {"dbo", "Users", "BASE TABLE"},
new[] {"dbo", "UserBio", "BASE TABLE"},
new[] { "dbo", "UserPayment", "BASE TABLE" },
new[] { "dbo", "Employee", "BASE TABLE" });
schemaProvider.SetColumns(new object[] {"dbo", "Users", "Id", true},
new[] {"dbo", "Users", "Name"},
new[] {"dbo", "Users", "Password"},
new[] {"dbo", "Users", "Age"},
new[] {"dbo", "UserBio", "UserId"},
new[] {"dbo", "UserBio", "Text"},
new[] { "dbo", "UserPayment", "UserId" },
new[] { "dbo", "UserPayment", "Amount" },
new[] { "dbo", "Employee", "Id" },
new[] {"dbo", "Employee", "Name"},
new[] {"dbo", "Employee", "ManagerId"});
schemaProvider.SetPrimaryKeys(new object[] { "dbo", "Users", "Id", 0 });
schemaProvider.SetForeignKeys(new object[] { "FK_Users_UserBio", "dbo", "UserBio", "UserId", "dbo", "Users", "Id", 0 },
new object[] { "FK_Users_UserPayment", "dbo", "UserPayment", "UserId", "dbo", "Users", "Id", 0 });
}
[Test]
public void SpecifyingColumnsShouldRestrictSelect()
{
_db.Users.All()
.Select(_db.Users.Name, _db.Users.Password)
.ToList();
GeneratedSqlIs("select [dbo].[users].[name],[dbo].[users].[password] from [dbo].[users]");
}
[Test]
public void SpecifyingColumnStarShouldSelectAllColumns()
{
_db.Users.All()
.Select(_db.Users.Name, _db.Users.UserBio.Star())
.ToList();
GeneratedSqlIs("select [dbo].[users].[name],[dbo].[userbio].[userid],[dbo].[userbio].[text] from [dbo].[users]" +
" left join [dbo].[userbio] on ([dbo].[users].[id] = [dbo].[userbio].[userid])");
}
[Test]
public void SpecifyingColumnAllColumnsShouldSelectAllColumns()
{
_db.Users.All()
.Select(_db.Users.Name, _db.Users.UserBio.AllColumns())
.ToList();
GeneratedSqlIs("select [dbo].[users].[name],[dbo].[userbio].[userid],[dbo].[userbio].[text] from [dbo].[users]" +
" left join [dbo].[userbio] on ([dbo].[users].[id] = [dbo].[userbio].[userid])");
}
[Test]
public void SpecifyingColumnWithAliasShouldAddAsClause()
{
_db.Users.All()
.Select(_db.Users.Name, _db.Users.Password.As("SuperSecretPassword"))
.ToList();
GeneratedSqlIs("select [dbo].[users].[name],[dbo].[users].[password] as [supersecretpassword] from [dbo].[users]");
}
[Test]
public void SpecifyingColumnsFromOtherTablesShouldAddJoin()
{
_db.Users.All()
.Select(_db.Users.Name, _db.Users.Password, _db.Users.UserBio.Text)
.ToList();
GeneratedSqlIs(
"select [dbo].[users].[name],[dbo].[users].[password],[dbo].[userbio].[text] from [dbo].[users]" +
" left join [dbo].[userbio] on ([dbo].[users].[id] = [dbo].[userbio].[userid])");
}
[Test]
public void SpecifyingColumnsAndAggregatesFromOtherTablesShouldAddJoins()
{
_db.Users.All()
.Select(_db.Users.Name, _db.Users.Password, _db.Users.UserBio.Text, _db.Users.UserPayments.Amount.Sum())
.ToList();
GeneratedSqlIs(
"select [dbo].[users].[name],[dbo].[users].[password],[dbo].[userbio].[text],sum([dbo].[userpayment].[amount]) from [dbo].[users]" +
" left join [dbo].[userbio] on ([dbo].[users].[id] = [dbo].[userbio].[userid])" +
" left join [dbo].[userpayment] on ([dbo].[users].[id] = [dbo].[userpayment].[userid])" +
" group by [dbo].[users].[name],[dbo].[users].[password],[dbo].[userbio].[text]"
);
}
[Test]
public void SpecifyingCountShouldSelectCount()
{
try
{
_db.Users.All().Count();
}
catch (SimpleDataException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select count(*) from [dbo].[users]");
}
[Test]
public void SpecifyingExistsShouldSelectDistinct1()
{
try
{
_db.Users.All().Exists();
}
catch (InvalidOperationException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select distinct 1 from [dbo].[users]");
}
[Test]
public void SpecifyingAnyShouldSelectDistinct1()
{
try
{
_db.Users.All().Any();
}
catch (InvalidOperationException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select distinct 1 from [dbo].[users]");
}
[Test]
public void SpecifyingMinShouldSelectFunction()
{
try
{
_db.Users.All().Select(_db.Users.Age.Min()).ToScalar();
}
catch (InvalidOperationException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
catch (SimpleDataException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select min([dbo].[users].[age]) from [dbo].[users]");
}
[Test]
public void SpecifyingNonAggregatedColumnAndMinShouldAddGroupBy()
{
try
{
_db.Users.All().Select(_db.Users.Name, _db.Users.Age.Min().As("Youngest")).ToList();
}
catch (InvalidOperationException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select [dbo].[users].[name],min([dbo].[users].[age]) as [youngest] from [dbo].[users] group by [dbo].[users].[name]");
}
[Test]
public void SpecifyingNonAggregateFunctionAndMinShouldAddGroupBy()
{
try
{
_db.Users.All().Select(_db.Users.Name.Length(), _db.Users.Age.Min().As("Youngest")).ToList();
}
catch (InvalidOperationException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select len([dbo].[users].[name]),min([dbo].[users].[age]) as [youngest] from [dbo].[users] group by len([dbo].[users].[name])");
}
[Test]
public void SpecifyingNonAggregateFunctionShouldNotApplyGroupBy()
{
try
{
_db.Users.All().Select(_db.Users.Name, _db.Users.Name.Length().As("NameLength")).ToList();
}
catch (InvalidOperationException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select [dbo].[users].[name],len([dbo].[users].[name]) as [namelength] from [dbo].[users]");
}
[Test]
public void SpecifyingJoinTableShouldCreateDirectQuery()
{
try
{
_db.Users.QueryById(1).UserBio.ToList();
}
catch (InvalidOperationException)
{
// This won't work on Mock provider, but the SQL should be generated OK
}
GeneratedSqlIs("select [dbo].[userbio].[userid],[dbo].[userbio].[text] from [dbo].[userbio]" +
" join [dbo].[users] on ([dbo].[users].[id] = [dbo].[userbio].[userid]) where [dbo].[users].[id] = @p1");
}
}
}