File: test_node_data_file.py

package info (click to toggle)
augur 24.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 25,312 kB
  • sloc: python: 14,253; sh: 227; makefile: 35
file content (76 lines) | stat: -rw-r--r-- 2,355 bytes parent folder | download | duplicates (2)
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
import json

from augur.__version__ import __version__
from augur.errors import AugurError
from augur.util_support.node_data_file import NodeDataFile

import pytest


@pytest.fixture()
def build_node_data_file(tmpdir):
    def _build_node_data_file(contents):
        with open(f"{tmpdir}/node.json", "w") as file:
            file.write(contents)

        return NodeDataFile(f"{tmpdir}/node.json")

    return _build_node_data_file


class TestNodeDataFile:
    def test_items(self, build_node_data_file):
        node_data_file = build_node_data_file(
            """
            {
                "generated_by": "some app",
                "nodes": { "a": 5 }
            }
            """
        )
        items = node_data_file.items()

        assert len(items) == 1
        assert "generated_by" not in items

    def test_validate_valid(self, build_node_data_file):
        # Implied assertion that no exceptions are raised
        build_node_data_file(
            f"""
            {{
                "annotations": {{ "nuc": {{ "start": 1, "end": 100 }} }},
                "generated_by": {{ "program": "augur", "version": "{__version__}" }},
                "nodes": {{ "a": 5 }}
            }}
            """
        )

    def test_validate_invalid_annotation(self, build_node_data_file):
        with pytest.raises(AugurError, match="annotations.*invalid JSON format"):
            build_node_data_file(
                """
                {
                    "annotations": "hhhhh",
                    "nodes": { "a": 5 }
                }
                """
            )

    def test_validate_nodes_not_dict(self, build_node_data_file):
        with pytest.raises(AugurError, match="is not a dictionary"):
            build_node_data_file('{ "nodes": "hhh" }')

    def test_validate_incompatible_augur_version(self, build_node_data_file):
        with pytest.raises(AugurError, match="incompatibility detected"):
            build_node_data_file(
                """
                {
                    "generated_by": { "program": "augur", "version": "0.1" },
                    "nodes": { "a": 5 }
                }
                """
            )

    def test_validate_malformed_json(self, build_node_data_file):
        with pytest.raises(json.decoder.JSONDecodeError):
            build_node_data_file("hhhhhhhhhh")