experiments: factories

This commit is contained in:
Matthew Ryan Dillon 2016-06-15 07:06:00 -07:00
parent 067448943e
commit f899a8c844
4 changed files with 103 additions and 4 deletions

View file

@ -0,0 +1,98 @@
from datetime import date, time
from factory import DjangoModelFactory, Sequence, SubFactory, \
LazyFunction, post_generation
from factory.fuzzy import FuzzyText, FuzzyDate, FuzzyInteger, FuzzyFloat
from factory.django import FileField
from .models import Flaw, Experiment, ProtocolAttachment, TreatmentType, \
Treatment, TreatmentReplicate, AliveDeadCount
from ..misc.factories import ContainerFactory
from ..locations.factories import StudyLocationFactory
from ..species.factories import SpeciesFactory
class FlawFactory(DjangoModelFactory):
class Meta:
model = Flaw
name = Sequence(lambda n: 'flaw{}'.format(n))
description = FuzzyText(length=255)
sort_order = Sequence(lambda n: n)
class ExperimentFactory(DjangoModelFactory):
class Meta:
model = Experiment
name = Sequence(lambda n: 'experiment{}'.format(n))
code = Sequence(lambda n: 'e{}'.format(n))
description = FuzzyText(length=255)
flaw = SubFactory(FlawFactory)
sort_order = Sequence(lambda n: n)
@post_generation
def collections(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for collection in extracted:
self.groups.add(collection)
class ProtocolAttachmentFactory(DjangoModelFactory):
class Meta:
model = ProtocolAttachment
experiment = SubFactory(ExperimentFactory)
protocol = FileField()
class TreatmentTypeFactory(DjangoModelFactory):
class Meta:
model = TreatmentType
experiment = SubFactory(ExperimentFactory)
name = Sequence(lambda n: 'treatment_type{}'.format(n))
code = Sequence(lambda n: 'tt{}'.format(n))
treatment_type = FuzzyText(length=50)
placement = FuzzyText(length=25)
description = FuzzyText(length=255)
sort_order = Sequence(lambda n: n)
class TreatmentFactory(DjangoModelFactory):
class Meta:
model = Treatment
treatment_type = SubFactory(TreatmentTypeFactory)
container = SubFactory(ContainerFactory)
study_location = SubFactory(StudyLocationFactory)
species = SubFactory(SpeciesFactory)
sex = FuzzyText(length=25)
flaw = SubFactory(FlawFactory)
class TreatmentReplicateFactory(DjangoModelFactory):
class Meta:
model = TreatmentReplicate
treatment_replicate = SubFactory(TreatmentFactory)
name = Sequence(lambda n: 'treatment_replicate{}'.format(n))
setup_date = FuzzyDate(date(2012, 1, 1))
setup_time = LazyFunction(time)
setup_sample_size = FuzzyInteger(0)
mass_g = FuzzyFloat(0.0)
flaw = SubFactory(FlawFactory)
class AliveDeadCountFactory(DjangoModelFactory):
class Meta:
model = AliveDeadCount
treatment_replicate = SubFactory(TreatmentFactory)
status_date = FuzzyDate(date(2012, 1, 1))
status_time = LazyFunction(time)
count_alive = FuzzyInteger(0)
count_dead = FuzzyInteger(0)
flaw = SubFactory(FlawFactory)

View file

@ -53,7 +53,7 @@ class TreatmentType(models.Model):
def __str__(self):
return "{} {} {} {}".format(self.experiment, self.name,
self.treatment_type, self.placement)
self.treatment_type, self.placement)
class Meta:
unique_together = ('experiment', 'name')
@ -71,7 +71,8 @@ class Treatment(models.Model):
def save(self, *args, **kwargs):
self.display_name = "{}_{}_{}_{}".format(self.treatment_type,
self.study_location, self.species, self.sex)
self.study_location, self.species,
self.sex)
super(Treatment, self).save(*args, **kwargs)
def __str__(self):
@ -90,8 +91,8 @@ class TreatmentReplicate(models.Model):
def save(self, *args, **kwargs):
self.display_name = "{}_{}_{}_{}".format(self.treatment,
self.setup_date.date(), self.name,
self.setup_sample_size)
self.setup_date.date(), self.name,
self.setup_sample_size)
super(TreatmentReplicate, self).save(*args, **kwargs)
def __str__(self):

View file

View file