Clean up unique on existing models

This commit is contained in:
Matthew Ryan Dillon 2016-06-16 06:45:59 -07:00
parent a44ac2fa81
commit 49eacdeef4
16 changed files with 141 additions and 12 deletions

View file

@ -13,7 +13,7 @@ class Migration(migrations.Migration):
name='ADFGPermit', name='ADFGPermit',
fields=[ fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)), ('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)), ('sort_order', models.IntegerField(blank=True, null=True)),
], ],
options={ options={
@ -82,7 +82,7 @@ class Migration(migrations.Migration):
name='Flaw', name='Flaw',
fields=[ fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)), ('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)), ('description', models.CharField(blank=True, max_length=255)),
('sort_order', models.IntegerField(blank=True, null=True)), ('sort_order', models.IntegerField(blank=True, null=True)),
], ],

View file

@ -29,7 +29,7 @@ class CollectionMethod(models.Model):
class Flaw(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) description = models.CharField(max_length=255, blank=True)
sort_order = models.IntegerField(blank=True, null=True) sort_order = models.IntegerField(blank=True, null=True)
@ -41,7 +41,7 @@ class Flaw(models.Model):
class ADFGPermit(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) sort_order = models.IntegerField(blank=True, null=True)
def __str__(self): def __str__(self):

View file

@ -42,6 +42,13 @@ class FlawTestCase(TestCase):
self.assertTrue(isinstance(f, Flaw)) self.assertTrue(isinstance(f, Flaw))
self.assertEqual(f.__str__(), f.name) 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): class ADFGPermitTestCase(TestCase):
def test_creation(self): def test_creation(self):
@ -49,6 +56,13 @@ class ADFGPermitTestCase(TestCase):
self.assertTrue(isinstance(a, ADFGPermit)) self.assertTrue(isinstance(a, ADFGPermit))
self.assertEqual(a.__str__(), a.name) 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): class CollectionTestCase(TestCase):
def test_creation(self): def test_creation(self):

View file

@ -27,7 +27,7 @@ class Migration(migrations.Migration):
name='Flaw', name='Flaw',
fields=[ fields=[
('id', models.AutoField(primary_key=True, serialize=False, auto_created=True, verbose_name='ID')), ('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)), ('description', models.CharField(blank=True, max_length=255)),
('sort_order', models.IntegerField(blank=True, null=True)), ('sort_order', models.IntegerField(blank=True, null=True)),
], ],
@ -84,6 +84,11 @@ class Migration(migrations.Migration):
('treatment_type', models.ForeignKey(to='experiments.TreatmentType')), ('treatment_type', models.ForeignKey(to='experiments.TreatmentType')),
], ],
), ),
migrations.AlterUniqueTogether(
name='treatment',
unique_together=set([('treatment_type', 'container', 'study_location',
'species', 'sex')]),
),
migrations.CreateModel( migrations.CreateModel(
name='TreatmentReplicate', name='TreatmentReplicate',
fields=[ fields=[
@ -113,6 +118,11 @@ class Migration(migrations.Migration):
('treatment_replicate', models.ForeignKey(to='experiments.TreatmentReplicate')), ('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( migrations.AddField(
model_name='experiment', model_name='experiment',
name='collections', name='collections',

View file

@ -76,7 +76,8 @@ class Migration(migrations.Migration):
'''): '''):
flaw = None flaw = None
if r[7]: 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], form = TreatmentReplicateForm(dict(treatment=r[0], name=r[2],
setup_date=r[13], setup_date=r[13],
setup_sample_size=r[5], mass_g=r[6], setup_sample_size=r[5], mass_g=r[6],
@ -101,7 +102,8 @@ class Migration(migrations.Migration):
'''): '''):
flaw = None flaw = None
if r[6]: 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], form = AliveDeadCountForm(dict(treatment_replicate=r[0],
status_date=r[12], status_date=r[12],
status_time=r[13].time() if r[13] else None, status_time=r[13].time() if r[13] else None,

View file

@ -2,7 +2,7 @@ from django.db import models
class Flaw(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) description = models.CharField(max_length=255, blank=True)
sort_order = models.IntegerField(blank=True, null=True) sort_order = models.IntegerField(blank=True, null=True)
@ -82,6 +82,10 @@ class Treatment(models.Model):
def __str__(self): def __str__(self):
return self.display_name return self.display_name
class Meta:
unique_together = ('treatment_type', 'container', 'study_location',
'species', 'sex')
class TreatmentReplicate(models.Model): class TreatmentReplicate(models.Model):
treatment = models.ForeignKey(Treatment, related_name='treatment_replicates') treatment = models.ForeignKey(Treatment, related_name='treatment_replicates')
@ -121,3 +125,5 @@ class AliveDeadCount(models.Model):
class Meta: class Meta:
verbose_name = 'Alive-dead Count' verbose_name = 'Alive-dead Count'
unique_together = ('treatment_replicate', 'status_date', 'status_time',
'count_alive', 'count_dead')

View file

@ -14,6 +14,13 @@ class FlawTestCase(TestCase):
self.assertTrue(isinstance(f, Flaw)) self.assertTrue(isinstance(f, Flaw))
self.assertEqual(f.__str__(), f.name) 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): class ExperimentTestCase(TestCase):
def test_creation(self): def test_creation(self):
@ -60,6 +67,15 @@ class TreatmentTestCase(TestCase):
t.species, t.sex) t.species, t.sex)
self.assertEqual(t.__str__(), label) 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): class TreatmentReplicateTestCase(TestCase):
def test_creation(self): def test_creation(self):
@ -85,3 +101,14 @@ class AliveDeadCountTestCase(TestCase):
self.assertTrue(isinstance(a, AliveDeadCount)) self.assertTrue(isinstance(a, AliveDeadCount))
label = "{}".format(a.status_date) label = "{}".format(a.status_date)
self.assertEqual(a.__str__(), label) 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))

View file

@ -81,6 +81,19 @@ class Migration(migrations.Migration):
'ordering': ['sort_order'], '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( migrations.AlterUniqueTogether(
name='region', name='region',
unique_together=set([('name', 'code')]), unique_together=set([('name', 'code')]),

View file

@ -25,6 +25,7 @@ class Site(models.Model):
return self.name return self.name
class Meta: class Meta:
unique_together = ('region', 'name', 'code')
ordering = ['sort_order'] ordering = ['sort_order']
@ -39,6 +40,7 @@ class MunicipalLocation(models.Model):
return self.name return self.name
class Meta: class Meta:
unique_together = ('name', 'code')
ordering = ['sort_order'] ordering = ['sort_order']
@ -76,4 +78,6 @@ class StorageLocation(models.Model):
return self.code return self.code
class Meta: class Meta:
unique_together = ('code', 'facility', 'building', 'room',
'freezer', 'temp_c')
ordering = ['sort_order'] ordering = ['sort_order']

View file

@ -26,6 +26,13 @@ class SiteTestCase(TestCase):
self.assertTrue(isinstance(s, Site)) self.assertTrue(isinstance(s, Site))
self.assertEqual(s.__str__(), s.name) 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): class MunicipalLocationTestCase(TestCase):
def test_creation(self): def test_creation(self):
@ -33,6 +40,13 @@ class MunicipalLocationTestCase(TestCase):
self.assertTrue(isinstance(m, MunicipalLocation)) self.assertTrue(isinstance(m, MunicipalLocation))
self.assertEqual(m.__str__(), m.name) 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): class StudyLocationTestCase(TestCase):
def test_creation(self): def test_creation(self):
@ -54,5 +68,11 @@ class StorageLocationTestCase(TestCase):
self.assertTrue(isinstance(s, StorageLocation)) self.assertTrue(isinstance(s, StorageLocation))
self.assertEqual(s.__str__(), s.code) 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))

View file

@ -80,6 +80,10 @@ class Migration(migrations.Migration):
name='measurementunit', name='measurementunit',
unique_together=set([('name', 'code')]), unique_together=set([('name', 'code')]),
), ),
migrations.AlterUniqueTogether(
name='measurementtype',
unique_together=set([('name', 'code', 'measurement_type_class')]),
),
migrations.AddField( migrations.AddField(
model_name='measurementtype', model_name='measurementtype',
name='default_measurement_unit', name='default_measurement_unit',
@ -123,4 +127,8 @@ class Migration(migrations.Migration):
name='default_measurement_unit', name='default_measurement_unit',
field=models.ForeignKey(blank=True, to='misc.MeasurementUnit', related_name='measurement_types', null=True), 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')]),
),
] ]

View file

@ -30,6 +30,7 @@ class MeasurementType(models.Model):
return self.name return self.name
class Meta: class Meta:
unique_together = ('name', 'code', 'measurement_type_class')
ordering = ['sort_order'] ordering = ['sort_order']
@ -79,4 +80,5 @@ class Container(models.Model):
return self.name return self.name
class Meta: class Meta:
unique_together = ('name', 'code', 'color', 'material', 'volume')
ordering = ['sort_order'] ordering = ['sort_order']

View file

@ -26,6 +26,14 @@ class MeasurementTypeTestCase(TestCase):
self.assertTrue(isinstance(m, MeasurementType)) self.assertTrue(isinstance(m, MeasurementType))
self.assertEqual(m.__str__(), m.name) 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): class MaterialTestCase(TestCase):
def test_creation(self): def test_creation(self):
@ -60,3 +68,11 @@ class ContainerTestCase(TestCase):
c = ContainerFactory() c = ContainerFactory()
self.assertTrue(isinstance(c, Container)) self.assertTrue(isinstance(c, Container))
self.assertEqual(c.__str__(), c.name) 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))

View file

@ -11,7 +11,7 @@ class Migration(migrations.Migration):
name='Flaw', name='Flaw',
fields=[ fields=[
('id', models.AutoField(auto_created=True, verbose_name='ID', serialize=False, primary_key=True)), ('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)), ('description', models.CharField(max_length=255, blank=True)),
('sort_order', models.IntegerField(null=True, blank=True)), ('sort_order', models.IntegerField(null=True, blank=True)),
], ],

View file

@ -30,7 +30,7 @@ class Reagent(models.Model):
class Flaw(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) description = models.CharField(max_length=255, blank=True)
sort_order = models.IntegerField(blank=True, null=True) sort_order = models.IntegerField(blank=True, null=True)

View file

@ -39,6 +39,13 @@ class FlawTestCase(TestCase):
self.assertTrue(isinstance(f, Flaw)) self.assertTrue(isinstance(f, Flaw))
self.assertEqual(f.__str__(), f.name) 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): class ProcessingTestCase(TestCase):
def test_creation(self): def test_creation(self):