Skip to content

Commit 9722975

Browse files
authored
Add new rule CustomSqlRule, null checks, more unit tests
1 parent 3a61fb7 commit 9722975

19 files changed

+392
-23
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.2.6</Version>
3+
<Version>0.3.0</Version>
44
</PropertyGroup>
55
</Project>

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `ObjectExits`: Check if table, view or stored procedure exists.
1313
- `RecordCount`: Check if record count in specific table equals an expression.
1414
- `RecordExist`: Check if a record exists in specific table.
15+
- `CustomScript`: Check if a custom script returns results. You can write a custom select query and check if it has any results.
1516

1617
Currently supports only relational databases: `SQLServer`, `MySQL` and `PostgreSQL`.
1718

@@ -46,6 +47,13 @@ var generator = new SqlServerTestGenerator(options =>
4647
Operator = "="
4748
}
4849
});
50+
51+
options.AddCustomSqlRule(
52+
new List<string>()
53+
{
54+
"SELECT * FROM [table] WHERE timestamp = '2016-05-31'",
55+
"SELECT * FROM [table] WHERE value < 1"
56+
});
4957
});
5058

5159
List<DatabaseTest> scripts = await generator.Generate();
@@ -56,6 +64,7 @@ Above example adds all three test types to the generator:
5664
- `AddDatabaseObjectExitsRule`: will check if a table `mytable` exists in the database.
5765
- `AddDatabaseRecordExitsRule`: will check if a record in table `mytable` with `name` equals `myname` exists.
5866
- `AddDatabaseRecordsCountRule`: will check if there is exactly 100 records in the `mytable` table.
67+
- `AddCustomSqlRule`: will check if a custom script returns any results. This way you have a lot of freedom to define your own queries. Please note, that the query you specify is wrapped in the `EXISTS` clause for a specific database vendor.
5968

6069
Alternatively if you want to use `MySQL` or `PostgreSQL` generators, you can use MySqlTestGenerator` or `PostgresqlTestGenerator` respectively.
6170

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using Microsoft.Extensions.Logging;
2+
using QAToolKit.Engine.Database.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
namespace QAToolKit.Engine.Database.Test
9+
{
10+
public class DatabaseTestGeneratorOptionsTests
11+
{
12+
private readonly ILogger<DatabaseTestGeneratorOptionsTests> _logger;
13+
14+
public DatabaseTestGeneratorOptionsTests(ITestOutputHelper testOutputHelper)
15+
{
16+
var loggerFactory = new LoggerFactory();
17+
loggerFactory.AddProvider(new XunitLoggerProvider(testOutputHelper));
18+
_logger = loggerFactory.CreateLogger<DatabaseTestGeneratorOptionsTests>();
19+
}
20+
21+
[Fact]
22+
public void AddCustomSqlRuleTest_Successful()
23+
{
24+
var options = new DatabaseTestGeneratorOptions();
25+
options.AddCustomSqlRule(new List<string>() {
26+
"query 1",
27+
"query 2"
28+
});
29+
30+
Assert.NotNull(options);
31+
}
32+
33+
[Fact]
34+
public void AddCustomSqlRuleTest_Failure()
35+
{
36+
var options = new DatabaseTestGeneratorOptions();
37+
Assert.Throws<ArgumentNullException>(() => options.AddCustomSqlRule(null));
38+
}
39+
40+
[Fact]
41+
public void AddDatabaseObjectExitsRuleTest_Successful()
42+
{
43+
var options = new DatabaseTestGeneratorOptions();
44+
options.AddDatabaseObjectExitsRule(new string[] { "sp1", "sp2" }, DatabaseObjectType.StoredProcedure);
45+
46+
Assert.NotNull(options);
47+
}
48+
49+
[Fact]
50+
public void AddDatabaseObjectExitsRuleTest_Failure()
51+
{
52+
var options = new DatabaseTestGeneratorOptions();
53+
Assert.Throws<ArgumentNullException>(() => options.AddDatabaseObjectExitsRule(null, DatabaseObjectType.StoredProcedure));
54+
}
55+
56+
[Fact]
57+
public void AddDatabaseRecordExitsRuleTest_Successful()
58+
{
59+
var options = new DatabaseTestGeneratorOptions();
60+
options.AddDatabaseRecordExitsRule(
61+
new List<DatabaseRecordExistRule>()
62+
{
63+
new DatabaseRecordExistRule()
64+
{
65+
TableName = "mytable",
66+
ColumnName = "name",
67+
Operator = "=",
68+
Value = "myname"
69+
}
70+
});
71+
72+
Assert.NotNull(options);
73+
}
74+
75+
[Fact]
76+
public void AddDatabaseRecordExitsRuleTest_Failure()
77+
{
78+
var options = new DatabaseTestGeneratorOptions();
79+
Assert.Throws<ArgumentNullException>(() => options.AddDatabaseRecordExitsRule(null));
80+
}
81+
82+
[Fact]
83+
public void AddDatabaseRecordsCountRuleTest_Successful()
84+
{
85+
var options = new DatabaseTestGeneratorOptions();
86+
options.AddDatabaseRecordsCountRule(
87+
new List<DatabaseRecordCountRule>()
88+
{
89+
new DatabaseRecordCountRule()
90+
{
91+
TableName = "mytable",
92+
Operator = "=",
93+
Count = 100
94+
}
95+
});
96+
97+
Assert.NotNull(options);
98+
}
99+
100+
[Fact]
101+
public void AddDatabaseRecordsCountRuleTest_Failure()
102+
{
103+
var options = new DatabaseTestGeneratorOptions();
104+
Assert.Throws<ArgumentNullException>(() => options.AddDatabaseRecordsCountRule(null));
105+
}
106+
}
107+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace QAToolKit.Engine.Database.Test
7+
{
8+
public class DatabaseTestRunnerOptionsTests
9+
{
10+
private readonly ILogger<DatabaseTestRunnerOptionsTests> _logger;
11+
12+
public DatabaseTestRunnerOptionsTests(ITestOutputHelper testOutputHelper)
13+
{
14+
var loggerFactory = new LoggerFactory();
15+
loggerFactory.AddProvider(new XunitLoggerProvider(testOutputHelper));
16+
_logger = loggerFactory.CreateLogger<DatabaseTestRunnerOptionsTests>();
17+
}
18+
19+
[Fact]
20+
public void AddMySQLConnectionTest_Successful()
21+
{
22+
var options = new DatabaseTestRunnerOptions();
23+
options.AddMySQLConnection("server=localhost;user=user;password=mypassword;Initial Catalog=myDatabase");
24+
25+
Assert.NotNull(options);
26+
}
27+
28+
[Fact]
29+
public void AddMySQLConnectionTest_Failure()
30+
{
31+
var options = new DatabaseTestRunnerOptions();
32+
Assert.Throws<ArgumentNullException>(() => options.AddMySQLConnection(null));
33+
}
34+
35+
[Fact]
36+
public void AddPostgreSQLConnectionTest_Successful()
37+
{
38+
var options = new DatabaseTestRunnerOptions();
39+
options.AddPostgreSQLConnection("server=localhost;user=user;password=mypassword;Initial Catalog=myDatabase");
40+
41+
Assert.NotNull(options);
42+
}
43+
44+
[Fact]
45+
public void AddPostgreSQLConnectionTest_Failure()
46+
{
47+
var options = new DatabaseTestRunnerOptions();
48+
Assert.Throws<ArgumentNullException>(() => options.AddPostgreSQLConnection(null));
49+
}
50+
51+
[Fact]
52+
public void AddSQLServerConnectionTest_Successful()
53+
{
54+
var options = new DatabaseTestRunnerOptions();
55+
options.AddSQLServerConnection("server=localhost;user=user;password=mypassword;Initial Catalog=myDatabase");
56+
57+
Assert.NotNull(options);
58+
}
59+
60+
[Fact]
61+
public void AddSQLServerConnectionTest_Failure()
62+
{
63+
var options = new DatabaseTestRunnerOptions();
64+
Assert.Throws<ArgumentNullException>(() => options.AddSQLServerConnection(null));
65+
}
66+
}
67+
}

src/QAToolKit.Engine.Database.Test/Generators/MySqlTestGeneratorTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public async Task MySqlRecordExistScriptTest_Success()
191191
results.ShouldEqual(await generator.Generate());
192192
Assert.Equal(DatabaseKind.MySQL, generator.DatabaseKind);
193193
}
194-
194+
195195
[Fact]
196196
public async Task MySqlRecordCountScriptTest_Success()
197197
{
@@ -221,5 +221,30 @@ public async Task MySqlRecordCountScriptTest_Success()
221221
results.ShouldEqual(await generator.Generate());
222222
Assert.Equal(DatabaseKind.MySQL, generator.DatabaseKind);
223223
}
224+
225+
[Fact]
226+
public async Task MySqlCustomScriptTest_Success()
227+
{
228+
var generator = new MySqlTestGenerator(options =>
229+
{
230+
options.AddCustomSqlRule(
231+
new List<string>()
232+
{
233+
"SELECT * FROM mytable WHERE (SELECT COUNT(*) AS count FROM mytable) = 50"
234+
});
235+
});
236+
237+
var results = new List<DatabaseTest>
238+
{
239+
new DatabaseTest(
240+
null,
241+
$@"SELECT EXISTS (SELECT * FROM mytable WHERE (SELECT COUNT(*) AS count FROM mytable) = 50);",
242+
DatabaseTestType.CustomScript,
243+
DatabaseKind.MySQL)
244+
}.ToExpectedObject();
245+
246+
results.ShouldEqual(await generator.Generate());
247+
Assert.Equal(DatabaseKind.MySQL, generator.DatabaseKind);
248+
}
224249
}
225250
}

src/QAToolKit.Engine.Database.Test/Generators/PostgresqlTestGeneratorTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,30 @@ public async Task PostgresqlRecordCountScriptTest_Success()
221221
results.ShouldEqual(await generator.Generate());
222222
Assert.Equal(DatabaseKind.PostgreSQL, generator.DatabaseKind);
223223
}
224+
225+
[Fact]
226+
public async Task PostgresqlCustomScriptTest_Success()
227+
{
228+
var generator = new PostgresqlTestGenerator(options =>
229+
{
230+
options.AddCustomSqlRule(
231+
new List<string>()
232+
{
233+
@"SELECT * FROM ""mytable"" WHERE (SELECT COUNT(*) AS ""count"" FROM ""mytable"") = 10"
234+
});
235+
});
236+
237+
var results = new List<DatabaseTest>
238+
{
239+
new DatabaseTest(
240+
null,
241+
$@"SELECT EXISTS(SELECT * FROM ""mytable"" WHERE (SELECT COUNT(*) AS ""count"" FROM ""mytable"") = 10);",
242+
DatabaseTestType.CustomScript,
243+
DatabaseKind.PostgreSQL)
244+
}.ToExpectedObject();
245+
246+
results.ShouldEqual(await generator.Generate());
247+
Assert.Equal(DatabaseKind.PostgreSQL, generator.DatabaseKind);
248+
}
224249
}
225250
}

src/QAToolKit.Engine.Database.Test/Generators/SqlServerTestGeneratorTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,30 @@ public async Task SqlServerRecordCountScriptTest_Success()
222222
results.ShouldEqual(await generator.Generate());
223223
Assert.Equal(DatabaseKind.SQLServer, generator.DatabaseKind);
224224
}
225+
226+
[Fact]
227+
public async Task SqlServerCustomScriptTest_Success()
228+
{
229+
var generator = new SqlServerTestGenerator(options =>
230+
{
231+
options.AddCustomSqlRule(
232+
new List<string>()
233+
{
234+
"SELECT * FROM [mytable] WHERE (SELECT COUNT(*) AS [count] FROM [mytable]) = 50"
235+
});
236+
});
237+
238+
var results = new List<DatabaseTest>
239+
{
240+
new DatabaseTest(
241+
null,
242+
$@"IF EXISTS (SELECT * FROM [mytable] WHERE (SELECT COUNT(*) AS [count] FROM [mytable]) = 50) BEGIN Select 1 END ELSE BEGIN Select 0 END;",
243+
DatabaseTestType.CustomScript,
244+
DatabaseKind.SQLServer)
245+
}.ToExpectedObject();
246+
247+
results.ShouldEqual(await generator.Generate());
248+
Assert.Equal(DatabaseKind.SQLServer, generator.DatabaseKind);
249+
}
225250
}
226251
}

src/QAToolKit.Engine.Database.Test/Runners/MySqlTestGeneratorTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using ExpectedObjects;
2-
using NSubstitute;
1+
using NSubstitute;
32
using QAToolKit.Engine.Database.Generators;
43
using QAToolKit.Engine.Database.Models;
54
using QAToolKit.Engine.Database.Runners;
65
using System;
7-
using System.Collections.Generic;
86
using System.Threading.Tasks;
97
using Xunit;
108

src/QAToolKit.Engine.Database.Test/Runners/PostgresqlTestGeneratorTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using ExpectedObjects;
2-
using NSubstitute;
1+
using NSubstitute;
32
using QAToolKit.Engine.Database.Generators;
43
using QAToolKit.Engine.Database.Models;
54
using QAToolKit.Engine.Database.Runners;
65
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
96
using System.Threading.Tasks;
107
using Xunit;
118

src/QAToolKit.Engine.Database.Test/Runners/SqlServerTestGeneratorTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using ExpectedObjects;
2-
using NSubstitute;
1+
using NSubstitute;
32
using QAToolKit.Engine.Database.Generators;
43
using QAToolKit.Engine.Database.Models;
54
using QAToolKit.Engine.Database.Runners;
65
using System;
7-
using System.Collections.Generic;
86
using System.Threading.Tasks;
97
using Xunit;
108

0 commit comments

Comments
 (0)