Code Monkey home page Code Monkey logo

mrdatabasecontentchecker's Introduction

MRDatabaseContentChecker

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.

mrdatabasecontentchecker's People

Contributors

mikerhodes avatar rhyshort avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.