def test_fetch_recipes_with_attr_constraint(self): """ Constrain a specific attribute """ comment = Comment(user_id=1, recipe_id=2, text='Delicious!') comment.save() resp = self.client.get('/api/v1/recipes/', {'search': 'comments > 0'}) self.assertEqual(len(resp.json()['results']), 1) self.assertEqual(resp.json()['results'][0]['id'], 2)
def test_fetch_recipes_with_special_characters(self): """ Constrain a specific attribute """ comment = Comment(user_id=1, recipe_id=2, text='Delicious!') comment.save() resp = self.client.get('/api/v1/recipes/', {'search': 'K\u00fcmmel'}) self.assertEqual(len(resp.json()['results']), 1) self.assertEqual(resp.json()['results'][0]['name'], 'End of Childcare Day')
def test_fetch_recipes_by_source(self): """ Constrain a specific attribute """ comment = Comment(user_id=1, recipe_id=2, text='Delicious!') comment.save() resp = self.client.get('/api/v1/recipes/', {'search': 'source = greg'}) self.assertEqual(len(resp.json()['results']), 2) self.assertEqual([r['name'] for r in resp.json()['results']], ['End of Childcare Day', 'Special Counsel'])
def test_comment_search(self): """ Search by comment text """ comment = Comment(user_id=2, recipe_id=2, text='Mediocre!') comment.save() resp = self.client.get('/api/v1/recipes/', {'search': 'comment = mediocre'}) self.assertEqual(len(resp.json()['results']), 1) self.assertEqual(resp.json()['results'][0]['id'], 2)
def test_fetch_recipes_comment_counts(self): """ Comment counts should be returned """ recipe = Recipe.objects.get(name='Last Word') comment = Comment(user_id=1, recipe_id=recipe.id, text='Delicious!') comment.save() recipe.comments.add(comment) resp = self.client.get('/api/v1/recipes/', {'search': 'last word'}) self.assertEqual(len(resp.json()['results']), 1) result = resp.json()['results'][0] self.assertEqual(result['name'], 'Last Word') self.assertEqual(result['comment_count'], 1)
def test_no_304_when_new_comment(self): """ Comment changes should update the recipe response """ stamp = now().isoformat() time.sleep(0.1) recipe = Recipe.objects.get(name='Last Word') Comment(text='Foo!', user_id=1, recipe=recipe).save() resp = self.client.get('/api/v1/recipes/', HTTP_IF_MODIFIED_SINCE=stamp, HTTP_X_COUNT='6') self.assertEqual(resp.status_code, 200)
def test_fetch_recipes_counts(self): """ Check that user list counts and user comment counts are sent when present """ ul = UserList(name='My List', user_id=1) ul.save() UserListRecipe(recipe_id=6, user_list=ul).save() Comment(user_id=1, recipe_id=6, text='Delicious!').save() resp = self.client.get('/api/v1/recipes/', {'search': 'childcare'}) results = resp.json()['results'] self.assertEqual(len(results), 1) self.assertEqual(results[0]['uc_count'], 1) self.assertEqual(results[0]['ul_count'], 1)