File size: 4,834 Bytes
b400ace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
from pathlib import Path

from src.tests.test_samples.sample1.boilerplate import TEMPLATE_BOILERPLATE
from src.tests.utils import (
    generate_write_jsons_and_run,
    run_entry_point,
    setup_mocker_patches,
)

FROZEN_TIMESTAMP = "1970-01-01"
CURRENT_DIR = Path("src/tests")
BASE_SAMPLE_PATH = CURRENT_DIR.joinpath("test_samples", "sample1")
BASE_SAMPLE_TEMPLATE_PATH = BASE_SAMPLE_PATH.joinpath("template.json")


def run_sample(mocker, input_path):
    setup_mocker_patches(mocker)
    output_dir = os.path.join("outputs", input_path)
    run_entry_point(input_path, output_dir)


write_jsons_and_run = generate_write_jsons_and_run(
    run_sample,
    sample_path=BASE_SAMPLE_PATH,
    template_boilerplate=TEMPLATE_BOILERPLATE,
)


def test_no_input_dir(mocker):
    try:
        run_sample(mocker, "X")
    except Exception as e:
        assert str(e) == "Given input directory does not exist: 'X'"


def test_no_template(mocker):
    if os.path.exists(BASE_SAMPLE_TEMPLATE_PATH):
        os.remove(BASE_SAMPLE_TEMPLATE_PATH)
    try:
        run_sample(mocker, BASE_SAMPLE_PATH)
    except Exception as e:
        assert (
            str(e)
            == "No template file found in the directory tree of src/tests/test_samples/sample1"
        )


def test_empty_template(mocker):
    def modify_template(_):
        return {}

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert (
        str(exception)
        == f"Provided Template JSON is Invalid: '{BASE_SAMPLE_TEMPLATE_PATH}'"
    )


def test_invalid_field_type(mocker):
    def modify_template(template):
        template["fieldBlocks"]["MCQ_Block_1"]["fieldType"] = "X"

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert (
        str(exception)
        == f"Provided Template JSON is Invalid: '{BASE_SAMPLE_TEMPLATE_PATH}'"
    )


def test_overflow_labels(mocker):
    def modify_template(template):
        template["fieldBlocks"]["MCQ_Block_1"]["fieldLabels"] = ["q1..100"]

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert (
        str(exception)
        == "Overflowing field block 'MCQ_Block_1' with origin [65, 60] and dimensions [189, 5173] in template with dimensions [300, 400]"
    )


def test_overflow_safe_dimensions(mocker):
    def modify_template(template):
        template["pageDimensions"] = [255, 400]

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert str(exception) == "No Error"


def test_field_strings_overlap(mocker):
    def modify_template(template):
        template["fieldBlocks"] = {
            **template["fieldBlocks"],
            "New_Block": {
                **template["fieldBlocks"]["MCQ_Block_1"],
                "fieldLabels": ["q5"],
            },
        }

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert str(exception) == (
        "The field strings for field block New_Block overlap with other existing fields"
    )


def test_custom_label_strings_overlap_single(mocker):
    def modify_template(template):
        template["customLabels"] = {
            "label1": ["q1..2", "q2..3"],
        }

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert (
        str(exception)
        == "Given field string 'q2..3' has overlapping field(s) with other fields in 'Custom Label: label1': ['q1..2', 'q2..3']"
    )


def test_custom_label_strings_overlap_multiple(mocker):
    def modify_template(template):
        template["customLabels"] = {
            "label1": ["q1..2"],
            "label2": ["q2..3"],
        }

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert (
        str(exception)
        == "The field strings for custom label 'label2' overlap with other existing custom labels"
    )


def test_missing_field_block_labels(mocker):
    def modify_template(template):
        template["customLabels"] = {"Combined": ["qX", "qY"]}

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert (
        str(exception)
        == "Missing field block label(s) in the given template for ['qX', 'qY'] from 'Combined'"
    )


def test_missing_output_columns(mocker):
    def modify_template(template):
        template["outputColumns"] = ["qX", "q1..5"]

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert str(exception) == (
        "Some columns are missing in the field blocks for the given output columns"
    )


def test_safe_missing_label_columns(mocker):
    def modify_template(template):
        template["outputColumns"] = ["q1..4"]

    exception = write_jsons_and_run(mocker, modify_template=modify_template)
    assert str(exception) == "No Error"