Skip to content

Commit

Permalink
Sebas review pt. 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrOertlin committed Apr 10, 2024
1 parent e993d7a commit 65f38fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
9 changes: 6 additions & 3 deletions genotype_api/database/crud/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions genotype_api/services/endpoint_services/snp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ 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)
new_snps: list[SNP] = self.store.create_snps(snps=snps)
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
21 changes: 14 additions & 7 deletions tests/database/crud/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 13 additions & 18 deletions tests/database/crud/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 65f38fe

Please sign in to comment.