HitchMySQL

Note

This documentation applies to the latest version of hitchmysql.

HitchMySQL is a Hitch Plugin created to make testing applications that use MySQL easier.

It contains:

  • A Hitch Package to download and install specified version(s) of MySQL.
  • A Service to set up an isolated MySQL environment and run it.

Note: the MySQL service destroys and sets up a new database during each test run in order to provide strict Isolation for your tests.

Installation

First, install the the plugin in your tests directory:

$ hitch install hitchmysql

Set up MySQL

In your test, define the MySQL installation you want to test with:

import hitchmysql

mysql_package = hitchmysql.MySQLPackage(
    version="5.6.26"  # Optional (default is the latest version of MySQL)
)

# Downloads & installs MySQL to ~/.hitchpkg if not already installed by previous test
mysql_package.build()

To use, define the service after initializing the Service Bundle but before starting it:

Note

See also: Generic Service API

# Define a MySQL user for your service to set up
mysql_user = hitchmysql.MySQLUser("newpguser", "pguserpassword")

# Define a MySQL database for your service to set up
mysql_database = hitchmysql.MySQLDatabase(
    name="databasename",                  # Mandatory
    owner=mysql_user,                      # Mandatory
    dump="dumps/yourdump.sql"             # Optional (default: create empty database)
)

self.services['MySQL'] = hitchmysql.MySQLService(
    mysql_package=mysql_package,       # Mandatory
    port=13306,                        # Optional (default: 13306)
    users=[mysql_user, ],              # Optional (default: no users)
    databases=[mysql_database, ]       # Optional (default: no databases)
)

Interacting with MySQL

Once it is running, you can interact with the service and its databases:

In [1]: self.services['MySQL'].databases[0].mysql("SELECT * FROM yourtable;").run()
[ Prints output ]

In [2]: self.services['MySQL'].databases[0].mysql().run()
[ Launches into mysql shell ]