about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--tests/uploader/test_genotype_collector_job.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/uploader/test_genotype_collector_job.py b/tests/uploader/test_genotype_collector_job.py
new file mode 100644
index 0000000..52eeee9
--- /dev/null
+++ b/tests/uploader/test_genotype_collector_job.py
@@ -0,0 +1,98 @@
+"""Tests for reading genotype collector jobs."""
+import json
+
+import pytest
+
+from uploader.genotypes.collector_job import InvalidCollectorJob, file_stats
+
+STATS = {
+    "total_lines": 3,
+    "description": "a comment",
+    "geno_symbols": ["A", "T"],
+}
+
+
+def _job(
+        job_type="genotype-file-stats",
+        status="completed",
+        species_id=1,
+        population_id=2,
+        dataset_id=3,
+        stdout=None):
+    """Build a collector job dict for the tests."""
+    return {
+        "job_id": "fafc2d21-ed00-4a52-986e-3d0fe2250d5e",
+        "metadata": {
+            "job-type": job_type,
+            "status": status,
+            "species_id": species_id,
+            "population_id": population_id,
+            "dataset_id": dataset_id,
+        },
+        "stdout": json.dumps(STATS) if stdout is None else stdout,
+    }
+
+
+@pytest.mark.unit_test
+def test_file_stats_parses_a_valid_completed_job():
+    """
+    GIVEN: a completed genotype-stats job for the requested dataset
+    WHEN: its file statistics are read
+    THEN: the parsed statistics are returned
+    """
+    assert file_stats(_job(), 1, 2, 3) == STATS
+
+
+@pytest.mark.unit_test
+def test_file_stats_rejects_the_wrong_job_type():
+    """
+    GIVEN: a job of a different type
+    WHEN: its file statistics are read
+    THEN: InvalidCollectorJob is raised
+    """
+    with pytest.raises(InvalidCollectorJob):
+        file_stats(_job(job_type="something-else"), 1, 2, 3)
+
+
+@pytest.mark.unit_test
+def test_file_stats_rejects_incomplete_jobs():
+    """
+    GIVEN: a job that has not completed
+    WHEN: its file statistics are read
+    THEN: InvalidCollectorJob is raised
+    """
+    with pytest.raises(InvalidCollectorJob):
+        file_stats(_job(status="running"), 1, 2, 3)
+
+
+@pytest.mark.unit_test
+def test_file_stats_rejects_jobs_for_another_dataset():
+    """
+    GIVEN: a collector job for a different dataset
+    WHEN: its file statistics are read for the requested dataset
+    THEN: InvalidCollectorJob is raised
+    """
+    with pytest.raises(InvalidCollectorJob):
+        file_stats(_job(), 1, 2, 999)
+
+
+@pytest.mark.unit_test
+def test_file_stats_rejects_missing_output():
+    """
+    GIVEN: a job with no recorded output
+    WHEN: its file statistics are read
+    THEN: InvalidCollectorJob is raised
+    """
+    with pytest.raises(InvalidCollectorJob):
+        file_stats(_job(stdout=""), 1, 2, 3)
+
+
+@pytest.mark.unit_test
+def test_file_stats_rejects_invalid_output():
+    """
+    GIVEN: a job whose output is not valid JSON
+    WHEN: its file statistics are read
+    THEN: InvalidCollectorJob is raised
+    """
+    with pytest.raises(InvalidCollectorJob):
+        file_stats(_job(stdout="not-json"), 1, 2, 3)