Skip to content

Commit e0c198e

Browse files
committed
1 parent 20f3fe7 commit e0c198e

4 files changed

Lines changed: 60 additions & 3 deletions

File tree

Simple.Data.BehaviourTest/Query/QueryTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,5 @@ public void SpecifyingJoinTableShouldCreateDirectQuery()
228228
GeneratedSqlIs("select [dbo].[userbio].[userid],[dbo].[userbio].[text] from [dbo].[userbio]" +
229229
" join [dbo].[users] on ([dbo].[users].[id] = [dbo].[userbio].[userid]) where [dbo].[users].[id] = @p1");
230230
}
231-
232-
233231
}
234232
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace Simple.Data.IntegrationTest
2+
{
3+
using System;
4+
using Mocking.Ado;
5+
using NUnit.Framework;
6+
7+
[TestFixture]
8+
public class WhereTest : DatabaseIntegrationContext
9+
{
10+
[Test]
11+
public void WhereWithNoParametersShouldThrowBadExpressionException()
12+
{
13+
Assert.Throws<BadExpressionException>(() => _db.Users.All().Where());
14+
}
15+
16+
[Test]
17+
public void WhereWithStringParameterShouldThrowBadExpressionException()
18+
{
19+
Assert.Throws<BadExpressionException>(() => _db.Users.All().Where("Answers"));
20+
}
21+
22+
[Test]
23+
public void WhereWithNullParameterShouldThrowArgumentNullException()
24+
{
25+
Assert.Throws<ArgumentNullException>(() => _db.Users.All().Where(null));
26+
}
27+
28+
protected override void SetSchema(MockSchemaProvider schemaProvider)
29+
{
30+
schemaProvider.SetTables(new[] { "dbo", "Users", "BASE TABLE" },
31+
new[] { "dbo", "UserBio", "BASE TABLE" },
32+
new[] { "dbo", "UserPayment", "BASE TABLE" },
33+
new[] { "dbo", "Employee", "BASE TABLE" });
34+
35+
schemaProvider.SetColumns(new object[] { "dbo", "Users", "Id", true },
36+
new[] { "dbo", "Users", "Name" },
37+
new[] { "dbo", "Users", "Password" },
38+
new[] { "dbo", "Users", "Age" },
39+
new[] { "dbo", "UserBio", "UserId" },
40+
new[] { "dbo", "UserBio", "Text" },
41+
new[] { "dbo", "UserPayment", "UserId" },
42+
new[] { "dbo", "UserPayment", "Amount" },
43+
new[] { "dbo", "Employee", "Id" },
44+
new[] { "dbo", "Employee", "Name" },
45+
new[] { "dbo", "Employee", "ManagerId" });
46+
47+
schemaProvider.SetPrimaryKeys(new object[] { "dbo", "Users", "Id", 0 });
48+
schemaProvider.SetForeignKeys(new object[] { "FK_Users_UserBio", "dbo", "UserBio", "UserId", "dbo", "Users", "Id", 0 },
49+
new object[] { "FK_Users_UserPayment", "dbo", "UserPayment", "UserId", "dbo", "Users", "Id", 0 });
50+
}
51+
}
52+
}

Simple.Data.BehaviourTest/Simple.Data.BehaviourTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="Query\FunctionTest.cs" />
7979
<Compile Include="Query\HavingTest.cs" />
8080
<Compile Include="Query\QueryTest.cs" />
81+
<Compile Include="Query\WhereTest.cs" />
8182
<Compile Include="Query\WithCountTest.cs" />
8283
<Compile Include="Query\WithTest.cs" />
8384
<Compile Include="RangeAndArrayFindTest.cs" />

Simple.Data/SimpleQuery.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public SimpleQuery ReplaceWhere(SimpleExpression criteria)
183183

184184
public SimpleQuery Where(SimpleExpression criteria)
185185
{
186+
if (criteria == null) throw new ArgumentNullException("criteria");
186187
return new SimpleQuery(this, _clauses.Append(new WhereClause(criteria)));
187188
}
188189

@@ -333,7 +334,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
333334
}
334335
try
335336
{
336-
var methodInfo = typeof(SimpleQuery).GetMethod(binder.Name);
337+
var methodInfo = typeof(SimpleQuery).GetMethod(binder.Name, args.Select(a => (a ?? new object()).GetType()).ToArray());
337338
if (methodInfo != null)
338339
{
339340
methodInfo.Invoke(this, args);
@@ -343,6 +344,11 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
343344
{
344345
}
345346

347+
if (binder.Name.Equals("where", StringComparison.InvariantCultureIgnoreCase) || binder.Name.Equals("replacewhere", StringComparison.InvariantCultureIgnoreCase))
348+
{
349+
throw new BadExpressionException("Where methods require a single criteria expression.");
350+
}
351+
346352
return false;
347353
}
348354

0 commit comments

Comments
 (0)