Project factories
This commit is contained in:
parent
13925cef39
commit
2ec7fcabef
2 changed files with 19 additions and 33 deletions
|
@ -1,3 +1,5 @@
|
|||
from datetime import datetime
|
||||
|
||||
import factory
|
||||
|
||||
from .models import Project, Grant, GrantReport
|
||||
|
@ -28,9 +30,11 @@ class GrantReportFactory(factory.DjangoModelFactory):
|
|||
class Meta:
|
||||
model = GrantReport
|
||||
|
||||
grant = 0
|
||||
grant = factory.SubFactory(GrantFactory)
|
||||
title = factory.Sequence(lambda n: 'grant{}'.format(n))
|
||||
code = factory.Sequence(lambda n: 'g{}'.format(n))
|
||||
report_type = 'lorem ipsum'
|
||||
description = 'lorem ipsum'
|
||||
due_date = factory.LazyFunction(datetime.now)
|
||||
submitted_date = factory.LazyFunction(datetime.now)
|
||||
sort_order = factory.Sequence(lambda n: n)
|
||||
|
||||
|
|
|
@ -4,64 +4,46 @@ from django.test import TestCase
|
|||
from django.db import IntegrityError, transaction
|
||||
|
||||
from ..models import Project, Grant, GrantReport
|
||||
|
||||
|
||||
def _project(name='project', code='p', iacuc_number='xyz', description='lorem ipsum',
|
||||
sort_order=1):
|
||||
return Project.objects.create(name=name, code=code, iacuc_number=iacuc_number,
|
||||
description=description, sort_order=sort_order)
|
||||
|
||||
def _grant(title='grant', code='g', description='lorem ipsum', sort_order=1):
|
||||
return Grant.objects.create(title=title, code=code, description=description,
|
||||
sort_order=sort_order)
|
||||
|
||||
|
||||
def _grant_report(title='grant report', grant=None, report_type='g',
|
||||
description='lorem ipsum', due_date=datetime.now(), sort_order=1):
|
||||
if not grant:
|
||||
grant = _grant()
|
||||
return GrantReport.objects.create(grant=grant, title=title, report_type=report_type,
|
||||
description=description, due_date=due_date,
|
||||
sort_order=sort_order)
|
||||
from ..factories import ProjectFactory, GrantFactory, GrantReportFactory
|
||||
|
||||
|
||||
class ProjectTestCase(TestCase):
|
||||
def test_creation(self):
|
||||
p = _project()
|
||||
p = ProjectFactory()
|
||||
self.assertTrue(isinstance(p, Project))
|
||||
self.assertEqual(p.__str__(), p.name)
|
||||
|
||||
def test_uniqueness(self):
|
||||
p1 = _project()
|
||||
p1 = ProjectFactory()
|
||||
with transaction.atomic(), self.assertRaises(IntegrityError):
|
||||
p2 = _project()
|
||||
p3 = _project(name='abc')
|
||||
p2 = ProjectFactory(name=p1.name, code=p1.code)
|
||||
p3 = ProjectFactory()
|
||||
self.assertTrue(isinstance(p3, Project))
|
||||
|
||||
|
||||
class GrantTestCase(TestCase):
|
||||
def test_creation(self):
|
||||
g = _grant()
|
||||
g = GrantFactory()
|
||||
self.assertTrue(isinstance(g, Grant))
|
||||
self.assertEqual(g.__str__(), g.title)
|
||||
|
||||
def test_uniqueness(self):
|
||||
g1 = _grant()
|
||||
g1 = GrantFactory()
|
||||
with transaction.atomic(), self.assertRaises(IntegrityError):
|
||||
g2 = _grant()
|
||||
g3 = _grant(title='abc')
|
||||
g2 = GrantFactory(title=g1.title, code=g1.code)
|
||||
g3 = GrantFactory()
|
||||
self.assertTrue(isinstance(g3, Grant))
|
||||
|
||||
|
||||
class GrantReportTestCase(TestCase):
|
||||
def test_creation(self):
|
||||
g = _grant_report()
|
||||
g = GrantReportFactory()
|
||||
self.assertTrue(isinstance(g, GrantReport))
|
||||
self.assertEqual(g.__str__(), g.title)
|
||||
|
||||
def test_uniqueness(self):
|
||||
g1 = _grant_report()
|
||||
g1 = GrantReportFactory()
|
||||
with transaction.atomic(), self.assertRaises(IntegrityError):
|
||||
g2 = _grant_report()
|
||||
g3 = _grant_report(title='abc')
|
||||
g2 = GrantReportFactory(title=g1.title, grant=g1.grant)
|
||||
g3 = GrantReportFactory()
|
||||
self.assertTrue(isinstance(g3, GrantReport))
|
||||
|
|
Loading…
Add table
Reference in a new issue