Skip to content

main · v3.2 (unreleased)

Test on PostgreSQL or SQL Server

Users · How-to guide · CurrentMarkdownAgent index

Status: Current. Where this is implemented names the code and the tests.

Use a PostgreSQL or SQL Server test instance to run the domain tests with the same database adapter that the application uses in production. By default the tests use the Memory adapter. Where the adapters differ explains which behaviour depends on the adapter.

The guide assumes an application started from the sample, as Create an application describes, and its commands run from the root of that application. In the platform’s repository the same steps run the tests of the sample itself, with sample/ before the path of the project, after ./build.sh SampleGenerate; the build needs ALLORS_CONFIG_ROOT, for which a template serves.

The tests choose their adapter from ALLORS_TEST_ADAPTER and ignore the application’s configuration root and its settings adapter and ConnectionStrings:DefaultConnection. On either SQL adapter, a run drops any database named sample_domain_tests on the server in the admin connection and creates it again.

Before the first run, check that this name is free or belongs to a disposable database reserved for these tests. If the name is already in use for anything else, choose another test server or set the constant DatabaseName in Database/Domain.Tests/DatabaseFixture.cs to an unused lower-case name such as orders_domain_tests. The examples below use the default name.

The tests drop and create their database through the admin connection of the server: ALLORS_NPGSQL for PostgreSQL, ALLORS_SQLCLIENT for SQL Server. Its login has to be allowed to create a database. For PostgreSQL:

Terminal window
export ALLORS_NPGSQL='Host=localhost;Username=postgres;Password=…'

For SQL Server:

Terminal window
export ALLORS_SQLCLIENT='Server=localhost;User Id=sa;Password=…;TrustServerCertificate=true'

The tests connect to their database with the same login. To test on SQL Server LocalDB, name it: Server=(localdb)\MSSQLLocalDB;Integrated Security=true;TrustServerCertificate=true.

Set ALLORS_TEST_ADAPTER in the same terminal, here to PostgreSQL:

Terminal window
export ALLORS_TEST_ADAPTER=npgsql
ValueWhere the tests run
None, or memoryOn the Memory adapter, in the process of the tests.
snapshotOn the Snapshot adapter, in the process of the tests.
sqlclientIn the database sample_domain_tests on the SQL Server of ALLORS_SQLCLIENT.
npgsqlIn the database sample_domain_tests on the PostgreSQL of ALLORS_NPGSQL.

Upper and lower case are equal, and spaces around the value do not count.

Terminal window
dotnet test Database/Domain.Tests

The same tests pass as on the Memory adapter: the ten tests of the sample, if the application has not changed them yet.

Before the tests that build a population, the run drops the database sample_domain_tests, if the server has one, and creates it. The drop ends every session that is open on the database, such as one of step 4; on PostgreSQL before version 13 the drop fails instead while such a session is open. Each test then builds its population as it does on the Memory adapter: Init(), which builds the tables in the new database and empties them for each later test, and the setup of the application, which it commits.

The tests run one at a time. A run holds a lock named after its test database. A second run using that name on the same server waits for the lock, without a message and without a time limit.

The database stays after the run, with what the last test left in it. Its tables are those that The database schema describes. Connect to it with the admin login and list them:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'allors' ORDER BY table_name;

For the sample the query lists 24 tables: the objects table allors_o, a table for each class and a table for each relation type that needs one. The objects table holds the id, the class and the version of every object; this query counts the objects of each class, by the id of the class:

SELECT c, count(*) FROM allors.allors_o GROUP BY c ORDER BY count(*) DESC;

The next run creates the database again. To remove it in between, close connections to it and connect to postgres or master with the admin login. Use the name from DatabaseName in the following statement.

DROP DATABASE sample_domain_tests;

To run the tests on the Memory adapter again, remove the variable:

Terminal window
unset ALLORS_TEST_ADAPTER

A value of ALLORS_TEST_ADAPTER that names no adapter makes every test that builds a population fail with:

ALLORS_TEST_ADAPTER is 'postgres'. Set it to memory, snapshot, sqlclient or npgsql, or leave it unset for the Memory adapter.

Without the admin connection that the value needs, they fail with this message, or with the same one for ALLORS_SQLCLIENT:

ALLORS_NPGSQL is not set. Point it at an admin connection that can create the domain test database.

xUnit reports either message as the first of two errors. The second says that the class fixture Fixture had an unresolved constructor argument, DatabaseFixture; it follows from the first. The two tests of DatabaseFixtureTests, which build no population, pass.

An error of the server, such as a login that may not create a database, makes the same tests fail in the same way, with the message of the server as the first error. If a run seems to hang, check whether another run holds the lock for the same test database, as step 3 describes.

The platform’s continuous integration runs the domain tests of the sample on the Memory adapter only. A job that runs the tests of an application on a database server sets the same variables, with a server that the job can reach.

  • DatabaseFixture, in sample/Database/Domain.Tests, reads ALLORS_TEST_ADAPTER, takes the lock, drops and creates the database, and builds the database of each test. DatabaseCollectionDefinition makes it one fixture for every test that derives from DomainTest, so that a run creates the database once and runs those tests one at a time, and Fixture hands it to them. DomainTest builds the population of a test.
  • DatabaseFixtureTests checks that the SQL adapters require ALLORS_SQLCLIENT and ALLORS_NPGSQL, and AssemblyInfo.cs keeps it from running beside the domain tests.
  • Provisioning, in Allors.Database.Adapters.Sql.SqlClient and Allors.Database.Adapters.Sql.Npgsql under dotnet/System/Database/Adapters, drops and creates the database through the admin connection.