From 2ec7fcabef47e5bd0e4a0e03f8f99322555eb3da Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 8 Jun 2016 20:51:42 -0700 Subject: [PATCH] Project factories --- ccdb/projects/factories.py | 8 ++++-- ccdb/projects/tests/test_models.py | 44 +++++++++--------------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/ccdb/projects/factories.py b/ccdb/projects/factories.py index fc687a5..64cdbf2 100644 --- a/ccdb/projects/factories.py +++ b/ccdb/projects/factories.py @@ -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) diff --git a/ccdb/projects/tests/test_models.py b/ccdb/projects/tests/test_models.py index ed1f0bb..8b1040d 100644 --- a/ccdb/projects/tests/test_models.py +++ b/ccdb/projects/tests/test_models.py @@ -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))