diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ed9224f..c858141 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -154,14 +154,6 @@ jobs: if: steps.cache.outputs.cache-hit != 'true' working-directory: ${{env.WORKDIR}} run: pip install -r requirements-dev.txt - - name: Setup database - if: steps.cache.outputs.cache-hit != 'true' - run: | - PGPASSWORD=${{ needs.load-dotenv.outputs.postgres-password }} \ - psql -h localhost \ - -p ${{ needs.load-dotenv.outputs.postgres-port }} \ - -U ${{ needs.load-dotenv.outputs.postgres-username }} \ - -tc "$(cat database/assets/create_records_table.sql)" - name: Run tests if: steps.cache.outputs.cache-hit != 'true' run: | diff --git a/consumer/tests/test_adapters/test_upsert_iot_records/test_postgres/conftest.py b/consumer/tests/test_adapters/test_upsert_iot_records/test_postgres/conftest.py index db892d2..6e1d551 100644 --- a/consumer/tests/test_adapters/test_upsert_iot_records/test_postgres/conftest.py +++ b/consumer/tests/test_adapters/test_upsert_iot_records/test_postgres/conftest.py @@ -4,6 +4,32 @@ import pytest +@pytest.fixture(scope="session", autouse=True) +def init_postgres_tables() -> None: + with psycopg2.connect( + host=PostgresConfig.HOST, + port=PostgresConfig.PORT, + user=PostgresConfig.USERNAME, + password=PostgresConfig.PASSWORD, + database=PostgresConfig.DATABASE, + ) as conn: + with conn.cursor() as cursor: + cursor.execute( + """ + CREATE TABLE IF NOT EXISTS records ( + record_time TIMESTAMPTZ NOT NULL, + sensor_id CHAR(64) NOT NULL, + value DOUBLE PRECISION NOT NULL, + PRIMARY KEY(record_time, sensor_id) + ); + + CREATE INDEX IF NOT EXISTS idx_records_record_time ON records USING BTREE (record_time); + CREATE INDEX IF NOT EXISTS idx_records_sensor_id ON records USING BTREE (sensor_id); + """ + ) + conn.commit() + + @pytest.fixture(scope="function") def postgres_upsert_iot_records_client() -> PostgresUpsertIOTRecordsClient: return PostgresUpsertIOTRecordsClient(