main · v3.2 (unreleased)
Test on PostgreSQL or SQL Server
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.
1. Give the tests an admin connection
Section titled “1. Give the tests an admin connection”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:
export ALLORS_NPGSQL='Host=localhost;Username=postgres;Password=…'For SQL Server:
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.
2. Choose the adapter
Section titled “2. Choose the adapter”Set ALLORS_TEST_ADAPTER in the same terminal, here to PostgreSQL:
export ALLORS_TEST_ADAPTER=npgsql| Value | Where the tests run |
|---|---|
None, or memory | On the Memory adapter, in the process of the tests. |
snapshot | On the Snapshot adapter, in the process of the tests. |
sqlclient | In the database sample_domain_tests on the SQL Server of ALLORS_SQLCLIENT. |
npgsql | In 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.
3. Run the tests
Section titled “3. Run the tests”dotnet test Database/Domain.TestsThe 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.
4. Look at the database
Section titled “4. Look at the database”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;5. Remove the database
Section titled “5. Remove the database”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:
unset ALLORS_TEST_ADAPTERWhen a run fails
Section titled “When a run fails”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.
Where this is implemented
Section titled “Where this is implemented”DatabaseFixture, insample/Database/Domain.Tests, readsALLORS_TEST_ADAPTER, takes the lock, drops and creates the database, and builds the database of each test.DatabaseCollectionDefinitionmakes it one fixture for every test that derives fromDomainTest, so that a run creates the database once and runs those tests one at a time, andFixturehands it to them.DomainTestbuilds the population of a test.DatabaseFixtureTestschecks that the SQL adapters requireALLORS_SQLCLIENTandALLORS_NPGSQL, andAssemblyInfo.cskeeps it from running beside the domain tests.Provisioning, inAllors.Database.Adapters.Sql.SqlClientandAllors.Database.Adapters.Sql.Npgsqlunderdotnet/System/Database/Adapters, drops and creates the database through the admin connection.