Skip to content

Commit 40bf6eb

Browse files
committed
add tests for #53 and #52
1 parent d0b3dd3 commit 40bf6eb

File tree

5 files changed

+406
-23
lines changed

5 files changed

+406
-23
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,4 @@ cython_debug/
162162
**/.DS_Store
163163
**/track.json
164164
poetry.lock
165-
165+
CLAUDE.md

src/django_routines/management/commands/routine.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class Command(TyperCommand, rich_markup_mode="rich"):
110110
_routine_options: t.Dict[str, t.Any] = {}
111111
_previous_command: t.Optional[RCommand] = None
112112

113+
_results: t.List[t.Any] = []
114+
113115
@property
114116
def routine(self) -> t.Optional[Routine]:
115117
"""
@@ -149,6 +151,7 @@ def init(
149151
] = manage_script,
150152
verbosity: Verbosity = verbosity,
151153
):
154+
self._results = []
152155
self._routine_options = ctx.params.copy()
153156
self.verbosity = verbosity
154157
self._pass_verbosity = (
@@ -169,7 +172,7 @@ def finished(self, results: t.List[t.Any]):
169172
import_string(self.routine.finalize)
170173
if isinstance(self.routine.finalize, str)
171174
else self.routine.finalize
172-
)(self.routine, results)
175+
)(self.routine, self._results)
173176

174177
def _run_routine(
175178
self,
@@ -302,6 +305,7 @@ def _call_command(
302305
if self.verbosity > 0:
303306
self.secho(command.command_str, fg="cyan")
304307
command.result = call_command(cmd, *command.command_args, **options)
308+
self._results.append(command.result)
305309
if command.command_name == "makemigrations":
306310
importlib.invalidate_caches()
307311
if command.post_hook:
@@ -387,6 +391,7 @@ def _subprocess(
387391
self.secho(" ".join(args), fg="cyan")
388392

389393
command.result = subprocess.run(args, env=os.environ.copy())
394+
self._results.append(command.result)
390395
if command.result.returncode > 0:
391396
raise CommandError(
392397
_(

tests/django_routines_tests/management/commands/track.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ def handle(self, *args, **options) -> t.Optional[str]:
3434
track_file.write_text(json.dumps(track, indent=4))
3535
if options["raise"]:
3636
raise TestError("Kill the op.")
37+
return str(options["id"])

tests/test_core.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ def test_command(self, no_color=True, verbosity=None, subprocess=False):
129129
expected = [3, 4, 1]
130130
sys_expected = ["sys 1", "sys 2"]
131131
if verbosity is None or verbosity > 0:
132+
expected_output = []
133+
for exp in expected:
134+
if not subprocess:
135+
expected_output.append(f"track {exp}")
136+
expected_output.append(str(exp))
137+
for exp in sys_expected:
138+
expected_output.append(f"system_cmd.py {exp}")
132139
for line, exp in zip(
133140
self.lines(out.getvalue(), no_color=no_color),
134-
[
135-
*[f"track {exp}" for exp in expected],
136-
*[f"system_cmd.py {exp}" for exp in sys_expected],
137-
],
141+
expected_output,
138142
):
139-
self.assertTrue(exp in line)
143+
self.assertTrue(exp in line, f"{exp} not in {line}")
140144
else:
141145
self.assertTrue("track" not in out.getvalue().strip())
142146
self.assertTrue("system_cmd" not in out.getvalue().strip())
@@ -172,12 +176,16 @@ def test_command(self, no_color=True, verbosity=None, subprocess=False):
172176
call_command(*command, "--all", stdout=out)
173177
expected = [2, 0, 3, 4, 1, 5]
174178
if verbosity is None or verbosity > 0:
179+
expected_output = []
180+
for exp in expected:
181+
if not subprocess:
182+
expected_output.append(f"track {exp}")
183+
expected_output.append(str(exp))
184+
for exp in sys_expected:
185+
expected_output.append(f"system_cmd.py {exp}")
175186
for line, exp in zip(
176187
self.lines(out.getvalue(), no_color=no_color),
177-
[
178-
*[f"track {exp}" for exp in expected],
179-
*[f"system_cmd.py {exp}" for exp in sys_expected],
180-
],
188+
expected_output,
181189
):
182190
self.assertTrue(exp in line)
183191
else:
@@ -222,12 +230,16 @@ def test_command(self, no_color=True, verbosity=None, subprocess=False):
222230
call_command(*command, "--demo", stdout=out)
223231
expected = [2, 3, 4, 1, 5]
224232
if verbosity is None or verbosity > 0:
233+
expected_output = []
234+
for exp in expected:
235+
if not subprocess:
236+
expected_output.append(f"track {exp}")
237+
expected_output.append(str(exp))
238+
for exp in sys_expected:
239+
expected_output.append(f"system_cmd.py {exp}")
225240
for line, exp in zip(
226241
self.lines(out.getvalue(), no_color=no_color),
227-
[
228-
*[f"track {exp}" for exp in expected],
229-
*[f"system_cmd.py {exp}" for exp in sys_expected],
230-
],
242+
expected_output,
231243
):
232244
self.assertTrue(exp in line)
233245
else:
@@ -265,12 +277,16 @@ def test_command(self, no_color=True, verbosity=None, subprocess=False):
265277
call_command(*command, "--demo", "--import", stdout=out)
266278
expected = [2, 0, 3, 4, 1, 5]
267279
if verbosity is None or verbosity > 0:
280+
expected_output = []
281+
for exp in expected:
282+
if not subprocess:
283+
expected_output.append(f"track {exp}")
284+
expected_output.append(str(exp))
285+
for exp in sys_expected:
286+
expected_output.append(f"system_cmd.py {exp}")
268287
for line, exp in zip(
269288
self.lines(out.getvalue(), no_color=no_color),
270-
[
271-
*[f"track {exp}" for exp in expected],
272-
*[f"system_cmd.py {exp}" for exp in sys_expected],
273-
],
289+
expected_output,
274290
):
275291
self.assertTrue(exp in line)
276292
else:
@@ -315,12 +331,16 @@ def test_command(self, no_color=True, verbosity=None, subprocess=False):
315331
call_command(*command, "--import", stdout=out)
316332
expected = [2, 0, 3, 4, 1]
317333
if verbosity is None or verbosity > 0:
334+
expected_output = []
335+
for exp in expected:
336+
if not subprocess:
337+
expected_output.append(f"track {exp}")
338+
expected_output.append(str(exp))
339+
for exp in sys_expected:
340+
expected_output.append(f"system_cmd.py {exp}")
318341
for line, exp in zip(
319342
self.lines(out.getvalue(), no_color=no_color),
320-
[
321-
*[f"track {exp}" for exp in expected],
322-
*[f"system_cmd.py {exp}" for exp in sys_expected],
323-
],
343+
expected_output,
324344
):
325345
self.assertTrue(exp in line)
326346
else:

0 commit comments

Comments
 (0)