Typesafe Activator

Slick TestKit Example (Slick 2.1)

Slick TestKit Example (Slick 2.1)

typesafehub
Source
May 20, 2014
slick scala

This template shows you how to use Slick's TestKit library to test your own Slick driver.

How to get "Slick TestKit Example (Slick 2.1)" on your computer

There are several ways to get this template.

Option 1: Choose slick-testkit-example-2.1 in the Typesafe Activator UI.

Already have Typesafe Activator (get it here)? Launch the UI then search for slick-testkit-example-2.1 in the list of templates.

Option 2: Download the slick-testkit-example-2.1 project as a zip archive

If you haven't installed Activator, you can get the code by downloading the template bundle for slick-testkit-example-2.1.

  1. Download the Template Bundle for "Slick TestKit Example (Slick 2.1)"
  2. Extract the downloaded zip file to your system
  3. The bundle includes a small bootstrap script that can start Activator. To start Typesafe Activator's UI:

    In your File Explorer, navigate into the directory that the template was extracted to, right-click on the file named "activator.bat", then select "Open", and if prompted with a warning, click to continue:

    Or from a command line:

     C:\Users\typesafe\slick-testkit-example-2.1> activator ui 
    This will start Typesafe Activator and open this template in your browser.

Option 3: Create a slick-testkit-example-2.1 project from the command line

If you have Typesafe Activator, use its command line mode to create a new project from this template. Type activator new PROJECTNAME slick-testkit-example-2.1 on the command line.

Option 4: View the template source

The creator of this template maintains it at https://github.com/typesafehub/activator-slick-testkit-example#slick-2.1.

Option 5: Preview the tutorial below

We've included the text of this template's tutorial below, but it may work better if you view it inside Activator on your computer. Activator tutorials are often designed to be interactive.

Preview the tutorial

Slick TestKit

When you write your own database driver for Slick, you need a way to run all the standard unit tests on it (in addition to any custom tests you may want to add) to ensure that it works correctly and does not claim to support any capabilities which are not actually implemented. For this purpose the Slick unit tests have been factored out into a separate Slick TestKit project.

This template contains a copy of Slick's standard PostgreSQL driver and all the infrastructure required to build and test it from its own sbt / Activator project.

Scaffolding

The build.sbt file is straight-forward. Apart from the usual name and version settings, it adds the dependencies for Slick, the TestKit, junit-interface, Logback and the PostgreSQL JDBC driver, and it sets some options for the test runs.

There is a copy of Slick's logback configuration in logback-test.xml but you can swap out the logging framework if you prefer a different one.

Driver

The actual driver implementation can be found in MyPostgresDriver.scala. It is a copy of Slick's PostgresDriver which has been renamed to MyPostgresDriver.

Test Harness

In order to run the TestKit tests, you need to add a class that extends DriverTest, plus an implementation of TestDB which tells the TestKit how to connect to a test database, get a list of tables, clean up between tests, etc.

In the case of the PostgreSQL test harness in MyPostgresTest.scala most of the default implementations can be used out of the box. Only getLocalTables and getLocalSequences require custom implementations. We also modify the driver's capabilities to indicate that our driver is capable of running the standard Plain SQL tests:

@RunWith(classOf[Testkit])
class MyPostgresTest extends DriverTest(MyPostgresTest.tdb)

object MyPostgresTest {
  def tdb(cname: String) = new ExternalTestDB("mypostgres", MyPostgresDriver) {
    override def getLocalTables(implicit session: Session) = ...
    override def getLocalSequences(implicit session: Session) = ...
    override lazy val capabilities = ...
  }
}

The name of a configuration prefix, in this case mypostgres, is passed to ExternalTestDB:

def tdb(cname: String) =
  new ExternalTestDB("mypostgres", MyPostgresDriver) ...

Database Configuration

Since the PostgreSQL test harness is based on ExternalTestDB, it needs to be configured in databases.properties. In order to run the tests, you need a PostgreSQL server with an account which allows you to create tablespaces and databases. Enter the user name and password in the configuration (and don't forget to save the file):

mypostgres.user = postgres
mypostgres.password = secret

If you installed PotgreSQL on OSX with Homebrew, this should be your own user name and an empty password. For a standard PostgreSQL installation on Windows, use the credentials you entered in the installer.

Run the Tests

You can run the tests on the Test page (or with sbt test on the command line). sbt discovers the JUnit test MyPostgresTest and runs it with TestKit's JUnit runner. This in turn causes the database to be set up through the test harness and all tests which are applicable for the driver (as determined by the capabilities setting in the test harness) to be run.

Note: When running the tests here in Activator (as of version 1.1.3), you will only see a single-line summary of the passed tests in Test and you may not get a proper test report at all when a test fails. You can switch to Compile for the complete log output that shows the individual tests.

Next Steps

Check out the full Slick manual and API docs.

You can also find more Slick templates, contributed by both, the Slick team and the community, here in Activator.

comments powered by Disqus