Skip to content

Commit 34370c6

Browse files
committed
Merged in [11656] from rjsparks@nostrum.com:
Enhanced factories to simplify test writing. - Legacy-Id: 11682 Note: SVN reference [11656] has been migrated to Git commit 030ea1c
2 parents 6da2b2a + 030ea1c commit 34370c6

3 files changed

Lines changed: 54 additions & 29 deletions

File tree

ietf/doc/factories.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import factory
22

3-
from ietf.doc.models import Document, DocEvent, NewRevisionDocEvent
4-
from ietf.person.factories import PersonFactory
3+
from ietf.doc.models import Document, DocEvent, NewRevisionDocEvent, DocAlias, State
4+
5+
def draft_name_generator(type_id,group,n):
6+
return '%s-%s-%s-%s%d'%(
7+
type_id,
8+
'bogusperson',
9+
group.acronym if group else 'netherwhere',
10+
'musings',
11+
n,
12+
)
513

614
class DocumentFactory(factory.DjangoModelFactory):
715
class Meta:
@@ -10,26 +18,47 @@ class Meta:
1018
type_id = 'draft'
1119
title = factory.Faker('sentence',nb_words=6)
1220
rev = '00'
13-
group = None
21+
group = factory.SubFactory('ietf.group.factories.GroupFactory',type_id='individ')
22+
std_level_id = None
23+
intended_std_level_id = None
1424

1525
@factory.lazy_attribute_sequence
1626
def name(self, n):
17-
return '%s-%s-%s-%s%d'%(
18-
self.type_id,
19-
'bogusperson',
20-
self.group.acronym if self.group else 'netherwhere',
21-
'musings',
22-
n,
23-
)
27+
return draft_name_generator(self.type_id,self.group,n)
2428

2529
newrevisiondocevent = factory.RelatedFactory('ietf.doc.factories.NewRevisionDocEventFactory','doc')
2630

31+
alias = factory.RelatedFactory('ietf.doc.factories.DocAliasFactory','document')
32+
33+
@factory.post_generation
34+
def other_aliases(self, create, extracted, **kwargs):
35+
if create and extracted:
36+
for alias in extracted:
37+
self.docalias_set.create(name=alias)
38+
39+
@factory.post_generation
40+
def states(self, create, extracted, **kwargs):
41+
if create and extracted:
42+
for (state_type_id,state_slug) in extracted:
43+
self.set_state(State.objects.get(type_id=state_type_id,slug=state_slug))
44+
45+
class DocAliasFactory(factory.DjangoModelFactory):
46+
class Meta:
47+
model = DocAlias
48+
49+
document = factory.SubFactory('ietf.doc.factories.DocumentFactory')
50+
51+
@factory.lazy_attribute
52+
def name(self):
53+
return self.document.name
54+
55+
2756
class DocEventFactory(factory.DjangoModelFactory):
2857
class Meta:
2958
model = DocEvent
3059

3160
type = 'added_comment'
32-
by = factory.SubFactory(PersonFactory)
61+
by = factory.SubFactory('ietf.person.factories.PersonFactory')
3362
doc = factory.SubFactory(DocumentFactory)
3463
desc = factory.Faker('sentence',nb_words=6)
3564

ietf/doc/tests_ballot.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from ietf.doc.models import ( Document, State, DocEvent, BallotDocEvent,
1010
BallotPositionDocEvent, LastCallDocEvent, WriteupDocEvent, TelechatDocEvent )
1111
from ietf.doc.factories import DocumentFactory
12-
from ietf.group.factories import GroupFactory
1312
from ietf.group.models import Group, Role
1413
from ietf.name.models import BallotPositionName
1514
from ietf.iesg.models import TelechatDate
@@ -719,13 +718,11 @@ def setUp(self):
719718
class RegenerateLastCallTestCase(TestCase):
720719

721720
def test_regenerate_last_call(self):
722-
group = GroupFactory(type_id='individ')
723-
draft = DocumentFactory.create(stream_id='ietf',group=group)
724-
draft.docalias_set.create(name=draft.name) # factory should do this
725-
draft.set_state(State.objects.get(type='draft',slug='active'))
726-
draft.set_state(State.objects.get(type='draft-iesg',slug='pub-req'))
727-
draft.intended_std_level_id='ps'
728-
draft.save()
721+
draft = DocumentFactory.create(
722+
stream_id='ietf',
723+
states=[('draft','active'),('draft-iesg','pub-req')],
724+
intended_std_level_id='ps',
725+
)
729726

730727
url = urlreverse('doc_ballot_lastcall', kwargs=dict(name=draft.name))
731728
login_testing_unauthorized(self, "secretary", url)
@@ -739,15 +736,14 @@ def test_regenerate_last_call(self):
739736
self.assertTrue("Subject: Last Call" in lc_text)
740737
self.assertFalse("contains normative down" in lc_text)
741738

742-
rfc = DocumentFactory.create(stream_id='ise')
743-
rfc.docalias_set.create(name=rfc.name)
744-
rfc_alias = rfc.docalias_set.create(name='rfc6666')
745-
rfc.set_state(State.objects.get(type='draft',slug='rfc'))
746-
rfc.set_state(State.objects.get(type='draft-iesg',slug='pub'))
747-
rfc.std_level_id='inf'
748-
rfc.save()
739+
rfc = DocumentFactory.create(
740+
stream_id='ise',
741+
other_aliases=['rfc6666',],
742+
states=[('draft','rfc'),('draft-iesg','pub')],
743+
std_level_id='inf',
744+
)
749745

750-
draft.relateddocument_set.create(target=rfc_alias,relationship_id='refnorm')
746+
draft.relateddocument_set.create(target=rfc.docalias_set.get(name='rfc6666'),relationship_id='refnorm')
751747

752748
r = self.client.post(url, dict(regenerate_last_call_text="1"))
753749
self.assertEqual(r.status_code, 200)

ietf/group/factories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ class Meta:
1414
model = Role
1515

1616
group = factory.SubFactory(GroupFactory)
17-
person = factory.SubFactory('ietf.doc.factories.PersonFactory')
17+
person = factory.SubFactory('ietf.person.factories.PersonFactory')
1818
email = factory.LazyAttribute(lambda obj: obj.person.email())
1919

2020
class GroupEventFactory(factory.DjangoModelFactory):
2121
class Meta:
2222
model = GroupEvent
2323

2424
group = factory.SubFactory(GroupFactory)
25-
by = factory.SubFactory('ietf.doc.factories.PersonFactory')
25+
by = factory.SubFactory('ietf.person.factories.PersonFactory')
2626
type = 'comment'
2727
desc = factory.Faker('paragraph')

0 commit comments

Comments
 (0)