-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_test_files.py
122 lines (96 loc) · 3.13 KB
/
create_test_files.py
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
"""Create files for testing purposes"""
import sys
import os
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))
from utils.create_test_data import create_test_inv, create_test_rf_resp
# CONSTANTS
REPO_PATH = "../tests/repository/"
INV_NAME = "test_inv.nc"
RESP_NAME = "test_resp.nc"
TOML_NAME = "test.toml"
TOML_INVALID_NAME = "test_invalid.toml"
def create_test_directories(path_arr: list):
"""
Create new test directories if they do not exist.
Args:
path_arr (list): A list of paths to be created.
Returns:
None
Raises:
OSError: If the creation of a directory fails.
"""
for path in path_arr:
if not os.path.isdir(path):
msg = f"Create new test directory {path}"
print(msg)
os.makedirs(path)
def create_test_config_files(repo_path, valid_name, invalid_name):
"""
Create two configuration files for testing.
Args:
repo_path (str): The path to the repository.
valid_name (str): The name of the valid configuration file.
invalid_name (str): The name of the invalid configuration file.
Returns:
None
Raises:
OSError: If the creation of a file fails.
"""
file_path = repo_path + valid_name
if os.path.isfile(file_path):
msg = "Overwrite existing file " + file_path
print(msg)
with open(file_path, mode="w", encoding="utf-8") as valid_file:
valid_file.write(
'# Key-Value pair\
\nkey = "value"'
)
file_path = repo_path + invalid_name
if os.path.isfile(file_path):
msg = "Overwrite existing file " + file_path
print(msg)
with open(file_path, mode="w", encoding="utf-8") as invalid_file:
invalid_file.write(
'# Invalid Toml syntax\
\nkey ! "value"'
)
def create_test_inv_nc(repo_path, inv_name):
"""
Create an emission inventory netCDF file for testing.
Args:
repo_path (str): The path to the repository.
inv_name (str): The name of the emission inventory file.
Returns:
None
Raises:
OSError: If the creation of a file fails.
"""
file_path = repo_path + inv_name
if os.path.isfile(file_path):
msg = "Overwrite existing file " + file_path
print(msg)
inv = create_test_inv()
inv.to_netcdf(file_path)
def create_test_resp_nc(repo_path, resp_name):
"""
Create a response netCDF file for testing.
Args:
repo_path (str): The path to the repository.
resp_name (str): The name of the response file.
Returns:
None
Raises:
OSError: If the creation of a file fails.
"""
file_path = repo_path + resp_name
if os.path.isfile(file_path):
msg = "Overwrite existing file " + file_path
print(msg)
resp = create_test_rf_resp()
resp.to_netcdf(file_path)
if __name__ == "__main__":
create_test_directories([REPO_PATH])
create_test_config_files(REPO_PATH, TOML_NAME, TOML_INVALID_NAME)
create_test_inv_nc(REPO_PATH, INV_NAME)
create_test_resp_nc(REPO_PATH, RESP_NAME)