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')
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)
def test_delete_ingredients(self): populate_beers(3) query_ingredients = BeerIngredients.filter_beer_id(1) for query in query_ingredients: ingredient = BeerIngredients.get_ingredient_id(query.id) ingredient.delete() query_ingredients = BeerIngredients.filter_beer_id(1) assert len(query_ingredients) == 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 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 delete(ingredient_id): query_ingredient = IngredientsModel.get_ingredient_id(ingredient_id) if not query_ingredient: return error_does_not_exist(None, ingredient_id) query_ingredient.delete() return resp_successfully('Ingredient', 202)
def put(self, ingredient_id): ingredient_name = self.ingredients_args['ingredient_name'] beer_id = self.ingredients_args['beer_id'] query_ingredient = IngredientsModel.get_ingredient_id(ingredient_id) if not query_ingredient: return error_does_not_exist(None, ingredient_id) query_filter = IngredientsModel.filter_beer_id(beer_id) serialized = ingredients_serializer(query_filter, True) for item in serialized: if item['ingredient_name'] == ingredient_name: return error_already_exists() data = {'ingredient_name': ingredient_name, 'beer_id': beer_id} IngredientsModel.update(query_ingredient, data) return resp_successfully('Ingredient', 201)
def post(self): ingredient_name = self.ingredients_args['ingredient_name'] beer_id = self.ingredients_args['beer_id'] query_filter = IngredientsModel.filter_beer_id(beer_id) if not query_filter: error_does_not_exist(None, beer_id) serialized = ingredients_serializer(query_filter, True) for item in serialized: if item['ingredient_name'] == ingredient_name: return error_already_exists() ingredients = IngredientsModel(ingredient_name=ingredient_name, beer_id=beer_id) ingredients.save() return resp_successfully('Ingredient', 201)
def add_ingredients(query, serialized): count = 0 ingredients_list = [] for beer in query: query_filter = IngredientsModel.filter_beer_id(beer.id) if query_filter: ingredients_list.append(ingredients_serializer(query_filter, True)) serialized[count]['ingredients'] = ingredients_list[0] count += 1 ingredients_list = [] return serialized
def test_filter_ingredients_by_name(self): populate_beers(3) query_ingredients = BeerIngredients.filter_ingredient_name('cevada') serialized = ingredients_serializer(query_ingredients, True) expected = [ {'beer_id': 1, 'id': 1, 'ingredient_name': 'cevada'}, {'beer_id': 2, 'id': 5, 'ingredient_name': 'cevada'}, {'beer_id': 3, 'id': 8, 'ingredient_name': 'cevada'} ] assert len(serialized) == 3 for i in range(0, 3): assert serialized[i] == expected[i]
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]
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 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)
def test_filter_by_beer_id(self): populate_beers(3) query_beer_id = BeerIngredients.filter_beer_id(1) serialized = ingredients_serializer(query_beer_id, True) assert serialized[0]['beer_id'] == 1
def test_get_ingredient_by_id(self): populate_beers(3) query_ingredient = BeerIngredients.get_ingredient_id(1) serialized = ingredients_serializer(query_ingredient, False) assert serialized['beer_id'] == 1 assert serialized['ingredient_name'] == 'cevada'
def test_get_ingredients(self): populate_beers(3) query_ingredients = BeerIngredients.get_ingredients() serialized = ingredients_serializer(query_ingredients, True) assert serialized[0]['beer_id'] == 1 assert serialized[0]['ingredient_name'] == 'cevada'
def test_create_ingredients(self): populate_beers(3) get_ingredients = BeerIngredients.get_ingredient_id(1) assert get_ingredients.id == 1 assert get_ingredients.ingredient_name == 'cevada'