This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class substitution for state #41
- Loading branch information
Showing
7 changed files
with
107 additions
and
10 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
Empty file.
File renamed without changes.
File renamed without changes.
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,67 @@ | ||
from django.db import models | ||
from django.test import TestCase | ||
from django_fsm import FSMField, transition | ||
|
||
|
||
class Insect(models.Model): | ||
class STATE: | ||
CATERPILLAR = 'CTR' | ||
BUTTERFLY = 'BTF' | ||
|
||
STATE_CHOICES = ((STATE.CATERPILLAR, 'Caterpillar', 'Caterpillar'), | ||
(STATE.BUTTERFLY, 'Butterfly', 'Butterfly')) | ||
|
||
state = FSMField(default=STATE.CATERPILLAR, state_choices=STATE_CHOICES) | ||
|
||
@transition(field=state, source=STATE.CATERPILLAR, target=STATE.BUTTERFLY) | ||
def cocoon(self): | ||
pass | ||
|
||
def fly(self): | ||
raise NotImplementedError | ||
|
||
def crawl(self): | ||
raise NotImplementedError | ||
|
||
class Meta: | ||
app_label = 'testapp' | ||
|
||
|
||
class Caterpillar(Insect): | ||
def crawl(self): | ||
""" | ||
Do crawl | ||
""" | ||
|
||
class Meta: | ||
app_label = 'testapp' | ||
proxy = True | ||
|
||
|
||
class Butterfly(Insect): | ||
def fly(self): | ||
""" | ||
Do fly | ||
""" | ||
|
||
class Meta: | ||
app_label = 'testapp' | ||
proxy = True | ||
|
||
|
||
class TestStateProxy(TestCase): | ||
def test_initial_proxy_set_succeed(self): | ||
insect = Insect() | ||
self.assertTrue(isinstance(insect, Caterpillar)) | ||
|
||
def test_transition_proxy_set_succeed(self): | ||
insect = Insect() | ||
insect.cocoon() | ||
self.assertTrue(isinstance(insect, Butterfly)) | ||
|
||
def test_load_proxy_set(self): | ||
Insect.objects.create(state=Insect.STATE.CATERPILLAR) | ||
Insect.objects.create(state=Insect.STATE.BUTTERFLY) | ||
|
||
insects = Insect.objects.all() | ||
self.assertEqual(set([Caterpillar, Butterfly]), set(insect.__class__ for insect in insects)) |