Skip to content

Commit

Permalink
Refactor test fixtures, helpers, and tests for async support
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdamin committed Oct 7, 2024
1 parent 1efeb54 commit a2d96d4
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 255 deletions.
32 changes: 16 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import datetime
from pathlib import Path
from typing import Generator
from typing import AsyncGenerator

import pytest

from genotype_api.database.database import initialise_database, create_all_tables, drop_all_tables
from genotype_api.database.database import create_all_tables, drop_all_tables, get_session
from genotype_api.database.filter_models.plate_models import PlateSignOff
from genotype_api.database.filter_models.sample_models import SampleSexesUpdate
from genotype_api.database.models import User, Plate, SNP, Sample, Genotype, Analysis
from genotype_api.database.models import SNP, Analysis, Genotype, Plate, Sample, User
from genotype_api.database.store import Store
from tests.store_helpers import StoreHelpers

Expand All @@ -34,14 +34,14 @@ def date_tomorrow() -> datetime:
return datetime.date.today() + datetime.timedelta(days=1)


@pytest.fixture
def store() -> Generator[Store, None, None]:
@pytest.fixture(scope="session")
async def store() -> AsyncGenerator[Store, None]:
"""Return a CG store."""
initialise_database("sqlite:///")
_store = Store()
create_all_tables()
session = get_session()
_store = Store(session)
await create_all_tables()
yield _store
drop_all_tables()
await drop_all_tables()


@pytest.fixture
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_analyses(


@pytest.fixture
def base_store(
async def base_store(
store: Store,
helpers: StoreHelpers,
test_snps: list[SNP],
Expand All @@ -237,17 +237,17 @@ def base_store(
test_analyses: list[Analysis],
):
for snp in test_snps:
helpers.ensure_snp(store=store, snp=snp)
await helpers.ensure_snp(store=store, snp=snp)
for genotype in test_genotypes:
helpers.ensure_genotype(store=store, genotype=genotype)
await helpers.ensure_genotype(store=store, genotype=genotype)
for plate in test_plates:
helpers.ensure_plate(store=store, plate=plate)
await helpers.ensure_plate(store=store, plate=plate)
for user in test_users:
helpers.ensure_user(store=store, user=user)
await helpers.ensure_user(store=store, user=user)
for sample in test_samples:
helpers.ensure_sample(store=store, sample=sample)
await helpers.ensure_sample(store=store, sample=sample)
for analysis in test_analyses:
helpers.ensure_analysis(store=store, analysis=analysis)
await helpers.ensure_analysis(store=store, analysis=analysis)
return store


Expand Down
99 changes: 65 additions & 34 deletions tests/database/crud/test_create.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,119 @@
"""Module to test the create functionality of the genotype API CRUD."""

from genotype_api.database.models import Analysis, SNP, User, Genotype, Sample, Plate
from sqlalchemy.orm import Query

from genotype_api.database.models import SNP, Analysis, Genotype, Plate, Sample, User
from genotype_api.database.store import Store


def test_create_analysis(store: Store, test_analysis: Analysis):
async def test_create_analysis(store: Store, test_analysis: Analysis):
# GIVEN an analysis and an empty store

assert not store._get_query(Analysis).all()
analyses_query: Query = store._get_query(Analysis)
result = await store.session.execute(analyses_query)
assert not result.scalars().all()

# WHEN creating the analysis
store.create_analysis(analysis=test_analysis)
await store.create_analysis(analysis=test_analysis)

# THEN the analysis is created
assert store._get_query(Analysis).all()[0].id == test_analysis.id
result = await store.session.execute(analyses_query)
analyses = result.scalars().all()
assert analyses[0].id == test_analysis.id


def test_create_genotype(store: Store, test_genotype: Genotype):
async def test_create_genotype(store: Store, test_genotype: Genotype):
# GIVEN a genotype and an empty store

assert not store._get_query(Genotype).all()
genotypes_query: Query = store._get_query(Genotype)
result = await store.session.execute(genotypes_query)
assert not result.scalars().all()

# WHEN creating the genotype
store.create_genotype(genotype=test_genotype)
await store.create_genotype(genotype=test_genotype)

# THEN the genotype is created
assert store._get_query(Genotype).all()[0].id == test_genotype.id
result = await store.session.execute(genotypes_query)
genotypes = result.scalars().all()
assert genotypes[0].id == test_genotype.id


def test_create_snp(store: Store, test_snp: SNP):
async def test_create_snp(store: Store, test_snp: SNP):
# GIVEN a SNP and an empty store
assert not store._get_query(SNP).all()
snps_query: Query = store._get_query(SNP)
result = await store.session.execute(snps_query)
assert not result.scalars().all()

# WHEN creating the SNP
store.create_snps(snps=[test_snp])
await store.create_snps(snps=[test_snp])

# THEN the SNP is created
assert store._get_query(SNP).all()[0].id == test_snp.id
result = await store.session.execute(snps_query)
snps = result.scalars().all()
assert snps[0].id == test_snp.id


def test_create_user(store: Store, test_user: User):
async def test_create_user(store: Store, test_user: User):
# GIVEN a user and an empty store
assert not store._get_query(User).all()
users_query: Query = store._get_query(User)
result = await store.session.execute(users_query)
assert not result.scalars().all()

# WHEN creating the user
store.create_user(user=test_user)
await store.create_user(user=test_user)

# THEN the user is created
assert store._get_query(User).all()[0].id == test_user.id
result = await store.session.execute(users_query)
users = result.scalars().all()
assert users[0].id == test_user.id


def test_create_sample(store: Store, test_sample: Sample):
async def test_create_sample(store: Store, test_sample: Sample):
# GIVEN a sample and an empty store
assert not store._get_query(Sample).all()
samples_query: Query = store._get_query(Sample)
result = await store.session.execute(samples_query)
assert not result.scalars().all()

# WHEN creating the sample
store.create_sample(sample=test_sample)
await store.create_sample(sample=test_sample)

# THEN the sample is created
assert store._get_query(Sample).all()[0].id == test_sample.id
result = await store.session.execute(samples_query)
samples = result.scalars().all()
assert samples[0].id == test_sample.id


def test_create_plate(store: Store, test_plate: Plate):
async def test_create_plate(store: Store, test_plate: Plate):
# GIVEN a plate and an empty store
assert not store._get_query(Plate).all()
plates_query: Query = store._get_query(Plate)
result = await store.session.execute(plates_query)
assert not result.scalars().all()

# WHEN creating the plate
store.create_plate(plate=test_plate)
await store.create_plate(plate=test_plate)

# THEN the plate is created
assert store._get_query(Plate).all()[0].id == test_plate.id
result = await store.session.execute(plates_query)
plates = result.scalars().all()
assert plates[0].id == test_plate.id


def test_create_analyses_samples(store: Store, test_analysis: Analysis):
async def test_create_analyses_samples(store: Store, test_analysis: Analysis):
# GIVEN an analysis in a store
assert not store._get_query(Sample).all()
assert not store._get_query(Analysis).all()
samples_query: Query = store._get_query(Sample)
analyses_query: Query = store._get_query(Analysis)

result = await store.session.execute(samples_query)
assert not result.scalars().all()

result = await store.session.execute(analyses_query)
assert not result.scalars().all()

store.create_analysis(test_analysis)
await store.create_analysis(test_analysis)

# WHEN creating the analyses
store.create_analyses_samples(analyses=[test_analysis])
# WHEN creating the analyses samples
await store.create_analyses_samples(analyses=[test_analysis])

# THEN the samples are created
sample: Sample = store._get_query(Sample).all()[0]
result = await store.session.execute(samples_query)
sample: Sample = result.scalars().all()[0]
assert sample
assert sample.id == test_analysis.sample_id
51 changes: 32 additions & 19 deletions tests/database/crud/test_delete.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,72 @@
"""Module to test the delete functionality of the genotype API CRUD."""

from genotype_api.database.models import Analysis, Sample, User, Plate, SNP
from sqlalchemy.orm import Query

from genotype_api.database.models import SNP, Analysis, Plate, Sample, User
from genotype_api.database.store import Store


def test_delete_analysis(base_store: Store, test_analysis: Analysis):
async def test_delete_analysis(base_store: Store, test_analysis: Analysis):
# GIVEN an analysis and a store with the analysis
assert test_analysis in base_store._get_query(Analysis).all()
analyses = await base_store.get_analyses()
assert test_analysis in analyses

# WHEN deleting the analysis
base_store.delete_analysis(analysis=test_analysis)
await base_store.delete_analysis(analysis=test_analysis)

# THEN the analysis is deleted
assert test_analysis not in base_store._get_query(Analysis).all()
analyses = await base_store.get_analyses()
assert test_analysis not in analyses


def test_delete_sample(base_store: Store, test_sample: Sample):
async def test_delete_sample(base_store: Store, test_sample: Sample):
# GIVEN a sample and a store with the sample
assert test_sample in base_store._get_query(Sample).all()
query: Query = base_store._get_query(Sample)
result = await base_store.session.execute(query)
assert test_sample in result.scalars().all()

# WHEN deleting the sample
base_store.delete_sample(sample=test_sample)
await base_store.delete_sample(sample=test_sample)

# THEN the sample is deleted
assert test_sample not in base_store._get_query(Sample).all()
result = await base_store.session.execute(base_store._get_query(test_sample))
assert test_sample not in result.scalars().all()


def test_delete_plate(base_store: Store, test_plate: Plate):
async def test_delete_plate(base_store: Store, test_plate: Plate):
# GIVEN a plate and a store with the plate
assert test_plate in base_store._get_query(Plate).all()
result = await base_store.session.execute(base_store._get_query(test_plate))
assert test_plate in result.scalars().all()

# WHEN deleting the plate
base_store.delete_plate(plate=test_plate)
await base_store.delete_plate(plate=test_plate)

# THEN the plate is deleted
assert test_plate not in base_store._get_query(Plate).all()
result = await base_store.session.execute(base_store._get_query(test_plate))
assert test_plate not in result.scalars().all()


def test_delete_user(base_store: Store, test_user: User):
async def test_delete_user(base_store: Store, test_user: User):
# GIVEN a user and a store with the user
assert test_user in base_store._get_query(User).all()
result = await base_store.session.execute(base_store._get_query(test_user))
assert test_user in result.scalars().all()

# WHEN deleting the user
base_store.delete_user(user=test_user)

# THEN the user is deleted
assert test_user not in base_store._get_query(User).all()
result = await base_store.session.execute(base_store._get_query(test_user))
assert test_user not in result.scalars().all()


def test_delete_snps(base_store: Store, test_snp: SNP):
async def test_delete_snps(base_store: Store, test_snp: SNP):
# GIVEN an SNP and a store with the SNP
assert base_store._get_query(SNP).all()
result = await base_store.session.execute(base_store._get_query(test_snp))
assert result.scalars().all()

# WHEN deleting the SNP
base_store.delete_snps()

# THEN all SNPs are deleted
assert not base_store._get_query(SNP).all()
result = await base_store.session.execute(base_store._get_query(test_snp))
assert not result.scalars().all()
Loading

0 comments on commit a2d96d4

Please sign in to comment.