def test_get_herd_from_file(): path = os.path.join(os.path.dirname(__file__), 'test_herd.xml') expected_herd = [ LabYak(name='Princess', age_in_days=400, sex=Sex.female), LabYak(name='Prince', age_in_days=800, sex=Sex.male) ] actual_herd = get_herd_from_file(path) assert actual_herd == expected_herd
def test_production_report(monkeypatch): def fake_milking(labyak: LabYak, day: int) -> float: return labyak.age_in_days def fake_skins_grown(labyak: LabYak, day: int) -> float: return labyak.age_in_days monkeypatch.setattr("shepherd.product_calculator.milking", fake_milking) monkeypatch.setattr("shepherd.product_calculator.skins_grown", fake_skins_grown) herd = [ LabYak("Princess", 1, Sex.female), LabYak("Princess", 10, Sex.female), LabYak("Princess", 100, Sex.female), ] produced = production_report(herd, 3) assert produced == StockReport(3, 111, 111)
def test_print_production_report(monkeypatch): def fake_production_report(herd: List[LabYak], day: int) -> StockReport: return StockReport(3, 111.0047, 111) monkeypatch.setattr("shepherd.product_calculator.production_report", fake_production_report) herd = [ LabYak("Princess", 10, Sex.female), LabYak("Princess", 100, Sex.female), LabYak("Princess", 809, Sex.female), ] expected_report = 'In stock:\n' \ ' 111.005 liters of milk\n' \ ' 111 skins of wool\n' \ 'Herd:\n' \ ' Princess 0.13 years old\n' \ ' Princess 1.03 years old\n' \ ' Princess 8.12 years old' actual_report = print_production_report(herd, 3) assert actual_report == expected_report
import sqlite3 import pytest from shepherd import storage from shepherd.db import get_db from shepherd.model import LabYak, Sex, Order, OrderStatus test_herd = [ LabYak("Betty-1", 400, Sex.female), LabYak("Betty-2", 800, Sex.female), LabYak("Betty-3", 950, Sex.female), ] test_orders = [ Order('Frodo', 14, 1100, 3, 1100, 3, OrderStatus.succeeded), Order('Sam', 14, 1200, 3, 0, 3, OrderStatus.partially_succeeded), Order('Pipin', 14, 2000, 13, 0, 0, OrderStatus.failed), Order('Merry', 40, 1000, 1, 1000, 1, OrderStatus.succeeded), ] def test_get_close_db(app): with app.app_context(): db = get_db() assert db is get_db() with pytest.raises(sqlite3.ProgrammingError) as e: db.execute('SELECT 1') assert 'closed' in str(e.value)
def __dummy_labyak() -> LabYak: return LabYak("Betty-1", 400, Sex.female)
def __dict_to_labyak(d: dict) -> LabYak: return LabYak( name=d['name'], age_in_days=int(float(d['age']) * 100), sex=Sex.male if d['sex'] == 'm' else Sex.female )
def labyak_to_model(record) -> LabYak: return LabYak( name=record['name'], age_in_days=record['age'], sex=Sex.male if record['sex'] else Sex.female, )
def test_wool_production_before_shaving_age(age, day, skins): labyak = LabYak('Princess', age, Sex.female) skins_produced = round(skins_grown(labyak, day), 3) assert skins_produced == skins
def test_wool_production_after_death(age, day, skins): labyak = LabYak('Princess', age, Sex.female) skins_produced = round(skins_grown(labyak, day), 3) assert skins_produced == skins
def test_male_doesnt_produce_milk(): labyak = LabYak('Prince', 100, Sex.male) milk_produced = round(milking(labyak, 200), 3) assert milk_produced == 0
def test_milk_production_after_death(age, day, milk_liters): labyak = LabYak('Princess', age, Sex.female) milk_produced = round(milking(labyak, day), 3) assert milk_produced == milk_liters
def test_latest_shaving_day(age, day, latest_day): labyak = LabYak("Princess", age, Sex.female) assert latest_shaving_day(labyak, day) == latest_day
def test_empty_shaving_schedule_for_dead_labyak(): labyak = LabYak("Princess", 1000, Sex.female) actual_schedule = shaving_schedule(labyak) assert not actual_schedule
def test_shaving_schedule_starts_after_certain_age(): labyak = LabYak("Princess", 10, Sex.female) actual_schedule = shaving_schedule(labyak) assert actual_schedule[0] == 90 assert all(d >= 90 for d in actual_schedule)
def test_shaving_schedule(): labyak = LabYak("Princess", 950, Sex.female) expected_schedule = [0, 18, 36] actual_schedule = shaving_schedule(labyak) assert actual_schedule == expected_schedule