diff --git a/ccdb/species/factories.py b/ccdb/species/factories.py index fca9e26..5930503 100644 --- a/ccdb/species/factories.py +++ b/ccdb/species/factories.py @@ -1,7 +1,8 @@ -from factory import DjangoModelFactory, Sequence -from factory.fuzzy import FuzzyText, FuzzyChoice +from factory import DjangoModelFactory, Sequence, SubFactory +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): @@ -13,3 +14,14 @@ class SpeciesFactory(DjangoModelFactory): species = FuzzyText(length=50) parasite = FuzzyChoice(choices=[True, False]) 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]) diff --git a/ccdb/species/migrations/0005_DATA_species_collection.py b/ccdb/species/migrations/0005_DATA_species_collection.py new file mode 100644 index 0000000..487b7c4 --- /dev/null +++ b/ccdb/species/migrations/0005_DATA_species_collection.py @@ -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), + ] diff --git a/ccdb/species/tests/test_models.py b/ccdb/species/tests/test_models.py index 84ca539..4e18302 100644 --- a/ccdb/species/tests/test_models.py +++ b/ccdb/species/tests/test_models.py @@ -1,8 +1,8 @@ from django.test import TestCase from django.db import IntegrityError, transaction -from ..models import Species -from ..factories import SpeciesFactory +from ..models import Species, CollectionSpecies +from ..factories import SpeciesFactory, CollectionSpeciesFactory class SpeciesTestCase(TestCase): @@ -17,3 +17,18 @@ class SpeciesTestCase(TestCase): SpeciesFactory(common_name=s1.common_name, species=s1.species) s3 = SpeciesFactory() 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))