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 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_activity_record_cascade_on_activity(app):
    with app.app_context():
        running = Activity()
        running.id = "1234"
        running.name = "Running"
        running.intensity = 600  # 600kcal per hour
        db.session.add(running)
        db.session.commit()

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

        ac = ActivityRecord()
        ac.activity = running
        ac.person = runner
        ac.duration = 3600
        ac.timestamp = datetime.datetime.now()

        db.session.add(ac)
        db.session.commit()

        db.session.delete(running)
        db.session.commit()
        fetched = ActivityRecord.query.filter(
            ActivityRecord.activity_id == running.id).first()
        assert (fetched is None)
def test_activity_record_creation(app):
    with app.app_context():
        running = Activity()
        running.id = "1234"
        running.name = "Running"
        running.intensity = 600  # 600kcal per hour
        db.session.add(running)
        db.session.commit()

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

        ac = ActivityRecord()
        ac.activity = running
        ac.person = runner
        ac.duration = 3600
        ac.timestamp = datetime.datetime.now()

        db.session.add(ac)
        db.session.commit()

        fetched = ActivityRecord.query.filter(
            ActivityRecord.person_id == runner.id).first()
        assert (fetched.activity == running)

        # extensive cleanup, will work even when cascade might be broken
        db.session.delete(ac)
        db.session.delete(running)
        db.session.delete(runner)
def test_person_unique(app):
    with app.app_context():
        same_id = "414"
        p1 = Person(id=same_id)
        p2 = Person(id=same_id)
        db.session.add(p1)
        db.session.add(p2)
        with pytest.raises(IntegrityError):
            db.session.commit()
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_activity_record_cascade_many(app):
    with app.app_context():
        # make sure we can have many records for one person
        # and many records for one activity
        p1 = Person(id="333")
        p2 = Person(id="444")
        a1 = Activity(id="123", name="act1", intensity=200)
        a2 = Activity(id="456", name="act2", intensity=200)

        entities = [p1, p2, a1, a2]

        p1a1 = ActivityRecord()
        p1a1.person = p1
        p1a1.activity = a1
        p1a1.duration = 100
        p1a1.timestamp = datetime.datetime.now()
        entities.append(p1a1)

        p1a2 = ActivityRecord()
        p1a2.person = p1
        p1a2.activity = a2
        p1a2.duration = 100
        p1a2.timestamp = datetime.datetime.now()
        entities.append(p1a2)

        p2a1 = ActivityRecord()
        p2a1.person = p2
        p2a1.activity = a1
        p2a1.duration = 100
        p2a1.timestamp = datetime.datetime.now()
        entities.append(p2a1)

        p2a2 = ActivityRecord()
        p2a2.person = p2
        p2a2.activity = a2
        p2a2.duration = 100
        p2a2.timestamp = datetime.datetime.now()
        entities.append(p2a2)

        for i in entities:
            db.session.add(i)
        db.session.commit()
        db.session.delete(p1)
        db.session.delete(a1)
        db.session.commit()
        fetched = ActivityRecord.query.filter(Person.id == p1.id).first()
        assert (fetched is None)
        fetched = ActivityRecord.query.filter(Activity.id == a1.id).first()
        assert (fetched is None)
        fetched = ActivityRecord.query.filter(Person.id == p2.id).first()
        assert (fetched is not None)
        fetched = ActivityRecord.query.filter(Activity.id == a2.id).first()
        assert (fetched is not None)

        for i in entities:
            db.session.delete(i)
        db.session.commit()
Beispiel #8
0
    def post(cls):
        try:
            if request.json is None:
                return error_415()
        except BadRequest:
            return error_415()

        try:
            validate(request.json, schema=person_schema())
        except (SchemaError, ValidationError):
            return error_400()

        person_id = request.json['id']
        person = Person(id=person_id)
        db.session.add(person)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            return error_409()

        h = add_mason_response_header()
        h.add('Location', api.url_for(PersonItem, handle=person.id))

        return Response(
            status=201,
            headers=h
        )
def test_person_combo(app):
    with app.app_context():
        # create original person (id=677) and commit to db
        pid = "677"
        p_original = Person(id=pid)
        db.session.add(p_original)
        db.session.commit()
        # fetch the original person from the DB as 'fetched'
        fetched = Person.query.filter(Person.id == pid).first()
        assert (fetched.id == pid)
        # change the fetched ID and commit to the DB, primary key still unique
        new_id = "677-modified"
        fetched.id = new_id
        db.session.add(fetched)
        db.session.commit()
        # delete the entity
        db.session.delete(fetched)
        db.session.commit()
        deleted = Person.query.filter(Person.id == new_id).first()
        assert (deleted is None)
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
def test_person_creation(app):
    with app.app_context():
        person = Person()
        person.id = "123"
        db.session.add(person)
        db.session.commit()
def test_person_id_required(app):
    with app.app_context():
        person = Person()
        db.session.add(person)
        with pytest.raises(IntegrityError):
            db.session.commit()
def add_person_to_db(person_id):
    p = Person()
    p.id = person_id
    db.session.add(p)
    db.session.commit()