Skip to content

Commit 4867541

Browse files
iss-507: add more tests
1 parent ea8d2bd commit 4867541

File tree

3 files changed

+156
-2
lines changed

3 files changed

+156
-2
lines changed

QueryBuilder.Tests/MySqlExecutionTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,55 @@ public void ExistsShouldReturnTrueForNonEmptyTable()
200200
db.Drop("Transaction");
201201
}
202202

203+
[Fact]
204+
public void BasicSelectFilter()
205+
{
206+
var db = DB().Create("Transaction", new[] {
207+
"Id INT PRIMARY KEY AUTO_INCREMENT",
208+
"Date DATE NOT NULL",
209+
"Amount int NOT NULL",
210+
});
211+
212+
var data = new Dictionary<string, int> {
213+
// 2020
214+
{"2020-01-01", 10},
215+
{"2020-05-01", 20},
216+
217+
// 2021
218+
{"2021-01-01", 40},
219+
{"2021-02-01", 10},
220+
{"2021-04-01", -10},
221+
222+
// 2022
223+
{"2022-01-01", 80},
224+
{"2022-02-01", -30},
225+
{"2022-05-01", 50},
226+
};
227+
228+
foreach (var row in data)
229+
{
230+
db.Query("Transaction").Insert(new
231+
{
232+
Date = row.Key,
233+
Amount = row.Value
234+
});
235+
}
236+
237+
var query = db.Query("Transaction")
238+
.SelectSum("Amount as Total_2020", q => q.WhereDatePart("year", "date", 2020))
239+
.SelectSum("Amount as Total_2021", q => q.WhereDatePart("year", "date", 2021))
240+
.SelectSum("Amount as Total_2022", q => q.WhereDatePart("year", "date", 2022))
241+
;
242+
243+
var results = query.Get().ToList();
244+
Assert.Single(results);
245+
Assert.Equal(30, results[0].Total_2020);
246+
Assert.Equal(40, results[0].Total_2021);
247+
Assert.Equal(100, results[0].Total_2022);
248+
249+
db.Drop("Transaction");
250+
}
251+
203252
QueryFactory DB()
204253
{
205254
var host = System.Environment.GetEnvironmentVariable("SQLKATA_MYSQL_HOST");

QueryBuilder.Tests/SQLiteExecutionTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,57 @@ public void InlineTable()
207207
db.Drop("Transaction");
208208
}
209209

210+
211+
212+
[Fact]
213+
public void BasicSelectFilter()
214+
{
215+
var db = DB().Create("Transaction", new[] {
216+
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
217+
"Date DATE NOT NULL",
218+
"Amount int NOT NULL",
219+
});
220+
221+
var data = new Dictionary<string, int> {
222+
// 2020
223+
{"2020-01-01", 10},
224+
{"2020-05-01", 20},
225+
226+
// 2021
227+
{"2021-01-01", 40},
228+
{"2021-02-01", 10},
229+
{"2021-04-01", -10},
230+
231+
// 2022
232+
{"2022-01-01", 80},
233+
{"2022-02-01", -30},
234+
{"2022-05-01", 50},
235+
};
236+
237+
foreach (var row in data)
238+
{
239+
db.Query("Transaction").Insert(new
240+
{
241+
Date = row.Key,
242+
Amount = row.Value
243+
});
244+
}
245+
246+
var query = db.Query("Transaction")
247+
.SelectSum("Amount as Total_2020", q => q.WhereDatePart("year", "date", 2020))
248+
.SelectSum("Amount as Total_2021", q => q.WhereDatePart("year", "date", 2021))
249+
.SelectSum("Amount as Total_2022", q => q.WhereDatePart("year", "date", 2022))
250+
;
251+
252+
var results = query.Get().ToList();
253+
Assert.Single(results);
254+
Assert.Equal(30, results[0].Total_2020);
255+
Assert.Equal(40, results[0].Total_2021);
256+
Assert.Equal(100, results[0].Total_2022);
257+
258+
db.Drop("Transaction");
259+
}
260+
210261
QueryFactory DB()
211262
{
212263
var cs = $"Data Source=file::memory:;Cache=Shared";

QueryBuilder.Tests/SelectTests.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ public void EscapeClauseThrowsForMultipleCharacters()
819819
[Fact]
820820
public void BasicSelectRaw_WithNoTable()
821821
{
822-
var q = new Query().SelectRaw("somefunction() as c1");
822+
var q = new Query().SelectRaw("somefunction() as c1");
823823

824824
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
825825
Assert.Equal("SELECT somefunction() as c1", c.ToString());
@@ -848,6 +848,60 @@ public void BasicSelect_WithNoTableWhereRawClause()
848848
var c = Compilers.CompileFor(EngineCodes.SqlServer, q);
849849
Assert.Equal("SELECT [c1] WHERE 1 = 1", c.ToString());
850850
}
851-
851+
852+
[Fact]
853+
public void BasicSelectAggregate()
854+
{
855+
var q = new Query("Posts").Select("Title")
856+
.SelectAggregate("sum", "ViewCount");
857+
858+
var sqlServer = Compilers.CompileFor(EngineCodes.SqlServer, q);
859+
Assert.Equal("SELECT [Title], SUM([ViewCount]) FROM [Posts]", sqlServer.ToString());
860+
}
861+
862+
[Fact]
863+
public void SelectAggregateShouldIgnoreEmptyFilter()
864+
{
865+
var q = new Query("Posts").Select("Title")
866+
.SelectAggregate("sum", "ViewCount", q => q);
867+
868+
var sqlServer = Compilers.CompileFor(EngineCodes.SqlServer, q);
869+
Assert.Equal("SELECT [Title], SUM([ViewCount]) FROM [Posts]", sqlServer.ToString());
870+
}
871+
872+
[Fact]
873+
public void SelectAggregateShouldIgnoreEmptyQueryFilter()
874+
{
875+
var q = new Query("Posts").Select("Title")
876+
.SelectAggregate("sum", "ViewCount", new Query());
877+
878+
var sqlServer = Compilers.CompileFor(EngineCodes.SqlServer, q);
879+
Assert.Equal("SELECT [Title], SUM([ViewCount]) FROM [Posts]", sqlServer.ToString());
880+
}
881+
882+
[Fact]
883+
public void BasicSelectAggregateWithAlias()
884+
{
885+
var q = new Query("Posts").Select("Title")
886+
.SelectAggregate("sum", "ViewCount as TotalViews");
887+
888+
var sqlServer = Compilers.CompileFor(EngineCodes.SqlServer, q);
889+
Assert.Equal("SELECT [Title], SUM([ViewCount]) AS [TotalViews] FROM [Posts]", sqlServer.ToString());
890+
}
891+
892+
[Fact]
893+
public void SelectWithFilter()
894+
{
895+
var q = new Query("Posts").Select("Title")
896+
.SelectAggregate("sum", "ViewCount as Published_Jan", q => q.Where("Published_Month", "Jan"))
897+
.SelectAggregate("sum", "ViewCount as Published_Feb", q => q.Where("Published_Month", "Feb"));
898+
899+
var pgSql = Compilers.CompileFor(EngineCodes.PostgreSql, q);
900+
Assert.Equal("SELECT \"Title\", SUM(\"ViewCount\") FILTER (WHERE \"Published_Month\" = 'Jan') AS \"Published_Jan\", SUM(\"ViewCount\") FILTER (WHERE \"Published_Month\" = 'Feb') AS \"Published_Feb\" FROM \"Posts\"", pgSql.ToString());
901+
902+
var sqlServer = Compilers.CompileFor(EngineCodes.SqlServer, q);
903+
Assert.Equal("SELECT [Title], SUM(CASE WHEN [Published_Month] = 'Jan' THEN [ViewCount] END) AS [Published_Jan], SUM(CASE WHEN [Published_Month] = 'Feb' THEN [ViewCount] END) AS [Published_Feb] FROM [Posts]", sqlServer.ToString());
904+
}
905+
852906
}
853907
}

0 commit comments

Comments
 (0)