Skip to content

Database Integration Tests : SQL Generation : Adding new tests for a database

Ephrim Stanley edited this page Mar 15, 2022 · 3 revisions

1. Get a test database

The SQL generation tests are run against real databases. There are 2 ways to inject a database connection into the test framework :

  • Static test connections - These are test connections configured in the TestServer's configuration file. These are static in the sense that they point to databases that you might already have access to.
  • Dynamic test connections - These are test connections configured via code. The TestServer makes use of ConnectionTestManager implementations that can create databases and connections on demand. For e.g. spin up a database in a Docker container.

2. Start the legend-engine server

  • Build the legend-engine project
  • Start org.finos.legend.engine.server.test.shared.TestServer with the following configuration
server <path to>userTestConfig_withTestConnections.json

3. Verify test connections can be acquired from the legend-engine server

The following API

curl -X GET "http://localhost:6060/api/pure/v1/utilities/testConnection/getTestConnection/H2" -H "accept: application/json"

should return a response like

{
  "_type": "RelationalDatabaseConnection",
  ...
  "datasourceSpecification": {
    "_type": "static",
    "sourceInformation": null,
    "host": "127.0.0.1",
    "port": 4038,
    "databaseName": "temp"
  },
  "authenticationStrategy": {
    "_type": "h2Default",
    "sourceInformation": null
  },
  "databaseType": "H2",
  "postProcessors": []
}

4. Start the Pure IDE

  • Build the legend-pure project
  • Start the PureIDE Server with configuration that points to the test server started above
-Dlegend.test.server.host=127.0.0.1 -Dlegend.test.server.port=6060 -Dlegend.test.clientVersion=vX_X_X -Dlegend.test.serverVersion=v1 

5. Run existing tests

Add a go function to welcome.pure

Each database type has a top level "test runner" function which collects various test functions and executes them with a particular database type.

function go():Any[*]
{
   meta::relational::tests::dbSpecificTests::H2::runSQLQueryTests();
}

Hit F9 to run the tests. Test summary statistics are printed in the console pane

6. Using the light IDE, add a new test

function <<dbTest.Test>> meta::relational::tests::dbSpecificTests::sqlQueryTests::dynaFunctions::numeric::testFloor2(config:DbTestConfig[1]):String[0..1]
{
  let dynaFunc = ^DynaFunction(name='floor', parameters=[^Literal(value= 5.4)]);
  let expected = ^Literal(value=5);
  runDynaFunctionDatabaseTest($dynaFunc, $expected, $config);
}

7. Re-run the tests

Rinse and repeat

8. Commit the tests and submit a PR