From 45e51b642c63081b6fcc410f882640fe74df0c93 Mon Sep 17 00:00:00 2001 From: Shelley Gao Date: Fri, 24 May 2024 14:50:08 -0700 Subject: [PATCH] Added snapshot to add bulk test --- tests/__snapshots__/test_add_bulk.ambr | 453 +++++++++++++++++++++++++ tests/test_add_bulk.py | 24 +- virtool_cli/ref/event_index_cache.py | 6 +- 3 files changed, 472 insertions(+), 11 deletions(-) create mode 100644 tests/__snapshots__/test_add_bulk.ambr diff --git a/tests/__snapshots__/test_add_bulk.ambr b/tests/__snapshots__/test_add_bulk.ambr new file mode 100644 index 00000000..99538560 --- /dev/null +++ b/tests/__snapshots__/test_add_bulk.ambr @@ -0,0 +1,453 @@ +# serializer version: 1 +# name: test_add_ref_mini + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Nanovirus-like particle', + 'schema': list([ + ]), + 'taxid': 96892, + }) +# --- +# name: test_add_ref_mini.1 + set({ + 'NC_005954', + }) +# --- +# name: test_add_ref_mini.10 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Lagenaria siceraria endornavirus-California', + 'schema': list([ + ]), + 'taxid': 1468454, + }) +# --- +# name: test_add_ref_mini.11 + set({ + 'NC_023641', + }) +# --- +# name: test_add_ref_mini.12 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Dahlia latent viroid', + 'schema': list([ + ]), + 'taxid': 1278205, + }) +# --- +# name: test_add_ref_mini.13 + set({ + 'NC_020160', + }) +# --- +# name: test_add_ref_mini.14 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Pagoda yellow mosaic associated virus', + 'schema': list([ + ]), + 'taxid': 1505530, + }) +# --- +# name: test_add_ref_mini.15 + set({ + 'NC_024301', + }) +# --- +# name: test_add_ref_mini.16 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Maclura mosaic virus', + 'schema': list([ + ]), + 'taxid': 48201, + }) +# --- +# name: test_add_ref_mini.17 + set({ + 'NC_043133', + }) +# --- +# name: test_add_ref_mini.18 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Gaillardia latent virus', + 'schema': list([ + ]), + 'taxid': 1468172, + }) +# --- +# name: test_add_ref_mini.19 + set({ + 'NC_023892', + }) +# --- +# name: test_add_ref_mini.2 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Saccharum streak virus', + 'schema': list([ + ]), + 'taxid': 683179, + }) +# --- +# name: test_add_ref_mini.20 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Ugandan cassava brown streak virus', + 'schema': list([ + ]), + 'taxid': 946046, + }) +# --- +# name: test_add_ref_mini.21 + set({ + 'NC_014791', + }) +# --- +# name: test_add_ref_mini.22 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Faba bean necrotic stunt alphasatellite 1', + 'schema': list([ + ]), + 'taxid': 1441799, + }) +# --- +# name: test_add_ref_mini.23 + set({ + 'NC_023881', + }) +# --- +# name: test_add_ref_mini.24 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Kalanchoe latent virus', + 'schema': list([ + ]), + 'taxid': 132477, + }) +# --- +# name: test_add_ref_mini.25 + set({ + 'NC_013006', + }) +# --- +# name: test_add_ref_mini.26 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Tall oatgrass mosaic virus', + 'schema': list([ + ]), + 'taxid': 1414644, + }) +# --- +# name: test_add_ref_mini.27 + set({ + 'NC_022745', + }) +# --- +# name: test_add_ref_mini.28 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Oat blue dwarf virus', + 'schema': list([ + ]), + 'taxid': 56879, + }) +# --- +# name: test_add_ref_mini.29 + set({ + 'NC_001793', + }) +# --- +# name: test_add_ref_mini.3 + set({ + 'NC_013464', + }) +# --- +# name: test_add_ref_mini.30 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'East African cassava mosaic Cameroon virus', + 'schema': list([ + ]), + 'taxid': 223262, + }) +# --- +# name: test_add_ref_mini.31 + set({ + 'NC_004625', + 'NC_004630', + }) +# --- +# name: test_add_ref_mini.32 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Babaco mosaic virus', + 'schema': list([ + ]), + 'taxid': 2060511, + }) +# --- +# name: test_add_ref_mini.33 + set({ + 'NC_036587', + }) +# --- +# name: test_add_ref_mini.34 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Habenaria mosaic virus', + 'schema': list([ + ]), + 'taxid': 1324128, + }) +# --- +# name: test_add_ref_mini.35 + set({ + 'NC_021786', + }) +# --- +# name: test_add_ref_mini.36 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Cabbage leaf curl Jamaica virus', + 'schema': list([ + ]), + 'taxid': 345184, + }) +# --- +# name: test_add_ref_mini.37 + set({ + 'DQ178610', + 'DQ178611', + 'DQ178613', + 'DQ178614', + }) +# --- +# name: test_add_ref_mini.38 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Vallota mosaic virus', + 'schema': list([ + ]), + 'taxid': 430059, + }) +# --- +# name: test_add_ref_mini.39 + set({ + 'NC_043170', + }) +# --- +# name: test_add_ref_mini.4 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Abaca bunchy top virus', + 'schema': list([ + ]), + 'taxid': 438782, + }) +# --- +# name: test_add_ref_mini.40 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Impatiens flower break virus', + 'schema': list([ + ]), + 'taxid': 2170135, + }) +# --- +# name: test_add_ref_mini.41 + set({ + 'NC_030236', + }) +# --- +# name: test_add_ref_mini.42 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Yacon necrotic mottle virus', + 'schema': list([ + ]), + 'taxid': 1561150, + }) +# --- +# name: test_add_ref_mini.43 + set({ + 'NC_026472', + }) +# --- +# name: test_add_ref_mini.44 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Jacquemontia mosaic Yucatan virus', + 'schema': list([ + ]), + 'taxid': 1198450, + }) +# --- +# name: test_add_ref_mini.45 + set({ + 'NC_038796', + 'NC_038797', + }) +# --- +# name: test_add_ref_mini.46 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Radish leaf curl betasatellite', + 'schema': list([ + ]), + 'taxid': 662596, + }) +# --- +# name: test_add_ref_mini.47 + set({ + 'NC_010239', + }) +# --- +# name: test_add_ref_mini.5 + set({ + 'EF546802', + 'EF546803', + 'EF546804', + 'EF546805', + 'EF546806', + 'EF546807', + 'NC_010314', + 'NC_010315', + 'NC_010316', + 'NC_010317', + 'NC_010318', + 'NC_010319', + }) +# --- +# name: test_add_ref_mini.6 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Wasabi mottle virus', + 'schema': list([ + ]), + 'taxid': 1169032, + }) +# --- +# name: test_add_ref_mini.7 + set({ + 'AB017504', + 'KJ207375', + 'MH200607', + 'MK431779', + 'NC_003355', + }) +# --- +# name: test_add_ref_mini.8 + dict({ + 'acronym': '', + 'excluded_accessions': list([ + ]), + 'legacy_id': None, + 'molecule': None, + 'name': 'Zantedeschia mild mosaic virus', + 'schema': list([ + ]), + 'taxid': 270478, + }) +# --- +# name: test_add_ref_mini.9 + set({ + 'NC_011560', + }) +# --- diff --git a/tests/test_add_bulk.py b/tests/test_add_bulk.py index 17bf2e8d..0fd17c21 100644 --- a/tests/test_add_bulk.py +++ b/tests/test_add_bulk.py @@ -2,16 +2,16 @@ import shutil import subprocess - -@pytest.fixture() -def cache_example_path(test_files_path): - return test_files_path / "cache_test" +from syrupy import SnapshotAssertion +from syrupy.filters import props @pytest.fixture -def precached_repo(empty_repo, cache_example_path): +def precached_repo(empty_repo, test_files_path): shutil.copytree( - cache_example_path, (empty_repo.path / ".cache/ncbi"), dirs_exist_ok=True + (test_files_path / "cache_test"), + (empty_repo.path / ".cache/ncbi"), + dirs_exist_ok=True, ) yield empty_repo @@ -63,7 +63,9 @@ def ref_table_of_contents() -> dict[int, list[str]]: @pytest.mark.ncbi() -def test_init_and_add(ref_table_of_contents, precached_repo): +def test_add_ref_mini( + ref_table_of_contents, precached_repo, snapshot: SnapshotAssertion +): for taxid in ref_table_of_contents: subprocess.run( [ @@ -95,7 +97,9 @@ def test_init_and_add(ref_table_of_contents, precached_repo): for taxid in ref_table_of_contents: otu = precached_repo.get_otu_by_taxid(taxid) - assert otu is not None + assert otu.dict() == snapshot(exclude=props("id", "isolates")) + assert otu.accessions == snapshot - for accession in ref_table_of_contents[taxid]: - assert accession.split(".")[0] in otu.accessions + assert otu.accessions == set( + accession.split(".")[0] for accession in ref_table_of_contents[taxid] + ) diff --git a/virtool_cli/ref/event_index_cache.py b/virtool_cli/ref/event_index_cache.py index 612c5014..61e20bef 100644 --- a/virtool_cli/ref/event_index_cache.py +++ b/virtool_cli/ref/event_index_cache.py @@ -6,7 +6,7 @@ from collections.abc import Generator import orjson -from pydantic import Field, ValidationError +from pydantic import Field, ValidationError, model_validator from pydantic.dataclasses import dataclass from structlog import get_logger @@ -24,6 +24,10 @@ class OTUEventCache: events: list[int] """A list of event IDs""" + @model_validator(mode="after") + def sort_events(self): + return self.events.sort() + class EventIndexCacheError(Exception): """Event index cache is corrupted."""