def load_parent(p): parent = FosterParent( name=p["name"], address=p["address"], phone_number=p["phone"], max_animals=p["capacity"], ) for f in p["fostering"]: a = f["animal"] animal = Animal( name=a["name"], year_of_birth=a["year_of_birth"], gender=a["gender"], date_of_entry=parse_date(a["date_of_entry"]), species=a["species"], breed=a["breed"], ) foster = Foster( parent=parent, animal=animal, start_date=parse_date(f["start"]), end_date=parse_date(f.get("end")), ) animal.fosters.append(foster) parent.fosters.append(foster) return parent
def make_foster(*, parent=make_parent(), animal=make_animal(), start_date=date.today(), end_date=None): return Foster( parent=parent, animal=animal, start_date=start_date, end_date=end_date, )
def test_foster_parent(): parent = make_parent() assert type(parent.name) == str assert type(parent.address) == str assert type(parent.phone_number) == str assert type(parent.max_animals) == int assert type(parent.fosters) == list now = date.today() assert parent.has_animals(date=now) == 0 assert parent.is_available(date=now) foster = Foster(parent=parent, start_date=now - timedelta(days=1)) parent.fosters.append(foster) assert parent.has_animals(date=now) == 1 assert not parent.is_available(date=now) foster.end_date = now assert parent.has_animals(date=now + timedelta(days=1)) == 0 assert parent.is_available(date=now + timedelta(days=1))
def test_foster(): now = date.today() parent = FosterParent(name="Kek", address="Bur 1337", phone_number="13371337", max_animals=1) foster = Foster(parent=parent, start_date=now) assert type(foster.parent) == FosterParent assert type(foster.start_date) == date assert type(foster.end_date) == type(None) assert foster.is_colliding(date=now + timedelta(days=1)) assert not foster.is_colliding(date=now - timedelta(days=1)) foster.end_date = now + timedelta(days=2) assert foster.is_colliding(date=now + timedelta(days=1)) assert not foster.is_colliding(date=now + timedelta(days=3))
def load_animal(a): animal = Animal( name=a["name"], year_of_birth=a["year_of_birth"], gender=a["gender"], date_of_entry=parse_date(a["date_of_entry"]), species=a["species"], breed=a["breed"], ) for f in a["fostering"]: p = f["parent"] parent = FosterParent( name=p["name"], address=p["address"], phone_number=p["phone"], max_animals=None ) foster = Foster( parent=parent, animal=animal, start_date=parse_date(f["start"]), end_date=parse_date(f.get("end")), ) animal.fosters.append(foster) parent.fosters.append(foster) for e in a["exams"]: exam = Exam( vet=e["vet"], date=parse_date(e["date"]), report=e["report"], ) animal.exams.append(exam) adopted = a.get("adopted") if adopted is not None: adoption = Adoption( date=parse_date(adopted["date"]), adopter_name=adopted["name"], adopter_address=adopted["address"], ) animal.adoption = adoption return animal
def load(id, *, db): shelter = Shelter(id=id) foster_parents = {} shelter_parents = db.execute(query_select_shelter_parents, (shelter.id, )).fetchall() for (pid, parent_id, max_animals) in shelter_parents: (name, address, phone_number) = db.execute(query_select_foster_parent, (parent_id, )).fetchone() parent = FosterParent( id=pid, name=name, address=address, phone_number=phone_number, max_animals=max_animals, ) foster_parents[parent.id] = parent shelter.foster_parents.append(parent) shelter_animals = db.execute(query_select_shelter_animals, (shelter.id, )).fetchall() for (aid, animal_id, date_of_entry) in shelter_animals: (name, year_of_birth, gender, species, breed) = db.execute(query_select_animal, (animal_id, )).fetchone() animal = Animal( id=aid, name=name, year_of_birth=year_of_birth, gender=gender, date_of_entry=datetime.strptime(date_of_entry, "%Y-%m-%d").date(), species=species, breed=breed, ) exams = db.execute(query_select_exams, (aid, )).fetchall() for (exam_id, vet, date, report) in exams: exam = Exam( id=exam_id, vet=vet, date=datetime.strptime(date, "%Y-%m-%d").date(), report=report, ) animal.exams.append(exam) fosters = db.execute(query_select_fosters, (aid, )).fetchall() for (foster_id, parent_id, start_date, end_date) in fosters: parent = foster_parents[parent_id] foster = Foster( id=foster_id, parent=parent, animal=animal, start_date=datetime.strptime(start_date, "%Y-%m-%d").date(), end_date=None if end_date == None else datetime.strptime( end_date, "%Y-%m-%d").date(), ) animal.fosters.append(foster) parent.fosters.append(foster) adoption = db.execute(query_select_adoption, (aid, )).fetchone() if adoption is not None: (adopter_name, adopter_address, date) = adoption animal.adoption = Adoption( id=aid, date=datetime.strptime(date, "%Y-%m-%d").date(), adopter_name=adopter_name, adopter_address=adopter_address, ) shelter.animals.append(animal) return shelter