def test_meal_record_cascade_many(app):
    with app.app_context():
        # make sure we can have many records for one person
        # and many records for one meal
        p1 = Person(id="333")
        p2 = Person(id="444")
        m1 = Meal(id="123", name="meal1", servings=2)
        m2 = Meal(id="456", name="meal2", servings=2)

        entities = [p1, p2, m1, m2]

        p1m1 = MealRecord()
        p1m1.person = p1
        p1m1.meal = m1
        p1m1.amount = 1.5
        p1m1.timestamp = datetime.datetime.now()
        entities.append(p1m1)

        p1m2 = MealRecord()
        p1m2.person = p1
        p1m2.meal = m2
        p1m2.amount = 1
        p1m2.timestamp = datetime.datetime.now()
        entities.append(p1m2)

        p2m1 = MealRecord()
        p2m1.person = p2
        p2m1.meal = m1
        p2m1.amount = 1
        p2m1.timestamp = datetime.datetime.now()
        entities.append(p2m1)

        p2m2 = MealRecord()
        p2m2.person = p2
        p2m2.meal = m2
        p2m2.amount = 1
        p2m2.timestamp = datetime.datetime.now()
        entities.append(p2m2)

        for i in entities:
            db.session.add(i)
        db.session.commit()
        db.session.delete(p1)
        db.session.delete(m1)
        db.session.commit()
        fetched = MealRecord.query.filter(Person.id == p1.id).first()
        assert (fetched is None)
        fetched = MealRecord.query.filter(Meal.id == m1.id).first()
        assert (fetched is None)
        fetched = MealRecord.query.filter(Person.id == p2.id).first()
        assert (fetched is not None)
        fetched = MealRecord.query.filter(Meal.id == m2.id).first()
        assert (fetched is not None)

        for i in entities:
            db.session.delete(i)
        db.session.commit()
def test_meal_record_creation(app):
    with app.app_context():
        soup = Meal()
        soup.id = "1234"
        soup.name = "Fish Soup"
        soup.servings = 2.5
        db.session.add(soup)
        db.session.commit()

        person = Person()
        person.id = '4566'
        db.session.add(person)
        db.session.commit()

        mc = MealRecord()
        mc.meal = soup
        mc.person = person
        mc.amount = 1
        mc.timestamp = datetime.datetime.now()

        db.session.add(mc)
        db.session.commit()

        fetched = MealRecord.query.filter(
            MealRecord.person_id == person.id).first()
        assert (fetched.meal == soup)

        # extensive cleanup, will work even when cascade might be broken
        db.session.delete(mc)
        db.session.delete(soup)
        db.session.delete(person)
def test_meal_record_cascade_on_meal(app):
    with app.app_context():
        soup = Meal()
        soup.id = "678"
        soup.name = "Fish Soup"
        soup.servings = 2.5
        db.session.add(soup)
        db.session.commit()

        person = Person()
        person.id = '4566'
        db.session.add(person)
        db.session.commit()

        mc = MealRecord()
        mc.meal = soup
        mc.person = person
        mc.amount = 1.5
        mc.timestamp = datetime.datetime.now()

        db.session.add(mc)
        db.session.commit()

        db.session.delete(soup)
        db.session.commit()

        fetched = MealRecord.query.filter(
            MealRecord.person_id == person.id).first()
        assert (fetched is None)
def add_mealrecord_to_db(person_id, meal_id, timestamp):
    m = MealRecord()
    m.person_id = person_id
    m.meal_id = meal_id
    m.timestamp = timestamp
    m.amount = 4
    db.session.add(m)
    db.session.commit()
def db_load_example_data(db):
    person = Person()
    person.id = "123"

    portion1 = Portion()
    portion1.id = "olive-oil"
    portion1.calories = 700
    portion1.name = "Olive oil"
    portion1.density = 0.89
    portion1.fat = 100

    portion2 = Portion()
    portion2.id = "salmon"
    portion2.calories = 200
    portion2.name = "salmon"
    portion2.density = 0.95
    portion2.fat = 20

    portion3 = Portion()
    portion3.id = "cream"
    portion3.calories = 250
    portion3.name = "cream"
    portion3.density = 0.65
    portion3.fat = 15

    portion4 = Portion()
    portion4.id = "oat"
    portion4.calories = 250
    portion4.name = "oat"
    portion4.density = 0.65
    portion4.fat = 15

    portion5 = Portion()
    portion5.id = "milk"
    portion5.calories = 32
    portion5.name = "milk"
    portion5.density = 0.89
    portion5.fat = 2

    meal1 = Meal()
    meal1.id = "salmon-soup"
    meal1.name = "Salmon Soup"
    meal1.servings = 2.5

    meal2 = Meal()
    meal2.id = "oatmeal"
    meal2.name = "Oatmeal"
    meal2.servings = 2

    mp1 = MealPortion(meal_id=meal1.id,
                      portion_id=portion1.id,
                      weight_per_serving=10)
    mp2 = MealPortion(meal_id=meal1.id,
                      portion_id=portion2.id,
                      weight_per_serving=200)
    mp3 = MealPortion(meal_id=meal1.id,
                      portion_id=portion3.id,
                      weight_per_serving=200)

    mp4 = MealPortion(meal_id=meal2.id,
                      portion_id=portion4.id,
                      weight_per_serving=150)
    mp5 = MealPortion(meal_id=meal2.id,
                      portion_id=portion5.id,
                      weight_per_serving=50)

    m1 = MealRecord()
    m1.person = person
    m1.meal = meal1
    m1.amount = 1.5
    m1.timestamp = datetime.datetime.now()

    m2 = MealRecord()
    m2.person = person
    m2.meal = meal2
    m2.amount = 1
    m2.timestamp = datetime.datetime.now()

    m3 = MealRecord()
    m3.person = person
    m3.meal = meal1
    m3.amount = 1
    m3.timestamp = datetime.datetime(2020, 1, 31, 13, 14, 31)

    entities = [
        person, portion1, portion2, portion3, portion4, portion5, meal1, meal2,
        mp1, mp2, mp3, mp4, mp5, m1, m2, m3
    ]

    fetched = Person.query.filter(Person.id == person.id).first()
    if fetched is None:
        for i in entities:
            db.session.add(i)
        db.session.commit()
    else:
        return