def test_filter_by_one_condition(self): """Test filter_by returns a list of helpingmaterials that meet the filtering condition""" HelpingMaterialFactory.create_batch(3, media_url="algo") should_be_missing = HelpingMaterialFactory.create(media_url="new") retrieved_helpingmaterials = self.helping_repo.filter_by(media_url="algo") assert len(retrieved_helpingmaterials) == 3, retrieved_helpingmaterials assert should_be_missing not in retrieved_helpingmaterials, retrieved_helpingmaterials
def test_filter_by_one_condition(self): """Test filter_by returns a list of helpingmaterials that meet the filtering condition""" HelpingMaterialFactory.create_batch(3, media_url="algo") should_be_missing = HelpingMaterialFactory.create(media_url="new") retrieved_helpingmaterials = self.helping_repo.filter_by( media_url="algo") assert len(retrieved_helpingmaterials) == 3, retrieved_helpingmaterials assert should_be_missing not in retrieved_helpingmaterials, retrieved_helpingmaterials
def test_filter_by_limit_offset(self): """Test that filter_by supports limit and offset options""" HelpingMaterialFactory.create_batch(4) all_helpingmaterials = self.helping_repo.filter_by() first_two = self.helping_repo.filter_by(limit=2) last_two = self.helping_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_helpingmaterials[:2] assert last_two == all_helpingmaterials[2:]
def test_filter_by_limit_offset(self): """Test that filter_by supports limit and offset options""" HelpingMaterialFactory.create_batch(4) all_helpingmaterials = self.helping_repo.filter_by() first_two = self.helping_repo.filter_by(limit=2) last_two = self.helping_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_helpingmaterials[:2] assert last_two == all_helpingmaterials[2:]
def test_query_helpingmaterial(self): """Test API query for helpingmaterial endpoint works""" owner = UserFactory.create() user = UserFactory.create() project = ProjectFactory(owner=owner) helpingmaterials = HelpingMaterialFactory.create_batch(9, project_id=project.id) helpingmaterial = HelpingMaterialFactory.create() # As anon url = '/api/helpingmaterial' 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::' + helpingmaterials[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'] == helpingmaterials[0].project_id, data assert data[0]['media_url'] == helpingmaterials[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'] == helpingmaterial.media_url, item assert len(data) == 1, data # Keyset pagination res = self.app.get(url + '?limit=1&last_id=' + str(helpingmaterials[8].id)) data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0]['id'] == helpingmaterial.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/helpingmaterial?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/helpingmaterial?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." helpingmaterials.append(helpingmaterial) helpingmaterials_by_id = sorted(helpingmaterials, key=lambda x: x.id, reverse=False) for i in range(len(helpingmaterials)): assert helpingmaterials_by_id[i].id == data[i]['id'] # Desc filter url = "/api/helpingmaterial?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." helpingmaterials_by_id = sorted(helpingmaterials, key=lambda x: x.id, reverse=True) for i in range(len(helpingmaterials)): assert helpingmaterials_by_id[i].id == data[i]['id'] # Test priority filtering helpingmaterials.append(HelpingMaterialFactory.create(priority=1.0, project_id=project.id)) helpingmaterials.append(HelpingMaterialFactory.create(priority=0.5, project_id=project.id)) helpingmaterials.append(HelpingMaterialFactory.create(priority=0.1, project_id=project.id)) url = "/api/helpingmaterial?orderby=priority&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." helpingmaterials_by_priority = sorted(helpingmaterials, key=lambda x: x.priority, reverse=True) for i in range(3): assert helpingmaterials_by_priority[i].id == data[i]['id'], (helpingmaterials_by_priority[i].id, data[i]['id'])
def test_query_helpingmaterial(self): """Test API query for helpingmaterial endpoint works""" owner = UserFactory.create() user = UserFactory.create() project = ProjectFactory(owner=owner) helpingmaterials = HelpingMaterialFactory.create_batch(9, project_id=project.id) helpingmaterial = HelpingMaterialFactory.create() # As anon url = '/api/helpingmaterial' 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=foo::' + helpingmaterials[0].info['foo'] + '&project_id=' + str(project.id)) data = json.loads(res.data) # One result assert len(data) == 9, data # Correct result assert data[0]['project_id'] == helpingmaterials[0].project_id, data assert data[0]['media_url'] == helpingmaterials[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'] == helpingmaterial.media_url, item assert len(data) == 1, data # Keyset pagination res = self.app.get(url + '?limit=1&last_id=' + str(helpingmaterials[8].id)) data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0]['id'] == helpingmaterial.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/helpingmaterial?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/helpingmaterial?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." helpingmaterials.append(helpingmaterial) helpingmaterials_by_id = sorted(helpingmaterials, key=lambda x: x.id, reverse=False) for i in range(len(helpingmaterials)): assert helpingmaterials_by_id[i].id == data[i]['id'] # Desc filter url = "/api/helpingmaterial?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." helpingmaterials_by_id = sorted(helpingmaterials, key=lambda x: x.id, reverse=True) for i in range(len(helpingmaterials)): assert helpingmaterials_by_id[i].id == data[i]['id']