forked from PCWG/PCWG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare_test.py
183 lines (122 loc) · 6.04 KB
/
share_test.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
from os.path import join
from os import getcwd
from os import remove
from pcwg.share.share_factory import ShareAnalysisFactory
from pcwg.share.share import ShareXPortfolio
from pcwg.share.share_matrix import ShareMatrix
from pcwg.configuration.portfolio_configuration import PortfolioConfiguration
from xlrd import open_workbook
from zipfile import ZipFile
def test_almost_equal(a, b, tolerance):
if a is None and b is None:
return True
if a is None and b is not None:
return False
if a is not None and b is None:
return False
diff = abs(a-b)
if diff > tolerance:
return False
else:
return True
def is_string(value):
if isinstance(value, basestring):
return True
else:
return False
def is_string(value):
if value is None:
return True
if isinstance(value, basestring):
return True
else:
return False
def get_value(value):
if is_string(value):
if len(value.strip()) > 0:
return value
else:
return None
return value
def compare_sheets(test_report, test_sheet, benchmark, benchmark_sheet='Manual', tolerance=0.0001):
print 'TESTING: {0}'.format(test_sheet)
print("- Tolerance = {0:.4%}".format(tolerance))
test = get_sheet(test_report, test_sheet)
benchmark = get_sheet(benchmark, benchmark_sheet)
different = 0
total = 0
for column in range(39):
for row in range(76):
test_value = get_value(test.cell(row, column).value)
benchmark_value = get_value(benchmark.cell(row, column).value)
if not is_string(benchmark_value):
total += 1
if is_string(test_value):
different += 1
print '- R{0}C{1} difference detected: {2:.4%} expected vs {3} actual'.format(row + 1,column + 1,benchmark_value,test_value)
elif not test_almost_equal(benchmark_value, test_value, tolerance):
different += 1
print '- R{0}C{1} difference detected: {2:.4%} expected vs {3:.4%} actual'.format(row + 1,column + 1,benchmark_value,test_value)
elif not is_string(test_value):
different += 1
print '- R{0}C{1} difference detected: {2} expected vs {3:.4%} actual'.format(row + 1,column + 1,benchmark_value,test_value)
fraction_different = float(different) / float(total)
if different > 0:
print '- FAIL: {0:.1%} different'.format(fraction_different)
else:
print '- PASS'
def get_sheet(wb_path, name):
wb = open_workbook(wb_path)
for s in wb.sheets():
if s.name == name:
return s
raise Exception('Cant find sheet {0}'.format(name))
def check_zip_file_contains_n_files(file_name, n):
with ZipFile(file_name) as z:
if len(z.filelist) != n:
raise Exception('Zip file has unexpected number of files: {0}'.format(len(z.filelist)))
def get_test_file(zip_file_name):
with ZipFile(zip_file_name) as z:
for f in z.filelist:
if f.filename.lower() != 'summary.xls':
return f.filename
def extract_test_file(zip_file_name, member, path):
with ZipFile(zip_file_name) as z:
z.extract(member=member, path=path)
def test_matrix():
benchmark_folder = join(getcwd(), 'ShareBenchmarks')
portfolio_path = join(benchmark_folder, 'portfolio.xml')
portfolio = ShareMatrix(PortfolioConfiguration(portfolio_path))
def test_share(rerun=True, cleanup=True):
benchmark_folder = join(getcwd(), 'ShareBenchmarks')
if rerun:
portfolio_path = join(benchmark_folder, 'portfolio.xml')
portfolio = ShareXPortfolio(PortfolioConfiguration(portfolio_path),
ShareAnalysisFactory('Share03'))
zip_path = portfolio.get_zip_file_path()
else:
zip_path = join(benchmark_folder, 'portfolio (Share03).zip')
check_zip_file_contains_n_files(zip_path, 2)
test_report = get_test_file(zip_path)
test_path = join(benchmark_folder, test_report)
extract_test_file(zip_path, member=test_report, path=benchmark_folder)
compare_sheets(test_path, 'Baseline', join(benchmark_folder, 'Dataset 1 - Baseline - 01.xlsx'))
compare_sheets(test_path, 'Den & Turb', join(benchmark_folder, 'Dataset 1 - Turbulence Renormalisation - 01.xlsx'))
compare_sheets(test_path, 'Den & REWS (S)', join(benchmark_folder, 'Dataset 1 - REWS - 01.xlsx'))
compare_sheets(test_path, 'Den & REWS (S+V)', join(benchmark_folder, 'Dataset 1 - REWS and Veer - 01.xlsx'))
compare_sheets(test_path, 'Den & REWS (S+V+U)', join(benchmark_folder, 'Dataset 1 - REWS and Veer and Upflow - 01.xlsx'))
compare_sheets(test_path, 'Den & P by H', join(benchmark_folder, 'Dataset 1 - Prod By Height - 01.xlsx'))
compare_sheets(test_path, 'Den & Aug Turb (Relaxed)', join(benchmark_folder, 'Dataset 1 - Turbulence Renormalisation Augmented - 01.xlsx'))
compare_sheets(test_path, 'Den & RAWS (S)', join(benchmark_folder, 'Dataset 1 - RAWS - 01.xlsx'))
compare_sheets(test_path, 'Den & RAWS (S+V)', join(benchmark_folder, 'Dataset 1 - RAWS and Veer - 01.xlsx'))
compare_sheets(test_path, 'Den & RAWS (S+V+U)', join(benchmark_folder, 'Dataset 1 - RAWS and Veer and Upflow - 01.xlsx'))
compare_sheets(test_path, 'Den, REWS (S) & Turb',join(benchmark_folder, 'Dataset 1 - REWS and Turbulence Renormalisation - 01.xlsx'))
compare_sheets(test_path, 'Den, REWS (S+V) & Turb',join(benchmark_folder, 'Dataset 1 - REWS (S+V) and Turbulence Renormalisation - 01.xlsx'))
compare_sheets(test_path, 'Den, REWS (S+V+U) & Turb',join(benchmark_folder, 'Dataset 1 - REWS (S+V+U) and Turbulence Renormalisation - 01.xlsx'))
compare_sheets(test_path, 'Den & 2D PDM', join(benchmark_folder, 'Dataset 1 - 2D Matrix - 01.xlsx'))
compare_sheets(test_path, 'Den & 3D PDM', join(benchmark_folder, 'Dataset 1 - 3D Matrix - 01.xlsx'))
if cleanup:
remove(zip_path)
remove(test_path)
if __name__ == "__main__":
test_share(rerun=True, cleanup=True)