documentation:software:whiterabbit:test_framework
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| documentation:software:whiterabbit:test_framework [2016/06/02 11:03] – schuemie | documentation:software:whiterabbit:test_framework [2020/02/18 14:13] (current) – maximmoinat | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Rabbit-In-a-Hat testing framework ====== | ====== Rabbit-In-a-Hat testing framework ====== | ||
| + | |||
| + | **NOTICE FEB 2020:** the current documentation can be found on Github | ||
| + | http:// | ||
| + | |||
| Rabbit-In-a-Hat can generate a framework for creating a set of [[https:// | Rabbit-In-a-Hat can generate a framework for creating a set of [[https:// | ||
| + | |||
| + | Unit testing assumes that you have your data in source format somewhere in a database. You should already have created an ETL process that will extract from the source database, transform it into CDM format, and load it into a CDM schema. The unit test framework can be used to make sure that your ETL process is doing what it is supposed to do. For this you will need to create a new, empty database with exactly the same structure as your source database, and a new empty database where a test CDM database will live. The framework can be used to insert test data into the empty source schema. You can then run your ETL process on the test data to populate the test CDM database. you can then use the framework to verify that the output of the ETL in the test CDM database is what you'd expect given the test source data. | ||
| ===== Overview ===== | ===== Overview ===== | ||
| Line 18: | Line 24: | ||
| In Rabbit-in-a-Hat, | In Rabbit-in-a-Hat, | ||
| + | |||
| + | ===== Defining unit tests using the framework ===== | ||
| Next, create an empty R script, and start by sourcing the R file that was just created: | Next, create an empty R script, and start by sourcing the R file that was just created: | ||
| Line 27: | Line 35: | ||
| Be sure to run this command immediately to make the function definitions available to R-Studio. | Be sure to run this command immediately to make the function definitions available to R-Studio. | ||
| - | The test framework defines the following functions for each table in the source schema: | + | ==== Available functions ==== |
| + | |||
| + | The test framework defines the following functions | ||
| * '' | * '' | ||
| Line 33: | Line 43: | ||
| * '' | * '' | ||
| - | * '' | + | * '' |
| + | |||
| + | The following functions are defined **for each table in the CDM schema**: | ||
| + | |||
| + | * '' | ||
| + | |||
| + | * '' | ||
| + | |||
| + | * '' | ||
| + | |||
| + | One further function is available: | ||
| + | |||
| + | * '' | ||
| + | |||
| + | ==== Defining unit tests ==== | ||
| + | |||
| + | Using these functions, we can define tests. Here is an example unit test: | ||
| + | |||
| + | < | ||
| + | declareTest(101, | ||
| + | add_enrollment(member_id = " | ||
| + | add_enrollment(member_id = " | ||
| + | expect_person(person_id = 101, gender_concept_id = 8507, gender_source_value = " | ||
| + | expect_person(person_id = 102, gender_concept_id = 8532, gender_source_value = " | ||
| + | </ | ||
| + | |||
| + | In this example, we define a test for gender mappings. We specify that two records should be created in the '' | ||
| + | |||
| + | In this example we furthermore describe what we expect to see in the CDM data schema. In this case we formulate expectations for the '' | ||
| + | |||
| + | We can add many such tests to our R script. For an example of a full set of test definitions, | ||
| + | |||
| + | ==== Generate test data in the source data schema ==== | ||
| + | |||
| + | After we have defined all our tests we need to run | ||
| + | < | ||
| + | insertSql <- generateInsertSql(databaseSchema = " | ||
| + | testSql <- generateTestSql(databaseSchema = " | ||
| + | </ | ||
| + | to generate the SQL for inserting the test data in the database (insertSql), | ||
| + | |||
| + | < | ||
| + | library(DatabaseConnector) | ||
| + | connectionDetails <- createConnectionDetails(user = " | ||
| + | | ||
| + | dbms = "sql server", | ||
| + | | ||
| + | connection <- connect(connectionDetails) | ||
| + | |||
| + | executeSql(connection, | ||
| + | </ | ||
| + | |||
| + | ==== Run your ETL on the test data ==== | ||
| + | |||
| + | Now that the test source data is populated. You can run the ETL process you would like to test. The ETL should transform the data in '' | ||
| + | |||
| + | ==== Test whether the CDM data meets expectations ==== | ||
| + | |||
| + | The test SQL will create a table called '' | ||
| + | |||
| + | < | ||
| + | executeSql(connection, | ||
| + | </ | ||
| + | |||
| + | Aftwerwards, | ||
| + | |||
| + | < | ||
| + | querySql(connection, | ||
| + | </ | ||
| + | |||
| + | Which could return this table: | ||
| + | |||
| + | ^ID | ||
| + | |101 |Person gender mappings | ||
| + | |101 |Person gender mappings | ||
| + | |||
| + | In this case we see there were two expect statements under test 101 (Person gender mappings), and both expectations were met so the test passed. | ||
documentation/software/whiterabbit/test_framework.1464865380.txt.gz · Last modified: by schuemie
