Skip to content

Commit 154b1bc

Browse files
author
Michel Bieleveld
committed
Cleaned up test
1 parent e4b5f5c commit 154b1bc

6 files changed

Lines changed: 65 additions & 30 deletions

File tree

QueryBuilder.Tests/DefineTest.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,23 @@ public void Test_Define_SubQuery()
7474
public void Test_Define_Parameter_SubQuery()
7575
{
7676

77-
var subquery = new Query("Products")
78-
.AsAverage("unitprice")
79-
.DefineParameter("@UnitsInSt", 10)
80-
.Where("UnitsInStock", ">", Variable("@UnitsInSt"))
81-
.Where("UnitsInStock", ">", Variable("@UnitsInSt"));
82-
8377
var query = new Query("Products")
84-
.Where("unitprice", ">", subquery)
85-
.Where("UnitsOnOrder", ">", 5);
86-
87-
var c = Compile(query);
88-
89-
Assert.Equal("SELECT * FROM [Products] WHERE [unitprice] > (SELECT AVG([unitprice]) AS [avg] FROM [Products] WHERE"+
90-
" [UnitsInStock] > 10 AND [UnitsInStock] > 10) AND [UnitsOnOrder] > 5", c[EngineCodes.SqlServer]);
78+
.DefineParameter("@a", 1)
79+
.DefineParameter("@b","b")
80+
.Where(q=> q.Where("a", "=", Variable("@a")))
81+
.OrWhere(q =>
82+
q.Where("a", "=", Variable("@a"))
83+
.Where("b",">",Variable("@b"))
84+
.Where("a",">",0));
9185

9286
var s = Compilers.Compile(query)[EngineCodes.SqlServer];
93-
Assert.Equal(2,s.NamedBindings.Count);
87+
Assert.Equal("SELECT * FROM [Products] WHERE ([a] = @a) OR ([a] = @a AND [b] > @b AND [a] > @p3)", s.Sql);
88+
Assert.Equal(3,s.NamedBindings.Count);
9489
var expected = new Dictionary<string, object>()
9590
{
96-
{ "@UnitsInSt", 10 },
97-
{ "@p2", 5 }
91+
{ "@a", 1 },
92+
{ "@b", "b" },
93+
{ "@p3", 0 }
9894
};
9995
Assert.Equal(expected, s.NamedBindings);
10096
}

QueryBuilder.Tests/MySql/MySqlExecutionTest.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using MySql.Data.MySqlClient;
14
using SqlKata.Compilers;
2-
using Xunit;
35
using SqlKata.Execution;
4-
using MySql.Data.MySqlClient;
5-
using System;
6-
using System.Linq;
6+
using Xunit;
77
using static SqlKata.Expressions;
8-
using System.Collections.Generic;
98

10-
namespace SqlKata.Tests
9+
namespace SqlKata.Tests.MySql
1110
{
1211
public class MySqlExecutionTest
1312
{
@@ -134,6 +133,37 @@ public void QueryWithVariable()
134133
db.Drop("Cars");
135134
}
136135

136+
[Fact]
137+
public void QueryWithParameter()
138+
{
139+
var db = DB().Create("Cars", new[] {
140+
"Id INT PRIMARY KEY AUTO_INCREMENT",
141+
"Brand TEXT NOT NULL",
142+
"Year INT NOT NULL",
143+
"Color TEXT NULL",
144+
});
145+
146+
for (int i = 0; i < 10; i++)
147+
{
148+
db.Query("Cars").Insert(new
149+
{
150+
Brand = "Brand " + i,
151+
Year = "2020",
152+
});
153+
}
154+
155+
156+
var count = db.Query("Cars")
157+
.DefineParameter("Threshold", 5)
158+
.Where("Id", "<", Variable("Threshold"))
159+
.Where("Id", "<", Variable("Threshold"))
160+
.Count<int>();
161+
162+
Assert.Equal(4, count);
163+
164+
db.Drop("Cars");
165+
}
166+
137167
[Fact]
138168
public void InlineTable()
139169
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Use root/my-secret-pw as user/password credentials
2+
version: '3.1'
3+
4+
services:
5+
6+
db:
7+
image: mysql
8+
restart: always
9+
environment:
10+
MYSQL_ROOT_PASSWORD: my-secret-pw
11+
MYSQL_DATABASE: test
12+
ports:
13+
- "3306:3306"
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
namespace SqlKata;
22

3-
// public class NamedParameter(object value)
4-
// {
5-
// public object Value { get; set; } = value;
6-
// }
7-
83
public class NamedParameterVariable(string variable, object value)
94
{
105
public object Value { get; set; } = value;

QueryBuilder/Query.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public Query IncludeMany(string relationName, Query query, string foreignKey = n
371371
/// <returns></returns>
372372
public Query Define(string variable, object value)
373373
{
374-
Variables.Add(variable, value);
374+
Variables.Add($"@{variable.TrimStart('@')}", value);
375375

376376
return this;
377377
}
@@ -384,7 +384,8 @@ public Query Define(string variable, object value)
384384
/// <returns></returns>
385385
public Query DefineParameter(string variable, object value)
386386
{
387-
Variables.Add(variable, new NamedParameterVariable(variable,value));
387+
var name = $"@{variable.TrimStart('@')}";
388+
Variables.Add(name, new NamedParameterVariable(name,value));
388389

389390
return this;
390391
}

QueryBuilder/Variable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Variable
66

77
public Variable(string name)
88
{
9-
this.Name = name;
9+
this.Name = $"@{name.TrimStart('@')}";
1010
}
1111

1212
}

0 commit comments

Comments
 (0)