-
Notifications
You must be signed in to change notification settings - Fork 44
/
abstract_test_ods_output_diff.py
130 lines (115 loc) · 4.51 KB
/
abstract_test_ods_output_diff.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
123
124
125
126
127
128
129
130
# Copyright 2022 eprbell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
from datetime import date
from enum import Enum
from pathlib import Path
from subprocess import run
from typing import Dict, List, Optional
from ods_diff import ods_diff
from rp2.configuration import MAX_DATE, MIN_DATE
ROOT_PATH: Path = Path(os.path.dirname(__file__)).parent.absolute()
CONFIG_PATH: Path = ROOT_PATH / Path("config")
INPUT_PATH: Path = ROOT_PATH / Path("input")
GOLDEN_PATH: Path = INPUT_PATH / Path("golden")
class OutputPlugins(Enum):
OPEN_POSITIONS = "open_positions"
RP2_FULL_REPORT = "rp2_full_report"
TAX_REPORT_JP = "tax_report_jp"
TAX_REPORT_US = "tax_report_us"
TAX_REPORT_IE = "tax_report_ie"
class AbstractTestODSOutputDiff(unittest.TestCase):
# Temporarily removed lifo and hifo due to https://github.com/eprbell/rp2/issues/79
METHODS: List[str] = ["fifo", "hifo", "lifo", "lofo"]
def setUp(self) -> None:
self.maxDiff = None # pylint: disable=invalid-name
@staticmethod
def __get_time_interval(from_date: date = MIN_DATE, to_date: date = MAX_DATE) -> str:
time_interval: str = ""
if from_date > MIN_DATE and to_date < MAX_DATE:
time_interval = f"{from_date}_{to_date}_"
elif from_date > MIN_DATE and to_date >= MAX_DATE:
time_interval = f"{from_date}_infinity_"
elif from_date <= MIN_DATE and to_date < MAX_DATE:
time_interval = f"0_{to_date}_"
return time_interval
@classmethod
def _generate(
cls,
output_dir: Path,
test_name: str,
config: str,
method: str,
input_path: Path = INPUT_PATH,
from_date: date = MIN_DATE,
to_date: date = MAX_DATE,
allow_negative_balances: bool = False,
generation_language: Optional[str] = None,
country: str = "us",
env: Optional[Dict[str, str]] = None,
) -> None:
config = test_name if config is None else config
time_interval: str = cls.__get_time_interval(from_date, to_date)
arguments: List[str] = [
f"rp2_{country}",
"-o",
str(output_dir),
"-p",
f"{test_name}_{f'{country}_' if country != 'us' else ''}{f'{generation_language}_' if generation_language else ''}{time_interval}",
]
if method != "mixed":
arguments.extend(["-m", method])
if generation_language:
arguments.extend(["-g", generation_language])
if from_date:
arguments.extend(["-f", str(from_date)])
if to_date:
arguments.extend(["-t", str(to_date)])
if allow_negative_balances:
arguments.extend(["-n"])
arguments.extend(
[
str(CONFIG_PATH / Path(f"{config}.ini")),
str(input_path / Path(f"{test_name}.ods")),
]
)
if not env:
run(arguments, check=True)
else:
merged_env = os.environ.copy()
merged_env.update(env)
run(arguments, check=True, env=merged_env)
def _compare(
self,
output_dir: Path,
test_name: str,
method: str,
output_plugin: OutputPlugins,
from_date: date = MIN_DATE,
to_date: date = MAX_DATE,
generation_language: Optional[str] = None,
country: str = "us",
) -> None:
time_interval: str = self.__get_time_interval(from_date, to_date)
diff: str
output_file_name: Path = Path(
f"{test_name}_{f'{country}_' if country != 'us' else ''}"
f"{f'{generation_language}_' if generation_language else ''}"
f"{time_interval}{method}_{output_plugin.value}.ods"
)
full_output_file_name: Path = output_dir / output_file_name
full_golden_file_name: Path = GOLDEN_PATH / Path(f"{country}") / output_file_name
diff = ods_diff(full_golden_file_name, full_output_file_name, generate_ascii_representation=True)
self.assertFalse(diff, msg=diff)