def test_filter_by_one_condition(self): """Test filter_by returns a list of blogposts that meet the filtering condition""" BlogpostFactory.create_batch(3, title='my blogpost') should_be_missing = BlogpostFactory.create(title='another blogpost') retrieved_blogposts = self.blog_repo.filter_by(title='my blogpost') assert len(retrieved_blogposts) == 3, retrieved_blogposts assert should_be_missing not in retrieved_blogposts, retrieved_blogposts
def test_filter_by_limit_offset(self): """Test that filter_by supports limit and offset options""" BlogpostFactory.create_batch(4) all_blogposts = self.blog_repo.filter_by() first_two = self.blog_repo.filter_by(limit=2) last_two = self.blog_repo.filter_by(limit=2, offset=2) assert len(first_two) == 2, first_two assert len(last_two) == 2, last_two assert first_two == all_blogposts[:2] assert last_two == all_blogposts[2:]
def test_blogpost_query_list_project_ids(self): """Get a list of blogposts using a list of project_ids.""" projects = ProjectFactory.create_batch(3) blogposts = [] for project in projects: tmp = BlogpostFactory.create_batch(2, project=project) for t in tmp: blogposts.append(t) project_ids = [project.id for project in projects] url = '/api/blogpost?project_id=%s&limit=100' % project_ids res = self.app.get(url) data = json.loads(res.data) assert len(data) == 3 * 2, len(data) for blog in data: assert blog['project_id'] in project_ids blogpost_project_ids = list(set([blog['project_id'] for blog in data])) assert sorted(project_ids) == sorted(blogpost_project_ids) # more filters res = self.app.get(url + '&orderby=created&desc=true') data = json.loads(res.data) assert data[0]['id'] == blogposts[-1].id
def test_query_blogpost(self): """Test API query for blogpost endpoint works""" owner = UserFactory.create() user = UserFactory.create() project = ProjectFactory(owner=owner) blogposts = BlogpostFactory.create_batch(9) blogpost = BlogpostFactory.create(project=project) # As anon url = '/api/blogpost' res = self.app.get(url) data = json.loads(res.data) assert len(data) == 10, data assert data[9]['user_id'] == owner.id # As user res = self.app.get(url + '?api_key=' + user.api_key) data = json.loads(res.data) assert len(data) == 0, data # As owner res = self.app.get(url + '?api_key=' + owner.api_key) data = json.loads(res.data) assert len(data) == 1, data assert data[0]['user_id'] == owner.id # Valid field but wrong value res = self.app.get(url + "?title=wrongvalue") data = json.loads(res.data) assert len(data) == 0, data # Multiple fields res = self.app.get(url + '?title=' + blogpost.title + '&body=' + blogpost.body) data = json.loads(res.data) # One result assert len(data) == 10, data # Correct result assert data[0]['title'] == blogpost.title, data assert data[0]['body'] == blogpost.body, data # Limits res = self.app.get(url + "?limit=1") data = json.loads(res.data) for item in data: assert item['title'] == blogpost.title, item assert len(data) == 1, data # Keyset pagination res = self.app.get(url + '?limit=1&last_id=' + str(blogposts[8].id)) data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0]['id'] == blogpost.id # Errors res = self.app.get(url + "?something") err = json.loads(res.data) err_msg = "AttributeError exception should be raised" res.status_code == 415, err_msg assert res.status_code == 415, err_msg assert err['action'] == 'GET', err_msg assert err['status'] == 'failed', err_msg assert err['exception_cls'] == 'AttributeError', err_msg # Desc filter url = "/api/blogpost?orderby=wrongattribute" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should be 415." assert data['status'] == 'failed', data assert data['status_code'] == 415, data assert 'has no attribute' in data['exception_msg'], data # Desc filter url = "/api/blogpost?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." blogposts.append(blogpost) blogposts_by_id = sorted(blogposts, key=lambda x: x.id, reverse=False) for i in range(len(blogposts)): assert blogposts_by_id[i].id == data[i]['id'] # Desc filter url = "/api/blogpost?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." blogposts_by_id = sorted(blogposts, key=lambda x: x.id, reverse=True) for i in range(len(blogposts)): assert blogposts_by_id[i].id == data[i]['id']
def test_query_blogpost(self): """Test API query for blogpost endpoint works""" owner = UserFactory.create() user = UserFactory.create() project = ProjectFactory(owner=owner) blogposts = BlogpostFactory.create_batch(9) blogpost = BlogpostFactory.create(project=project) # As anon url = '/api/blogpost' res = self.app.get(url) data = json.loads(res.data) assert len(data) == 10, data assert data[9]['user_id'] == owner.id # As user res = self.app.get(url + '?api_key=' + user.api_key) data = json.loads(res.data) assert len(data) == 0, data # As owner res = self.app.get(url + '?api_key=' + owner.api_key) data = json.loads(res.data) assert len(data) == 1, data assert data[0]['user_id'] == owner.id # Valid field but wrong value res = self.app.get(url + "?title=wrongvalue") data = json.loads(res.data) assert len(data) == 0, data # Multiple fields res = self.app.get(url + '?title=' + blogpost.title + '&body=' + blogpost.body) data = json.loads(res.data) # One result assert len(data) == 10, data # Correct result assert data[0]['title'] == blogpost.title, data assert data[0]['body'] == blogpost.body, data assert data[0]['media_url'] == blogpost.media_url, data # Limits res = self.app.get(url + "?limit=1") data = json.loads(res.data) for item in data: assert item['title'] == blogpost.title, item assert len(data) == 1, data # Keyset pagination res = self.app.get(url + '?limit=1&last_id=' + str(blogposts[8].id)) data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0]['id'] == blogpost.id # Errors res = self.app.get(url + "?something") err = json.loads(res.data) err_msg = "AttributeError exception should be raised" res.status_code == 415, err_msg assert res.status_code == 415, err_msg assert err['action'] == 'GET', err_msg assert err['status'] == 'failed', err_msg assert err['exception_cls'] == 'AttributeError', err_msg # Desc filter url = "/api/blogpost?orderby=wrongattribute" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should be 415." assert data['status'] == 'failed', data assert data['status_code'] == 415, data assert 'has no attribute' in data['exception_msg'], data # Desc filter url = "/api/blogpost?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." blogposts.append(blogpost) blogposts_by_id = sorted(blogposts, key=lambda x: x.id, reverse=False) for i in range(len(blogposts)): assert blogposts_by_id[i].id == data[i]['id'] # Desc filter url = "/api/blogpost?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." blogposts_by_id = sorted(blogposts, key=lambda x: x.id, reverse=True) for i in range(len(blogposts)): assert blogposts_by_id[i].id == data[i]['id']