TST: Coverage and adding new model tests

This commit is contained in:
Matthew Ryan Dillon 2016-08-21 15:50:29 -07:00
parent 5c69e774f9
commit 31714215d0
10 changed files with 65 additions and 11 deletions

3
.coveragerc Normal file
View file

@ -0,0 +1,3 @@
[run]
omit = ./venv/*,./config/*,manage.py
branch = True

4
.gitignore vendored
View file

@ -6,4 +6,6 @@ media
*.py[cod] *.py[cod]
*.pyc *.pyc
__pycache__ __pycache__
.idea .idea
.coverage
htmlcov

View file

@ -32,7 +32,7 @@ class ExperimentFactory(DjangoModelFactory):
sort_order = Sequence(lambda n: n) sort_order = Sequence(lambda n: n)
@post_generation @post_generation
def collections(self, create, extracted, **kwargs): def collections(self, create, extracted, **kwargs): # pragma: no cover
if not create: if not create:
return return
if extracted: if extracted:

View file

@ -1,8 +1,9 @@
from factory import DjangoModelFactory, Sequence, SubFactory from factory import DjangoModelFactory, Sequence, SubFactory
from factory.fuzzy import FuzzyText, FuzzyChoice, FuzzyInteger from factory.fuzzy import FuzzyText, FuzzyChoice, FuzzyInteger
from .models import Species, CollectionSpecies from .models import Species, TrapSpecies, CollectionSpecies
from ..collections_ccdb.factories import CollectionFactory from ..collections_ccdb.factories import (CollectionFactory,
CollectionTrapFactory)
class SpeciesFactory(DjangoModelFactory): class SpeciesFactory(DjangoModelFactory):
@ -16,6 +17,17 @@ class SpeciesFactory(DjangoModelFactory):
sort_order = Sequence(lambda n: n) sort_order = Sequence(lambda n: n)
class TrapSpeciesFactory(DjangoModelFactory):
class Meta:
model = TrapSpecies
collection_trap = SubFactory(CollectionTrapFactory)
species = SubFactory(SpeciesFactory)
sex = FuzzyText(length=25)
count = FuzzyInteger(0)
count_estimated = FuzzyChoice(choices=[True, False])
class CollectionSpeciesFactory(DjangoModelFactory): class CollectionSpeciesFactory(DjangoModelFactory):
class Meta: class Meta:
model = CollectionSpecies model = CollectionSpecies

View file

@ -1,8 +1,9 @@
from django.test import TestCase from django.test import TestCase
from django.db import IntegrityError, transaction from django.db import IntegrityError, transaction
from ..models import Species, CollectionSpecies from ..models import Species, TrapSpecies, CollectionSpecies
from ..factories import SpeciesFactory, CollectionSpeciesFactory from ..factories import (SpeciesFactory, TrapSpeciesFactory,
CollectionSpeciesFactory)
class SpeciesTestCase(TestCase): class SpeciesTestCase(TestCase):
@ -19,6 +20,14 @@ class SpeciesTestCase(TestCase):
self.assertTrue(isinstance(s3, Species)) self.assertTrue(isinstance(s3, Species))
class TrapSpeciesTestCase(TestCase):
def test_creation(self):
t = TrapSpeciesFactory()
self.assertTrue(isinstance(t, TrapSpecies))
label = "{} {}".format(t.collection_trap, t.species)
self.assertEqual(t.__str__(), label)
class CollectionSpeciesTestCase(TestCase): class CollectionSpeciesTestCase(TestCase):
def test_creation(self): def test_creation(self):
c = CollectionSpeciesFactory() c = CollectionSpeciesFactory()
@ -29,6 +38,7 @@ class CollectionSpeciesTestCase(TestCase):
def test_uniqueness(self): def test_uniqueness(self):
c1 = CollectionSpeciesFactory() c1 = CollectionSpeciesFactory()
with transaction.atomic(), self.assertRaises(IntegrityError): with transaction.atomic(), self.assertRaises(IntegrityError):
CollectionSpeciesFactory(collection=c1.collection, species=c1.species) CollectionSpeciesFactory(collection=c1.collection,
species=c1.species)
c3 = CollectionSpeciesFactory() c3 = CollectionSpeciesFactory()
self.assertTrue(isinstance(c3, CollectionSpecies)) self.assertTrue(isinstance(c3, CollectionSpecies))

11
ccdb/users/factories.py Normal file
View file

@ -0,0 +1,11 @@
from factory import DjangoModelFactory, Sequence
from .models import User
class UserFactory(DjangoModelFactory):
class Meta:
model = User
name = Sequence(lambda n: 'user{}'.format(n))
username = Sequence(lambda n: 'username{}'.format(n))

View file

@ -1,5 +1,4 @@
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.core.urlresolvers import reverse
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -9,6 +8,3 @@ class User(AbstractUser):
def __str__(self): def __str__(self):
return self.username return self.username
def get_absolute_url(self):
return reverse('users:detail', kwargs={'username': self.username})

View file

View file

@ -0,0 +1,19 @@
from django.test import TestCase
from django.db import IntegrityError, transaction
from ..models import User
from ..factories import UserFactory
class UserTestCase(TestCase):
def test_creation(self):
u = UserFactory()
self.assertTrue(isinstance(u, User))
self.assertEqual(u.__str__(), u.username)
def test_uniqueness(self):
u1 = UserFactory()
with transaction.atomic(), self.assertRaises(IntegrityError):
UserFactory(username=u1.username)
u3 = UserFactory()
self.assertTrue(isinstance(u3, User))

View file

@ -6,3 +6,4 @@ flake8==3.0.4
pygraphviz==1.3.1 pygraphviz==1.3.1
ipython==5.1.0 ipython==5.1.0
factory-boy==2.7.0 factory-boy==2.7.0
coverage==4.2