Skip to content
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>0.1.6</Version>
<Version>0.2.0</Version>
</PropertyGroup>
</Project>
56 changes: 52 additions & 4 deletions src/QAToolKit.Engine.Database.Test/MySqlTestGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ExpectedObjects;
using QAToolKit.Engine.Database.Generators;
using QAToolKit.Engine.Database.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -163,12 +162,61 @@ public void MySqlObjectExistScriptNullDbKindTest_Success()
}

[Fact]
public async Task MySqlObjectExistScriptNullOptionsTest_Fails()
public async Task MySqlRecordExistScriptTest_Success()
{
var generator = new MySqlTestGenerator();
var generator = new MySqlTestGenerator(options =>
{
options.AddDatabaseRecordExitsRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "= 'myname'"
}
});
});

var results = new List<DatabaseScript>
{
new DatabaseScript(
"mytable",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE = 'myname');",
DatabaseTestType.RecordExist,
DatabaseKind.MySQL)
}.ToExpectedObject();

results.ShouldEqual(await generator.Generate());
Assert.Equal(DatabaseKind.MySQL, generator.DatabaseKind);
}

[Fact]
public async Task MySqlRecordCountScriptTest_Success()
{
var generator = new MySqlTestGenerator(options =>
{
options.AddDatabaseRecordsCountRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "=100"
}
});
});

var results = new List<DatabaseScript>
{
new DatabaseScript(
"mytable",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE (SELECT count(*) FROM mytable)=100);",
DatabaseTestType.RecordCount,
DatabaseKind.MySQL)
}.ToExpectedObject();

results.ShouldEqual(await generator.Generate());
Assert.Equal(DatabaseKind.MySQL, generator.DatabaseKind);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await generator.Generate());
}
}
}
56 changes: 52 additions & 4 deletions src/QAToolKit.Engine.Database.Test/PostgresqlTestGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ExpectedObjects;
using QAToolKit.Engine.Database.Generators;
using QAToolKit.Engine.Database.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -163,12 +162,61 @@ public void PostgresqlObjectExistScriptNullDbKindTest_Success()
}

[Fact]
public async Task PostgresqlObjectExistScriptNullOptionsTest_Fails()
public async Task PostgresqlRecordExistScriptTest_Success()
{
var generator = new PostgresqlTestGenerator();
var generator = new PostgresqlTestGenerator(options =>
{
options.AddDatabaseRecordExitsRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "= 'myname'"
}
});
});

var results = new List<DatabaseScript>
{
new DatabaseScript(
"mytable",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE = 'myname');",
DatabaseTestType.RecordExist,
DatabaseKind.PostgreSQL)
}.ToExpectedObject();

results.ShouldEqual(await generator.Generate());
Assert.Equal(DatabaseKind.PostgreSQL, generator.DatabaseKind);
}

[Fact]
public async Task PostgresqlRecordCountScriptTest_Success()
{
var generator = new PostgresqlTestGenerator(options =>
{
options.AddDatabaseRecordsCountRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "=100"
}
});
});

var results = new List<DatabaseScript>
{
new DatabaseScript(
"mytable",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE (SELECT count(*) FROM mytable)=100);",
DatabaseTestType.RecordCount,
DatabaseKind.PostgreSQL)
}.ToExpectedObject();

results.ShouldEqual(await generator.Generate());
Assert.Equal(DatabaseKind.PostgreSQL, generator.DatabaseKind);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await generator.Generate());
}
}
}
57 changes: 53 additions & 4 deletions src/QAToolKit.Engine.Database.Test/SqlServerTestGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ExpectedObjects;
using QAToolKit.Engine.Database.Generators;
using QAToolKit.Engine.Database.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -162,13 +161,63 @@ public void SqlServerObjectExistScriptNullDbKindTest_Success()
Assert.Equal(DatabaseKind.SQLServer, generator.DatabaseKind);
}


[Fact]
public async Task SqlServerObjectExistScriptNullOptionsTest_Fails()
public async Task SqlServerRecordExistScriptTest_Success()
{
var generator = new SqlServerTestGenerator();
var generator = new SqlServerTestGenerator(options =>
{
options.AddDatabaseRecordExitsRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "= 'myname'"
}
});
});

var results = new List<DatabaseScript>
{
new DatabaseScript(
"mytable",
$@"IF EXISTS(SELECT 1 FROM mytable WHERE = 'myname') BEGIN Select 1 END ELSE BEGIN Select 0 END",
DatabaseTestType.RecordExist,
DatabaseKind.SQLServer)
}.ToExpectedObject();

results.ShouldEqual(await generator.Generate());
Assert.Equal(DatabaseKind.SQLServer, generator.DatabaseKind);
}

[Fact]
public async Task SqlServerRecordCountScriptTest_Success()
{
var generator = new SqlServerTestGenerator(options =>
{
options.AddDatabaseRecordsCountRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "=100"
}
});
});

var results = new List<DatabaseScript>
{
new DatabaseScript(
"mytable",
$@"IF EXISTS(SELECT 1 FROM mytable WHERE (SELECT count(*) FROM mytable)=100) BEGIN Select 1 END ELSE BEGIN Select 0 END",
DatabaseTestType.RecordCount,
DatabaseKind.SQLServer)
}.ToExpectedObject();

results.ShouldEqual(await generator.Generate());
Assert.Equal(DatabaseKind.SQLServer, generator.DatabaseKind);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await generator.Generate());
}
}
}
36 changes: 36 additions & 0 deletions src/QAToolKit.Engine.Database/DatabaseTestGeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace QAToolKit.Engine.Database
public class DatabaseTestGeneratorOptions
{
internal Dictionary<DatabaseObjectType, string[]> DatabaseObjectsExistRules { get; private set; }
internal List<DatabaseRule> DatabaseRecordsCountRules { get; private set; }
internal List<DatabaseRule> DatabaseRecordsExitsRules { get; private set; }

/// <summary>
/// Add database object exist rules
Expand All @@ -27,5 +29,39 @@ public DatabaseTestGeneratorOptions AddDatabaseObjectExitsRule(string[] objects,

return this;
}

/// <summary>
/// Add database record count rules
/// </summary>
/// <param name="objects"></param>
/// <returns></returns>
public DatabaseTestGeneratorOptions AddDatabaseRecordsCountRule(List<DatabaseRule> objects)
{
if (DatabaseRecordsCountRules == null)
{
DatabaseRecordsCountRules = new List<DatabaseRule>();
}

DatabaseRecordsCountRules.AddRange(objects);

return this;
}

/// <summary>
/// Add database record exsit rules
/// </summary>
/// <param name="objects"></param>
/// <returns></returns>
public DatabaseTestGeneratorOptions AddDatabaseRecordExitsRule(List<DatabaseRule> objects)
{
if (DatabaseRecordsExitsRules == null)
{
DatabaseRecordsExitsRules = new List<DatabaseRule>();
}

DatabaseRecordsExitsRules.AddRange(objects);

return this;
}
}
}
20 changes: 20 additions & 0 deletions src/QAToolKit.Engine.Database/Generators/MySqlTestGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,25 @@ protected override string GetStoredProcedureExistScript(string storedProcedure)
{
return $@"SELECT EXISTS(SELECT * FROM information_schema.routines WHERE routine_name = '{storedProcedure}');";
}

/// <summary>
/// Get MySQL script to check if record exist
/// </summary>
/// <param name="recordExist"></param>
/// <returns></returns>
protected override string GetRecordExistScript(DatabaseRule recordExist)
{
return $@"SELECT EXISTS (SELECT 1 FROM {recordExist.TableName} WHERE {recordExist.PredicateValue});";
}

/// <summary>
/// Get MySQL script to count the records in a table
/// </summary>
/// <param name="recordCount"></param>
/// <returns></returns>
protected override string GetRecordCountScript(DatabaseRule recordCount)
{
return $@"SELECT EXISTS (SELECT 1 FROM {recordCount.TableName} WHERE (SELECT count(*) FROM {recordCount.TableName}){recordCount.PredicateValue});";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,25 @@ protected override string GetStoredProcedureExistScript(string storedProcedure)
{
return $@"SELECT EXISTS (SELECT * FROM information_schema.routines WHERE routine_name = '{storedProcedure}');";
}

/// <summary>
/// Get PostgreSQL script to check if record exist
/// </summary>
/// <param name="recordExist"></param>
/// <returns></returns>
protected override string GetRecordExistScript(DatabaseRule recordExist)
{
return $@"SELECT EXISTS (SELECT 1 FROM {recordExist.TableName} WHERE {recordExist.PredicateValue});";
}

/// <summary>
/// Get PostgreSQL script to count the records in a table
/// </summary>
/// <param name="recordCount"></param>
/// <returns></returns>
protected override string GetRecordCountScript(DatabaseRule recordCount)
{
return $@"SELECT EXISTS (SELECT 1 FROM {recordCount.TableName} WHERE (SELECT count(*) FROM {recordCount.TableName}){recordCount.PredicateValue});";
}
}
}
Loading