Skip to content

Commit

Permalink
Extend describe decorator with component and layer
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrostriletskyi committed Sep 13, 2024
1 parent b2d4e26 commit 5a4998c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .project-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.7
0.0.8
10 changes: 5 additions & 5 deletions fixtures/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)


@describe(object='Accounts Service', domain='accounts')
@describe(domain='accounts', component='accounts', layer='service')
class TestAccountsService:

def test_transfer_money_with_insufficient_balance(self):
Expand All @@ -30,7 +30,7 @@ def test_transfer_money_with_sufficient_balance(self):
pass


@describe(object='Accounts Service', domain='accounts')
@describe(domain='accounts', component='accounts', layer='service')
def test_transfer_money_to_non_existing_receiver_account():
with when('Receiver account does not exist'):
pass
Expand All @@ -42,7 +42,7 @@ def test_transfer_money_to_non_existing_receiver_account():
pass


@describe(object='Investments Service', domain='investments')
@describe(domain='investments', component='investments', layer='service')
class TestInvestmentsService:

def test_invest_money_into_stocks(self):
Expand All @@ -60,7 +60,7 @@ def test_invest_money_into_crypto(self):
pass


@describe(object='Investments Service', domain='investments')
@describe(domain='investments', component='investments', layer='service')
def test_invest_into_non_existing_stocks():
with when('Stock to buy does not exist'):
pass
Expand All @@ -83,6 +83,6 @@ def test_invest_into_non_existing_crypto():
pass


@describe(object='Investments Service', domain='investments')
@describe(domain='investments', component='investments', layer='service')
def test_sum():
assert 4 == 2 + 2
2 changes: 1 addition & 1 deletion intentions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class expect(AbstractIntention):
"""


def describe(object: str, domain: str) -> Callable[[Callable[..., T]], Callable[..., T]]: # noqa: ARG001
def describe(domain: str, component: str, layer: str) -> Callable[[Callable[..., T]], Callable[..., T]]: # noqa: ARG001
def decorator(func: [..., T]) -> Callable[..., T]:
def wrapper(*args: tuple[Any, ...], **kwargs: dict[str, Any]) -> T:
result = func(*args, **kwargs)
Expand Down
10 changes: 6 additions & 4 deletions intentions/render/ast_.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ def get_describe(self, decorators: [ast.Call]) -> Optional[Describe]:
if decorator.func.id != 'describe':
continue

assert decorator.keywords[0].arg == 'object' # noqa: S101
assert decorator.keywords[1].arg == 'domain' # noqa: S101
assert decorator.keywords[0].arg == 'domain' # noqa: S101
assert decorator.keywords[1].arg == 'component' # noqa: S101
assert decorator.keywords[2].arg == 'layer' # noqa: S101

return Describe(
object=decorator.keywords[0].value.value,
domain=decorator.keywords[1].value.value,
domain=decorator.keywords[0].value.value,
component=decorator.keywords[1].value.value,
layer=decorator.keywords[2].value.value,
)

return None
3 changes: 2 additions & 1 deletion intentions/render/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

@dataclass
class Describe:
object: str # noqa: A003
domain: str
component: str
layer: str


@dataclass
Expand Down
9 changes: 6 additions & 3 deletions intentions/render/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ def collect_test_cases(storage: dict, file: Path) -> None:
if describe.domain not in storage:
storage[describe.domain] = {}

if describe.object not in storage[describe.domain]:
storage[describe.domain][describe.object] = []
if describe.component not in storage[describe.domain]:
storage[describe.domain][describe.component] = {}

if describe.layer not in storage[describe.domain][describe.component]:
storage[describe.domain][describe.component][describe.layer] = []

test_function = TestCase(
function_name=function_node.name,
Expand All @@ -105,7 +108,7 @@ def collect_test_cases(storage: dict, file: Path) -> None:
)

test_case_as_dict = asdict(test_function)
storage[describe.domain][describe.object].append(test_case_as_dict)
storage[describe.domain][describe.component][describe.layer].append(test_case_as_dict)


def create_intentions_json(directory: str) -> None:
Expand Down
18 changes: 9 additions & 9 deletions tests/render/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def test_create_intentions_json(self, remove_intentions_json) -> None:
assert intentions_json['investments'] is not None

with expect('Each intentions JSON domain consist of its objects'):
assert intentions_json['accounts']['Accounts Service']
assert intentions_json['investments']['Investments Service']
assert intentions_json['accounts']['accounts']['service']
assert intentions_json['investments']['investments']['service']

def test_create_intentions_json_accounts_service(self, remove_intentions_json) -> None:
with when('Tests folder with tests for accounts service exist'):
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_create_intentions_json_accounts_service(self, remove_intentions_json) -
]
}

assert expected_test_case in intentions_json['accounts']['Accounts Service']
assert expected_test_case in intentions_json['accounts']['accounts']['service']

with expect('Test transfer money with sufficient balance test case is in accounts service test cases'):
expected_test_case = {
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_create_intentions_json_accounts_service(self, remove_intentions_json) -
]
}

assert expected_test_case in intentions_json['accounts']['Accounts Service']
assert expected_test_case in intentions_json['accounts']['accounts']['service']

with expect('Test transfer money to non existing receiver account test case is in accounts service test cases'):
expected_test_case = {
Expand Down Expand Up @@ -120,7 +120,7 @@ def test_create_intentions_json_accounts_service(self, remove_intentions_json) -
]
}

assert expected_test_case in intentions_json['accounts']['Accounts Service']
assert expected_test_case in intentions_json['accounts']['accounts']['service']

def test_create_intentions_json_investments_service(self, remove_intentions_json) -> None:
with when('Tests folder with tests for investments service exist'):
Expand Down Expand Up @@ -153,7 +153,7 @@ def test_create_intentions_json_investments_service(self, remove_intentions_json
]
}

assert expected_test_case in intentions_json['investments']['Investments Service']
assert expected_test_case in intentions_json['investments']['investments']['service']

with expect('Invest money into crypto test case is in investments service test cases'):
expected_test_case = {
Expand All @@ -176,7 +176,7 @@ def test_create_intentions_json_investments_service(self, remove_intentions_json
]
}

assert expected_test_case in intentions_json['investments']['Investments Service']
assert expected_test_case in intentions_json['investments']['investments']['service']

with expect('Invest money into non-existing stocks test case is in investments service test cases'):
expected_test_case = {
Expand Down Expand Up @@ -204,7 +204,7 @@ def test_create_intentions_json_investments_service(self, remove_intentions_json
]
}

assert expected_test_case in intentions_json['investments']['Investments Service']
assert expected_test_case in intentions_json['investments']['investments']['service']

def test_create_intentions_json_ignore_test_cases_without_intentions(self, remove_intentions_json) -> None:
with when('Tests folder with described test cases without intentions'):
Expand All @@ -226,4 +226,4 @@ def test_create_intentions_json_ignore_test_cases_without_intentions(self, remov
'intentions': [],
}

assert expected_test_case not in intentions_json['investments']['Investments Service']
assert expected_test_case not in intentions_json['investments']['investments']['service']

0 comments on commit 5a4998c

Please sign in to comment.