-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_build_software.py
More file actions
264 lines (204 loc) · 8.17 KB
/
test_build_software.py
File metadata and controls
264 lines (204 loc) · 8.17 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
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
"""Tests for build_software.py using REAL files - no mocks.
All tests create real Excel files and real HTML files on disk
to verify the actual behavior of the build script.
"""
import pytest
from pathlib import Path
import tempfile
import openpyxl
from build_software import (
load_software,
generate_software_item,
generate_section_content,
build_software,
)
class TestLoadSoftware:
"""Test loading software data from Excel."""
@pytest.fixture
def temp_dir(self):
"""Create a real temporary directory."""
with tempfile.TemporaryDirectory() as td:
yield Path(td)
def test_loads_single_sheet(self, temp_dir):
"""Test loading data from a single sheet."""
xlsx_path = temp_dir / 'software.xlsx'
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'python'
ws.append(['name', 'description', 'links_html'])
ws.append(['TestTool', 'A test tool.', '[<a href="#">Link</a>]'])
wb.save(xlsx_path)
data = load_software(xlsx_path)
assert 'python' in data
assert len(data['python']) == 1
assert data['python'][0]['name'] == 'TestTool'
def test_loads_multiple_sheets(self, temp_dir):
"""Test loading data from multiple sheets."""
xlsx_path = temp_dir / 'software.xlsx'
wb = openpyxl.Workbook()
wb.remove(wb.active)
for sheet_name in ['python', 'javascript', 'matlab']:
ws = wb.create_sheet(title=sheet_name)
ws.append(['name', 'description', 'links_html'])
ws.append([f'{sheet_name}_tool', 'Description', ''])
wb.save(xlsx_path)
data = load_software(xlsx_path)
assert len(data) == 3
assert 'python' in data
assert 'javascript' in data
assert 'matlab' in data
def test_handles_empty_cells(self, temp_dir):
"""Test that empty cells become empty strings."""
xlsx_path = temp_dir / 'software.xlsx'
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'python'
ws.append(['name', 'description', 'links_html'])
ws.append(['Name', None, None])
wb.save(xlsx_path)
data = load_software(xlsx_path)
assert data['python'][0]['description'] == ''
assert data['python'][0]['links_html'] == ''
class TestGenerateSoftwareItem:
"""Test HTML generation for software items."""
def test_generates_complete_item(self):
"""Test generating a complete software item."""
item = {
'name': 'MyTool',
'description': 'Does something useful.',
'github_link': 'https://github.com/example/mytool'
}
html = generate_software_item(item)
assert '<p>' in html
assert '</p>' in html
assert '<strong>MyTool.</strong>' in html
assert 'Does something useful.' in html
assert 'GitHub' in html
assert 'https://github.com/example/mytool' in html
def test_handles_missing_links(self):
"""Test item without links."""
item = {
'name': 'NoLinks',
'description': 'A tool without links.',
'links_html': ''
}
html = generate_software_item(item)
assert '<strong>NoLinks.</strong>' in html
assert 'A tool without links.' in html
assert html.count('[') == 0
def test_preserves_html_in_description(self):
"""Test that HTML in description is preserved."""
item = {
'name': 'LinkTool',
'description': 'Works with <a href="#">other tool</a> and stuff.',
'links_html': ''
}
html = generate_software_item(item)
assert '<a href="#">other tool</a>' in html
class TestGenerateSectionContent:
"""Test HTML generation for software sections."""
def test_generates_multiple_items(self):
"""Test generating content with multiple items."""
items = [
{'name': 'Tool A', 'description': 'Desc A', 'links_html': ''},
{'name': 'Tool B', 'description': 'Desc B', 'links_html': ''},
]
html = generate_section_content(items)
assert html.count('<p>') == 2
assert 'Tool A' in html
assert 'Tool B' in html
def test_handles_empty_list(self):
"""Test generating content with no items."""
html = generate_section_content([])
assert html == ''
class TestBuildSoftware:
"""Test full build process."""
@pytest.fixture
def temp_dir(self):
"""Create a real temporary directory."""
with tempfile.TemporaryDirectory() as td:
yield Path(td)
def test_builds_complete_page(self, temp_dir):
"""Test building a complete software page."""
# Create data file
xlsx_path = temp_dir / 'data.xlsx'
wb = openpyxl.Workbook()
wb.remove(wb.active)
for sheet_name in ['python', 'javascript', 'matlab']:
ws = wb.create_sheet(title=sheet_name)
ws.append(['name', 'description', 'links_html'])
if sheet_name == 'python':
ws.append(['TestPython', 'Python tool.', '[<a href="#">Link</a>]'])
wb.save(xlsx_path)
# Create template
template_path = temp_dir / 'template.html'
template_path.write_text('''<!DOCTYPE html>
<html>
<body>
<div id="python"><!-- PYTHON_CONTENT --></div>
<div id="javascript"><!-- JAVASCRIPT_CONTENT --></div>
<div id="matlab"><!-- MATLAB_CONTENT --></div>
</body>
</html>''')
# Build
output_path = temp_dir / 'output.html'
build_software(xlsx_path, template_path, output_path)
# Verify
result = output_path.read_text()
assert 'TestPython' in result
assert 'Python tool.' in result
def test_handles_special_characters(self, temp_dir):
"""Test that special characters are preserved."""
xlsx_path = temp_dir / 'data.xlsx'
wb = openpyxl.Workbook()
wb.remove(wb.active)
for sheet_name in ['python', 'javascript', 'matlab']:
ws = wb.create_sheet(title=sheet_name)
ws.append(['name', 'description', 'links_html'])
if sheet_name == 'matlab':
ws.append(['Sane pColor', "Doesn't look blurry.", ''])
wb.save(xlsx_path)
template_path = temp_dir / 'template.html'
template_path.write_text('''<!DOCTYPE html>
<html>
<body>
<div id="python"><!-- PYTHON_CONTENT --></div>
<div id="javascript"><!-- JAVASCRIPT_CONTENT --></div>
<div id="matlab"><!-- MATLAB_CONTENT --></div>
</body>
</html>''')
output_path = temp_dir / 'output.html'
build_software(xlsx_path, template_path, output_path)
result = output_path.read_text()
assert "Doesn't" in result
class TestIntegration:
"""Integration tests using actual project files."""
def test_can_load_real_software_data(self):
"""Test loading the actual software.xlsx file."""
project_root = Path(__file__).parent.parent
xlsx_path = project_root / 'data' / 'software.xlsx'
if not xlsx_path.exists():
pytest.skip("software.xlsx not found")
data = load_software(xlsx_path)
# Should have all expected sections
assert 'python' in data
assert 'javascript' in data
assert 'matlab' in data
# Should have some content
assert len(data['python']) > 0
assert len(data['matlab']) > 0
def test_can_build_from_real_data(self):
"""Test building from actual project files."""
project_root = Path(__file__).parent.parent
data_path = project_root / 'data' / 'software.xlsx'
template_path = project_root / 'templates' / 'software.html'
if not data_path.exists() or not template_path.exists():
pytest.skip("Required files not found")
with tempfile.TemporaryDirectory() as td:
output_path = Path(td) / 'software.html'
build_software(data_path, template_path, output_path)
# Verify output exists and has content
assert output_path.exists()
content = output_path.read_text()
assert len(content) > 3000 # Should be a substantial page
assert 'software-list' in content