def test_remove_from_cabinet(self): UserIngredient(user_id=1, ingredient=Ingredient.objects.get(name='Gin')).save() UserIngredient( user_id=1, ingredient=Ingredient.objects.get(name='Lemon Juice')).save() resp = self.client.put('/api/v1/users/1/cabinet/', ['Gin'], format='json') self.assertEqual(resp.status_code, 200) self.assertEqual([ ui.ingredient.name for ui in UserIngredient.objects.filter(user_id=1).iterator() ], ['Gin'])
def test_fetch_recipes_cabinet_filtering(self): """ Filter by the ingredients in your liquor cabinet """ for name in [ 'Gin', 'Green Chartreuse', 'Luxardo Maraschino', 'Lime Juice' ]: UserIngredient( user_id=1, ingredient=Ingredient.objects.get(name=name)).save() resp = self.client.get('/api/v1/recipes/', {'search': 'cabinet = true'}) self.assertEqual(len(resp.json()['results']), 1) self.assertEqual(resp.json()['results'][0]['name'], 'Last Word')
def to_internal_value(self, data): ingredient = next((i for i in self.context['ingredients'] if i.name == data), None) if ingredient is None: raise ValidationError(detail='Unknown ingredient') try: return next((ui for ui in self.context['user_ingredients'] if ui.ingredient.name == data)) except StopIteration: return UserIngredient( user=self.context['request'].user, ingredient=ingredient )