add collection-species to data import and tests

This commit is contained in:
Matthew Ryan Dillon 2016-06-14 21:19:02 -07:00
parent 1b5cf2ebea
commit 067448943e
3 changed files with 73 additions and 5 deletions

View file

@ -1,7 +1,8 @@
from factory import DjangoModelFactory, Sequence from factory import DjangoModelFactory, Sequence, SubFactory
from factory.fuzzy import FuzzyText, FuzzyChoice from factory.fuzzy import FuzzyText, FuzzyChoice, FuzzyInteger
from .models import Species from .models import Species, CollectionSpecies
from ..collections_ccdb.factories import CollectionFactory
class SpeciesFactory(DjangoModelFactory): class SpeciesFactory(DjangoModelFactory):
@ -13,3 +14,14 @@ class SpeciesFactory(DjangoModelFactory):
species = FuzzyText(length=50) species = FuzzyText(length=50)
parasite = FuzzyChoice(choices=[True, False]) parasite = FuzzyChoice(choices=[True, False])
sort_order = Sequence(lambda n: n) sort_order = Sequence(lambda n: n)
class CollectionSpeciesFactory(DjangoModelFactory):
class Meta:
model = CollectionSpecies
collection = SubFactory(CollectionFactory)
species = SubFactory(SpeciesFactory)
sex = FuzzyText(length=25)
count = FuzzyInteger(0)
count_estimated = FuzzyChoice(choices=[True, False])

View file

@ -0,0 +1,41 @@
from django.db import migrations
from django.forms import modelform_factory
from ccdb.utils.data import get_data_sources
class Migration(migrations.Migration):
def migrate(apps, schema_editor):
sources = get_data_sources()
if not sources:
return
c = sources['db0']
CollectionSpecies = apps.get_model('species', 'CollectionSpecies')
CollectionSpecies.objects.all().delete()
CollectionSpeciesForm = modelform_factory(CollectionSpecies, fields='__all__')
for r in c.execute('SELECT * FROM tbl_hash_collection_species;'):
form = CollectionSpeciesForm(dict(collection=r[0], species=r[1], sex=r[2],
count=r[3], count_estimated=r[4]))
if form.is_valid():
# No PK in Andre's file
form.save()
else:
print('collection species', r[0:], form.errors.as_data())
def rollback(apps, schema_editor):
CollectionSpecies = apps.get_model('species', 'CollectionSpecies')
CollectionSpecies.objects.all().delete()
dependencies = [
('species', '0004_DATA_initial'),
('collections_ccdb', '0005_DATA_initial'),
]
operations = [
migrations.RunPython(migrate, rollback),
]

View file

@ -1,8 +1,8 @@
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 from ..models import Species, CollectionSpecies
from ..factories import SpeciesFactory from ..factories import SpeciesFactory, CollectionSpeciesFactory
class SpeciesTestCase(TestCase): class SpeciesTestCase(TestCase):
@ -17,3 +17,18 @@ class SpeciesTestCase(TestCase):
SpeciesFactory(common_name=s1.common_name, species=s1.species) SpeciesFactory(common_name=s1.common_name, species=s1.species)
s3 = SpeciesFactory() s3 = SpeciesFactory()
self.assertTrue(isinstance(s3, Species)) self.assertTrue(isinstance(s3, Species))
class CollectionSpeciesTestCase(TestCase):
def test_creation(self):
c = CollectionSpeciesFactory()
self.assertTrue(isinstance(c, CollectionSpecies))
label = "{} {}".format(c.collection, c.species)
self.assertEqual(c.__str__(), label)
def test_uniqueness(self):
c1 = CollectionSpeciesFactory()
with transaction.atomic(), self.assertRaises(IntegrityError):
CollectionSpeciesFactory(collection=c1.collection, species=c1.species)
c3 = CollectionSpeciesFactory()
self.assertTrue(isinstance(c3, CollectionSpecies))