afscgap.test.test_tools

Convienence functions for testing afscgap.

(c) 2023 Regents of University of California / The Eric and Wendy Schmidt Center for Data Science and the Environment at UC Berkeley.

This file is part of afscgap released under the BSD 3-Clause License. See LICENSE.md.

 1"""
 2Convienence functions for testing afscgap.
 3
 4(c) 2023 Regents of University of California / The Eric and Wendy Schmidt Center
 5for Data Science and the Environment at UC Berkeley.
 6
 7This file is part of afscgap released under the BSD 3-Clause License. See
 8LICENSE.md.
 9"""
10import json
11import os
12import pathlib
13import unittest
14
15# pylint: disable=C0115, C0116
16
17
18def get_test_file_path(filename: str) -> str:
19    parent_dir = pathlib.Path(__file__).parent.absolute()
20    data_dir = os.path.join(parent_dir, 'data')
21    full_path = os.path.join(data_dir, filename)
22    return full_path
23
24
25def load_test_data_json(filename: str) -> dict:
26    full_path = get_test_file_path(filename)
27
28    with open(full_path) as f:
29        loaded_data = json.load(f)
30
31    return loaded_data
32
33
34def load_test_data_text(filename: str) -> str:
35    full_path = get_test_file_path(filename)
36
37    with open(full_path) as f:
38        loaded_data = f.read()
39
40    return loaded_data
41
42
43def make_result_json(filename: str):
44    new_mock = unittest.mock.MagicMock()
45    new_mock.status_code = 200
46
47    loaded_data = load_test_data_json(filename)
48
49    new_mock.json = unittest.mock.MagicMock(
50        return_value=loaded_data
51    )
52
53    return new_mock
54
55
56def make_result_text(filename: str):
57    new_mock = unittest.mock.MagicMock()
58    new_mock.status_code = 200
59
60    loaded_data = load_test_data_text(filename)
61    new_mock.text = loaded_data
62
63    return new_mock
def get_test_file_path(filename: str) -> str:
19def get_test_file_path(filename: str) -> str:
20    parent_dir = pathlib.Path(__file__).parent.absolute()
21    data_dir = os.path.join(parent_dir, 'data')
22    full_path = os.path.join(data_dir, filename)
23    return full_path
def load_test_data_json(filename: str) -> dict:
26def load_test_data_json(filename: str) -> dict:
27    full_path = get_test_file_path(filename)
28
29    with open(full_path) as f:
30        loaded_data = json.load(f)
31
32    return loaded_data
def load_test_data_text(filename: str) -> str:
35def load_test_data_text(filename: str) -> str:
36    full_path = get_test_file_path(filename)
37
38    with open(full_path) as f:
39        loaded_data = f.read()
40
41    return loaded_data
def make_result_json(filename: str):
44def make_result_json(filename: str):
45    new_mock = unittest.mock.MagicMock()
46    new_mock.status_code = 200
47
48    loaded_data = load_test_data_json(filename)
49
50    new_mock.json = unittest.mock.MagicMock(
51        return_value=loaded_data
52    )
53
54    return new_mock
def make_result_text(filename: str):
57def make_result_text(filename: str):
58    new_mock = unittest.mock.MagicMock()
59    new_mock.status_code = 200
60
61    loaded_data = load_test_data_text(filename)
62    new_mock.text = loaded_data
63
64    return new_mock