forked from jpwhite4/xdmod-python
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_datawarehouse_regression.py
280 lines (251 loc) · 7.8 KB
/
test_datawarehouse_regression.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from datetime import date
from dotenv import load_dotenv
import numpy
import os
import pandas
from pathlib import Path
import plotly.express as px
import plotly.io as pio
# The next line is used for the line `pio.templates.default = 'timeseries'`.
# Thus, we don't want the linter to think it is unused.
import xdmod_data.themes # noqa: F401
import pytest
from xdmod_data.warehouse import DataWarehouse
XDMOD_HOST = os.environ['XDMOD_HOST']
XDMOD_VERSION = os.environ['XDMOD_VERSION']
TOKEN_PATH = '~/.xdmod-data-token'
load_dotenv(Path(os.path.expanduser(TOKEN_PATH)), override=True)
# When outputting dataframes for debugging, don't truncate any of the output.
pandas.set_option('display.max_rows', None)
pandas.set_option('display.max_columns', None)
pandas.set_option('display.width', None)
pandas.set_option('display.max_colwidth', None)
@pytest.fixture(scope='module')
def valid_dw():
with DataWarehouse(XDMOD_HOST) as dw:
yield dw
def __assert_dfs_equal(
data_file,
actual,
dtype='object',
index_col='id',
columns_name=None,
override_default_data=False,
):
data_dir = os.path.dirname(__file__) + '/data/' + (
XDMOD_VERSION if override_default_data
else 'default'
)
if 'GENERATE_DATA_FILES' in os.environ: # pragma: no cover
try:
os.mkdir(data_dir)
except FileExistsError:
pass
actual.to_csv(data_dir + '/' + data_file)
else:
expected = pandas.read_csv(
data_dir + '/' + data_file,
dtype=dtype,
index_col=index_col,
keep_default_na=False,
na_values=[''],
).fillna(numpy.nan)
expected.columns = expected.columns.astype('string')
expected.columns.name = columns_name
if index_col == 'Time':
expected.index = pandas.to_datetime(expected.index)
assert expected.equals(actual), (
'\nEXPECTED:\n' + str(expected) + '\nACTUAL:\n' + str(actual)
)
@pytest.mark.parametrize(
'additional_params, number, csv_title',
[
(
{},
('54747' if XDMOD_VERSION == 'xdmod-10-5' else '54748'),
'raw-data-every-1000-no-fields-no-filters.csv',
),
(
{
'fields':
(
'Local Job Id',
'Quality of Service',
'GPUs',
'Start Time',
'Department',
),
'filters':
{
'Resource':
[
'mortorq',
'frearson',
],
},
},
('33345' if XDMOD_VERSION == 'xdmod-10-5' else '33346'),
'raw-data-every-1000-with-fields-and-filters.csv',
),
],
ids=('no-fields-and-filters', 'with-fields-and-filters'),
)
def test_get_raw_data(valid_dw, capsys, additional_params, number, csv_title):
defult_params = {
'duration': ('2016-01-01', '2016-12-31'),
'realm': 'Jobs',
'show_progress': True,
}
params = {**defult_params, **additional_params}
data = valid_dw.get_raw_data(**params).iloc[::1000]
data.index = data.index.astype('string')
__assert_dfs_equal(
csv_title,
data,
dtype='string',
index_col=0,
override_default_data=(XDMOD_VERSION == 'xdmod-10-5'),
)
assert 'Got ' + number + ' rows...DONE' in capsys.readouterr().out
def __assert_descriptor_dfs_equal(
data_file,
actual,
override_default_data=False,
):
__assert_dfs_equal(
data_file,
actual,
dtype='string',
override_default_data=override_default_data,
)
def test_describe_realms(valid_dw):
__assert_descriptor_dfs_equal(
'realms.csv',
valid_dw.describe_realms(),
override_default_data=(XDMOD_VERSION == 'xdmod-10-5'),
)
def test_describe_metrics(valid_dw):
__assert_descriptor_dfs_equal(
'jobs-metrics.csv',
valid_dw.describe_metrics('Jobs'),
override_default_data=(XDMOD_VERSION == 'xdmod-10-5'),
)
def test_describe_dimensions(valid_dw):
__assert_descriptor_dfs_equal(
'jobs-dimensions.csv',
valid_dw.describe_dimensions('Jobs'),
override_default_data=(XDMOD_VERSION == 'xdmod-10-5'),
)
def test_get_filter_values(valid_dw):
__assert_descriptor_dfs_equal(
'jobs-pi-group-filter-values.csv',
valid_dw.get_filter_values('Jobs', 'PI Group'),
)
def test_get_data_filter_user(valid_dw):
# Make sure the filter validation works for a user whose list position is
# greater than 10000 — this will raise an exception if it doesn't work.
valid_dw.get_data(
duration=('2016-01-01', '2017-12-31'),
realm='Jobs',
metric='CPU Hours: Total',
dataset_type='aggregate',
filters={'User': '10332'},
)
@pytest.mark.parametrize(
'aggregation_unit,data_file',
[
('Month', 'jobs-2016-2017-month.csv'),
('Quarter', 'jobs-2016-2017-quarters.csv'),
('Year', 'jobs-2016-2017-years.csv'),
],
ids=('month', 'quarter', 'year'),
)
def test_get_data(valid_dw, aggregation_unit, data_file):
data = valid_dw.get_data(
duration=('2016-01-01', '2017-12-31'),
realm='Jobs',
metric='CPU Hours: Total',
aggregation_unit=aggregation_unit,
)
__assert_dfs_equal(
data_file,
data,
index_col='Time',
columns_name='Metric',
dtype={'CPU Hours: Total': 'Float64'},
)
def test_get_aggregation_units(valid_dw):
expected_agg_units = ('Auto', 'Day', 'Month', 'Quarter', 'Year')
actual_agg_units = valid_dw.get_aggregation_units()
assert expected_agg_units == actual_agg_units
def test_get_durations(valid_dw):
expected_durations = [
'Yesterday',
'7 day',
'30 day',
'90 day',
'Month to date',
'Previous month',
'Quarter to date',
'Previous quarter',
'Year to date',
'Previous year',
'1 year',
'2 year',
'3 year',
'5 year',
'10 year',
]
today_date = date.today()
current_year = today_date.year
for count in range(0, 7):
year = current_year - count
year = str(year)
expected_durations.append(year)
actual_durations = list(valid_dw.get_durations())
assert expected_durations == actual_durations
def test_first_example_notebook(valid_dw):
pio.templates.default = 'timeseries'
with valid_dw:
data = valid_dw.get_data(
duration=('2016-01-01', '2017-12-31'),
realm='Jobs',
metric='Number of Users: Active',
)
plot = px.line(data, y='Number of Users: Active')
plot.show()
data['Day Name'] = data.index.strftime('%a')
plot = px.box(
data,
x='Day Name',
y='Number of Users: Active',
category_orders={
'Day Name': ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'),
},
)
plot.show()
metric_label = 'Number of Users: Active'
with valid_dw:
data = valid_dw.get_data(
duration=('2016-01-01', '2017-12-31'),
realm='Jobs',
metric=metric_label,
dimension='Resource',
)
plot = px.line(data, labels={'value': metric_label})
plot.show()
metric_label = 'Number of Users: Active'
with valid_dw:
data = valid_dw.get_data(
duration=('2016-01-01', '2017-12-31'),
realm='Jobs',
metric=metric_label,
dimension='Resource',
dataset_type='aggregate',
)
plot = px.bar(data, labels={'value': metric_label})
plot.update_layout(
showlegend=False,
xaxis_automargin=True,
)
plot.show()