|
| 1 | +"""Unit tests for UpdateRoleInput.to_updater().""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import uuid |
| 6 | + |
| 7 | +from ai.backend.common.data.permission.types import RoleStatus |
| 8 | +from ai.backend.manager.api.gql.rbac.types.role import RoleStatusGQL, UpdateRoleInput |
| 9 | +from ai.backend.manager.repositories.permission_controller.updaters import RoleUpdaterSpec |
| 10 | +from ai.backend.manager.types import _TriStateEnum |
| 11 | + |
| 12 | + |
| 13 | +class TestUpdateRoleInputToUpdater: |
| 14 | + """Tests for UpdateRoleInput.to_updater() method.""" |
| 15 | + |
| 16 | + def test_description_with_none_creates_nullify_tristate(self) -> None: |
| 17 | + """UpdateRoleInput with description=None → TriState.nullify() in updater spec.""" |
| 18 | + role_input = UpdateRoleInput( |
| 19 | + id=uuid.uuid4(), |
| 20 | + description=None, |
| 21 | + ) |
| 22 | + |
| 23 | + updater = role_input.to_updater() |
| 24 | + spec = updater.spec |
| 25 | + assert isinstance(spec, RoleUpdaterSpec) |
| 26 | + |
| 27 | + assert spec.description._state == _TriStateEnum.NULLIFY |
| 28 | + |
| 29 | + def test_description_with_unset_creates_nop_tristate(self) -> None: |
| 30 | + """UpdateRoleInput with description=UNSET → TriState.nop() in updater spec.""" |
| 31 | + role_input = UpdateRoleInput( |
| 32 | + id=uuid.uuid4(), |
| 33 | + # description is omitted (UNSET by default) |
| 34 | + ) |
| 35 | + |
| 36 | + updater = role_input.to_updater() |
| 37 | + spec = updater.spec |
| 38 | + assert isinstance(spec, RoleUpdaterSpec) |
| 39 | + |
| 40 | + assert spec.description._state == _TriStateEnum.NOP |
| 41 | + |
| 42 | + def test_description_with_string_creates_update_tristate(self) -> None: |
| 43 | + """UpdateRoleInput with description="text" → TriState.update("text") in updater spec.""" |
| 44 | + role_input = UpdateRoleInput( |
| 45 | + id=uuid.uuid4(), |
| 46 | + description="some text", |
| 47 | + ) |
| 48 | + |
| 49 | + updater = role_input.to_updater() |
| 50 | + spec = updater.spec |
| 51 | + assert isinstance(spec, RoleUpdaterSpec) |
| 52 | + |
| 53 | + assert spec.description._state == _TriStateEnum.UPDATE |
| 54 | + assert spec.description.value() == "some text" |
| 55 | + |
| 56 | + def test_name_with_unset_creates_nop_optionalstate(self) -> None: |
| 57 | + """UpdateRoleInput with name=UNSET → OptionalState.nop() in updater spec.""" |
| 58 | + role_input = UpdateRoleInput( |
| 59 | + id=uuid.uuid4(), |
| 60 | + # name is omitted (UNSET by default) |
| 61 | + ) |
| 62 | + |
| 63 | + updater = role_input.to_updater() |
| 64 | + spec = updater.spec |
| 65 | + assert isinstance(spec, RoleUpdaterSpec) |
| 66 | + |
| 67 | + assert spec.name._state == _TriStateEnum.NOP |
| 68 | + |
| 69 | + def test_name_with_string_creates_update_optionalstate(self) -> None: |
| 70 | + """UpdateRoleInput with name="new name" → OptionalState.update("new name") in updater spec.""" |
| 71 | + role_input = UpdateRoleInput( |
| 72 | + id=uuid.uuid4(), |
| 73 | + name="new name", |
| 74 | + ) |
| 75 | + |
| 76 | + updater = role_input.to_updater() |
| 77 | + spec = updater.spec |
| 78 | + assert isinstance(spec, RoleUpdaterSpec) |
| 79 | + |
| 80 | + assert spec.name._state == _TriStateEnum.UPDATE |
| 81 | + assert spec.name.value() == "new name" |
| 82 | + |
| 83 | + def test_status_with_unset_creates_nop_optionalstate(self) -> None: |
| 84 | + """UpdateRoleInput with status=UNSET → OptionalState.nop() in updater spec.""" |
| 85 | + role_input = UpdateRoleInput( |
| 86 | + id=uuid.uuid4(), |
| 87 | + # status is omitted (UNSET by default) |
| 88 | + ) |
| 89 | + |
| 90 | + updater = role_input.to_updater() |
| 91 | + spec = updater.spec |
| 92 | + assert isinstance(spec, RoleUpdaterSpec) |
| 93 | + |
| 94 | + assert spec.status._state == _TriStateEnum.NOP |
| 95 | + |
| 96 | + def test_status_with_active_creates_update_optionalstate(self) -> None: |
| 97 | + """UpdateRoleInput with status=RoleStatusGQL.ACTIVE → OptionalState.update(RoleStatus.ACTIVE).""" |
| 98 | + role_input = UpdateRoleInput( |
| 99 | + id=uuid.uuid4(), |
| 100 | + status=RoleStatusGQL.ACTIVE, |
| 101 | + ) |
| 102 | + |
| 103 | + updater = role_input.to_updater() |
| 104 | + spec = updater.spec |
| 105 | + assert isinstance(spec, RoleUpdaterSpec) |
| 106 | + |
| 107 | + assert spec.status._state == _TriStateEnum.UPDATE |
| 108 | + assert spec.status.value() == RoleStatus.ACTIVE |
| 109 | + |
| 110 | + def test_status_with_inactive_creates_update_optionalstate(self) -> None: |
| 111 | + """UpdateRoleInput with status=RoleStatusGQL.INACTIVE → OptionalState.update(RoleStatus.INACTIVE).""" |
| 112 | + role_input = UpdateRoleInput( |
| 113 | + id=uuid.uuid4(), |
| 114 | + status=RoleStatusGQL.INACTIVE, |
| 115 | + ) |
| 116 | + |
| 117 | + updater = role_input.to_updater() |
| 118 | + spec = updater.spec |
| 119 | + assert isinstance(spec, RoleUpdaterSpec) |
| 120 | + |
| 121 | + assert spec.status._state == _TriStateEnum.UPDATE |
| 122 | + assert spec.status.value() == RoleStatus.INACTIVE |
| 123 | + |
| 124 | + def test_multiple_fields_with_mixed_states(self) -> None: |
| 125 | + """Test multiple fields with different states.""" |
| 126 | + role_id = uuid.uuid4() |
| 127 | + role_input = UpdateRoleInput( |
| 128 | + id=role_id, |
| 129 | + name="updated role", |
| 130 | + description=None, |
| 131 | + status=RoleStatusGQL.ACTIVE, |
| 132 | + ) |
| 133 | + |
| 134 | + updater = role_input.to_updater() |
| 135 | + spec = updater.spec |
| 136 | + assert isinstance(spec, RoleUpdaterSpec) |
| 137 | + |
| 138 | + assert updater.pk_value == role_id |
| 139 | + assert spec.name._state == _TriStateEnum.UPDATE |
| 140 | + assert spec.name.value() == "updated role" |
| 141 | + assert spec.description._state == _TriStateEnum.NULLIFY |
| 142 | + assert spec.status._state == _TriStateEnum.UPDATE |
| 143 | + assert spec.status.value() == RoleStatus.ACTIVE |
| 144 | + |
| 145 | + def test_all_fields_unset_creates_all_nop(self) -> None: |
| 146 | + """When all fields are UNSET, all spec fields should be nop.""" |
| 147 | + role_input = UpdateRoleInput( |
| 148 | + id=uuid.uuid4(), |
| 149 | + # all fields omitted (UNSET by default) |
| 150 | + ) |
| 151 | + |
| 152 | + updater = role_input.to_updater() |
| 153 | + spec = updater.spec |
| 154 | + assert isinstance(spec, RoleUpdaterSpec) |
| 155 | + |
| 156 | + assert spec.name._state == _TriStateEnum.NOP |
| 157 | + assert spec.description._state == _TriStateEnum.NOP |
| 158 | + assert spec.status._state == _TriStateEnum.NOP |
| 159 | + |
| 160 | + def test_updater_pk_value_matches_input_id(self) -> None: |
| 161 | + """Updater.pk_value should match the input id.""" |
| 162 | + role_id = uuid.uuid4() |
| 163 | + role_input = UpdateRoleInput(id=role_id) |
| 164 | + |
| 165 | + updater = role_input.to_updater() |
| 166 | + |
| 167 | + assert updater.pk_value == role_id |
0 commit comments