def add_portion_to_db(portion_id, cal, c=0, p=0, f=0, a=0, density=1):
    portion = Portion()
    portion.id = portion_id
    portion.name = portion_id
    portion.calories = cal
    portion.density = density
    portion.alcohol = a
    portion.carbohydrate = c
    portion.protein = p
    portion.fat = f
    db.session.add(portion)
    db.session.commit()
def test_portion_with_valid_values(app):
    with app.app_context():
        """
        Test Portion with valid columns
        """
        pid = "olive-oil"
        name = "Olive oil"
        density = 0.89
        fat = 100
        calories = 700

        portion = Portion()
        portion.id = pid
        portion.calories = calories
        portion.name = name
        portion.density = density
        portion.fat = fat

        db.session.add(portion)
        db.session.commit()

        o = Portion.query.filter(Portion.id == pid).first()
        assert (o.name == name)
        assert (o.id == pid)
        assert (o.density == density)
        assert (o.fat == fat)
        assert (o.calories == calories)
def test_portion_defaults(app):
    with app.app_context():
        """
        Test Portion default values
        """
        pid = 'dummy-defaults'
        name = 'Dummy default'
        portion = Portion()
        portion.id = pid
        portion.calories = 320
        portion.name = name

        db.session.add(portion)
        db.session.commit()

        p = Portion.query.filter(Portion.id == pid).first()
        assert (p.density is None)
        assert (p.alcohol == 0)
        assert (p.protein == 0)
        assert (p.carbohydrate == 0)
        assert (p.fat == 0)
        assert (p.id == pid)
        assert (p.name == name)
def test_meal_portion_cascade_meal(app):
    with app.app_context():
        """
        Test for MealPortion cascade delete when meal is deleted
        """
        pid = "olive-oil"
        name = "Olive oil"
        density = 0.89
        fat = 100
        calories = 720

        portion = Portion()
        portion.id = pid
        portion.calories = calories
        portion.name = name
        portion.density = density
        portion.fat = fat

        db.session.add(portion)
        db.session.commit()

        soup = Meal()
        mid = "olive-oil-soup"
        soup.id = mid
        soup.name = "Olive oil Soup"
        soup.servings = 2.5

        db.session.add(soup)
        db.session.commit()

        # Olive oil soup is total of 2.5 servings and has 10g of oil per serving
        mp = MealPortion(meal_id=mid, portion_id=pid, weight_per_serving=10)

        db.session.add(mp)
        db.session.commit()

        soup = Meal.query.filter(Meal.id == mid).first()
        assert len(soup.portions) == 1
        assert soup.portions[0].portion_id == pid

        assert MealPortion.query.filter(
            MealPortion.meal_id == mid).first() is not None

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

        assert MealPortion.query.filter(
            MealPortion.meal_id == mid).first() is None
Ejemplo n.º 5
0
    def post(cls):
        try:
            if request.json is None:
                return error_415()
        except BadRequest:
            return error_415()

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

        portion_id = request.json['id']
        portion_name = request.json['name']
        portion_calories = request.json['calories']
        optional_fields = extract_optional_fields(request.json)

        # First set required fields
        portion = Portion(
            id=portion_id,
            name=portion_name,
            calories=portion_calories
        )
        # Then optional, which are stored in 'found' dict
        for k in optional_fields.keys():
            setattr(portion, k, optional_fields[k])

        db.session.add(portion)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            return error_409()

        h = add_mason_response_header()
        h.add('Location', api.url_for(PortionItem, handle=portion.id))

        return Response(
            status=201,
            headers=h
        )
def test_portion_columns(app):
    with app.app_context():
        """
        Tests for required columns Portion
        """
        portion = Portion()
        portion.id = "123"
        portion.calories = 325.5

        portion.name = None
        db.session.add(portion)
        with pytest.raises(IntegrityError):
            db.session.commit()

        db.session.rollback()

        portion.id = None
        db.session.add(portion)
        with pytest.raises(IntegrityError):
            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