Skip to content

Commit

Permalink
feat(api): query command builder (#3085)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevski committed Oct 11, 2022
1 parent a187aec commit cc7f90b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 82 deletions.
84 changes: 3 additions & 81 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion renku/command/command_builder/command.py
Expand Up @@ -22,7 +22,7 @@
import threading
from collections import defaultdict
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any, Callable, Dict, List, Optional, Type, Union

import inject

Expand Down Expand Up @@ -270,6 +270,23 @@ def finalized(self) -> bool:
return self._builder.finalized
return self._finalized

def any_builder_is_instance_of(self, cls: Type) -> bool:
"""Check if any 'chained' command builder is an instance of a specific command builder class."""
if isinstance(self, cls):
return True
elif "_builder" in self.__dict__:
return self._builder.any_builder_is_instance_of(cls)
else:
return False

@property
def will_write_to_database(self) -> bool:
"""Will running the command write anything to the metadata store."""
try:
return self._write
except AttributeError:
return False

@check_finalized
def add_injection_pre_hook(self, order: int, hook: Callable):
"""Add a pre-execution hook for dependency injection.
Expand Down
43 changes: 43 additions & 0 deletions tests/command_builder/test_command.py
@@ -0,0 +1,43 @@
from typing import Type

import pytest

from renku.command.command_builder import Command
from renku.command.command_builder.database import DatabaseCommand
from renku.command.command_builder.repo import Commit, Isolation, RequireClean


@pytest.mark.parametrize(
"input,cls,expected",
[
(Command(), Command, True),
(Command(), DatabaseCommand, False),
(Command().with_git_isolation(), Isolation, True),
(Command().with_git_isolation(), RequireClean, False),
(Command().with_git_isolation().with_commit(), Command, True),
(Command().with_git_isolation().with_commit(), Isolation, True),
(Command().with_git_isolation().with_commit(), Commit, True),
(Command().with_git_isolation().with_commit(), DatabaseCommand, False),
(Command().with_git_isolation().with_commit().with_database(), DatabaseCommand, True),
],
)
def test_any_builder_is_instance_of(input: Command, cls: Type, expected: bool):
assert input.any_builder_is_instance_of(cls) == expected


@pytest.mark.parametrize(
"input,expected",
[
(Command(), False),
(Command().with_git_isolation(), False),
(Command().with_git_isolation().with_commit(), False),
(Command().with_git_isolation().with_commit().with_database(write=True), True),
(Command().with_git_isolation().with_commit().with_database(write=False), False),
(Command().with_database(write=True), True),
(Command().with_database(write=False), False),
(Command().with_git_isolation().with_database(write=True).with_commit(), True),
(Command().with_git_isolation().with_database(write=False).with_commit(), False),
],
)
def test_will_write_to_database(input: Command, expected: bool):
assert input.will_write_to_database == expected

0 comments on commit cc7f90b

Please sign in to comment.