-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
129 lines (100 loc) · 4.76 KB
/
test_main.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
"""unit test main.py"""
from datetime import datetime
from unittest.mock import MagicMock, patch
import pytest
import main
# pylint: disable=missing-function-docstring
date1 = datetime(year=2024, month=10, day=1)
date2 = datetime(year=2024, month=10, day=6)
@pytest.mark.parametrize("date, is_month, measurement_name, name, is_watt", [
(date1, True, 'measurement1', 'Measurement 1', False),
(date2, False, 'measurement2', 'Measurement 2', True),
])
def test_process_and_log(date, is_month, measurement_name, name, is_watt):
with patch('main.process_measurement_watt') as mock_process_watt, \
patch('main.process_measurement_kwh') as mock_process_kwh, \
patch('main.log_difference') as mock_log_difference:
# Mock return values based on is_watt
if is_watt:
mock_process_watt.return_value = ((101, 102), (date, date))
expected_output = 1
mock_log_difference.return_value = expected_output
else:
mock_process_kwh.return_value = ((102, 101), (date, date))
expected_output = -1
mock_log_difference.return_value = expected_output
result = main.process_and_log(date, is_month, measurement_name, name, is_watt)
assert result == expected_output
if is_watt:
mock_process_watt.assert_called_once_with(date, is_month, measurement_name)
else:
mock_process_kwh.assert_called_once_with(date, is_month, measurement_name)
mock_log_difference.assert_called_once()
@pytest.mark.parametrize("test_date, verify_date, is_first_of_month", [
(datetime(year=2024, month=10, day=1), datetime(year=2024, month=10, day=1), True),
(datetime(year=2024, month=10, day=2), datetime(year=2024, month=10, day=1), True),
(datetime(year=2024, month=10, day=6), datetime(year=2024, month=10, day=6), False),
(datetime(year=2024, month=10, day=7), datetime(year=2024, month=10, day=6), False),
])
def test_main(test_date, verify_date, is_first_of_month):
"""test main function with different combinations"""
with patch('main.process') as mock_process, \
patch('main.datetime') as mock_datetime, \
patch('main.create_bar_chart') as _mock_create_bar_chart:
mock_datetime.now.return_value = test_date
main.main(today=test_date)
mock_process.assert_any_call(date=verify_date, is_month=is_first_of_month)
@pytest.mark.parametrize(
"date, is_month, measurement_name, expected_usage",
[
# Add your test cases for process_measurement_kwh here
(date1, True, 'measurement_kwh_1', 101),
(date2, False, 'measurement_kwh_2', 103),
# Add more cases as needed
])
def test_process_measurement_kwh(date, is_month, measurement_name, expected_usage):
with patch('main.GetFromInflux') as mock_influx:
mock_influx_instance = mock_influx.return_value
mock_influx_instance.get_values_from_influx.return_value = [0, expected_usage]
result, _ = main.process_measurement_kwh(date, is_month, measurement_name)
assert result[1] == expected_usage
@pytest.mark.parametrize(
"date, is_month, measurement_name, expected_usage",
[
# Add your test cases for process_measurement_watt here
(date1, True, 'measurement_watt_1', 101),
(date2, False, 'measurement_watt_2', 102),
# Add more cases as needed
])
def test_process_measurement_watt(date, is_month, measurement_name, expected_usage):
with patch('main.GetFromInflux') as mock_influx:
mock_influx_instance = mock_influx.return_value
mock_influx_instance.get_total_kwh_consumed_from_influx.return_value = expected_usage
result, _ = main.process_measurement_watt(date, is_month, measurement_name)
assert result[1] == expected_usage
@pytest.fixture
def mock_helpers():
with patch('main.is_first_of_month', return_value=False), \
patch('main.is_sunday', return_value=False), \
patch('main.log_difference', return_value={}):
yield
@pytest.fixture
def mock_influx():
with patch('main.GetFromInflux') as mock_influx:
mock_instance = MagicMock()
mock_influx.return_value = mock_instance
mock_instance.get_values_from_influx.return_value = (100, 200)
mock_instance.get_total_kwh_consumed_from_influx.return_value = 0.1
yield mock_instance
def test_process_monthly(mock_helpers, mock_influx):
date = datetime(2023, 9, 1)
is_month = True
result = main.process(date, is_month)
assert isinstance(result, list)
assert len(result) > 0
def test_process_weekly(mock_helpers, mock_influx):
date = datetime(2023, 9, 3) # Assume this is a Sunday
is_month = False
result = main.process(date, is_month)
assert isinstance(result, list)
assert len(result) > 0