From 49eacdeef4ec282d8598d71749bf15bb558d371e Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Thu, 16 Jun 2016 06:45:59 -0700 Subject: [PATCH] Clean up unique on existing models --- .../migrations/0001_initial.py | 4 +-- ccdb/collections_ccdb/models.py | 4 +-- ccdb/collections_ccdb/tests/test_models.py | 14 ++++++++++ ccdb/experiments/migrations/0001_initial.py | 12 ++++++++- .../migrations/0002_DATA_initial.py | 6 +++-- ccdb/experiments/models.py | 8 +++++- ccdb/experiments/tests/test_models.py | 27 +++++++++++++++++++ ccdb/locations/migrations/0001_initial.py | 13 +++++++++ ccdb/locations/models.py | 4 +++ ccdb/locations/tests/test_models.py | 24 +++++++++++++++-- ccdb/misc/migrations/0001_initial.py | 8 ++++++ ccdb/misc/models.py | 2 ++ ccdb/misc/tests/test_models.py | 16 +++++++++++ ccdb/processing/migrations/0001_initial.py | 2 +- ccdb/processing/models.py | 2 +- ccdb/processing/tests/test_models.py | 7 +++++ 16 files changed, 141 insertions(+), 12 deletions(-) diff --git a/ccdb/collections_ccdb/migrations/0001_initial.py b/ccdb/collections_ccdb/migrations/0001_initial.py index 9430510..60132ee 100644 --- a/ccdb/collections_ccdb/migrations/0001_initial.py +++ b/ccdb/collections_ccdb/migrations/0001_initial.py @@ -13,7 +13,7 @@ class Migration(migrations.Migration): name='ADFGPermit', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)), - ('name', models.CharField(max_length=200)), + ('name', models.CharField(max_length=200, unique=True)), ('sort_order', models.IntegerField(blank=True, null=True)), ], options={ @@ -82,7 +82,7 @@ class Migration(migrations.Migration): name='Flaw', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)), - ('name', models.CharField(max_length=200)), + ('name', models.CharField(max_length=200, unique=True)), ('description', models.CharField(blank=True, max_length=255)), ('sort_order', models.IntegerField(blank=True, null=True)), ], diff --git a/ccdb/collections_ccdb/models.py b/ccdb/collections_ccdb/models.py index eaefd6d..f414055 100644 --- a/ccdb/collections_ccdb/models.py +++ b/ccdb/collections_ccdb/models.py @@ -29,7 +29,7 @@ class CollectionMethod(models.Model): class Flaw(models.Model): - name = models.CharField(max_length=200) + name = models.CharField(max_length=200, unique=True) description = models.CharField(max_length=255, blank=True) sort_order = models.IntegerField(blank=True, null=True) @@ -41,7 +41,7 @@ class Flaw(models.Model): class ADFGPermit(models.Model): - name = models.CharField(max_length=200) + name = models.CharField(max_length=200, unique=True) sort_order = models.IntegerField(blank=True, null=True) def __str__(self): diff --git a/ccdb/collections_ccdb/tests/test_models.py b/ccdb/collections_ccdb/tests/test_models.py index ec1c4b1..b9d53e0 100644 --- a/ccdb/collections_ccdb/tests/test_models.py +++ b/ccdb/collections_ccdb/tests/test_models.py @@ -42,6 +42,13 @@ class FlawTestCase(TestCase): self.assertTrue(isinstance(f, Flaw)) self.assertEqual(f.__str__(), f.name) + def test_uniqueness(self): + f1 = FlawFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + FlawFactory(name=f1.name) + f3 = FlawFactory() + self.assertTrue(isinstance(f3, Flaw)) + class ADFGPermitTestCase(TestCase): def test_creation(self): @@ -49,6 +56,13 @@ class ADFGPermitTestCase(TestCase): self.assertTrue(isinstance(a, ADFGPermit)) self.assertEqual(a.__str__(), a.name) + def test_uniqueness(self): + a1 = ADFGPermitFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + ADFGPermitFactory(name=a1.name) + a3 = ADFGPermitFactory() + self.assertTrue(isinstance(a3, ADFGPermit)) + class CollectionTestCase(TestCase): def test_creation(self): diff --git a/ccdb/experiments/migrations/0001_initial.py b/ccdb/experiments/migrations/0001_initial.py index 5ce31a2..5e8c382 100644 --- a/ccdb/experiments/migrations/0001_initial.py +++ b/ccdb/experiments/migrations/0001_initial.py @@ -27,7 +27,7 @@ class Migration(migrations.Migration): name='Flaw', fields=[ ('id', models.AutoField(primary_key=True, serialize=False, auto_created=True, verbose_name='ID')), - ('name', models.CharField(max_length=200)), + ('name', models.CharField(max_length=200, unique=True)), ('description', models.CharField(blank=True, max_length=255)), ('sort_order', models.IntegerField(blank=True, null=True)), ], @@ -84,6 +84,11 @@ class Migration(migrations.Migration): ('treatment_type', models.ForeignKey(to='experiments.TreatmentType')), ], ), + migrations.AlterUniqueTogether( + name='treatment', + unique_together=set([('treatment_type', 'container', 'study_location', + 'species', 'sex')]), + ), migrations.CreateModel( name='TreatmentReplicate', fields=[ @@ -113,6 +118,11 @@ class Migration(migrations.Migration): ('treatment_replicate', models.ForeignKey(to='experiments.TreatmentReplicate')), ], ), + migrations.AlterUniqueTogether( + name='alivedeadcount', + unique_together=set([('treatment_replicate', 'status_date', 'status_time', + 'count_alive', 'count_dead')]), + ), migrations.AddField( model_name='experiment', name='collections', diff --git a/ccdb/experiments/migrations/0002_DATA_initial.py b/ccdb/experiments/migrations/0002_DATA_initial.py index 2867fff..68f1fa9 100644 --- a/ccdb/experiments/migrations/0002_DATA_initial.py +++ b/ccdb/experiments/migrations/0002_DATA_initial.py @@ -76,7 +76,8 @@ class Migration(migrations.Migration): '''): flaw = None if r[7]: - flaw = Flaw.objects.create(name=r[10]).pk + flaw, _ = Flaw.objects.get_or_create(name=r[10]) + flaw = flaw.pk form = TreatmentReplicateForm(dict(treatment=r[0], name=r[2], setup_date=r[13], setup_sample_size=r[5], mass_g=r[6], @@ -101,7 +102,8 @@ class Migration(migrations.Migration): '''): flaw = None if r[6]: - flaw = Flaw.objects.create(name=r[9]).pk + flaw, _ = Flaw.objects.get_or_create(name=r[9]) + flaw = flaw.pk form = AliveDeadCountForm(dict(treatment_replicate=r[0], status_date=r[12], status_time=r[13].time() if r[13] else None, diff --git a/ccdb/experiments/models.py b/ccdb/experiments/models.py index 4cadce8..3af990c 100644 --- a/ccdb/experiments/models.py +++ b/ccdb/experiments/models.py @@ -2,7 +2,7 @@ from django.db import models class Flaw(models.Model): - name = models.CharField(max_length=200) + name = models.CharField(max_length=200, unique=True) description = models.CharField(max_length=255, blank=True) sort_order = models.IntegerField(blank=True, null=True) @@ -82,6 +82,10 @@ class Treatment(models.Model): def __str__(self): return self.display_name + class Meta: + unique_together = ('treatment_type', 'container', 'study_location', + 'species', 'sex') + class TreatmentReplicate(models.Model): treatment = models.ForeignKey(Treatment, related_name='treatment_replicates') @@ -121,3 +125,5 @@ class AliveDeadCount(models.Model): class Meta: verbose_name = 'Alive-dead Count' + unique_together = ('treatment_replicate', 'status_date', 'status_time', + 'count_alive', 'count_dead') diff --git a/ccdb/experiments/tests/test_models.py b/ccdb/experiments/tests/test_models.py index 64c1cb2..9be378b 100644 --- a/ccdb/experiments/tests/test_models.py +++ b/ccdb/experiments/tests/test_models.py @@ -14,6 +14,13 @@ class FlawTestCase(TestCase): self.assertTrue(isinstance(f, Flaw)) self.assertEqual(f.__str__(), f.name) + def test_uniqueness(self): + f1 = FlawFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + FlawFactory(name=f1.name) + f3 = FlawFactory() + self.assertTrue(isinstance(f3, Flaw)) + class ExperimentTestCase(TestCase): def test_creation(self): @@ -60,6 +67,15 @@ class TreatmentTestCase(TestCase): t.species, t.sex) self.assertEqual(t.__str__(), label) + def test_uniqueness(self): + t1 = TreatmentFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + TreatmentFactory(treatment_type=t1.treatment_type, container=t1.container, + study_location=t1.study_location, species=t1.species, + sex=t1.sex) + t3 = TreatmentFactory() + self.assertTrue(isinstance(t3, Treatment)) + class TreatmentReplicateTestCase(TestCase): def test_creation(self): @@ -85,3 +101,14 @@ class AliveDeadCountTestCase(TestCase): self.assertTrue(isinstance(a, AliveDeadCount)) label = "{}".format(a.status_date) self.assertEqual(a.__str__(), label) + + def test_uniqueness(self): + a1 = AliveDeadCountFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + AliveDeadCountFactory(treatment_replicate=a1.treatment_replicate, + status_date=a1.status_date, + status_time=a1.status_time, + count_alive=a1.count_alive, + count_dead=a1.count_dead) + a3 = AliveDeadCountFactory() + self.assertTrue(isinstance(a3, AliveDeadCount)) diff --git a/ccdb/locations/migrations/0001_initial.py b/ccdb/locations/migrations/0001_initial.py index 5976929..b509054 100644 --- a/ccdb/locations/migrations/0001_initial.py +++ b/ccdb/locations/migrations/0001_initial.py @@ -81,6 +81,19 @@ class Migration(migrations.Migration): 'ordering': ['sort_order'], }, ), + migrations.AlterUniqueTogether( + name='site', + unique_together=set([('region', 'name', 'code')]), + ), + migrations.AlterUniqueTogether( + name='municipallocation', + unique_together=set([('name', 'code')]), + ), + migrations.AlterUniqueTogether( + name='storagelocation', + unique_together=set([('code', 'facility', 'building', 'room', + 'freezer', 'temp_c')]), + ), migrations.AlterUniqueTogether( name='region', unique_together=set([('name', 'code')]), diff --git a/ccdb/locations/models.py b/ccdb/locations/models.py index 977b801..3e4eda2 100644 --- a/ccdb/locations/models.py +++ b/ccdb/locations/models.py @@ -25,6 +25,7 @@ class Site(models.Model): return self.name class Meta: + unique_together = ('region', 'name', 'code') ordering = ['sort_order'] @@ -39,6 +40,7 @@ class MunicipalLocation(models.Model): return self.name class Meta: + unique_together = ('name', 'code') ordering = ['sort_order'] @@ -76,4 +78,6 @@ class StorageLocation(models.Model): return self.code class Meta: + unique_together = ('code', 'facility', 'building', 'room', + 'freezer', 'temp_c') ordering = ['sort_order'] diff --git a/ccdb/locations/tests/test_models.py b/ccdb/locations/tests/test_models.py index 4f114d3..01c94f9 100644 --- a/ccdb/locations/tests/test_models.py +++ b/ccdb/locations/tests/test_models.py @@ -26,6 +26,13 @@ class SiteTestCase(TestCase): self.assertTrue(isinstance(s, Site)) self.assertEqual(s.__str__(), s.name) + def test_uniqueness(self): + s1 = SiteFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + SiteFactory(region=s1.region, name=s1.name, code=s1.code) + s3 = SiteFactory() + self.assertTrue(isinstance(s3, Site)) + class MunicipalLocationTestCase(TestCase): def test_creation(self): @@ -33,6 +40,13 @@ class MunicipalLocationTestCase(TestCase): self.assertTrue(isinstance(m, MunicipalLocation)) self.assertEqual(m.__str__(), m.name) + def test_uniqueness(self): + m1 = MunicipalLocationFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + MunicipalLocationFactory(name=m1.name, code=m1.code) + m3 = MunicipalLocationFactory() + self.assertTrue(isinstance(m3, MunicipalLocation)) + class StudyLocationTestCase(TestCase): def test_creation(self): @@ -54,5 +68,11 @@ class StorageLocationTestCase(TestCase): self.assertTrue(isinstance(s, StorageLocation)) self.assertEqual(s.__str__(), s.code) - - + def test_uniqueness(self): + s1 = StorageLocationFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + StorageLocationFactory(code=s1.code, facility=s1.facility, + building=s1.building, room=s1.room, + freezer=s1.freezer, temp_c=s1.temp_c) + s3 = StorageLocationFactory() + self.assertTrue(isinstance(s3, StorageLocation)) diff --git a/ccdb/misc/migrations/0001_initial.py b/ccdb/misc/migrations/0001_initial.py index 6c59cde..84ec0bd 100644 --- a/ccdb/misc/migrations/0001_initial.py +++ b/ccdb/misc/migrations/0001_initial.py @@ -80,6 +80,10 @@ class Migration(migrations.Migration): name='measurementunit', unique_together=set([('name', 'code')]), ), + migrations.AlterUniqueTogether( + name='measurementtype', + unique_together=set([('name', 'code', 'measurement_type_class')]), + ), migrations.AddField( model_name='measurementtype', name='default_measurement_unit', @@ -123,4 +127,8 @@ class Migration(migrations.Migration): name='default_measurement_unit', field=models.ForeignKey(blank=True, to='misc.MeasurementUnit', related_name='measurement_types', null=True), ), + migrations.AlterUniqueTogether( + name='container', + unique_together=set([('name', 'code', 'color', 'material', 'volume')]), + ), ] diff --git a/ccdb/misc/models.py b/ccdb/misc/models.py index ef002ef..5118500 100644 --- a/ccdb/misc/models.py +++ b/ccdb/misc/models.py @@ -30,6 +30,7 @@ class MeasurementType(models.Model): return self.name class Meta: + unique_together = ('name', 'code', 'measurement_type_class') ordering = ['sort_order'] @@ -79,4 +80,5 @@ class Container(models.Model): return self.name class Meta: + unique_together = ('name', 'code', 'color', 'material', 'volume') ordering = ['sort_order'] diff --git a/ccdb/misc/tests/test_models.py b/ccdb/misc/tests/test_models.py index 354a3a6..02d2e06 100644 --- a/ccdb/misc/tests/test_models.py +++ b/ccdb/misc/tests/test_models.py @@ -26,6 +26,14 @@ class MeasurementTypeTestCase(TestCase): self.assertTrue(isinstance(m, MeasurementType)) self.assertEqual(m.__str__(), m.name) + def test_uniqueness(self): + m1 = MeasurementTypeFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + MeasurementTypeFactory(name=m1.name, code=m1.code, + measurement_type_class=m1.measurement_type_class) + m3 = MeasurementTypeFactory() + self.assertTrue(isinstance(m3, MeasurementType)) + class MaterialTestCase(TestCase): def test_creation(self): @@ -60,3 +68,11 @@ class ContainerTestCase(TestCase): c = ContainerFactory() self.assertTrue(isinstance(c, Container)) self.assertEqual(c.__str__(), c.name) + + def test_uniqueness(self): + c1 = ContainerFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + ContainerFactory(name=c1.name, code=c1.code, color=c1.color, + material=c1.material, volume=c1.volume) + c3 = ContainerFactory() + self.assertTrue(isinstance(c3, Container)) diff --git a/ccdb/processing/migrations/0001_initial.py b/ccdb/processing/migrations/0001_initial.py index f2b4b18..6a8a168 100644 --- a/ccdb/processing/migrations/0001_initial.py +++ b/ccdb/processing/migrations/0001_initial.py @@ -11,7 +11,7 @@ class Migration(migrations.Migration): name='Flaw', fields=[ ('id', models.AutoField(auto_created=True, verbose_name='ID', serialize=False, primary_key=True)), - ('name', models.CharField(max_length=200)), + ('name', models.CharField(max_length=200, unique=True)), ('description', models.CharField(max_length=255, blank=True)), ('sort_order', models.IntegerField(null=True, blank=True)), ], diff --git a/ccdb/processing/models.py b/ccdb/processing/models.py index c988ed9..4833f18 100644 --- a/ccdb/processing/models.py +++ b/ccdb/processing/models.py @@ -30,7 +30,7 @@ class Reagent(models.Model): class Flaw(models.Model): - name = models.CharField(max_length=200) + name = models.CharField(max_length=200, unique=True) description = models.CharField(max_length=255, blank=True) sort_order = models.IntegerField(blank=True, null=True) diff --git a/ccdb/processing/tests/test_models.py b/ccdb/processing/tests/test_models.py index 836e7bc..74d02bf 100644 --- a/ccdb/processing/tests/test_models.py +++ b/ccdb/processing/tests/test_models.py @@ -39,6 +39,13 @@ class FlawTestCase(TestCase): self.assertTrue(isinstance(f, Flaw)) self.assertEqual(f.__str__(), f.name) + def test_uniqueness(self): + f1 = FlawFactory() + with transaction.atomic(), self.assertRaises(IntegrityError): + FlawFactory(name=f1.name) + f3 = FlawFactory() + self.assertTrue(isinstance(f3, Flaw)) + class ProcessingTestCase(TestCase): def test_creation(self):