-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor test fixtures, helpers, and tests for async support
- Loading branch information
Showing
13 changed files
with
391 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.