Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions .claude/skills/naksha-test/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Use ONLY when asked to run tests for the Naksha project. Do NOT use
---

# General
Most tests require a database. If the tests are executed without environment variables, they will start docker containers. When unclear, ask the user if they want to run the tests using automatically created Docker containers, or if they prefer to run the tests against their own, possibly local, PostgresQL test database.
Most tests require Postgres. `here-naksha-lib-psql` starts a Docker container if no database URL is configured. The app-service tests default to a standalone Postgres at localhost; use `TEST_CONTAINERS` to have them start a managed container. When unclear, ask the user which mode they want.

# Environment Variables
All environment variables contain some placeholders that need to be replaced:
Expand All @@ -16,24 +16,29 @@ All environment variables contain some placeholders that need to be replaced:

You can test the connection to the database. If you detect that the connection to the database fails due to wrong credentials or hostname, ask the user for host, port, user, and password _(whatever is needed)_. Use defaults for any value not provided. Tell the user the defaults.

## Library tests (here-naksha-lib-psql)
Only needs one variable. If not set, Docker auto-starts:
## Shared Postgres URL
Set one shared URL when the tests should all use the same standalone Postgres instance:

```bash
export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false"
export NAKSHA_TEST_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false"
```

## Server tests (here-naksha-app-service)
Needs all variables. These tests require a running Naksha server and will fail without one. Skip unless explicitly asked:
`NAKSHA_TEST_DB_URL` is used by `here-naksha-lib-psql` and as the fallback for admin and data
storage in `here-naksha-app-service`. The specific variables `NAKSHA_TEST_PSQL_DB_URL`,
`NAKSHA_TEST_ADMIN_DB_URL`, and `NAKSHA_TEST_DATA_DB_URL` override it for their respective test
storage when separate databases are needed. `NAKSHA_TEST_STORAGE_ID` defaults to
`local_psql_test_storage`, and the Gradle test tasks default `HUB_ADMIN_STORAGE_ID` to the same value,
so both suites agree on the admin catalog ID without setting them.

```bash
export NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE
export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage
export HUB_ADMIN_STORAGE_ID=local_psql_test_storage
export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false"
export NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false"
export NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://{host}:{port}/postgres?user={user}&password={password}&ssl=false"
```
Use a dedicated test database: test setup may drop Naksha schemas.
For app-service tests, per-storage URLs in `~/.config/naksha/test_admin_db.url` and
`~/.config/naksha/test_data_db.url` take precedence over environment variables.

## App-service tests (`here-naksha-app-service`)
These tests start a Naksha app instance as part of the test suite; no separately running server is
required. By default, they expect standalone Postgres on localhost, so the context variable is
optional. Set `NAKSHA_APP_SERVICE_TEST_CONTEXT=TEST_CONTAINERS` to use the app-service managed
container instead.

# Commands

Expand All @@ -44,8 +49,8 @@ Docker auto-starts if no env vars are set:
./gradlew :here-naksha-lib-model:jvmTest :here-naksha-lib-psql:jvmTest :here-naksha-lib-jbon:jvmTest :here-naksha-lib-geo:jvmTest
```

## All JVM tests (includes server tests that will fail without a running server):
Docker auto-starts if no env vars are set. This includes `here-naksha-app-service:jvmTest` which requires a running Naksha server and will fail with `ConnectException` if no server is available:
## All JVM tests:
App-service tests start Naksha as part of the suite. They default to standalone Postgres at localhost; set `NAKSHA_TEST_DB_URL` if it is running elsewhere, or explicitly select `TEST_CONTAINERS` for the app-service managed container. `here-naksha-lib-psql` starts its own container when neither URL variable is set.

```bash
./gradlew jvmTest
Expand All @@ -56,15 +61,15 @@ Docker auto-starts if no env vars are set. This includes `here-naksha-app-servic
./gradlew :here-naksha-lib-model:jsTest :here-naksha-lib-jbon:jsTest :here-naksha-lib-geo:jsTest
```

## Server tests
Only run if user explicitly asks. Requires a running Naksha server:
## App-service tests
The test suite starts its own Naksha app instance and requires standalone Postgres or the `TEST_CONTAINERS` context:

```bash
./gradlew :here-naksha-app-service:jvmTest
```

# Common Issues
- Kotlintest discovery errors: If `here-naksha-lib-psql:jvmTest` fails with test discovery errors, try `./gradlew clean` first
- Docker not available: The psql tests require Docker. If Docker isn't running, set `NAKSHA_TEST_PSQL_DB_URL` to an external Postgres instance
- Port conflicts: The Docker container uses host port 15432. If this port is in use, the container will fail to start
- Server tests fail with ConnectException: This is expected when no Naksha server is running. Skip these tests unless the server is available
- Connection refused: verify that the standalone Postgres host and port in `NAKSHA_TEST_DB_URL` are reachable from the Gradle process
- Port conflicts: The `here-naksha-lib-psql` Docker container uses host port 15432. If this port is in use, it will fail to start
- App-service tests fail with ConnectException: verify that Postgres is available and that the app-service test context is configured as intended
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,36 +248,50 @@ Naksha tests can currently run in two modes:
1. Against a Postgres database that you start yourself (for example the containerized Postgres described above).
2. Against a Postgres container started automatically by the build via [Testcontainers](https://github.com/testcontainers).

Before running Gradle, set the environment variables for the mode you want.
For a standalone Postgres instance, set `NAKSHA_TEST_DB_URL` to its JDBC URL. This shared URL is used by
`here-naksha-lib-psql` tests and by both admin and data storage tests in `here-naksha-app-service`.
The app-service test context defaults to standalone Postgres when `NAKSHA_APP_SERVICE_TEST_CONTEXT` is
unset.

### Option 1: Run tests against a local standalone Postgres

```bash
export NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE
export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage
export HUB_ADMIN_STORAGE_ID=local_psql_test_storage
export NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false"
./gradlew cleanAndTest
```

`NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE` is optional because standalone is the default.

All test suites sharing one database must agree on the storage ID stored in the admin catalog. The test
storage ID (`NAKSHA_TEST_STORAGE_ID`) defaults to `local_psql_test_storage`, and the Gradle test tasks set
`HUB_ADMIN_STORAGE_ID` to the same value unless it is set explicitly, so neither needs to be exported.

Use a dedicated test database: test setup may drop Naksha schemas. If tests need different databases,
override the shared URL with one or more specific URLs. A specific URL takes precedence over
`NAKSHA_TEST_DB_URL` for its test storage:

```bash
# For `here-naksha-lib-psql` tests.
export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password"

# For `here-naksha-app-service` admin storage tests.
export NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password"

# For `here-naksha-app-service` data storage tests.
# Kept separate from the admin DB URL to allow deployment-like test setups,
# where administrative entities are stored in a different database/storage than data entities.
export NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password"

./gradlew cleanAndTest
export NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/other_db?user=postgres&password=password"
```

For app-service tests, a URL in `~/.config/naksha/test_admin_db.url` or
`~/.config/naksha/test_data_db.url` takes precedence over the corresponding environment variables.

### Option 2: Run tests with Testcontainers-managed Postgres

The `here-naksha-lib-psql` tests start their own Postgres container when neither
`NAKSHA_TEST_PSQL_DB_URL` nor `NAKSHA_TEST_DB_URL` is set. The app-service tests also need their
context set to `TEST_CONTAINERS` to start their managed container.

```bash
export NAKSHA_APP_SERVICE_TEST_CONTEXT=TEST_CONTAINERS
export HUB_ADMIN_STORAGE_ID=local_psql_test_storage
export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage

./gradlew cleanAndTest
```

Expand Down
31 changes: 15 additions & 16 deletions ai/test.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
# Tests
Always first clear the local storage _(please replave `PGPASSWORD` with you own local password)_:
Use a dedicated test Postgres database. Test setup can drop Naksha schemas, so do not point these commands at a database with data you need to keep. Replace the example password with your test database password.

## Clear Storage
```bash
# Clear local storage storage
PGPASSWORD='XprclPDUTMsWpHvvtusuVCNDWsJZPVTP' \
NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
# Clear schemas from the dedicated local test database only.
PGPASSWORD='password' \
psql -h localhost -p 5432 -U postgres -c 'DROP SCHEMA IF EXISTS "naksha-hub-admin" CASCADE; DROP SCHEMA IF EXISTS naksha_data_schema CASCADE; DROP SCHEMA IF EXISTS "naksha~admin" CASCADE;'
```

## Run all tests
```bash
# Run app-service tests
PGPASSWORD='XprclPDUTMsWpHvvtusuVCNDWsJZPVTP' \
NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
# Run all JVM tests against the dedicated local test database.
NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false" \
NAKSHA_SUPPRESS_PERF_TEST=true \
NAKSHA_DEBUG=true \
./gradlew jvmTest
Expand All @@ -27,14 +21,19 @@ The environment variable `NAKSHA_SUPPRESS_PERF_TEST=true` will disable performan

## Run a specific test
```bash
# Run app-service tests
PGPASSWORD='XprclPDUTMsWpHvvtusuVCNDWsJZPVTP' \
NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=$PGPASSWORD&ssl=false" \
# Run the PostgreSQL library tests.
NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false" \
NAKSHA_SUPPRESS_PERF_TEST=true \
NAKSHA_DEBUG=true \
./gradlew :here-naksha-lib-psql:jvmTest
```

For only app-service tests:

```bash
NAKSHA_TEST_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password&ssl=false" \
NAKSHA_SUPPRESS_PERF_TEST=true \
./gradlew :here-naksha-app-service:jvmTest
```

If a specific individual test is wished, just append `--tests "{full qualified classname}"` to the above `gradlew` call.
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ allprojects {
from(rootProject.file("LICENSE"))
into("")
}
tasks.withType<Test>().configureEach {
// Align the hub admin storage ID with the default test storage ID, so that all test suites
// sharing one database agree on the admin catalog ID (otherwise: STORAGE_ID_MISMATCH).
if (System.getenv("HUB_ADMIN_STORAGE_ID") == null) {
environment("HUB_ADMIN_STORAGE_ID", System.getenv("NAKSHA_TEST_STORAGE_ID") ?: "local_psql_test_storage")
}
}
val jsNoduleConfig = jsModules[_name]
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
extensions.configure<KotlinMultiplatformExtension> {
Expand Down Expand Up @@ -311,8 +318,6 @@ allprojects {
getByName<Test>("jvmTest") {
useJUnitPlatform()
maxHeapSize = "8g"
val dbUrl = System.getenv("NAKSHA_TEST_PSQL_DB_URL")
if (dbUrl != null) environment("NAKSHA_TEST_PSQL_DB_URL", dbUrl)
}
}

Expand Down
34 changes: 23 additions & 11 deletions here-naksha-app-service/src/jvmTest/POSTGRES_SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,50 @@ Currently, there are two options to provision Postgres in test environment
- utilizing TestContainers library that initializes container with Postgres image ([described in detail below](#test-containers))


To choose the desired option, one should define `NAKSHA_LOCAL_TEST_CONTEXT` environment variable which is checked upon test start to decide how to act.
The app-service tests use `NAKSHA_APP_SERVICE_TEST_CONTEXT` to choose how Postgres is provided. If the variable is unset, tests use a standalone Postgres instance.

| NAKSHA_LOCAL_TEST_CONTEXT | Chosen option |
| `NAKSHA_APP_SERVICE_TEST_CONTEXT` | Chosen option |
| - | - |
| undefined / empty | Local standalone Postgres instance |
| 'LOCAL_STANDALONE' | Local standalone Postgres instance |
| 'TEST_CONTAINERS' | Container spawned by TestContainers |
| 'unknown value' | Local standalone Postgres instance |
| `LOCAL_STANDALONE` | Local standalone Postgres instance |
| `TEST_CONTAINERS` | Container spawned by Testcontainers |
| Unknown value | Local standalone Postgres instance |

For tests that use a standalone database, the shared URL variable `NAKSHA_TEST_DB_URL` can configure
the connection for `here-naksha-lib-psql` as well as both admin and data storage tests in
`here-naksha-app-service`. The specific variables `NAKSHA_TEST_PSQL_DB_URL`,
`NAKSHA_TEST_ADMIN_DB_URL`, and `NAKSHA_TEST_DATA_DB_URL` can override the shared URL when a test
needs a separate database. When running both suites against the same database, their admin catalog
IDs must match: `NAKSHA_TEST_STORAGE_ID` defaults to `local_psql_test_storage`, and the Gradle test
tasks default `HUB_ADMIN_STORAGE_ID` to the same value, so neither has to be set. Ensure the URL points to a dedicated test database: the tests may drop Naksha schemas during
setup.

For app-service tests, per-storage URLs in `~/.config/naksha/test_admin_db.url` and
`~/.config/naksha/test_data_db.url` take precedence over environment variables.

Running tests (or any other `gradle` task) that triggers JUnit pipeline "happens as usual":

| Command | Behavior |
| - | - |
| `gradle test` | will run gradle tests expecting local standalone Postgres instance |
| `NAKSHA_LOCAL_TEST_CONTEXT=TEST_CONTAINERS gradle test` | will run gradle test in TestContainers mode |
| `NAKSHA_LOCAL_TEST_CONTEXT=LOCAL_STANDALONE gradle test` | will run gradle tests expecting local standalone Postgres instance |
| `NAKSHA_APP_SERVICE_TEST_CONTEXT=TEST_CONTAINERS gradle test` | will run gradle test in Testcontainers mode |
| `NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE gradle test` | will run gradle tests expecting local standalone Postgres instance |

The same applies to other gradle tasks one might want to use that depend on `test` task (like `shadowJar`). See [the main project README](../../../README.md) to get familiar with all options.

### Local standalone Postgres instance \[default\]

This is the default and most basic approach. It assumes that there's a Postgres DB running in our local environment.\
That is also the way our tests are executed on GH pipeline.
The CI pipeline may use either this option or Testcontainers; see the workflow configuration for the selected mode.

#### Prerequisites:
- Postgres with PostGIS extension running on localhost
- Postgres instance must be publishing **5432** port
- Environment variable `NAKSHA_LOCAL_TEST_CONTEXT` must be null, empty or set to `LOCAL_STANDALONE`
- By default, Postgres is expected on localhost port **5432**; use `NAKSHA_TEST_DB_URL` to specify another host or port
- Environment variable `NAKSHA_APP_SERVICE_TEST_CONTEXT` can be unset or set to `LOCAL_STANDALONE`

#### Notes:
- The schema used by tests: `naksha_data_schema`
- This schema will dropped before test suite starts
- This schema will be dropped before the test suite starts
- When running tests with local standalone instance, tests don't expect any extensions enabled (see [test-config.json](../jvmMain/resources/test-config.json)
- Related class: `com.here.naksha.app.init.context.LocalTestContext`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public TestStorageConfig(String mapId, PgConfig pgConfig) {
}

/**
* Reads the configuration from a configuration file from user home directory ({@code ~/.config/naksha/filename}) or from the environment
* variable, if none is possible, a default localhost configuration is used.
* Reads the configuration from a configuration file from user home directory ({@code ~/.config/naksha/filename}), or from the
* {@code envName} environment variable, or from the shared {@code NAKSHA_TEST_DB_URL} environment variable; if none is set, a
* default localhost configuration is used.
*
* @param filename The filename to search for in {@code ~/.config/naksha/}.
* @param envName The environment variable to check.
Expand All @@ -43,7 +44,7 @@ public TestStorageConfig(String mapId, PgConfig pgConfig) {
PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url);
return new TestStorageConfig(mapId, pgConfig);
}
url = System.getenv("TEST_NAKSHA_PSQL_URL");
url = System.getenv("NAKSHA_TEST_DB_URL");
if (url != null && url.startsWith("jdbc:postgresql://")) {
PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url);
return new TestStorageConfig(mapId, pgConfig);
Expand Down
Loading
Loading