Skip to content

Commit 8ccb434

Browse files
authored
Func tests (#35)
* More functional tests * More functional tests * Don't re-run if there are previous results * Tidy
1 parent fcc6472 commit 8ccb434

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

advent_of_action/main.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ def main() -> None:
105105
# │ ├── python_person
106106
# │ │ └── solution.py
107107
answers = ("", "") # todo
108+
109+
# Get the previous results.
110+
# todo Refactor this into a function.
111+
readme = Path("README.md")
112+
old_content = readme.read_text()
113+
section_begins = old_content.find("\n\n## Stats")
114+
if section_begins > -1:
115+
section_ends = old_content.find("\n\n##", section_begins + 1)
116+
section = old_content[section_begins:section_ends] if section_ends else old_content[section_begins:]
117+
results = from_table(section)
118+
108119
for dirpath, dirnames, filenames in path.walk(top_down=True):
109120
if ".optout" in filenames:
110121
continue
@@ -120,11 +131,12 @@ def main() -> None:
120131

121132
if len(dirpath.parts) == 2:
122133
directory = dirpath.parts[1]
123-
for name, runner in RUNTIMES.items():
124-
language: Language = directory[0 : directory.find("_")]
125-
person: Person = directory[directory.find("_") + 1 :]
126-
if language == name:
127-
results[(day, language, person)] = measure_execution_time(answers, dirpath, runner)
134+
language: Language = directory[0 : directory.find("_")]
135+
person: Person = directory[directory.find("_") + 1 :]
136+
if (day, language, person) not in results:
137+
for name, runner in RUNTIMES.items():
138+
if language == name:
139+
results[(day, language, person)] = measure_execution_time(answers, dirpath, runner)
128140

129141
write_results(results)
130142

tests/README_TEMPLATE_3.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Title
2+
3+
A sentence.
4+
5+
## Stats
6+
7+
| day | language | who | part | time (s) | mem (KB) | notes |
8+
| --- | --- | --- | --- | --- | --- | --- |
9+
| 99 | jupyter | iain | one | 1 | 8 | |
10+
| 99 | jupyter | iain | two | | | Different answer |
11+
| 99 | ocaml | iain | one | 1 | 8 | |
12+
| 99 | ocaml | iain | two | | | Different answer |
13+
| 99 | python | iain | one | 1 | 8 | |
14+
| 99 | python | iain | two | 1 | 8 | |
15+
| 99 | python | zain | one | | | Error(z) |
16+
| 99 | python | zain | two | | | Error(z) |
17+
| 99 | racket | iain | one | 1 | 8 | |
18+
| 99 | racket | iain | two | | | Different answer |
19+
| 99 | rust | iain | one | 1 | 8 | |
20+
21+
22+
## Section
23+
24+
Another sentence.

tests/test_main.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@
88

99

1010
class TestMain(unittest.TestCase):
11+
@classmethod
12+
def setUpClass(cls) -> None:
13+
os.environ["GPG_PASS"] = "yourpassword"
14+
1115
def setUp(self) -> None:
12-
"""Setup the test environment."""
16+
"""Set up the test environment."""
1317

1418
# Undo anything that might have been set by failing tests.
1519
Path("./README.md").unlink(missing_ok=True)
1620
Path("./input.txt").unlink(missing_ok=True)
17-
os.unsetenv("GPG_PASS")
1821

1922
def test_main(self) -> None:
2023
with patch("subprocess.run") as mock_run:
2124
mock_run.return_value.stdout = "helloo"
2225
mock_run.return_value.stderr = "1792,0.01,0.02"
2326
Path("README.md").write_text("")
2427
main.main()
25-
timings: list[str] = ["/usr/bin/time", "-f", "%M,%S,%U"]
26-
a: tuple[list[str], ...] = tuple(
28+
timings = ["/usr/bin/time", "-f", "%M,%S,%U"]
29+
a = (
2730
[["dotnet", "fsi", str(PosixPath("day_99/fsharp_iain/solution.fsx")), x] for x in ("one", "two")]
2831
+ [
2932
["ipython", "-c", f"%run {str(PosixPath('day_99/jupyter_iain/solution.ipynb'))}", x]
@@ -59,6 +62,31 @@ def test_main(self) -> None:
5962
],
6063
)
6164

65+
def test_main_two(self) -> None:
66+
with patch("subprocess.run") as mock_run:
67+
mock_run.return_value.stdout = "helloo"
68+
mock_run.return_value.stderr = "1792,0.01,0.02"
69+
shutil.copy(Path("README_TEMPLATE_3.md"), Path("README.md"))
70+
main.main()
71+
timings = ["/usr/bin/time", "-f", "%M,%S,%U"]
72+
a = [["dotnet", "fsi", str(PosixPath("day_99/fsharp_iain/solution.fsx")), x] for x in ("one", "two")] + [
73+
["cargo", "run", "--quiet", "--manifest-path", str(PosixPath("day_99/rust_iain/Cargo.toml")), x]
74+
for x in ("one", "two")
75+
]
76+
self.assertListEqual(
77+
mock_run.call_args_list,
78+
[
79+
call(
80+
timings + x,
81+
capture_output=True,
82+
timeout=60,
83+
text=True,
84+
check=True,
85+
)
86+
for x in a
87+
],
88+
)
89+
6290
def test_measure_one(self) -> None:
6391
# Check that we measure the run.
6492
actual = main.measure_execution_time(("answer", "answer"), Path("."), lambda x, y: (1792, 0.03, "answer"))
@@ -157,7 +185,6 @@ def test_write_results_two(self) -> None:
157185
self.assertEqual(expected_readme_txt, readme.read_text())
158186

159187
def test_get_answers(self) -> None:
160-
os.environ["GPG_PASS"] = "yourpassword"
161188
actual = main.get_answers(Path("day_99/"))
162189
self.assertEqual(("answer", "answer2"), actual)
163190

0 commit comments

Comments
 (0)