about summary refs log tree commit diff
path: root/tests/unit/test_jobs.py
blob: a0268705853bae3386f25d59abda0f510b291393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Tests for background jobs storage helpers."""
import time
import uuid

import pytest

from gn_libs import sqlite3
from gn_libs.jobs import jobs
from gn_libs.jobs.migrations import run_migrations


@pytest.fixture()
def jobsdb(tmp_path):
    """Create a migrated jobs database."""
    db = tmp_path / "jobs.db"
    run_migrations(str(db))
    return db


def _job(conn, job_type, **metadata):
    """Create a job with the given type and metadata."""
    return jobs.initialise_job(
        conn, str(uuid.uuid4()), ["true"], job_type, **metadata)


@pytest.mark.unit_test
def test_jobs_metadata_has_key_value_index(jobsdb):
    """
    GIVEN: a migrated jobs database
    WHEN: the schema is inspected
    THEN: the metadata key/value index exists
    """
    with sqlite3.connection(str(jobsdb)) as conn, sqlite3.cursor(conn) as cursor:
        cursor.execute(
            "SELECT name FROM sqlite_master WHERE type='index' "
            "AND name='idx_tbl_jobs_metadata_cols_key_value'")
        assert cursor.fetchone() is not None


@pytest.mark.unit_test
def test_jobs_by_metadata_matches_all_pairs(jobsdb):
    """
    GIVEN: jobs with various metadata
    WHEN: jobs are looked up by several metadata pairs
    THEN: only jobs matching all pairs are returned, with their metadata
    """
    with sqlite3.connection(str(jobsdb)) as conn:
        wanted = _job(conn, "qc", genotype_collector_job_id="c1")
        _job(conn, "qc", genotype_collector_job_id="c2")
        _job(conn, "other", genotype_collector_job_id="c1")
        found = jobs.jobs_by_metadata(
            conn, {"genotype_collector_job_id": "c1", "job-type": "qc"})
        assert [job["job_id"] for job in found] == [wanted["job_id"]]
        assert found[0]["metadata"]["genotype_collector_job_id"] == "c1"


@pytest.mark.unit_test
def test_jobs_by_metadata_returns_newest_first(jobsdb):
    """
    GIVEN: two matching jobs created at different times
    WHEN: jobs are looked up by metadata
    THEN: the newest job is returned first
    """
    with sqlite3.connection(str(jobsdb)) as conn:
        first = _job(conn, "qc", genotype_collector_job_id="c1")
        time.sleep(0.01)
        second = _job(conn, "qc", genotype_collector_job_id="c1")
        found = jobs.jobs_by_metadata(
            conn, {"genotype_collector_job_id": "c1"})
        assert [job["job_id"] for job in found] == [
            second["job_id"], first["job_id"]]


@pytest.mark.unit_test
def test_jobs_by_metadata_returns_empty_when_no_match(jobsdb):
    """
    GIVEN: jobs that do not match the query
    WHEN: jobs are looked up by metadata
    THEN: an empty tuple is returned
    """
    with sqlite3.connection(str(jobsdb)) as conn:
        _job(conn, "qc", genotype_collector_job_id="c1")
        assert jobs.jobs_by_metadata(
            conn, {"genotype_collector_job_id": "nope"}) == ()


@pytest.mark.unit_test
def test_initialise_job_treats_metadata_kwargs_as_metadata(jobsdb):
    """
    GIVEN: metadata and reserved operational arguments
    WHEN: a job is initialised
    THEN: the metadata is stored, while expiry_seconds is not metadata
    """
    with sqlite3.connection(str(jobsdb)) as conn:
        job = jobs.initialise_job(
            conn, str(uuid.uuid4()), ["true"], "qc",
            genotype_collector_job_id="c1",
            external_id="user-1", expiry_seconds=60)
        stored = jobs.job(conn, job["job_id"])
        assert stored["metadata"]["genotype_collector_job_id"] == "c1"
        assert stored["metadata"]["external_id"] == "user-1"
        assert "expiry_seconds" not in stored["metadata"]
        assert jobs.jobs_by_external_id(conn, "user-1")[0]["job_id"] == (
            job["job_id"])