def test_filter_by_one_condition(self): """Test filter_by returns a list of pages that meet the filtering condition""" PageFactory.create_batch(3, slug="algo") should_be_missing = PageFactory.create(slug="new") retrieved_pages = self.page_repo.filter_by(slug="algo") assert len(retrieved_pages) == 3, retrieved_pages assert should_be_missing not in retrieved_pages, retrieved_pages
def test_filter_by_limit_offset(self): """Test that filter_by supports limit and offset options""" PageFactory.create_batch(4) all_pages = self.page_repo.filter_by() first_two = self.page_repo.filter_by(limit=2) last_two = self.page_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_pages[:2] assert last_two == all_pages[2:]
def test_query_page(self): """Test API query for page endpoint works""" owner = UserFactory.create() user = UserFactory.create() project = ProjectFactory(owner=owner) pages = PageFactory.create_batch(9, project_id=project.id) page = PageFactory.create() # As anon url = '/api/page' res = self.app.get(url) data = json.loads(res.data) assert len(data) == 10, data # 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) == 9, data # Valid field but wrong value res = self.app.get(url + "?media_url=wrongvalue") data = json.loads(res.data) assert len(data) == 0, data # Multiple fields res = self.app.get(url + '?info=container::' + pages[0].info['container'] + '&project_id=' + str(project.id)) data = json.loads(res.data) # One result assert len(data) == 9, data # Correct result assert data[0]['project_id'] == pages[0].project_id, data assert data[0]['media_url'] == pages[0].media_url, data # Limits res = self.app.get(url + "?limit=1") data = json.loads(res.data) for item in data: assert item['media_url'] == page.media_url, item assert len(data) == 1, data # Keyset pagination res = self.app.get(url + '?limit=1&last_id=' + str(pages[8].id)) data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0]['id'] == page.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/page?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/page?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." pages.append(page) pages_by_id = sorted(pages, key=lambda x: x.id, reverse=False) for i in range(len(pages)): assert pages_by_id[i].id == data[i]['id'] # Desc filter url = "/api/page?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." pages_by_id = sorted(pages, key=lambda x: x.id, reverse=True) for i in range(len(pages)): assert pages_by_id[i].id == data[i]['id']