-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LITE-28075 Add abort action for deployment requests
- Loading branch information
Showing
7 changed files
with
290 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import types | ||
|
||
from connect_ext_ppr.errors import ExtensionValidationError | ||
|
||
|
||
class transition: | ||
|
||
def __init__(self, target, sources) -> None: | ||
self.target = target | ||
self.sources = sources if isinstance(sources, list) else [sources] | ||
|
||
def __call__(self, fn): | ||
def inner(*args, **kwargs): | ||
self._validate_transition(args[0]) | ||
setattr(self.instance, self.field_name, self.target) | ||
return fn(*args, **kwargs) | ||
return inner | ||
|
||
def __get__(self, instance, owner=None): | ||
return types.MethodType(self, instance) if instance is not None else self | ||
|
||
def _validate_transition(self, instance): | ||
self.instance = instance | ||
assert getattr(self.instance, 'track_field', False), ( | ||
"Must implement a `track_field` property to target the transition field of the model." | ||
) | ||
|
||
self.field_name = self.instance.track_field | ||
current_state = getattr(self.instance, self.field_name) | ||
if current_state not in self.sources: | ||
raise ExtensionValidationError.VAL_005( | ||
format_kwargs={ | ||
'source': current_state, | ||
'field_name': self.field_name, | ||
'target': self.target, | ||
'allowed': ', '.join(self.sources), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from connect.client import ClientError | ||
|
||
from connect_ext_ppr.models.models_utils import transition | ||
|
||
import pytest | ||
|
||
|
||
class Animal: | ||
|
||
def __init__(self, foo) -> None: | ||
self.foo = foo | ||
|
||
@property | ||
def track_field(self): | ||
return 'foo' | ||
|
||
@transition(target='parrot', sources=['bird']) | ||
def convert(self): | ||
... | ||
|
||
|
||
class ImproperlyConfigured: | ||
def __init__(self, foo) -> None: | ||
self.foo = foo | ||
|
||
@transition(target='parrot', sources=['bird']) | ||
def convert(self): | ||
... | ||
|
||
|
||
def test_transition_ok(): | ||
obj = Animal(foo='bird') | ||
assert obj.convert() is None | ||
|
||
|
||
def test_transition_error(): | ||
obj = Animal(foo='dog') | ||
|
||
with pytest.raises(ClientError) as ex: | ||
obj.convert() | ||
|
||
assert ex.value.message == ( | ||
"Transition not allowed: can not set foo from `dog` to 'parrot'" | ||
", allowed foo sources for 'parrot' are 'bird'." | ||
) | ||
|
||
|
||
def test_improperly_configured_error(): | ||
obj = ImproperlyConfigured(foo='bird') | ||
|
||
with pytest.raises(AssertionError) as ex: | ||
obj.convert() | ||
|
||
assert ex.value.args[0] == ( | ||
"Must implement a `track_field` property to target the transition field of the model." | ||
) |