Skip to content

Commit

Permalink
Added the init db test script
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-au-922 committed Dec 2, 2023
1 parent c446ac2 commit e31dee1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e31dee1

Please sign in to comment.