From 65f38fe7d17ccf9b37328640dd1a7b8366d69bf8 Mon Sep 17 00:00:00 2001 From: Christian Oertlin Date: Wed, 10 Apr 2024 11:03:46 +0200 Subject: [PATCH] Sebas review pt. 1 --- genotype_api/database/crud/delete.py | 9 ++++-- .../services/endpoint_services/snp_service.py | 4 +-- tests/database/crud/test_create.py | 21 ++++++++----- tests/database/crud/test_delete.py | 31 ++++++++----------- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/genotype_api/database/crud/delete.py b/genotype_api/database/crud/delete.py index 0fb6218..8cc642f 100644 --- a/genotype_api/database/crud/delete.py +++ b/genotype_api/database/crud/delete.py @@ -26,7 +26,10 @@ def delete_user(self, user: User) -> None: self.session.delete(user) self.session.commit() - def delete_snps(self) -> any: - result = self.session.execute(delete(SNP)) + def delete_snps(self) -> int: + snps: list[SNP] = self._get_query(SNP).all() + count: int = len(snps) + for snp in snps: + self.session.delete(snp) self.session.commit() - return result + return count diff --git a/genotype_api/services/endpoint_services/snp_service.py b/genotype_api/services/endpoint_services/snp_service.py index e19328a..dacf4b9 100644 --- a/genotype_api/services/endpoint_services/snp_service.py +++ b/genotype_api/services/endpoint_services/snp_service.py @@ -22,7 +22,7 @@ def get_snps(self, skip: int, limit: int) -> list[SNPResponse]: def upload_snps(self, snps_file: UploadFile) -> list[SNPResponse]: """Upload snps to the database, raises an error when SNPs already exist.""" - existing_snps: list[SNP] = self.store.get_snps(self.session) + existing_snps: list[SNP] = self.store.get_snps() if existing_snps: raise SNPExistsError snps: list[SNP] = SNPReaderService.read_snps_from_file(snps_file) @@ -30,5 +30,5 @@ def upload_snps(self, snps_file: UploadFile) -> list[SNPResponse]: return [self._get_snp_response(new_snp) for new_snp in new_snps] def delete_all_snps(self) -> int: - result = self.store.delete_snps(self.session) + result = self.store.delete_snps() return result.rowcount diff --git a/tests/database/crud/test_create.py b/tests/database/crud/test_create.py index b3561b5..a4336fd 100644 --- a/tests/database/crud/test_create.py +++ b/tests/database/crud/test_create.py @@ -6,66 +6,73 @@ def test_create_analysis(store: Store, test_analysis: Analysis): # GIVEN an analysis and an empty store - + assert not store._get_query(Analysis).all() # WHEN creating the analysis store.create_analysis(analysis=test_analysis) # THEN the analysis is created - assert store._get_query(Analysis).all()[0] + assert store._get_query(Analysis).all()[0].id == test_analysis.id def test_create_genotype(store: Store, test_genotype: Genotype): # GIVEN a genotype and an empty store + assert not store._get_query(Genotype).all() # WHEN creating the genotype store.create_genotype(genotype=test_genotype) # THEN the genotype is created - assert store._get_query(Genotype).all()[0] + assert store._get_query(Genotype).all()[0].id == test_genotype.id def test_create_snp(store: Store, test_snp: SNP): # GIVEN a SNP and an empty store + assert not store._get_query(SNP).all() # WHEN creating the SNP store.create_snps(snps=[test_snp]) # THEN the SNP is created - assert store._get_query(SNP).all()[0] + assert store._get_query(SNP).all()[0].id == test_snp.id def test_create_user(store: Store, test_user: User): # GIVEN a user and an empty store + assert not store._get_query(User).all() # WHEN creating the user store.create_user(user=test_user) # THEN the user is created - assert store._get_query(User).all()[0] + assert store._get_query(User).all()[0].id == test_user.id def test_create_sample(store: Store, test_sample: Sample): # GIVEN a sample and an empty store + assert not store._get_query(Sample).all() # WHEN creating the sample store.create_sample(sample=test_sample) # THEN the sample is created - assert store._get_query(Sample).all()[0] + assert store._get_query(Sample).all()[0].id == test_sample.id def test_create_plate(store: Store, test_plate: Plate): # GIVEN a plate and an empty store + assert not store._get_query(Plate).all() # WHEN creating the plate store.create_plate(plate=test_plate) # THEN the plate is created - assert store._get_query(Plate).all()[0] + assert store._get_query(Plate).all()[0].id == test_plate.id 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() store.create_analysis(test_analysis) # WHEN creating the analyses diff --git a/tests/database/crud/test_delete.py b/tests/database/crud/test_delete.py index 7e655c6..ebfc32f 100644 --- a/tests/database/crud/test_delete.py +++ b/tests/database/crud/test_delete.py @@ -6,59 +6,54 @@ def test_delete_analysis(base_store: Store, test_analysis: Analysis): # GIVEN an analysis and a store with the analysis - assert base_store._get_query(Analysis).all()[0] + assert test_analysis in base_store._get_query(Analysis).all() # WHEN deleting the analysis base_store.delete_analysis(analysis=test_analysis) # THEN the analysis is deleted - for analysis in base_store._get_query(Analysis).all(): - assert analysis != test_analysis + assert test_analysis not in base_store._get_query(Analysis).all() def test_delete_sample(base_store: Store, test_sample: Sample): # GIVEN a sample and a store with the sample - assert base_store._get_query(Sample).all()[0] + assert test_sample in base_store._get_query(Sample).all() # WHEN deleting the sample base_store.delete_sample(sample=test_sample) # THEN the sample is deleted - for sample in base_store._get_query(Sample).all(): - assert sample != test_sample + assert test_sample not in base_store._get_query(Sample).all() def test_delete_plate(base_store: Store, test_plate: Plate): # GIVEN a plate and a store with the plate - assert base_store._get_query(Plate).all()[0] + assert test_plate in base_store._get_query(Plate).all() # WHEN deleting the plate base_store.delete_plate(plate=test_plate) # THEN the plate is deleted - for plate in base_store._get_query(Plate).all(): - assert plate != test_plate + assert test_plate not in base_store._get_query(Plate).all() def test_delete_user(base_store: Store, test_user: User): # GIVEN a user and a store with the user - assert base_store._get_query(User).all()[0] + assert test_user in base_store._get_query(User).all() # WHEN deleting the user base_store.delete_user(user=test_user) # THEN the user is deleted - for user in base_store._get_query(User).all(): - assert user != test_user + assert test_user not in base_store._get_query(User).all() -def test_delete_snps(base_store: Store, test_snp): - # GIVEN a SNP and a store with the SNP - assert base_store._get_query(SNP).all()[0] +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() # WHEN deleting the SNP base_store.delete_snps() - # THEN the SNP is deleted - for snp in base_store._get_query(SNP).all(): - assert snp != test_snp + # THEN all SNPs are deleted + assert not base_store._get_query(SNP).all()