TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | Custom |
ReleasedLast Release | Jan 2016 |
Maintained by Mike Rhodes.
MRDatabaseContentChecker is a small library to make checking the contents of a database easier and more literate in your tests. It tries to stay out your way and make writing tedious checks simpler and less error prone.
You can test the content of either tables or queries.
MRDatabaseContentChecker requires that you are using FMDB.
You can check either the full content of database tables, or the results of a query (as they're both table-like structures).
The simplest example, using SenTest, is something like the following:
FMDatabase *db = [...];
MRDatabaseContentChecker *dc = [[MRDatabaseContentChecker alloc] init];
NSError *validationError;
NSArray *expectedRows = @[
@[@"first_name", @"surname"], // first array is column headers
@[@"Mike", @"Rhodes"], // expected data starts in row 2
@[@"John", @"Smith"] // and continues...
];
STAssertTrue([dc checkDatabase:db
table:@"users"
hasRows:expectedRows
error:&validationError],
[dc formattedErrors:validationError]);
Essentially:
-checkDatabase:table:hasRows:error:
in a testing macro.NO
, use the -formattedErrors
helper to print the
failures.The above example can be rewritten to use a query rather than a table name:
FMDatabase *db = [...];
MRDatabaseContentChecker *dc = [[MRDatabaseContentChecker alloc] init];
NSError *validationError;
NSArray *expectedRows = @[ /* as before */ ];
STAssertTrue([dc checkDatabase:db
query:@"select first_name, surname from users;"
hasRows:expectedRows
error:&validationError],
[dc formattedErrors:validationError]);
At the moment, there's no support for placeholders; you control the SQL you're generating in your tests. Or, if not, feel free to open a PR as I'm not in the slightest averse to including further features.
ORDER BY
clause to be included.-checkDatabase:table:hasRows:error:
and -checkDatabase:query:hasRows:error:
check:
As shown above, expected values are passed as an array of arrays. Each expected value is check against its corresponding entry in the result set based on its type. The allowed types are:
Using an unsupported type (or a class that isn't a subclass of a supported type) will result in a failure checking the value.
You can pass different datatypes for a given column's expected values:
expectedRows = @[
@[@"name", @"github_username", @"age"],
@[@"Mike Rhodes", @"mikerhodes", @30],
@[@"John Smith", [NSNull null], @27]
];
MRDatabaseContentChecker depends on:
Michael Rhodes, [email protected]
MRDatabaseContentChecker is available under the Apache v2 license. See the LICENSE file for more info.