Project factories

This commit is contained in:
Matthew Ryan Dillon 2016-06-08 20:51:42 -07:00
parent 13925cef39
commit 2ec7fcabef
2 changed files with 19 additions and 33 deletions

View file

@ -1,3 +1,5 @@
from datetime import datetime
import factory import factory
from .models import Project, Grant, GrantReport from .models import Project, Grant, GrantReport
@ -28,9 +30,11 @@ class GrantReportFactory(factory.DjangoModelFactory):
class Meta: class Meta:
model = GrantReport model = GrantReport
grant = 0 grant = factory.SubFactory(GrantFactory)
title = factory.Sequence(lambda n: 'grant{}'.format(n)) title = factory.Sequence(lambda n: 'grant{}'.format(n))
code = factory.Sequence(lambda n: 'g{}'.format(n)) report_type = 'lorem ipsum'
description = 'lorem ipsum' description = 'lorem ipsum'
due_date = factory.LazyFunction(datetime.now)
submitted_date = factory.LazyFunction(datetime.now)
sort_order = factory.Sequence(lambda n: n) sort_order = factory.Sequence(lambda n: n)

View file

@ -4,64 +4,46 @@ from django.test import TestCase
from django.db import IntegrityError, transaction from django.db import IntegrityError, transaction
from ..models import Project, Grant, GrantReport from ..models import Project, Grant, GrantReport
from ..factories import ProjectFactory, GrantFactory, GrantReportFactory
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)
class ProjectTestCase(TestCase): class ProjectTestCase(TestCase):
def test_creation(self): def test_creation(self):
p = _project() p = ProjectFactory()
self.assertTrue(isinstance(p, Project)) self.assertTrue(isinstance(p, Project))
self.assertEqual(p.__str__(), p.name) self.assertEqual(p.__str__(), p.name)
def test_uniqueness(self): def test_uniqueness(self):
p1 = _project() p1 = ProjectFactory()
with transaction.atomic(), self.assertRaises(IntegrityError): with transaction.atomic(), self.assertRaises(IntegrityError):
p2 = _project() p2 = ProjectFactory(name=p1.name, code=p1.code)
p3 = _project(name='abc') p3 = ProjectFactory()
self.assertTrue(isinstance(p3, Project)) self.assertTrue(isinstance(p3, Project))
class GrantTestCase(TestCase): class GrantTestCase(TestCase):
def test_creation(self): def test_creation(self):
g = _grant() g = GrantFactory()
self.assertTrue(isinstance(g, Grant)) self.assertTrue(isinstance(g, Grant))
self.assertEqual(g.__str__(), g.title) self.assertEqual(g.__str__(), g.title)
def test_uniqueness(self): def test_uniqueness(self):
g1 = _grant() g1 = GrantFactory()
with transaction.atomic(), self.assertRaises(IntegrityError): with transaction.atomic(), self.assertRaises(IntegrityError):
g2 = _grant() g2 = GrantFactory(title=g1.title, code=g1.code)
g3 = _grant(title='abc') g3 = GrantFactory()
self.assertTrue(isinstance(g3, Grant)) self.assertTrue(isinstance(g3, Grant))
class GrantReportTestCase(TestCase): class GrantReportTestCase(TestCase):
def test_creation(self): def test_creation(self):
g = _grant_report() g = GrantReportFactory()
self.assertTrue(isinstance(g, GrantReport)) self.assertTrue(isinstance(g, GrantReport))
self.assertEqual(g.__str__(), g.title) self.assertEqual(g.__str__(), g.title)
def test_uniqueness(self): def test_uniqueness(self):
g1 = _grant_report() g1 = GrantReportFactory()
with transaction.atomic(), self.assertRaises(IntegrityError): with transaction.atomic(), self.assertRaises(IntegrityError):
g2 = _grant_report() g2 = GrantReportFactory(title=g1.title, grant=g1.grant)
g3 = _grant_report(title='abc') g3 = GrantReportFactory()
self.assertTrue(isinstance(g3, GrantReport)) self.assertTrue(isinstance(g3, GrantReport))