You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`QAToolKit.Engine.Database` is a .NET standard library, which can be used to do database fitness tests. For example, if you want to test that table is present in database, or certain number of records exist in specific table or if a record exists.
10
+
11
+
`QAToolKit.Engine.Database` is a .NET standard library, which can be used to do database fitness tests. For example, if
12
+
you want to test that table is present in database, or certain number of records exist in specific table or if a record
13
+
exists.
10
14
11
15
`DatabaseTestType` enumeration currently described those three test types:
16
+
12
17
-`ObjectExits`: Check if table, view or stored procedure exists.
13
18
-`RecordCount`: Check if record count in specific table equals an expression.
14
19
-`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.
20
+
-`CustomScript`: Check if a custom script returns results. You can write a custom select query and check if it has any
21
+
results.
16
22
17
23
Currently supports only relational databases: `SQLServer`, `MySQL` and `PostgreSQL`.
The code above will generate a SQLServer `DatabaseTest` list, which will be used by runner to run the tests against database.
77
+
78
+
The code above will generate a SQLServer `DatabaseTest` list, which will be used by runner to run the tests against
79
+
database.
62
80
63
81
Above example adds all three test types to the generator:
82
+
64
83
-`AddDatabaseObjectExitsRule`: will check if a table `mytable` exists in the database.
65
84
-`AddDatabaseRecordExitsRule`: will check if a record in table `mytable` with `name` equals `myname` exists.
66
85
-`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.
86
+
-`AddCustomSqlRule`: will check if a custom script returns any results. This way you have a lot of freedom to define
87
+
your own queries. Please note, that the query you specify is wrapped in the `EXISTS` clause for a specific database
88
+
vendor.
89
+
-`CaptureQueryStatistics`: capture the `time` and/or `IO` statistics by running the specified queries. Runner will
90
+
collect the results from
91
+
the [InfoMessage event](https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.infomessage?view=dotnet-plat-ext-5.0)
92
+
. This is currently implemented only for SQL Server.
68
93
69
-
Alternatively if you want to use `MySQL` or `PostgreSQL` generators, you can use MySqlTestGenerator` or `PostgresqlTestGenerator` respectively.
94
+
Alternatively if you want to use `MySQL` or `PostgreSQL` generators, you can use MySqlTestGenerator` or `
95
+
PostgresqlTestGenerator` respectively.
70
96
71
97
To run the tests, we create a `SqlServerTestRunner` runner:
72
98
@@ -79,34 +105,140 @@ var runner = new SqlServerTestRunner(scripts, options =>
Alternatively if you want to use `MySQL` or `PostgreSQL` runners, you can use MySqlTestRunner` or `PostgresqlTestRunner` respectively.
108
+
With version 0.4.0 you can run runners in parallel, by specifying the number in Run() method:
109
+
110
+
```csharp
111
+
//Run in 5 parallels
112
+
varresults=runner.Run(5);
113
+
```
114
+
115
+
Alternatively if you want to use `MySQL` or `PostgreSQL` runners, you can use MySqlTestRunner` or `PostgresqlTestRunner`
116
+
respectively.
117
+
118
+
Please note that **your user must have correct database permissions**. I suggest a read-only permissions that can also
119
+
access `sys` or `information_schema` schemas.
120
+
121
+
Below is a sample of database test result:
122
+
123
+
```json
124
+
[
125
+
{
126
+
"Id": "c9ac5d7f-d4fd-44e5-a743-837be2740b72",
127
+
"Hash": "0x4E263AF99F6C0D3AB0268A249CC14E27",
128
+
"RunAt": "2021-03-07T12:39:16.4181917+01:00",
129
+
"DatabaseResult": null,
130
+
"Variable": null,
131
+
"Script": "SET STATISTICS TIME ON;SET STATISTICS IO ON;SELECT * FROM myTable1 INNER JOIN myTable ON myTable1.id=myTable.sampleID;SET STATISTICS TIME OFF;SET STATISTICS IO OFF;",
132
+
"DatabaseTestType": 4,
133
+
"DatabaseKind": 1,
134
+
"ServerCpuTime": 234,
135
+
"ServerElapsedTime": 494,
136
+
"TotalElapsedTime": 887,
137
+
"Statistics": {
138
+
"Workfile": {
139
+
...
140
+
},
141
+
"Worktable": {
142
+
...
143
+
},
144
+
"myTable": {
145
+
"ScanCount": 1,
146
+
"LogicalReads": 1674,
147
+
"PhysicalReads": 0,
148
+
"PageServerReads": 0,
149
+
"ReadAheadReads": 0,
150
+
"PageServerReadAheadReads": 0,
151
+
"LobLogicalReads": 0,
152
+
"LobPhysicalReads": 0,
153
+
"LobPageServerReads": 0,
154
+
"LobReadAheadReads": 0,
155
+
"LobPageServerReadAheadReads": 0
156
+
},
157
+
"myTable1": {
158
+
"ScanCount": 1,
159
+
"LogicalReads": 218,
160
+
"PhysicalReads": 0,
161
+
"PageServerReads": 0,
162
+
"ReadAheadReads": 0,
163
+
"PageServerReadAheadReads": 0,
164
+
"LobLogicalReads": 0,
165
+
"LobPhysicalReads": 0,
166
+
"LobPageServerReads": 0,
167
+
"LobReadAheadReads": 0,
168
+
"LobPageServerReadAheadReads": 0
169
+
}
170
+
}
171
+
}
172
+
]
173
+
```
174
+
A bit of explanations:
175
+
176
+
-`Id`: A unique GUID generated on the fly for every result.
177
+
-`Hash`: is a MurMurHash of the script string.
178
+
-`Statistics`: those are collected only if a `DatabaseTest` with `CaptureQueryStatistics` is used. It contains the parsed information retrieved from the SQL server (currently there is no support PostgreSQL or MySQL).
179
+
180
+
## Asserters
181
+
182
+
If you want to assert query statistics in version `0.4.0` and above, you can assert the results with `TestAsserter`.
183
+
Runner will return `DatabaseResult = null`, because it can not assert the vast arrays of results when executing a query.
184
+
You need to do that after you run your queries.
185
+
186
+
Sample code for asserter:
187
+
188
+
```csharp
189
+
vargenerator=newSqlServerTestGenerator(options=>
190
+
{
191
+
options.CaptureQueryStatistics(newstring[]
192
+
{
193
+
@"SELECT * FROM myTable1 INNER JOIN myTable ON myTable1.id=myTable.sampleID;"
194
+
},
195
+
newQueryStatisticsType[]
196
+
{
197
+
QueryStatisticsType.Time, QueryStatisticsType.Io
198
+
});
199
+
});
83
200
84
-
Please note that **your user must have correct database permissions**. I suggest a read-only permissions that can also access `sys` or `information_schema` schemas.
0 commit comments