def test_add_existing_pantry_ingredients(self): dbrb = json_to_ingredient(self.rootbeer, access_db=True) dbic = json_to_ingredient(self.icecream, access_db=True) user = json_to_user(self.user_obj, access_db=True) db.session.commit() i = Ingredient.query.filter(Ingredient.name == 'rootbeer').first() u = User.query.filter(User.username == 'Frank').first() pantry_ingredient = PantryIngredient(ingredient=i, user=u, value=11.0) db.session.add(pantry_ingredient) with self.client: response = req_user_login(self, 'Frank', 'magnum') user_data = json.loads(response.data.decode()) ingredients_to_quantity = {'rootbeer': 7.0, 'icecream': 3.0} response = req_add_ingredient_to_pantry(self, ingredients_to_quantity, user_data) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') ing = Ingredient.query.filter( Ingredient.name == 'rootbeer').first() use = User.query.filter(User.username == 'Frank').first() pan = PantryIngredient.query.filter( (PantryIngredient.user_id == use.id) & (PantryIngredient.ingredient_id == ing.id)).first() self.assertEqual(pan.value, 7.0)
def seed_sampledata(): """ Seeds the db with sample data. """ with open('../sampledata/sampleingredients.json') as f: contents = json.load(f) added = [ json_to_ingredient(x, access_db=True) for x in contents['Ingredient'] ] # db.session.add_all(to_add) db.session.commit() with open('../sampledata/sampleusers.json') as f: contents = json.load(f) added = [json_to_user(x, access_db=True) for x in contents['Users']] # db.session.add_all(to_add) db.session.commit() with open('../sampledata/samplerecipes.json') as f: contents = json.load(f) added = [ json_to_recipe(x, access_db=True) for x in contents['Recipes'] ] # db.session.add_all(to_add) db.session.commit()
def test_prepare_recipe(self): """ Test user cooking a recipe """ meat = {'name': 'Meat', 'measurement': 'MASS', 'value': 5.0} water = {'name': 'Dirty Water', 'measurement': 'VOLUME', 'value': 5.0} dog_food = {'name': 'Dog Food', 'measurement': 'MASS', 'value': 5.0} meat_water = { 'name': 'Meat Water', 'ingredients': [meat, water], 'description': 'Watery meat', 'steps': ['Place meat in bowl', 'Add water'], 'rating': 1.0 } dog_food_recipe = { 'name': 'Dog Food Recipe', 'ingredients': [dog_food], 'description': 'This is for dogs. Do not eat.', 'steps': ['Place in dog bowl.', 'Consume.'], 'rating': 3.0 } user_obj = { 'email': '*****@*****.**', 'username': '******', 'password': '******', 'ingredients': [meat, water] } dbmeat = json_to_ingredient(meat, access_db=True) dbwater = json_to_ingredient(water, access_db=True) dbdog_food = json_to_ingredient(dog_food, access_db=True) user = json_to_user(user_obj, access_db=True) db.session.commit() recipe = json_to_recipe(meat_water, access_db=True) df_recipe = json_to_recipe(dog_food_recipe, access_db=True) db.session.commit() with self.client: response = req_user_login(self, 'Frank', 'magnum') user_data = json.loads(response.data.decode()) response = req_prepare_recipe(self, user_data, recipe) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(data['has_all_ingredients'], True) response = req_prepare_recipe(self, user_data, recipe) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'fail')
def test_create_recipe(self): """ Test user creating a recipe """ meat = {'name': 'Meat', 'measurement': 'MASS', 'value': 5.0} water = {'name': 'Dirty Water', 'measurement': 'VOLUME', 'value': 5.0} meat_water = { 'name': 'Meat Water', 'ingredients': [meat, water], 'description': 'Watery meat', 'steps': ['Place meat in bowl', 'Add water'], } user_obj = { 'email': '*****@*****.**', 'username': '******', 'password': '******', 'ingredients': [meat, water] } dbmeat = json_to_ingredient(meat, access_db=True) dbwater = json_to_ingredient(water, access_db=True) user = json_to_user(user_obj, access_db=True) db.session.commit() with self.client: response = req_user_login(self, 'Frank', 'magnum') user_data = json.loads(response.data.decode()) response = req_create_recipe(self, user_data, meat_water) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') response = req_create_recipe(self, user_data, meat_water) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'fail')
def test_get_ingredients(self): dbrb = json_to_ingredient(self.rootbeer, access_db=True) dbic = json_to_ingredient(self.icecream, access_db=True) user = json_to_user(self.user_obj, access_db=True) db.session.commit() with self.client: response = req_user_login(self, 'Frank', 'magnum') user_data = json.loads(response.data.decode()) ingredients_to_quantity = {'rootbeer': 7.0, 'icecream': 3.0} response = req_add_ingredient_to_pantry(self, ingredients_to_quantity, user_data) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') response = get_ingredients_from_pantry(self, user_data) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') for entry in data['data']['ingredients']: if entry['name'] == 'rootbeer': self.assertEqual(entry['value'], 7.0) elif entry['name'] == 'icecream': self.assertEqual(entry['value'], 3.0)
def test_search_recipes(self): """ Test searching for recipes """ meat = {'name': 'Meat', 'measurement': 'MASS', 'value': 5.0} water = {'name': 'Dirty Water', 'measurement': 'VOLUME', 'value': 5.0} dog_food = {'name': 'Dog Food', 'measurement': 'MASS', 'value': 5.0} meat_water = { 'name': 'Meat Water', 'ingredients': [meat, water], 'description': 'Watery meat', 'steps': ['Place meat in bowl', 'Add water'], 'rating': 1.0 } dog_food_recipe = { 'name': 'Dog Food Recipe', 'ingredients': [dog_food], 'description': 'This is for dogs. Do not eat.', 'steps': ['Place in dog bowl.', 'Consume.'], 'rating': 3.0 } user_obj = { 'email': '*****@*****.**', 'username': '******', 'password': '******', 'ingredients': [meat, water] } dbmeat = json_to_ingredient(meat, access_db=True) dbwater = json_to_ingredient(water, access_db=True) dbdog_food = json_to_ingredient(dog_food, access_db=True) user = json_to_user(user_obj, access_db=True) db.session.commit() recipe = json_to_recipe(meat_water, access_db=True) df_recipe = json_to_recipe(dog_food_recipe, access_db=True) db.session.commit() meat_water_simple = { 'name': 'Meat Water', 'description': 'Watery meat', 'rating': 1.0 } dog_food_simple = { 'name': 'Dog Food Recipe', 'description': 'This is for dogs. Do not eat.', 'rating': 3.0 } meat_water_simple['recipe_id'] = str(uuid.UUID(hex=recipe.id.hex)) with self.client: response = req_user_login(self, 'Frank', 'magnum') user_data = json.loads(response.data.decode()) response = req_search_recipe(self, user_data, filter_='true') data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(data['data']['recipes'], [meat_water_simple]) response = req_search_recipe(self, user_data, 'meat water', filter_='true') data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(data['data']['recipes'], [meat_water_simple]) response = req_search_recipe(self, user_data, 'random query', filter_='true') data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(data['data']['recipes'], []) response = req_search_recipe(self, user_data) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') # print(data['data']['recipes']) self.assertEqual(len(data['data']['recipes']), 2)
def test_rate_recipe(self): """ Test rating a recipe """ meat = {'name': 'Meat', 'measurement': 'MASS', 'value': 5.0} water = {'name': 'Dirty Water', 'measurement': 'VOLUME', 'value': 5.0} dog_food = {'name': 'Dog Food', 'measurement': 'MASS', 'value': 5.0} meat_water = { 'name': 'Meat Water', 'ingredients': [meat, water], 'description': 'Watery meat', 'steps': ['Place meat in bowl', 'Add water'], } frank_obj = { 'email': '*****@*****.**', 'username': '******', 'password': '******', 'ingredients': [meat, water] } dennis_obj = { 'email': '*****@*****.**', 'username': '******', 'password': '******', 'ingredients': [meat, water] } dbmeat = json_to_ingredient(meat, access_db=True) dbwater = json_to_ingredient(water, access_db=True) frank = json_to_user(frank_obj, access_db=True) dennis = json_to_user(dennis_obj, access_db=True) db.session.commit() recipe = json_to_recipe(meat_water, access_db=True) db.session.commit() with self.client: response = req_user_login(self, 'Frank', 'magnum') user_data = json.loads(response.data.decode()) response = req_rate_recipe(self, user_data, recipe, 5.0) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(recipe.rating, 5.0) response = req_rate_recipe(self, user_data, recipe, 1.0) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(recipe.rating, 1.0) response = req_user_login(self, 'Dennis', 'mistertibbs') user_data = json.loads(response.data.decode()) response = req_rate_recipe(self, user_data, recipe, 5.0) data = json.loads(response.data.decode()) self.assertEqual(data['status'], 'success') self.assertEqual(recipe.rating, 3.0)