Latest 0.4.0
Homepage https://github.com/mikerhodes/MRDatabaseContentChecker
License Apache Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Platforms ios 6.0, osx 10.8, requires ARC
Dependencies FMDB
Authors

Version
Platform

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.

Usage

You can check either the full content of database tables, or the results of
a query (as they’re both table-like structures).

Tables

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:

  1. Create your expected values array. This is an array of arrays. The first
    array always contains the column headers you want to check. The following
    rows define the data you expect to find.
  2. Call -checkDatabase:table:hasRows:error: in a testing macro.
  3. If that returns NO, use the -formattedErrors helper to print the
    failures.

Queries

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.

Caveats

  • The table-checking variant relies on the "undefined" ordering that SQLite
    returns rows in. Right now, if this doesn’t work, using the query variant is
    neccessary to allow an ORDER BY clause to be included.
  • There is no limit enforced on the number of errors reported. This may be
    a problem if there are millions of errors as every error has an
    entry in the error object passed out of the method.

Checks reference

-checkDatabase:table:hasRows:error: and -checkDatabase:query:hasRows:error:
check:

  • Each expected value matches is corresponding item in the result set.
  • There are the same number of results as expected.

Types

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:

NSString
NSString objects are checked using -isEqual.
NSNumber
NSNumber objects are checked using -isEqual against -longLongIntForColumn
values from the database.
BOOL (boxed)
As a boxed BOOL is an NSNumber, see NSNumber.
NSData
NSData objects are checked using -isEqual against -dataForColumn
value from database.
NSRegularExpression
This check passes if the return value from
-numberOfMatchesInString:options:error: returns one or more matches.
[NSNull null]
This check checks [NSNull null] against the value returned for
the column by -objectForColumnName:.

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]
];

Requirements

MRDatabaseContentChecker depends on:

  • FMDB

Installation

MRDatabaseContentChecker is available through CocoaPods,
to install it add the following line to your Podfile:

pod "MRDatabaseContentChecker"

Author

Michael Rhodes, [email protected]

License

MRDatabaseContentChecker is available under the Apache v2 license. See the LICENSE file for more info.

Latest podspec

{
    "name": "MRDatabaseContentChecker",
    "version": "0.4.0",
    "summary": "Check what's in your database during testing",
    "description": "MRDatabaseContentChecker will check the content ofndatabase tables and queries against arrays containingnthe expected rows. It'll intelligently figure out thentypes of the expected values and use appropriate checksnfor equality (e.g., regex matching).",
    "homepage": "https://github.com/mikerhodes/MRDatabaseContentChecker",
    "license": {
        "type": "Apache",
        "text": "Licensed under the Apache License, Version 2.0 (the "License");nyou may not use this file except in compliance with the License.nYou may obtain a copy of the License atnnhttp://www.apache.org/licenses/LICENSE-2.0nnUnless required by applicable law or agreed to in writing, softwarendistributed under the License is distributed on an "AS IS" BASIS,nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.nSee the License for the specific language governing permissions andnlimitations under the License.n"
    },
    "authors": {
        "Michael Rhodes": "[email protected]"
    },
    "source": {
        "git": "https://github.com/mikerhodes/MRDatabaseContentChecker.git",
        "tag": "0.4.0"
    },
    "platforms": {
        "ios": "6.0",
        "osx": "10.8"
    },
    "requires_arc": true,
    "source_files": "Classes",
    "ios": {
        "exclude_files": "Classes/osx"
    },
    "osx": {
        "exclude_files": "Classes/ios"
    },
    "dependencies": {
        "FMDB": [
            "~> 2.0"
        ]
    }
}

Pin It on Pinterest

Share This