Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 605 sqlite execution test #607

Merged
merged 3 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions QueryBuilder.Tests/MySqlExecutionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,5 @@ QueryFactory DB()



}
static class QueryFactoryExtensions
{
public static QueryFactory Create(this QueryFactory db, string table, IEnumerable<string> cols)
{
db.Drop(table);
db.Statement($"CREATE TABLE `{table}`({string.Join(", ", cols)})");
return db;
}

public static QueryFactory Drop(this QueryFactory db, string table)
{
db.Statement($"DROP TABLE IF EXISTS `{table}`");
return db;
}
}
}
1 change: 1 addition & 0 deletions QueryBuilder.Tests/QueryBuilder.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RootNamespace>SqlKata.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="MySql.Data" Version="8.0.30" />
<PackageReference Include="xunit" Version="2.3.1" />
Expand Down
19 changes: 19 additions & 0 deletions QueryBuilder.Tests/QueryFactoryExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

using System.Collections.Generic;
using SqlKata.Execution;

static class QueryFactoryExtensions
{
public static QueryFactory Create(this QueryFactory db, string table, IEnumerable<string> cols)
{
db.Drop(table);
db.Statement($"CREATE TABLE `{table}`({string.Join(", ", cols)});");
return db;
}

public static QueryFactory Drop(this QueryFactory db, string table)
{
db.Statement($"DROP TABLE IF EXISTS `{table}`;");
return db;
}
}
224 changes: 224 additions & 0 deletions QueryBuilder.Tests/SQLiteExecutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
using SqlKata.Compilers;
using Xunit;
using SqlKata.Execution;
using MySql.Data.MySqlClient;
using System;
using System.Linq;
using static SqlKata.Expressions;
using System.Collections.Generic;
using Microsoft.Data.Sqlite;

namespace SqlKata.Tests
{
public class SqliteExecutionTest
{
[Fact]
public void EmptySelect()
{

var db = DB().Create("Cars", new[] {
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
"Brand TEXT NOT NULL",
"Year INT NOT NULL",
"Color TEXT NULL",
});


var tables = db.Select(@"SELECT name FROM sqlite_schema WHERE type ='table' AND name NOT LIKE 'sqlite_%'");

var rows = db.Query("Cars").Get();

Assert.Empty(rows);

db.Drop("Cars");
}

[Fact]
public void SelectWithLimit()
{
var db = DB().Create("Cars", new[] {
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
"Brand TEXT NOT NULL",
"Year INT NOT NULL",
"Color TEXT NULL",
});

db.Statement("INSERT INTO `Cars`(Brand, Year) VALUES ('Honda', 2020)");

var rows = db.Query("Cars").Get().ToList();

Assert.Single(rows);

db.Drop("Cars");
}

[Fact]
public void InsertGetId()
{
var db = DB().Create("Cars", new[] {
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
"Brand TEXT NOT NULL",
"Year INT NOT NULL",
});

var id = db.Query("Cars").InsertGetId<int>(new
{
Brand = "Toyota",
Year = 1900
});

Assert.Equal(1, id);

id = db.Query("Cars").InsertGetId<int>(new
{
Brand = "Toyota 2",
Year = 1901
});

Assert.Equal(2, id);

id = db.Query("Cars").InsertGetId<int>(new
{
Brand = "Toyota 2",
Year = 1901
});

Assert.Equal(3, id);


db.Drop("Cars");
}



[Fact]
public void Count()
{
var db = DB().Create("Cars", new[] {
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
"Brand TEXT NOT NULL",
"Year INT NOT NULL",
"Color TEXT NULL",
});

db.Statement("INSERT INTO `Cars`(Brand, Year) VALUES ('Honda', 2020)");
var count = db.Query("Cars").Count<int>();
Assert.Equal(1, count);

db.Statement("INSERT INTO `Cars`(Brand, Year) VALUES ('Toyota', 2021)");
count = db.Query("Cars").Count<int>();
Assert.Equal(2, count);

int affected = db.Query("Cars").Delete();
Assert.Equal(2, affected);

count = db.Query("Cars").Count<int>();
Assert.Equal(0, count);

db.Drop("Cars");
}

[Fact]
public void CloneThenCount()
{
var db = DB().Create("Cars", new[] {
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
"Brand TEXT NOT NULL",
"Year INT NOT NULL",
"Color TEXT NULL",
});

for (int i = 0; i < 10; i++)
{
db.Query("Cars").Insert(new
{
Brand = "Brand " + i,
Year = "2020",
});
}

var query = db.Query("Cars").Where("Id", "<", 5);
var count = query.Count<int>();
var cloneCount = query.Clone().Count<int>();

Assert.Equal(4, count);
Assert.Equal(4, cloneCount);

db.Drop("Cars");
}

[Fact]
public void QueryWithVariable()
{
var db = DB().Create("Cars", new[] {
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
"Brand TEXT NOT NULL",
"Year INT NOT NULL",
"Color TEXT NULL",
});

for (int i = 0; i < 10; i++)
{
db.Query("Cars").Insert(new
{
Brand = "Brand " + i,
Year = "2020",
});
}


var count = db.Query("Cars")
.Define("Threshold", 5)
.Where("Id", "<", SqlKata.Expressions.Variable("Threshold"))
.Count<int>();

Assert.Equal(4, count);

db.Drop("Cars");
}

[Fact]
public void InlineTable()
{
var db = DB().Create("Transaction", new[] {
"Id INTEGER PRIMARY KEY AUTOINCREMENT",
"Amount int NOT NULL",
"Date DATE NOT NULL",
});

db.Query("Transaction").Insert(new
{
Date = "2022-01-01",
Amount = 10
});


var rows = db.Query("Transaction")
.With("Rates", new[] { "Date", "Rate" }, new object[][] {
new object[] {"2022-01-01", 0.5},
})
.Join("Rates", "Rates.Date", "Transaction.Date")
.SelectRaw("([Transaction].[Amount] * [Rates].[Rate]) as AmountConverted")
.Get();

Assert.Single(rows);
Assert.Equal(5, rows.First().AmountConverted);

db.Drop("Transaction");
}

QueryFactory DB()
{
var cs = $"Data Source=file::memory:;Cache=Shared";

var connection = new SqliteConnection(cs);

var db = new QueryFactory(connection, new SqliteCompiler());

return db;
}



}
}