|
| 1 | +from behave import given, then, when |
| 2 | +from features.test_helpers import get_current_scenario_context |
| 3 | + |
| 4 | +from archipy.models.dtos.base_dtos import BaseDTO, FieldStr |
| 5 | +from archipy.models.dtos.pagination_dto import PaginationDTO |
| 6 | + |
| 7 | + |
| 8 | +@given('a DTO subclass with fields "name" and "email"') |
| 9 | +def step_given_dto_subclass(context): |
| 10 | + scenario_context = get_current_scenario_context(context) |
| 11 | + |
| 12 | + class SampleDTO(BaseDTO): |
| 13 | + name: str |
| 14 | + email: str |
| 15 | + |
| 16 | + scenario_context.store("dto_class", SampleDTO) |
| 17 | + |
| 18 | + |
| 19 | +@when("I access the field names as class attributes") |
| 20 | +def step_when_access_field_names(context): |
| 21 | + scenario_context = get_current_scenario_context(context) |
| 22 | + dto_class = scenario_context.get("dto_class") |
| 23 | + scenario_context.store("name_attr", dto_class.name) |
| 24 | + scenario_context.store("email_attr", dto_class.email) |
| 25 | + |
| 26 | + |
| 27 | +@then("each attribute should be a FieldStr instance") |
| 28 | +def step_then_attributes_are_fieldstr(context): |
| 29 | + scenario_context = get_current_scenario_context(context) |
| 30 | + assert isinstance(scenario_context.get("name_attr"), FieldStr) |
| 31 | + assert isinstance(scenario_context.get("email_attr"), FieldStr) |
| 32 | + |
| 33 | + |
| 34 | +@then("each attribute value should match its field name") |
| 35 | +def step_then_values_match_field_names(context): |
| 36 | + scenario_context = get_current_scenario_context(context) |
| 37 | + assert scenario_context.get("name_attr") == "name" |
| 38 | + assert scenario_context.get("email_attr") == "email" |
| 39 | + |
| 40 | + |
| 41 | +@when('I create an instance with name "{name}" and email "{email}"') |
| 42 | +def step_when_create_instance(context, name, email): |
| 43 | + scenario_context = get_current_scenario_context(context) |
| 44 | + dto_class = scenario_context.get("dto_class") |
| 45 | + instance = dto_class(name=name, email=email) |
| 46 | + scenario_context.store("instance", instance) |
| 47 | + |
| 48 | + |
| 49 | +@then('instance attribute "{attr}" should return "{expected}"') |
| 50 | +def step_then_instance_attr_returns(context, attr, expected): |
| 51 | + scenario_context = get_current_scenario_context(context) |
| 52 | + instance = scenario_context.get("instance") |
| 53 | + assert getattr(instance, attr) == expected |
| 54 | + |
| 55 | + |
| 56 | +@then('class attribute "{attr}" should still return FieldStr "{expected}"') |
| 57 | +def step_then_class_attr_still_fieldstr(context, attr, expected): |
| 58 | + scenario_context = get_current_scenario_context(context) |
| 59 | + dto_class = scenario_context.get("dto_class") |
| 60 | + class_attr = getattr(dto_class, attr) |
| 61 | + assert isinstance(class_attr, FieldStr) |
| 62 | + assert class_attr == expected |
| 63 | + |
| 64 | + |
| 65 | +@when("I use a FieldStr class attribute as a dictionary key") |
| 66 | +def step_when_use_fieldstr_as_dict_key(context): |
| 67 | + scenario_context = get_current_scenario_context(context) |
| 68 | + dto_class = scenario_context.get("dto_class") |
| 69 | + test_dict = {dto_class.name: "Alice", dto_class.email: "alice@example.com"} |
| 70 | + scenario_context.store("test_dict", test_dict) |
| 71 | + |
| 72 | + |
| 73 | +@then("the dictionary should be accessible with the equivalent plain string") |
| 74 | +def step_then_dict_accessible_with_plain_string(context): |
| 75 | + scenario_context = get_current_scenario_context(context) |
| 76 | + test_dict = scenario_context.get("test_dict") |
| 77 | + assert test_dict["name"] == "Alice" |
| 78 | + assert test_dict["email"] == "alice@example.com" |
| 79 | + |
| 80 | + |
| 81 | +@when("I compare the FieldStr class attribute to a plain string") |
| 82 | +def step_when_compare_fieldstr(context): |
| 83 | + scenario_context = get_current_scenario_context(context) |
| 84 | + dto_class = scenario_context.get("dto_class") |
| 85 | + scenario_context.store("match_result", dto_class.name == "name") |
| 86 | + scenario_context.store("no_match_result", dto_class.name == "other") |
| 87 | + |
| 88 | + |
| 89 | +@then("the comparison should return true for matching strings") |
| 90 | +def step_then_comparison_true(context): |
| 91 | + scenario_context = get_current_scenario_context(context) |
| 92 | + assert scenario_context.get("match_result") is True |
| 93 | + |
| 94 | + |
| 95 | +@then("the comparison should return false for non-matching strings") |
| 96 | +def step_then_comparison_false(context): |
| 97 | + scenario_context = get_current_scenario_context(context) |
| 98 | + assert scenario_context.get("no_match_result") is False |
| 99 | + |
| 100 | + |
| 101 | +@given('a parent DTO with field "base_field" and a child DTO with field "child_field"') |
| 102 | +def step_given_parent_child_dto(context): |
| 103 | + scenario_context = get_current_scenario_context(context) |
| 104 | + |
| 105 | + class ParentDTO(BaseDTO): |
| 106 | + base_field: str |
| 107 | + |
| 108 | + class ChildDTO(ParentDTO): |
| 109 | + child_field: str |
| 110 | + |
| 111 | + scenario_context.store("parent_class", ParentDTO) |
| 112 | + scenario_context.store("child_class", ChildDTO) |
| 113 | + |
| 114 | + |
| 115 | +@when("I access field names on the child class") |
| 116 | +def step_when_access_child_fields(context): |
| 117 | + pass |
| 118 | + |
| 119 | + |
| 120 | +@then('the child should have FieldStr for "{field_name}"') |
| 121 | +def step_then_child_has_fieldstr(context, field_name): |
| 122 | + scenario_context = get_current_scenario_context(context) |
| 123 | + child_class = scenario_context.get("child_class") |
| 124 | + attr = getattr(child_class, field_name) |
| 125 | + assert isinstance(attr, FieldStr) |
| 126 | + assert attr == field_name |
| 127 | + |
| 128 | + |
| 129 | +@given("the PaginationDTO class") |
| 130 | +def step_given_pagination_dto(context): |
| 131 | + scenario_context = get_current_scenario_context(context) |
| 132 | + scenario_context.store("pagination_class", PaginationDTO) |
| 133 | + |
| 134 | + |
| 135 | +@when("I access its field name class attributes") |
| 136 | +def step_when_access_pagination_fields(context): |
| 137 | + pass |
| 138 | + |
| 139 | + |
| 140 | +@then('"{field_name}" should be a FieldStr with value "{expected}"') |
| 141 | +def step_then_field_is_fieldstr_with_value(context, field_name, expected): |
| 142 | + scenario_context = get_current_scenario_context(context) |
| 143 | + pagination_class = scenario_context.get("pagination_class") |
| 144 | + attr = getattr(pagination_class, field_name) |
| 145 | + assert isinstance(attr, FieldStr), f"Expected FieldStr but got {type(attr).__name__}" |
| 146 | + assert attr == expected, f"Expected '{expected}' but got '{attr}'" |
0 commit comments