Skip to content

Commit cc7f90b

Browse files
authored
feat(api): query command builder (#3085)
1 parent a187aec commit cc7f90b

File tree

3 files changed

+64
-82
lines changed

3 files changed

+64
-82
lines changed

poetry.lock

Lines changed: 3 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

renku/command/command_builder/command.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import threading
2323
from collections import defaultdict
2424
from pathlib import Path
25-
from typing import Any, Callable, Dict, List, Optional, Union
25+
from typing import Any, Callable, Dict, List, Optional, Type, Union
2626

2727
import inject
2828

@@ -270,6 +270,23 @@ def finalized(self) -> bool:
270270
return self._builder.finalized
271271
return self._finalized
272272

273+
def any_builder_is_instance_of(self, cls: Type) -> bool:
274+
"""Check if any 'chained' command builder is an instance of a specific command builder class."""
275+
if isinstance(self, cls):
276+
return True
277+
elif "_builder" in self.__dict__:
278+
return self._builder.any_builder_is_instance_of(cls)
279+
else:
280+
return False
281+
282+
@property
283+
def will_write_to_database(self) -> bool:
284+
"""Will running the command write anything to the metadata store."""
285+
try:
286+
return self._write
287+
except AttributeError:
288+
return False
289+
273290
@check_finalized
274291
def add_injection_pre_hook(self, order: int, hook: Callable):
275292
"""Add a pre-execution hook for dependency injection.

tests/command_builder/test_command.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import Type
2+
3+
import pytest
4+
5+
from renku.command.command_builder import Command
6+
from renku.command.command_builder.database import DatabaseCommand
7+
from renku.command.command_builder.repo import Commit, Isolation, RequireClean
8+
9+
10+
@pytest.mark.parametrize(
11+
"input,cls,expected",
12+
[
13+
(Command(), Command, True),
14+
(Command(), DatabaseCommand, False),
15+
(Command().with_git_isolation(), Isolation, True),
16+
(Command().with_git_isolation(), RequireClean, False),
17+
(Command().with_git_isolation().with_commit(), Command, True),
18+
(Command().with_git_isolation().with_commit(), Isolation, True),
19+
(Command().with_git_isolation().with_commit(), Commit, True),
20+
(Command().with_git_isolation().with_commit(), DatabaseCommand, False),
21+
(Command().with_git_isolation().with_commit().with_database(), DatabaseCommand, True),
22+
],
23+
)
24+
def test_any_builder_is_instance_of(input: Command, cls: Type, expected: bool):
25+
assert input.any_builder_is_instance_of(cls) == expected
26+
27+
28+
@pytest.mark.parametrize(
29+
"input,expected",
30+
[
31+
(Command(), False),
32+
(Command().with_git_isolation(), False),
33+
(Command().with_git_isolation().with_commit(), False),
34+
(Command().with_git_isolation().with_commit().with_database(write=True), True),
35+
(Command().with_git_isolation().with_commit().with_database(write=False), False),
36+
(Command().with_database(write=True), True),
37+
(Command().with_database(write=False), False),
38+
(Command().with_git_isolation().with_database(write=True).with_commit(), True),
39+
(Command().with_git_isolation().with_database(write=False).with_commit(), False),
40+
],
41+
)
42+
def test_will_write_to_database(input: Command, expected: bool):
43+
assert input.will_write_to_database == expected

0 commit comments

Comments
 (0)