Clean up unique on existing models
This commit is contained in:
parent
a44ac2fa81
commit
49eacdeef4
16 changed files with 141 additions and 12 deletions
|
@ -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',
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue