diff --git a/ccdb/experiments/factories.py b/ccdb/experiments/factories.py new file mode 100644 index 0000000..e9ade70 --- /dev/null +++ b/ccdb/experiments/factories.py @@ -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) diff --git a/ccdb/experiments/models.py b/ccdb/experiments/models.py index e2934ee..546903e 100644 --- a/ccdb/experiments/models.py +++ b/ccdb/experiments/models.py @@ -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): diff --git a/ccdb/experiments/tests/__init__.py b/ccdb/experiments/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ccdb/experiments/tests/test_models.py b/ccdb/experiments/tests/test_models.py new file mode 100644 index 0000000..e69de29