-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parse_lab_manual.py
More file actions
222 lines (188 loc) · 7.88 KB
/
test_parse_lab_manual.py
File metadata and controls
222 lines (188 loc) · 7.88 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
"""Tests for parse_lab_manual.py."""
import tempfile
import textwrap
from pathlib import Path
import pytest
from parse_lab_manual import (
parse_members_chapter,
add_member_to_lab_manual,
move_member_to_alumni,
commit_and_push_lab_manual,
)
MINIMAL_TEX = textwrap.dedent(r"""
\chapter{Lab members and alumni}\label{ch:members}
\begin{fullwidth}
\subsection{Current lab members}\label{sec:curr_members}
\newthought{PI}
\bigskip
\enskip Jeremy R. Manning (2015 -- )
\newthought{Graduate Students}
\begin{multicols}{2}\raggedcolumns
\begin{list}{\quad}{}
\item Alice Smith (2022 -- )
\item Bob Jones (2023 -- )
\end{list}
\end{multicols}
\newthought{Undergraduate RAs}
\begin{multicols}{2}\raggedcolumns
\begin{list}{\quad}{}
\item Charlie Brown (2024 -- )
\end{list}
\end{multicols}
\subsection{Lab alumni}
\newthought{Graduate Students}
\begin{multicols}{2}\raggedcolumns
\begin{list}{\quad}{}
\item Dana White (2018 -- 2022)
\end{list}
\end{multicols}
\newthought{Undergraduate RAs}
\begin{multicols}{2}\raggedcolumns
\begin{list}{\quad}{}
\item Eve Black (2020 -- 2021)
\item Frank Green (2019)
\end{list}
\end{multicols}
\end{fullwidth}
""").strip()
@pytest.fixture
def tex_file(tmp_path):
"""Create a temporary tex file with minimal member data."""
p = tmp_path / "lab_manual.tex"
p.write_text(MINIMAL_TEX, encoding='utf-8')
return p
class TestParseBasic:
def test_parses_all_entries(self, tex_file):
records = parse_members_chapter(tex_file)
assert len(records) == 7
def test_parses_pi(self, tex_file):
records = parse_members_chapter(tex_file)
pi = [r for r in records if r['role_category'] == 'PI']
assert len(pi) == 1
assert pi[0]['name'] == 'Jeremy R. Manning'
assert pi[0]['start_year'] == 2015
assert pi[0]['end_year'] is None
assert pi[0]['is_active'] is True
def test_parses_active_members(self, tex_file):
records = parse_members_chapter(tex_file)
active = [r for r in records if r['is_active']]
assert len(active) == 4
names = {r['name'] for r in active}
assert 'Alice Smith' in names
assert 'Bob Jones' in names
assert 'Charlie Brown' in names
def test_parses_alumni(self, tex_file):
records = parse_members_chapter(tex_file)
alumni = [r for r in records if not r['is_active']]
assert len(alumni) == 3
dana = next(r for r in alumni if r['name'] == 'Dana White')
assert dana['start_year'] == 2018
assert dana['end_year'] == 2022
assert dana['role_category'] == 'Graduate Students'
def test_parses_single_year_entry(self, tex_file):
records = parse_members_chapter(tex_file)
frank = next(r for r in records if r['name'] == 'Frank Green')
assert frank['start_year'] == 2019
assert frank['end_year'] is None # single year has no end
assert frank['is_active'] is False # in alumni section
def test_role_categories(self, tex_file):
records = parse_members_chapter(tex_file)
roles = {r['role_category'] for r in records}
assert 'PI' in roles
assert 'Graduate Students' in roles
assert 'Undergraduate RAs' in roles
class TestParseEdgeCases:
def test_commented_section(self, tmp_path):
tex = MINIMAL_TEX.replace(
r'\newthought{Undergraduate RAs}' + '\n'
r'\begin{multicols}{2}\raggedcolumns' + '\n'
r'\begin{list}{\quad}{}' + '\n'
r'\item Charlie Brown (2024 -- )' + '\n'
r'\end{list}' + '\n'
r'\end{multicols}' + '\n\n'
r'\subsection{Lab alumni}',
'% \\newthought{Undergraduate RAs}\n'
'% \\begin{multicols}{2}\\raggedcolumns\n'
'% \\begin{list}{\\quad}{}\n'
'% \\end{list}\n'
'% \\end{multicols}\n\n'
'\\subsection{Lab alumni}'
)
p = tmp_path / "lab_manual.tex"
p.write_text(tex, encoding='utf-8')
records = parse_members_chapter(p)
names = {r['name'] for r in records}
assert 'Charlie Brown' not in names
def test_missing_chapter_raises(self, tmp_path):
p = tmp_path / "lab_manual.tex"
p.write_text("\\chapter{Something else}", encoding='utf-8')
with pytest.raises(ValueError, match="Could not find"):
parse_members_chapter(p)
def test_empty_list_section(self, tmp_path):
tex = MINIMAL_TEX.replace(
'\\item Charlie Brown (2024 -- )\n',
''
)
p = tmp_path / "lab_manual.tex"
p.write_text(tex, encoding='utf-8')
records = parse_members_chapter(p)
names = {r['name'] for r in records}
assert 'Charlie Brown' not in names
class TestParseRealData:
def test_parses_real_lab_manual(self):
real_path = Path(__file__).parent.parent / 'lab-manual' / 'lab_manual.tex'
if not real_path.exists():
pytest.skip("Lab-manual submodule not initialized")
records = parse_members_chapter(real_path)
assert len(records) > 50
active = [r for r in records if r['is_active']]
alumni = [r for r in records if not r['is_active']]
assert len(active) > 5
assert len(alumni) > 20
# PI should always be present
pi = [r for r in records if r['role_category'] == 'PI']
assert len(pi) == 1
assert 'Manning' in pi[0]['name']
def test_multi_role_person_in_real_data(self):
real_path = Path(__file__).parent.parent / 'lab-manual' / 'lab_manual.tex'
if not real_path.exists():
pytest.skip("Lab-manual submodule not initialized")
records = parse_members_chapter(real_path)
# Paxton Fitzpatrick appears as undergrad RA alumni, lab manager alumni, and grad student
paxton_records = [r for r in records if 'Paxton' in r['name'] and 'Fitzpatrick' in r['name']]
assert len(paxton_records) >= 2
class TestAddMember:
def test_adds_grad_student(self, tex_file):
add_member_to_lab_manual(tex_file, 'New Person', 'grad student', 2026)
records = parse_members_chapter(tex_file)
new = next(r for r in records if r['name'] == 'New Person')
assert new['role_category'] == 'Graduate Students'
assert new['start_year'] == 2026
assert new['is_active'] is True
def test_adds_undergrad(self, tex_file):
add_member_to_lab_manual(tex_file, 'Test Undergrad', 'undergrad', 2026)
records = parse_members_chapter(tex_file)
new = next(r for r in records if r['name'] == 'Test Undergrad')
assert new['role_category'] == 'Undergraduate RAs'
assert new['is_active'] is True
def test_invalid_role_raises(self, tex_file):
with pytest.raises(ValueError, match="Could not find"):
add_member_to_lab_manual(tex_file, 'Test', 'wizard', 2026)
class TestMoveMember:
def test_moves_to_alumni(self, tex_file):
move_member_to_alumni(tex_file, 'Alice Smith', 2026)
records = parse_members_chapter(tex_file)
# Should no longer be active
active_names = {r['name'] for r in records if r['is_active']}
assert 'Alice Smith' not in active_names
# Should be in alumni
alice = next(r for r in records if r['name'] == 'Alice Smith' and not r['is_active'])
assert alice['end_year'] == 2026
assert alice['role_category'] == 'Graduate Students'
def test_move_nonexistent_raises(self, tex_file):
with pytest.raises(ValueError, match="Could not find"):
move_member_to_alumni(tex_file, 'Nobody Here', 2026)
class TestCommitAndPush:
def test_raises_when_not_initialized(self, tmp_path):
with pytest.raises(RuntimeError, match="not initialized"):
commit_and_push_lab_manual(tmp_path / 'nonexistent', 'test')