Esempio n. 1
0
    def post(self):
        beer_name = self.beer_args['beer_name']
        description = self.beer_args['description']
        harmonization = self.beer_args['harmonization']
        color = self.beer_args['color']
        alcohol = self.beer_args['alcohol']
        temperature = self.beer_args['temperature']
        beer_ingredients = self.ingredients_args['ingredients']

        beers = BeerModel(
            beer_name=beer_name.lower(),
            description=description,
            harmonization=harmonization,
            color=color,
            alcohol=alcohol,
            temperature=temperature
        )
        beers.save()

        beer_id = beers.id
        for ingredient in beer_ingredients['names']:
            ingredients = IngredientsModel(
                ingredient_name=ingredient,
                beer_id=beer_id
            )
            ingredients.save()
        return resp_successfully('Beer', 201)
Esempio n. 2
0
    def put(self, beer_id):
        beer_name = self.beer_args['beer_name']
        description = self.beer_args['description']
        harmonization = self.beer_args['harmonization']
        color = self.beer_args['color']
        alcohol = self.beer_args['alcohol']
        temperature = self.beer_args['temperature']
        beer_ingredients = self.ingredients_args['ingredients']

        query_beer = BeerModel.get_beer_id(beer_id)
        if not query_beer:
            return error_does_not_exist(None, beer_id)

        for ingredient in beer_ingredients['list']:
            query_ingredient = IngredientsModel.get_ingredient_id(
                ingredient['id'])
            if not query_ingredient:
                return error_does_not_exist(None, ingredient['id'])

            data_ingredient = {
                'ingredient_name': ingredient['ingredient_name'],
                'beer_id': beer_id
            }
            IngredientsModel.update(query_ingredient, data_ingredient)

        data_beer = {
            'beer_name': beer_name.lower(),
            'description': description,
            'harmonization': harmonization,
            'color': color,
            'alcohol': alcohol,
            'temperature': temperature
        }
        BeerModel.update(query_beer, data_beer)
        return resp_updated_successfully('Beer')
Esempio n. 3
0
 def test_update_beer(self):
     populate_beers(2)
     query_beer = Beer.get_beer_id(1)
     Beer.update(query_beer, NEW_BEER_DATA)
     get_beer = Beer.get_beer_id(1)
     assert get_beer.id == 1
     assert get_beer.beer_name == NEW_BEER_DATA['beer_name']
 def test_update_beer(self):
     populate_beers(2)
     new_data = update_beer
     query_beer = Beer.get_beer_id(1)
     Beer.update(query_beer, new_data)
     get_beer = Beer.get_beer_id(1)
     assert get_beer.id == 1
     assert get_beer.beer_name == new_data['beer_name']
Esempio n. 5
0
def populate_beers(amount):
    for r in range(0, amount):
        beer = Beer(beer_name=beers_data[r]['beer_name'],
                    description=beers_data[r]['description'],
                    color=beers_data[r]['color'],
                    alcohol=beers_data[r]['alcohol'],
                    temperature=beers_data[r]['temperature'],
                    harmonization=beers_data[r]['harmonization'])
        beer.save()

        beer_id = beer.id
        for ingredient in ingredients_data[r]['ingredients'][0]['names']:
            ingredients = BeerIngredients(ingredient_name=ingredient,
                                          beer_id=beer_id)
            ingredients.save()
 def test_filter_beer_by_temperature(self):
     populate_beers(2)
     beer_temperature = beers_data[0]['temperature']
     query_filter_temperature = Beer.filter_beer_temperature(beer_temperature)
     serialized = beers_serializer(query_filter_temperature, True)
     assert serialized[0]['temperature'] == beers_data[0]['temperature']
     assert serialized[0]['beer_name'] == beers_data[0]['beer_name']
 def test_filter_beer_by_name(self):
     populate_beers(2)
     beer_name = beers_data[0]['beer_name']
     query_filter_name = Beer.filter_beer_name(beer_name)
     serialized = beers_serializer(query_filter_name, True)
     assert serialized[0]['id'] == 1
     assert serialized[0]['beer_name'] == beers_data[0]['beer_name']
 def test_filter_beer_by_color(self):
     populate_beers(2)
     beer_color = beers_data[0]['color']
     query_filter_color = Beer.filter_beer_color(beer_color)
     serialized = beers_serializer(query_filter_color, True)
     assert serialized[0]['color'] == beers_data[0]['color']
     assert serialized[0]['beer_name'] == beers_data[0]['beer_name']
 def test_filter_beer_by_alcohol(self):
     populate_beers(2)
     beer_alcohol = beers_data[0]['alcohol']
     query_filter_alcohol = Beer.filter_beer_alcohol(beer_alcohol)
     serialized = beers_serializer(query_filter_alcohol, True)
     assert serialized[0]['alcohol'] == beers_data[0]['alcohol']
     assert serialized[0]['beer_name'] == beers_data[0]['beer_name']
Esempio n. 10
0
 def test_filter_beer_by_alcohol(self):
     populate_beers(2)
     beer_alcohol = BEERS_DATA[0]['alcohol']
     query_filter_alcohol = Beer.filter_beer_alcohol(beer_alcohol)
     serialized = beers_serializer(query_filter_alcohol, True)
     assert str(serialized[0]['alcohol']) == str(
         float(BEERS_DATA[0]['alcohol']))
     assert serialized[0]['beer_name'] == BEERS_DATA[0]['beer_name']
Esempio n. 11
0
    def get(self):
        beer_name = self.beer_args['beer_name']
        color = self.beer_args['color']
        alcohol = self.beer_args['alcohol']
        temperature = self.beer_args['temperature']
        ingredient = self.ingredients_args['ingredient']

        query_beers = BeerModel.get_beers()
        serialized = beers_serializer(query_beers, True)
        if not query_beers:
            return resp_empty_data_base()

        if beer_name:
            query_name = BeerModel.filter_beer_name(beer_name)
            error_does_not_exist(query_name, beer_name)
            serialized = serializer(query_name)
            return resp_content_successfully(serialized)

        if color:
            query_color = BeerModel.filter_beer_color(color)
            error_does_not_exist(query_color, color)
            serialized = serializer(query_color)
            return resp_content_successfully(serialized)

        if alcohol:
            query_alcohol = BeerModel.filter_beer_alcohol(alcohol)
            error_does_not_exist(query_alcohol, alcohol)
            serialized = serializer(query_alcohol)
            return resp_content_successfully(serialized)

        if temperature:
            query_temperature = BeerModel.filter_beer_temperature(temperature)
            error_does_not_exist(query_temperature, temperature)
            serialized = serializer(query_temperature)
            return resp_content_successfully(serialized)

        if ingredient:
            query_ingredient = IngredientsModel.filter_ingredient_name(
                ingredient)
            error_does_not_exist(query_ingredient, ingredient)
            serialized = add_ingredients(query_beers, serialized)
            response_parser = parser_beers(serialized, ingredient)
            return resp_content_successfully(response_parser)

        serialized = add_ingredients(query_beers, serialized)
        return resp_content_successfully(serialized)
Esempio n. 12
0
 def test_filter_beer_by_temperature(self):
     populate_beers(2)
     beer_temperature = BEERS_DATA[0]['temperature']
     query_filter_temperature = Beer.filter_beer_temperature(
         beer_temperature)
     serialized = beers_serializer(query_filter_temperature, True)
     assert str(serialized[0]['temperature']) == str(
         float(BEERS_DATA[0]['temperature']))
     assert serialized[0]['beer_name'] == BEERS_DATA[0]['beer_name']
Esempio n. 13
0
    def delete(beer_id):
        query_beer = BeerModel.get_beer_id(beer_id)
        if not query_beer:
            return error_does_not_exist(None, beer_id)

        query_ingredients = IngredientsModel.filter_beer_id(beer_id)
        for query in query_ingredients:
            ingredient = IngredientsModel.get_ingredient_id(query.id)
            ingredient.delete()
        query_beer.delete()
        return resp_deleted_successfully('Beer')
    def test_update_ingredients(self):
        populate_beers(3)
        new_beers = beers_data[1]
        new_ingredients = ingredients_data[2]['ingredients'][0]['names']

        query_beer = Beer.get_beer_id(2)
        query_ingredients = BeerIngredients.filter_beer_id(query_beer.id)
        count = 0
        for ingredient in new_ingredients:
            data_ingredients = {
                'ingredient_name': ingredient,
                'beer_id': query_beer.id
            }
            query_ingredient = query_ingredients[count]
            BeerIngredients.update(query_ingredient, data_ingredients)
            count += 1

        Beer.update(query_beer, new_beers)

        query_update_ingredients = BeerIngredients.filter_beer_id(query_beer.id)
        serialized = ingredients_serializer(query_update_ingredients, True)
        for r in range(0, len(serialized)):
            assert serialized[r]['ingredient_name'] == new_ingredients[r]
Esempio n. 15
0
    def get(beer_id):
        query_beer = BeerModel.get_beer_id(beer_id)
        if not query_beer:
            error_does_not_exist(None, beer_id)

        serialized = beers_serializer(query_beer, False)
        query_filter = IngredientsModel.filter_beer_id(beer_id)

        count = 0
        ingredients_list = []
        for item in query_filter:
            ingredients_list.append(ingredients_serializer(item, False))
            serialized['ingredients'] = ingredients_list
            count += 1
        return resp_content_successfully(serialized)
 def test_list_all_beers(self):
     populate_beers(2)
     get_all_beers = Beer.get_beers()
     serialized = beers_serializer(get_all_beers, True)
     assert len(serialized) > 0
     assert serialized[0]['beer_name'] == beers_data[0]['beer_name']
 def test_list_all_beers_empty(self):
     get_all_beers = Beer.get_beers()
     assert len(get_all_beers) == 0
 def test_delete_beer(self):
     populate_beers(2)
     query_beer = Beer.get_beer_id(1)
     query_beer.delete()
     get_beer = Beer.get_beer_id(1)
     assert get_beer is None
 def test_create_beer(self):
     populate_beers(2)
     get_beer = Beer.get_beer_id(1)
     assert get_beer.id == 1
     assert get_beer.beer_name == beers_data[0]['beer_name']
Esempio n. 20
0
 def test_create_beer(self):
     populate_beers(2)
     get_beer = Beer.get_beer_id(1)
     assert get_beer.id == 1
     assert get_beer.beer_name == BEERS_DATA[0]['beer_name']