---
title: "Test on PostgreSQL or SQL Server"
description: "Run the domain tests of an application started from the sample on PostgreSQL or SQL Server: give the tests an admin connection, choose the adapter, run the tests, look at the database that they leave and remove it."
audience: "Users"
kind: "How-to guide"
status: "Current"
version: "main · v3.2 (unreleased)"
slug: "test-on-postgresql-or-sql-server"
url: "https://docs.allors.com/allors3/test-on-postgresql-or-sql-server/"
---

# Test on PostgreSQL or SQL Server

> **Status: Current.** [Where this is implemented](#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](https://docs.allors.com/allors3/database-adapters.md#where-the-adapters-differ) explains which behaviour depends on the adapter.

The guide assumes an application started from the sample, as [Create an application](https://docs.allors.com/allors3/create-an-application.md) 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](https://docs.allors.com/allors3/configuration.md#templates) 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.

<a id="1-give-the-tests-an-admin-connection"></a>

## 1\. Give the tests an admin connection

The tests drop and create their database through the [admin connection](https://docs.allors.com/allors3/configuration.md#admin-connections) 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

```sh
export ALLORS_NPGSQL='Host=localhost;Username=postgres;Password=…'
```

For SQL Server:

Terminal window

```sh
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`.

<a id="2-choose-the-adapter"></a>

## 2\. Choose the adapter

Set `ALLORS_TEST_ADAPTER` in the same terminal, here to PostgreSQL:

Terminal window

```sh
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.

<a id="3-run-the-tests"></a>

## 3\. Run the tests

Terminal window

```sh
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.

<a id="4-look-at-the-database"></a>

## 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](https://docs.allors.com/allors3/database-schema.md) describes. Connect to it with the admin login and list them:

```sql
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:

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

<a id="5-remove-the-database"></a>

## 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.

```sql
DROP DATABASE sample_domain_tests;
```

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

Terminal window

```sh
unset ALLORS_TEST_ADAPTER
```

<a id="when-a-run-fails"></a>

## When a run fails

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

```text
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`:

```text
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.

<a id="where-this-is-implemented"></a>

## Where this is implemented

-   `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.
