-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_loadshift_time.py
More file actions
232 lines (208 loc) · 8.45 KB
/
test_loadshift_time.py
File metadata and controls
232 lines (208 loc) · 8.45 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import codegreen_core.tools as tools
import pytest
from codegreen_core.utilities.message import CodegreenDataError,Message
from datetime import datetime,timezone,timedelta
import codegreen_core.tools.loadshift_time as ts
import pandas as pd
import pytz
# Optimal time predications
class TestOptimalTimeCore:
# some common data for testing
dummy_energy_data_1 = pd.DataFrame({"startTimeUTC":[1,2,3],"totalRenewable":[1,2,3],"percent_renewable":[1,2,3]})
request_time_1 = datetime(2024,1,5,0,0)
request_time_2 = datetime(2024,1,10,0,0)
hard_finish_time_1 = datetime(2024,1,5,15,0)
hard_finish_time_2 = datetime(2024,1,15,15,0)
def test_energy_data_blank(self):
"""test if no energy data is provided, the result defaults to the request time """
timestamp, message, average_percent_renewable = ts.predict_optimal_time(None,1,1,1,self.hard_finish_time_1,self.request_time_1)
assert timestamp == int(self.request_time_1.timestamp())
assert message == Message.NO_DATA
assert average_percent_renewable == 0
def test_neg_hour(self):
"""test if negative hour value is provided, the result defaults to the request time """
timestamp, message, average_percent_renewable = ts.predict_optimal_time(self.dummy_energy_data_1,-1,1,1,self.hard_finish_time_1,self.request_time_1)
assert timestamp == int(self.request_time_1.timestamp())
assert message == Message.INVALID_DATA
assert average_percent_renewable == 0
def test_zero_hour(self):
"""test if hour value is 0, the result defaults to the request time """
timestamp, message, average_percent_renewable = ts.predict_optimal_time(self.dummy_energy_data_1,0,1,1,self.hard_finish_time_1,self.request_time_1)
assert timestamp == int(self.request_time_1.timestamp())
assert message == Message.INVALID_DATA
assert average_percent_renewable == 0
def test_neg_min(self):
"""test if negative hour value is provided, the result defaults to the request time """
timestamp, message, average_percent_renewable = ts.predict_optimal_time(self.dummy_energy_data_1,1,-1,1,self.hard_finish_time_1,self.request_time_1)
assert timestamp == int(self.request_time_1.timestamp())
assert message == Message.INVALID_DATA
assert average_percent_renewable == 0
def test_zero_per_renew(self):
"""test if 0 % renewable , the result defaults to the request time """
timestamp, message, average_percent_renewable = ts.predict_optimal_time(self.dummy_energy_data_1,1,0,-10,self.hard_finish_time_1,self.request_time_1)
assert timestamp == int(self.request_time_1.timestamp())
assert message == Message.NEGATIVE_PERCENT_RENEWABLE
assert average_percent_renewable == 0
def test_neg_per_renew(self):
"""test if negative -ve % renew is provided, the result defaults to the request time """
timestamp, message, average_percent_renewable = ts.predict_optimal_time(self.dummy_energy_data_1,1,0,0,self.hard_finish_time_1,self.request_time_1)
assert timestamp == int(self.request_time_1.timestamp())
assert message == Message.NEGATIVE_PERCENT_RENEWABLE
#assert average_percent_renewable == 0
def test_less_energy_data(self):
"""to test if the request time + running time > hard finish , then return the request time """
timestamp, message, average_percent_renewable = ts.predict_optimal_time(self.dummy_energy_data_1,20,0,10,self.hard_finish_time_1,self.request_time_1)
assert timestamp == int(self.request_time_1.timestamp())
assert message == Message.RUNTIME_LONGER_THAN_DEADLINE_ALLOWS
def test_if_incorrect_data_provided(self):
"""this is to test if energy data provided does not contain the data for the request time """
data = pd.read_csv("data/DE_forecast1.csv")
timestamp, message, average_percent_renewable = ts.predict_optimal_time(data,20,0,10,self.hard_finish_time_2,self.request_time_2)
assert timestamp == int(self.request_time_2.timestamp())
assert message == Message.NO_DATA
def test_multiple(self):
data = pd.read_csv("data/DE_forecast1.csv")
hard_finish_time = datetime(2024,1,7,0,0)
request_time = datetime(2024,1,5,0,0)
cases = [
{
"hd":hard_finish_time,
"rd":request_time,
"h":1,
"p":30,
"start":1704412800
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":2,
"p":30,
"start":1704412800
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":10,
"p":30,
"start":1704412800
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":20,
"p":30,
"start":1704412800
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":2,
"p":40,
"start":1704420000
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":5,
"p":40,
"start":1704420000
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":5,
"p":42,
"start":1704423600
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":1,
"p":45,
"start":1704445200 # percent renewable prioritized over the start time
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":5,
"p":45,
"start":1704445200
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":5,
"p":50,
"start":1704452400 # why 1704427200
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":10,
"p":50,
"start":1704452400
},
{
"hd":hard_finish_time,
"rd":request_time,
"h":1,
"p":50,
"start":1704445200
},
# {
# "hd":hard_finish_time,
# "rd":request_time,
# "h":10,
# "p":60,
# "start":1704412800 # no match , just start now
# }
]
assert 1==1
def test_data_validation_country(self):
timestamp1 = int(datetime.now(timezone.utc).timestamp())
timestamp, message, average_percent_renewable = ts.predict_now("UFO",10,0,datetime(2024,9,7),"percent_renewable",30)
print(timestamp1,timestamp, message)
assert timestamp - timestamp1 <= 10
assert message == Message.ENERGY_DATA_FETCHING_ERROR
def test_all_country_test(self):
test_cases = pd.read_csv("./data/test_cases_time.csv")
data = pd.read_csv("./data/prediction_testing_data.csv")
for index, row in test_cases.iterrows():
edata_filter = data["file_id"] == row["country"]
energy_data = data[edata_filter].copy()
start = datetime.strptime(row["start_time"], '%Y-%m-%d %H:%M:%S')
end = (start + timedelta(hours=row["hard_deadline_hour"]))
a,b,c = ts.predict_optimal_time(energy_data,row["runtime_hour"],row["runtime_min"],row["percent_renewable"],end,start)
print(a,b,c)
assert int(a) == row["expected_timestamp"]
# for case in cases:
# #print(case)
# print(str(case["p"])+"%,"+str(case["h"])+" h")
# timestamp, message, average_percent_renewable = ts.predict_optimal_time(data,case["h"],0,case["p"],case["hd"],case["rd"])
# print(timestamp)
# assert timestamp == case["start"]
# test if request time is none current time is being used
def test_all_country():
test_cases = pd.read_csv("./data/test_cases_time.csv")
data = pd.read_csv("./data/prediction_testing_data.csv")
for _ , row in test_cases.iterrows():
print(row)
edata_filter = data["file_id"] == row["country"]
energy_data = data[edata_filter].copy()
start_utc = datetime.strptime(row["start_time"], '%Y-%m-%d %H:%M:%S')
start_utc = pytz.UTC.localize(start_utc)
start = start_utc.astimezone(pytz.timezone('Europe/Berlin'))
end = (start + timedelta(hours=row["hard_deadline_hour"]))
a,b,c = ts.predict_optimal_time(energy_data,row["runtime_hour"],row["runtime_min"],row["percent_renewable"],end,start)
print(a,b,c)
assert int(a) == row["expected_timestamp"]
print("====")
test_all_country()
# def data_validation_country():
# timestamp1 = int(datetime.now(timezone.utc).timestamp())
# timestamp, message, average_percent_renewable = ts.predict_now("DE",10,0,datetime(2024,9,7),"percent_renewable",30)
# print(timestamp1,timestamp, message)
# #assert timestamp - timestamp1 <= 10
# #assert message == Message.ENERGY_DATA_FETCHING_ERROR
# data_validation_country()
# a,b,c = ts.predict_now("DE",2,30,datetime.fromtimestamp(1726092000),percent_renewable=50)