def populate_crops(): for common_name in CROPS: common_name = common_name family = CROPS[common_name][0] genus = CROPS[common_name][1] species = CROPS[common_name][2] existing_family = Family.objects.filter(name=family) if existing_family.count() > 0: print " Family ", family, " already exists in database." existing_family = existing_family[0] else: f = Family(name=family) f.save() existing_family = f print " Saved", family, "as Family." existing_genus = Genus.objects.filter(name=genus) if existing_genus.count() > 0: print " Genus ", genus, " already exists in database." existing_genus = existing_genus[0] else: g = Genus(name=genus, family=existing_family) g.save() existing_genus = g print " Saved", genus, "as Genus." existing_species = Species.objects.filter(species=species, genus=existing_genus) if existing_species.count() > 0: print " Species ", species, " already exists in database." existing_species = existing_species[0] else: s = Species(species=species, genus=existing_genus) s.save() existing_species = s print " Saved", species, "as Species." existing_crop = Crop.objects.filter(species=existing_species) if existing_crop.count() > 0: print " Crop ", common_name, " already exists in database." existing_crop = existing_crop[0] else: c = Crop(species=existing_species) c.save() existing_crop = c print " Saved", common_name, "as Crop." existing_common_name = CommonName.objects.filter(crop=existing_crop, name=common_name) if existing_common_name.count() > 0: print " Common name for ", species, " already exists in database." existing_common_name = existing_common_name[0] else: cn = CommonName(crop=existing_crop, name=common_name, preferred=True) cn.save() existing_common_name = cn print " Saved", common_name, " for ", species, "as CommonName."
def populate_families(): for name in FAMILIES: existing_family = Family.objects.filter(name=name) if existing_family.count() > 0: print " Family ", name, " already exists in database." else: f = Family(name=name) f.save() print " Saved", name, "as Family."
class PersonTest(TestCase): """ Tests the behaviour of class Person. """ def setUp(self): """ Initialize needed resources. """ self.d = datetime.datetime(2011, 10, 1, 15, 26) self.ff = Family() self.ff.sure_name = u"Father family's sure name" self.ff.leadership = Family.LEADERSHIP_PATRIARCHAL_CHOICE self.ff.save() self.father = Person() self.father.sex = Person.SEX_MALE_CHOICE self.father.name = u"Father's name" self.father.family = self.ff self.father.born_in = self.d self.father.save() self.mf = Family() self.mf.sure_name = u"Mother family's sure name" self.mf.leadership = Family.LEADERSHIP_MATRIARCHAL_CHOICE self.mf.save() self.mother = Person() self.mother.sex = Person.SEX_FEMALE_CHOICE self.mother.name = u"Mother's name" self.mother.family = self.mf self.mother.born_in = self.d self.mother.save() self.son = Person() self.son.name = u"Son's name" self.son.father = self.father self.son.mother = self.mother self.son.born_in = self.d self.son.family = self.ff self.son.save() self.other_son = Person() self.other_son.name = u"Other son's name" self.other_son.father = self.father self.other_son.mother = self.mother self.other_son.born_in = self.d self.other_son.family = self.ff self.other_son.save() def test_family_leader_patriarchal(self): """ Must return the correct family leader based patriarchal configured leadership. """ self.son.family = self.ff self.assertEqual(self.son.family_leader(), self.father) def test_family_leader_matriarchal(self): """ Must return the correct family leader based matriarchal configured leadership. """ self.son.family = self.mf self.assertEqual(self.son.family_leader(), self.mother) def test_family_leader_not_defined(self): """ Must return a None object when the leadership is not defined. """ self.son.family = self.mf self.son.family.leadership = None self.assertIsNone(self.son.family_leader()) def test_is_alive_not_defined(self): """ Tests the return value when a Person hasn't defined a born date nor a deceased date. """ self.son.born_in = None self.assertIsNone(self.son.is_alive()) def test_is_alive_not_alive(self): """ Tests the return value when a Person is dead, based on the object dates. """ self.son.born_in = self.d self.son.deceased_in = self.d self.assertFalse(self.son.is_alive()) def test_is_alive_alive(self): """ Tests the return value when a Person is dead, based on the object dates. """ self.son.born_in = self.d self.assertTrue(self.son.is_alive()) def test_brothers(self): """ Tests the ``brothers()`` method to validate that returns other :model:`family.Person` instances related by parents, but not the current object. """ self.assertTrue(self not in self.son.brothers()) def test_parents(self): """ Tests the ``parents()`` method to validate that returns other :model:`family.Person` instances related to the current one as parents. """ parents = self.son.parents() self.assertEqual(2, len(parents)) self.assertTrue(self.son.father in parents) self.assertTrue(self.son.mother in parents) def test_descendence_invalid(self): """ Tests the ``descendence()`` method to validate that raises an exception if current instance has not sex value defined. """ self.father.sex = None self.assertRaises(ValueError, self.father.descendence) def test_father_descendence(self): """ Tests the ``descendence()`` method to validate that returns other :model:`family.Person` instances related to the current one as father's descendence. """ d = self.father.descendence() self.assertEqual(2, len(d)) self.assertTrue(self.son in d) self.assertTrue(self.other_son in d) def test_mother_descendence(self): """ Tests the ``descendence()`` method to validate that returns other :model:`family.Person` instances related to the current one as mother's descendence. """ d = self.mother.descendence() self.assertEqual(2, len(d)) self.assertTrue(self.son in d) self.assertTrue(self.other_son in d)