Skip to content

Commit

Permalink
Added extensions to unit tests as GitHub version didn't have them
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Langdale committed Oct 9, 2024
1 parent 717288c commit 1a07d6b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ jobs:
- name: Run pytest
run: |
uv run pytest
uv run pytest
- name: Dump docker logs
if: failure()
uses: jwalton/gh-docker-logs@v2
31 changes: 16 additions & 15 deletions test/fixtures/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import hashlib
import logging
import os
import random
from typing import Callable, Generator

Expand Down Expand Up @@ -48,7 +47,7 @@ def db_clear_all() -> Callable[[Engine], None]:
"""

def _db_clear_all(db_engine: Engine) -> None:
db_metadata = MetaData(schema=os.getenv("SCHEMA"))
db_metadata = MetaData(schema="test")
db_metadata.reflect(bind=db_engine)
with Session(db_engine) as session:
for table in reversed(db_metadata.sorted_tables):
Expand Down Expand Up @@ -121,21 +120,21 @@ def _db_add_data(db_engine: Engine) -> None:
crn_companies.to_sql(
"crn",
con=conn,
schema=os.getenv("SCHEMA"),
schema="test",
if_exists="replace",
index=False,
)
duns_companies.to_sql(
"duns",
con=conn,
schema=os.getenv("SCHEMA"),
schema="test",
if_exists="replace",
index=False,
)
cdms_companies.to_sql(
"cdms",
con=conn,
schema=os.getenv("SCHEMA"),
schema="test",
if_exists="replace",
index=False,
)
Expand All @@ -144,17 +143,17 @@ def _db_add_data(db_engine: Engine) -> None:

datasets = {
"crn_table": {
"schema": os.getenv("SCHEMA"),
"schema": "test",
"table": "crn",
"id": "id",
},
"duns_table": {
"schema": os.getenv("SCHEMA"),
"schema": "test",
"table": "duns",
"id": "id",
},
"cdms_table": {
"schema": os.getenv("SCHEMA"),
"schema": "test",
"table": "cdms",
"id": "id",
},
Expand Down Expand Up @@ -424,9 +423,14 @@ def db_engine(
)

with engine.connect() as conn:
# Install relevant extensions
conn.execute(text('create extension if not exists "uuid-ossp";'))
conn.execute(text("create extension if not exists pgcrypto;"))
conn.commit()

# Create CMF schema
if not inspect(conn).has_schema(os.getenv("SCHEMA")):
conn.execute(CreateSchema(os.getenv("SCHEMA")))
if not inspect(conn).has_schema("test"):
conn.execute(CreateSchema("test"))
conn.commit()

# Create CMF tables
Expand All @@ -452,12 +456,9 @@ def cleanup(db_engine, request):
def teardown():
with db_engine.connect() as conn:
inspector = inspect(conn)
for table_name in inspector.get_table_names(schema=os.getenv("SCHEMA")):
for table_name in inspector.get_table_names(schema="test"):
conn.execute(
text(
f'DROP TABLE IF EXISTS "{os.getenv("SCHEMA")}".'
f'"{table_name}" CASCADE;'
)
text(f'DROP TABLE IF EXISTS "{"test"}".' f'"{table_name}" CASCADE;')
)
conn.commit()

Expand Down
9 changes: 4 additions & 5 deletions test/test_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import itertools
import logging
import os

from dotenv import find_dotenv, load_dotenv
from matchbox.admin import add_dataset
Expand Down Expand Up @@ -35,7 +34,7 @@ def test_database(db_engine):
"""
Test the database contains all the tables we expect.
"""
tables = set(inspect(db_engine).get_table_names(schema=os.getenv("SCHEMA")))
tables = set(inspect(db_engine).get_table_names(schema="test"))
to_check = {
"crn",
"duns",
Expand Down Expand Up @@ -113,11 +112,11 @@ def test_insert_data(db_engine, crn_companies, duns_companies, cdms_companies):
]
with Session(db_engine) as session:
# Reflect the table and insert the data
db_metadata = MetaData(schema=os.getenv("SCHEMA"))
db_metadata = MetaData(schema="test")
crn_table = Table(
"crn",
db_metadata,
schema=os.getenv("SCHEMA"),
schema="test",
autoload_with=session.get_bind(),
)
session.execute(insert(crn_table), new_data)
Expand All @@ -126,7 +125,7 @@ def test_insert_data(db_engine, crn_companies, duns_companies, cdms_companies):
# Add the dataset again
add_dataset(
{
"schema": os.getenv("SCHEMA"),
"schema": "test",
"table": "crn",
"id": "id",
},
Expand Down

0 comments on commit 1a07d6b

Please sign in to comment.