diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..1a4cfb8 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] +omit = ./venv/*,./config/*,manage.py +branch = True diff --git a/.gitignore b/.gitignore index e021330..f5f2774 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ media *.py[cod] *.pyc __pycache__ -.idea \ No newline at end of file +.idea +.coverage +htmlcov diff --git a/ccdb/experiments/factories.py b/ccdb/experiments/factories.py index cdb8cee..8f32dc3 100644 --- a/ccdb/experiments/factories.py +++ b/ccdb/experiments/factories.py @@ -32,7 +32,7 @@ class ExperimentFactory(DjangoModelFactory): sort_order = Sequence(lambda n: n) @post_generation - def collections(self, create, extracted, **kwargs): + def collections(self, create, extracted, **kwargs): # pragma: no cover if not create: return if extracted: diff --git a/ccdb/species/factories.py b/ccdb/species/factories.py index 5930503..43f873e 100644 --- a/ccdb/species/factories.py +++ b/ccdb/species/factories.py @@ -1,8 +1,9 @@ from factory import DjangoModelFactory, Sequence, SubFactory from factory.fuzzy import FuzzyText, FuzzyChoice, FuzzyInteger -from .models import Species, CollectionSpecies -from ..collections_ccdb.factories import CollectionFactory +from .models import Species, TrapSpecies, CollectionSpecies +from ..collections_ccdb.factories import (CollectionFactory, + CollectionTrapFactory) class SpeciesFactory(DjangoModelFactory): @@ -16,6 +17,17 @@ class SpeciesFactory(DjangoModelFactory): 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 Meta: model = CollectionSpecies diff --git a/ccdb/species/tests/test_models.py b/ccdb/species/tests/test_models.py index 4e18302..df42b7a 100644 --- a/ccdb/species/tests/test_models.py +++ b/ccdb/species/tests/test_models.py @@ -1,8 +1,9 @@ from django.test import TestCase from django.db import IntegrityError, transaction -from ..models import Species, CollectionSpecies -from ..factories import SpeciesFactory, CollectionSpeciesFactory +from ..models import Species, TrapSpecies, CollectionSpecies +from ..factories import (SpeciesFactory, TrapSpeciesFactory, + CollectionSpeciesFactory) class SpeciesTestCase(TestCase): @@ -19,6 +20,14 @@ class SpeciesTestCase(TestCase): 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): def test_creation(self): c = CollectionSpeciesFactory() @@ -29,6 +38,7 @@ class CollectionSpeciesTestCase(TestCase): def test_uniqueness(self): c1 = CollectionSpeciesFactory() with transaction.atomic(), self.assertRaises(IntegrityError): - CollectionSpeciesFactory(collection=c1.collection, species=c1.species) + CollectionSpeciesFactory(collection=c1.collection, + species=c1.species) c3 = CollectionSpeciesFactory() self.assertTrue(isinstance(c3, CollectionSpecies)) diff --git a/ccdb/users/factories.py b/ccdb/users/factories.py new file mode 100644 index 0000000..8c11276 --- /dev/null +++ b/ccdb/users/factories.py @@ -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)) diff --git a/ccdb/users/models.py b/ccdb/users/models.py index c1afa06..c365fd9 100644 --- a/ccdb/users/models.py +++ b/ccdb/users/models.py @@ -1,5 +1,4 @@ from django.contrib.auth.models import AbstractUser -from django.core.urlresolvers import reverse from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -9,6 +8,3 @@ class User(AbstractUser): def __str__(self): return self.username - - def get_absolute_url(self): - return reverse('users:detail', kwargs={'username': self.username}) diff --git a/ccdb/users/tests/__init__.py b/ccdb/users/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ccdb/users/tests/test_models.py b/ccdb/users/tests/test_models.py new file mode 100644 index 0000000..72f47f1 --- /dev/null +++ b/ccdb/users/tests/test_models.py @@ -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)) diff --git a/requirements/local.txt b/requirements/local.txt index 397bd7b..a6b8167 100644 --- a/requirements/local.txt +++ b/requirements/local.txt @@ -6,3 +6,4 @@ flake8==3.0.4 pygraphviz==1.3.1 ipython==5.1.0 factory-boy==2.7.0 +coverage==4.2