-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
Missing documentation yet.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from travispy.entities._stateful import Stateful | ||
import pytest | ||
|
||
|
||
#=================================================================================================== | ||
# Test | ||
#=================================================================================================== | ||
class Test: | ||
|
||
@pytest.mark.parametrize( | ||
'state, created, queued, started, passed, failed, errored, canceled, ready, pending, running, finished, successful, unsuccessful, color, green, yellow, red', [ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
(Stateful.CREATED, True, False, False, False, False, False, False, False, True, False, False, False, False, Stateful.YELLOW, False, True, False), | ||
(Stateful.QUEUED, True, True, False, False, False, False, False, False, True, False, False, False, False, Stateful.YELLOW, False, True, False), | ||
(Stateful.STARTED, True, True, True, False, False, False, False, False, True, True, False, False, False, Stateful.YELLOW, False, True, False), | ||
(Stateful.PASSED, True, True, True, True, False, False, False, False, False, False, True, True, False, Stateful.GREEN, True, False, False), | ||
(Stateful.FAILED, True, True, True, False, True, False, False, False, False, False, True, False, True, Stateful.RED, False, False, True), | ||
(Stateful.ERRORED, True, True, True, False, False, True, False, False, False, False, True, False, True, Stateful.RED, False, False, True), | ||
(Stateful.CANCELED, True, True, True, False, False, False, True, False, False, False, True, False, True, Stateful.RED, False, False, True), | ||
(Stateful.READY, True, True, True, False, False, False, False, True, False, False, True, False, False, Stateful.GREEN, True, False, False), | ||
]) | ||
def testStateful(self, state, created, queued, started, passed, failed, errored, canceled, ready, pending, running, finished, successful, unsuccessful, color, green, yellow, red): | ||
This comment has been minimized.
Sorry, something went wrong.
nicoddemus
Contributor
|
||
stateful = Stateful(None) | ||
|
||
assert hasattr(stateful, 'state') == False | ||
with pytest.raises(AttributeError): | ||
stateful.created | ||
|
||
stateful.state = None | ||
with pytest.raises(ValueError): | ||
stateful.created | ||
|
||
stateful.state = state | ||
assert stateful.created == created | ||
assert stateful.queued == queued | ||
assert stateful.started == started | ||
assert stateful.passed == passed | ||
assert stateful.failed == failed | ||
assert stateful.errored == errored | ||
assert stateful.canceled == canceled | ||
assert stateful.ready == ready | ||
assert stateful.pending == pending | ||
assert stateful.running == running | ||
assert stateful.finished == finished | ||
assert stateful.successful == successful | ||
assert stateful.unsuccessful == unsuccessful | ||
assert stateful.color == color | ||
assert stateful.green == green | ||
assert stateful.yellow == yellow | ||
assert stateful.red == red |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,23 @@ | |
class Stateful(Entity): | ||
''' | ||
Base class for stateful entities such as :class:`.Repo`, :class:`.Build` and :class:`.Job`. | ||
:ivar str state: | ||
Current state. Possible values are: | ||
- created | ||
This comment has been minimized.
Sorry, something went wrong.
nicoddemus
Contributor
|
||
- queued | ||
- started | ||
- passed | ||
- failed | ||
- errored | ||
- canceled | ||
- ready | ||
''' | ||
|
||
|
||
__slots__ = ['state'] | ||
|
||
# States --------------------------------------------------------------------------------------- | ||
CANCELED = 'canceled' | ||
CREATED = 'created' | ||
ERRORED = 'errored' | ||
|
@@ -18,82 +33,121 @@ class Stateful(Entity): | |
QUEUED = 'queued' | ||
READY = 'ready' | ||
STARTED = 'started' | ||
|
||
STATES = [ | ||
'canceled', | ||
'created', | ||
'errored', | ||
'failed', | ||
'passed', | ||
'queued', | ||
'ready', | ||
'started', | ||
] | ||
|
||
__slots__ = [ | ||
'created?', | ||
'errored?', | ||
'failed?', | ||
'finished?', | ||
'green?', | ||
'passed?', | ||
'pending?', | ||
'queued?', | ||
'red?', | ||
'running?', | ||
'started?', | ||
'successful?', | ||
'unsuccessful?', | ||
'yellow?', | ||
'color', | ||
'state', | ||
] | ||
|
||
# Colors --------------------------------------------------------------------------------------- | ||
GREEN = 'green' | ||
YELLOW = 'yellow' | ||
RED = 'red' | ||
|
||
@property | ||
def ready(self): | ||
return self.state == self.READY | ||
def created(self): | ||
self._check_state() | ||
return hasattr(self, 'state') | ||
|
||
|
||
@property | ||
def pending(self): | ||
return self.state in [self.CREATED, self.STARTED, self.QUEUED] | ||
def queued(self): | ||
self._check_state() | ||
return self.state != self.CREATED | ||
|
||
|
||
@property | ||
def started(self): | ||
self._check_state() | ||
return self.state not in [self.CREATED, self.QUEUED] | ||
|
||
|
||
@property | ||
def queued(self): | ||
return self.state != self.CREATED | ||
def passed(self): | ||
self._check_state() | ||
return self.state == self.PASSED | ||
|
||
|
||
@property | ||
def finished(self): | ||
return not self.pending | ||
def failed(self): | ||
self._check_state() | ||
return self.state == self.FAILED | ||
|
||
|
||
@property | ||
def passed(self): | ||
return self.state == self.PASSED | ||
def errored(self): | ||
self._check_state() | ||
return self.state == self.ERRORED | ||
|
||
|
||
@property | ||
def errored(self): | ||
return self.state == self.ERRORED | ||
def canceled(self): | ||
self._check_state() | ||
return self.state == self.CANCELED | ||
|
||
|
||
@property | ||
def failed(self): | ||
return self.state == self.FAILED | ||
def ready(self): | ||
return self.state == self.READY | ||
|
||
|
||
@property | ||
def canceled(self): | ||
return self.state == self.CANCELED | ||
def pending(self): | ||
self._check_state() | ||
return self.state in [self.CREATED, self.STARTED, self.QUEUED] | ||
|
||
|
||
@property | ||
def running(self): | ||
return self.state == self.STARTED | ||
|
||
|
||
@property | ||
def finished(self): | ||
return not self.pending | ||
|
||
|
||
@property | ||
def successful(self): | ||
return self.passed | ||
|
||
|
||
@property | ||
def unsuccessful(self): | ||
return self.errored or self.failed or self.canceled | ||
|
||
|
||
@property | ||
def color(self): | ||
self._check_state() | ||
if self.passed or self.ready: | ||
return self.GREEN | ||
|
||
elif self.pending: | ||
return self.YELLOW | ||
|
||
elif self.unsuccessful: | ||
return self.RED | ||
|
||
|
||
@property | ||
def green(self): | ||
return self.color == self.GREEN | ||
|
||
|
||
@property | ||
def yellow(self): | ||
return self.color == self.YELLOW | ||
|
||
|
||
@property | ||
def red(self): | ||
return self.color == self.RED | ||
|
||
def _check_state(self): | ||
if self.state not in [ | ||
self.CANCELED, | ||
self.CREATED, | ||
self.ERRORED, | ||
self.FAILED, | ||
self.PASSED, | ||
self.QUEUED, | ||
self.READY, | ||
self.STARTED, | ||
]: | ||
raise ValueError('unknown state %s for %s' % (self.state, self.__class__.__name__)) |
Suggestion: for
True
andFalse
properties, you could replace all explicit calls for a singleset
of expected values, like so:Seems easier to maintain the state table this way. 😄