def test_max_limit(self): """Test ACTIVITY FEED limit works.""" ProjectFactory.create_batch(101) update_feed = get_update_feed() err_msg = "There should be at max 100 updates." assert len(update_feed) == 100, err_msg
def test_n_draft_with_drafts(self): """Test CACHE PROJECTS _n_draft returns 2 if there are 2 draft projects""" ProjectFactory.create_batch(2, published=False) number_of_drafts = cached_projects._n_draft() assert number_of_drafts == 2, number_of_drafts
def test_n_draft_with_drafts(self): """Test CACHE PROJECTS _n_draft returns 2 if there are 2 draft projects""" # Here, we are suposing that a project is draft iff has no presenter AND has no tasks ProjectFactory.create_batch(2, info={}) number_of_drafts = cached_projects._n_draft() assert number_of_drafts == 2, number_of_drafts
def test_filter_by_one_condition(self): """Test filter_by returns a list of projects that meet the filtering condition""" ProjectFactory.create_batch(3, allow_anonymous_contributors=False) should_be_missing = ProjectFactory.create(allow_anonymous_contributors=True) retrieved_projects = self.project_repo.filter_by(allow_anonymous_contributors=False) assert len(retrieved_projects) == 3, retrieved_projects assert should_be_missing not in retrieved_projects, retrieved_projects
def test_filter_by_multiple_conditions(self): """Test filter_by supports multiple-condition queries""" ProjectFactory.create_batch(2, allow_anonymous_contributors=False, featured=False) project = ProjectFactory.create(allow_anonymous_contributors=False, featured=True) retrieved_projects = self.project_repo.filter_by( allow_anonymous_contributors=False, featured=True) assert len(retrieved_projects) == 1, retrieved_projects assert project in retrieved_projects, retrieved_projects
def test_filter_by_limit_offset(self): """Test that filter_by supports limit and offset options""" ProjectFactory.create_batch(4) all_projects = self.project_repo.filter_by() first_two = self.project_repo.filter_by(limit=2) last_two = self.project_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_projects[:2] assert last_two == all_projects[2:]
def test_project_filter_by_category_works(self): """Test API project filter by category works.""" category = CategoryFactory.create() projects_published = ProjectFactory.create_batch(2, published=True, category=category) projects_not_published = ProjectFactory.create_batch(2, published=False, category=category) res = self.app.get('/api/project?category_id=%s' % category.id) data = json.loads(res.data) assert len(data) == 2, data assert data[0]['id'] == projects_published[0].id assert data[1]['id'] == projects_published[1].id
def test_project_query(self): """ Test API project query""" project1 = ProjectFactory.create(updated='2015-01-01T14:37:30.642119', info={ 'total': 150, 'task_presenter': 'foo' }) projects = ProjectFactory.create_batch(8, info={ 'total': 150, 'task_presenter': 'foo' }) project2 = ProjectFactory.create(updated='2019-01-01T14:37:30.642119', info={ 'total': 150, 'task_presenter': 'foo' }) projects.insert(0, project1) projects.append(project2) # Test a non-existant ID res = self.app.get('/api/projectbyname/notthere') err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'projectbynameapi', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err
def test_project_query(self): """ Test API project query""" projects = ProjectFactory.create_batch(10, info={'total': 150}) res = self.app.get('/api/project') data = json.loads(res.data) assert len(data) == 10, data project = data[0] assert project['info']['total'] == 150, data # The output should have a mime-type: application/json assert res.mimetype == 'application/json', res # Test a non-existant ID res = self.app.get('/api/project/0') err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'project', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err # Limits res = self.app.get("/api/project?limit=5") data = json.loads(res.data) assert len(data) == 5, data # Keyset pagination url = "/api/project?limit=5&last_id=%s" % (projects[4].id) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, data assert data[0]['id'] == projects[5].id, data
def test_n_published_counts_published_projects(self): published_project = ProjectFactory.create_batch(2, published=True) ProjectFactory.create(published=False) number_of_published = cached_projects.n_published() assert number_of_published == 2, number_of_published
def test_get_query_with_api_key_context(self): """ Test API GET query with an API-KEY requesting only APIKEY results.""" users = UserFactory.create_batch(4) project_oc = ProjectFactory.create(owner=users[0], info={'total': 150}) projects = ProjectFactory.create_batch(3, owner=users[1]) task_oc = TaskFactory.create(project=project_oc, info={'url': 'my url'}) taskrun_oc = TaskRunFactory.create(task=task_oc, user=users[0], info={'answer': 'annakarenina'}) for p in projects: print p.owner_id task_tmp = TaskFactory.create(project=p) TaskRunFactory.create(task=task_tmp) # For project owner with associated data for endpoint in self.endpoints: url = '/api/' + endpoint + '?api_key=' + users[0].api_key res = self.app.get(url) data = json.loads(res.data) if endpoint == 'project': assert len(data) == 1, data project = data[0] assert project['owner_id'] == users[0].id, project['owner_id'] assert project['info']['total'] == 150, data assert res.mimetype == 'application/json', res if endpoint == 'task': assert len(data) == 1, data task = data[0] assert task['project_id'] == project_oc.id, task assert task['info']['url'] == 'my url', data assert res.mimetype == 'application/json', res if endpoint == 'taskrun': assert len(data) == 1, data taskrun = data[0] assert taskrun['project_id'] == project_oc.id, taskrun assert taskrun['info']['answer'] == 'annakarenina', data assert res.mimetype == 'application/json', res # For authenticated with non-associated data for endpoint in self.endpoints: url = '/api/' + endpoint + '?api_key=' + users[3].api_key res = self.app.get(url) data = json.loads(res.data) if endpoint == 'project': assert len(data) == 0, data assert res.mimetype == 'application/json', res if endpoint == 'task': assert len(data) == 0, data assert res.mimetype == 'application/json', res if endpoint == 'taskrun': assert len(data) == 0, data assert res.mimetype == 'application/json', res
def test_taskrun_query_list_project_ids(self): """Get a list of tasks runs using a list of project_ids.""" admin = UserFactory.create() projects = ProjectFactory.create_batch(3) task_runs = [] for project in projects: tmp = TaskRunFactory.create_batch(2, project=project) for t in tmp: task_runs.append(t) project_ids = [project.id for project in projects] url = '/api/taskrun?project_id=%s&limit=100&all=1&api_key=%s' % ( project_ids, admin.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 3 * 2, len(data) for task in data: assert task['project_id'] in project_ids task_run_project_ids = list(set([task['project_id'] for task in data])) assert sorted(project_ids) == sorted(task_run_project_ids) # more filters res = self.app.get(url + '&orderby=created&desc=true') data = json.loads(res.data) assert data[0]['id'] == task_runs[-1].id
def test_project_query_with_context(self): """ Test API project query with context.""" user = UserFactory.create() project_oc = ProjectFactory.create(owner=user, info={ 'total': 150, 'data_classification': dict(input_data="L4 - public", output_data="L4 - public") }) ProjectFactory.create() # Test a non-existant ID res = self.app.get('/api/projectbyname/notthere?api_key=' + user.api_key) err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'projectbynameapi', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err # Limits user_two = UserFactory.create() projects = ProjectFactory.create_batch(9, owner=user)
def test_project_list(self, logged_client, account): projects = ProjectFactory.create_batch(5) projects[0].members.add(account) response = logged_client.get(self.project_list_url) for project in projects: assert str(project.name) in response.content
def test_number_of_created_jobs(self): """Test number of projects created in last 30 days""" date_now = datetime.datetime.utcnow() date_60_days_old = (datetime.datetime.utcnow() - datetime.timedelta(60)).isoformat() projects = ProjectFactory.create_batch(5, created=date_now) old_project = ProjectFactory.create(created=date_60_days_old) total_projects = stats.number_of_created_jobs() assert total_projects == 5, "Total number of projects created in last 30 days should be 5"
def test_delete_mess(self, account): projects = ProjectFactory.create_batch(5, team__area__leader=account) assert 5 == Project.objects.count() projects[0].delete() assert 4 == Project.objects.count()
def test_limits_query(self): """Test API GET limits works""" owner = UserFactory.create() projects = ProjectFactory.create_batch(30, owner=owner) for project in projects: task = TaskFactory.create(project=project) TaskRunFactory.create(task=task) res = self.app.get('/api/project') data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/project?limit=10') data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/project?limit=10&offset=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] # Keyset pagination url = '/api/project?limit=10&last_id=%s' % projects[9].id res = self.app.get(url) data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] res = self.app.get('/api/task') data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/taskrun') data = json.loads(res.data) assert len(data) == 20, len(data) UserFactory.create_batch(30) res = self.app.get('/api/user') data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/user?limit=10') data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/user?limit=10&offset=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data res = self.app.get('/api/user?limit=10&last_id=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data
def test_delete_for_project_multiple(self): user = UserFactory.create() projects = ProjectFactory.create_batch(2) for p in projects: stat = PerformanceStatsFactory.create(project_id=p.id, user_id=user.id) assert len(self.repo.filter_by(user_id=user.id)) == 2 self.repo.bulk_delete(projects[0].id, stat.field, stat.stat_type) stats = self.repo.filter_by(user_id=user.id) assert len(stats) == 1 assert all(stat.project_id == projects[1].id for stat in stats)
def get_all_returns_list_of_all_projects(self): """Test get_all returns a list of all the existing projects""" projects = ProjectFactory.create_batch(3) retrieved_projects = self.project_repo.get_all() assert isinstance(retrieved_projects, list) assert len(retrieved_projects) == len(projects), retrieved_projects for project in retrieved_projects: assert project in projects, project
def test_get_top5_projects_24_hours_returns_best_5_only(self): projects = ProjectFactory.create_batch(5) i = 5 for project in projects: TaskRunFactory.create_batch(i, project=project) i -= 1 worst_project = ProjectFactory.create() top5 = stats.get_top5_projects_24_hours() top5_ids = [top['id'] for top in top5] assert len(top5) == 5 assert worst_project.id not in top5_ids for i in range(len(top5)): assert projects[i].id == top5_ids[i]
def test_project_query(self): """ Test API project query""" project = ProjectFactory.create(updated='2015-01-01T14:37:30.642119', info={'total': 150}) projects = ProjectFactory.create_batch(8, info={'total': 150}) project = ProjectFactory.create(updated='2019-01-01T14:37:30.642119', info={'total': 150}) projects.insert(0, project) projects.append(project) res = self.app.get('/api/project') data = json.loads(res.data) dataNoDesc = data assert len(data) == 10, data project = data[0] assert project['info']['total'] == 150, data # The output should have a mime-type: application/json assert res.mimetype == 'application/json', res # Test a non-existant ID res = self.app.get('/api/project/0') err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'project', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err # Limits res = self.app.get("/api/project?limit=5") data = json.loads(res.data) assert len(data) == 5, data # Keyset pagination url = "/api/project?limit=5&last_id=%s" % (projects[4].id) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, data assert data[0]['id'] == projects[5].id, data # Desc filter url = "/api/project?desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." assert data[0]['updated'] == projects[len(projects)-1].updated, err_msg
def test_task_query_list_project_ids(self, auth): """Get a list of tasks using a list of project_ids.""" auth.return_value = True projects = ProjectFactory.create_batch(3) tasks = [] for project in projects: tmp = TaskFactory.create_batch(2, project=project) for t in tmp: tasks.append(t) user = UserFactory.create() project_ids = [project.id for project in projects] url = '/api/task?all=1&project_id=%s&limit=100&api_key=%s' % ( project_ids, user.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 3 * 2, len(data) for task in data: assert task['project_id'] in project_ids task_project_ids = list(set([task['project_id'] for task in data])) assert sorted(project_ids) == sorted(task_project_ids) # more filters res = self.app.get(url + '&orderby=created&desc=true') data = json.loads(res.data) assert data[0]['id'] == tasks[-1].id task_orig = tasks[0] task_run = TaskRunFactory.create(task=task_orig, user=user) project_ids = [project.id for project in projects] url = '/api/task?project_id=%s&limit=100&participated=true&api_key=%s' % ( project_ids, user.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == (3 * 2) - 1, len(data) for task in data: assert task['project_id'] in project_ids task_project_ids = list(set([task['project_id'] for task in data])) assert sorted(project_ids) == sorted(task_project_ids) task_ids = [task['id'] for task in data] err_msg = 'This task should not be in the list as the user participated.' assert task_orig.id not in task_ids, err_msg
def test_blogpost_get_one_errors(self): """Test blogposts GET non existing posts raises errors""" self.register() user = user_repo.get(1) project1, project2 = ProjectFactory.create_batch(2, owner=user) blogpost = BlogpostFactory.create(project=project1) # To a non-existing project url = "/project/non-existing-project/%s" % blogpost.id res = self.app.get(url, follow_redirects=True) assert res.status_code == 404, res.status_code # To a non-existing post url = "/project/%s/999999" % project1.short_name res = self.app.get(url, follow_redirects=True) assert res.status_code == 404, res.status_code # To an existing post but with a project in the URL it does not belong to url = "/project/%s/%s" % (project2.short_name, blogpost.id) res = self.app.get(url, follow_redirects=True) assert res.status_code == 404, res.status_code
def test_task_query_list_project_ids(self): """Get a list of tasks using a list of project_ids.""" projects = ProjectFactory.create_batch(3) tasks = [] for project in projects: tmp = TaskFactory.create_batch(2, project=project) for t in tmp: tasks.append(t) project_ids = [project.id for project in projects] url = '/api/task?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 task in data: assert task['project_id'] in project_ids task_project_ids = list(set([task['project_id'] for task in data])) assert sorted(project_ids) == sorted(task_project_ids) # more filters res = self.app.get(url + '&orderby=created&desc=true') data = json.loads(res.data) assert data[0]['id'] == tasks[-1].id user = UserFactory.create() task_orig = tasks[0] task_run = TaskRunFactory.create(task=task_orig, user=user) project_ids = [project.id for project in projects] url = '/api/task?project_id=%s&limit=100&participated=true&api_key=%s' % (project_ids, user.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == (3 * 2) - 1, len(data) for task in data: assert task['project_id'] in project_ids task_project_ids = list(set([task['project_id'] for task in data])) assert sorted(project_ids) == sorted(task_project_ids) task_ids = [task['id'] for task in data] err_msg = 'This task should not be in the list as the user participated.' assert task_orig.id not in task_ids, err_msg
def test_delete_for_project_user(self): users = UserFactory.create_batch(2) projects = ProjectFactory.create_batch(2) types = [StatType.confusion_matrix, StatType.accuracy] for p in projects: for u in users: for t in types: stat = PerformanceStatsFactory.create_batch( 2, project_id=p.id, user_id=u.id, stat_type=t) assert len(self.repo.filter_by()) == 16 _type = types[0] user = users[0] project = projects[0] self.repo.bulk_delete(project.id, stat[0].field, _type, user_id=user.id) stats = self.repo.filter_by() assert len(stats) == 14 assert all(stat.user_id != user.id or stat.project_id != project.id or stat.stat_type != _type for stat in stats)
def test_taskrun_query_list_project_ids(self): """Get a list of tasks runs using a list of project_ids.""" projects = ProjectFactory.create_batch(3) task_runs = [] for project in projects: tmp = TaskRunFactory.create_batch(2, project=project) for t in tmp: task_runs.append(t) project_ids = [project.id for project in projects] url = '/api/taskrun?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 task in data: assert task['project_id'] in project_ids task_run_project_ids = list(set([task['project_id'] for task in data])) assert sorted(project_ids) == sorted(task_run_project_ids) # more filters res = self.app.get(url + '&orderby=created&desc=true') data = json.loads(res.data) assert data[0]['id'] == task_runs[-1].id
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_projectstats_filter_project(self): owner = UserFactory.create() project1, project2 = ProjectFactory.create_batch(2, owner=owner) stats = PerformanceStatsFactory.create(user_id=owner.id, project_id=project1.id, info={'project': project1.id}) stats = PerformanceStatsFactory.create(user_id=owner.id, project_id=project2.id, info={'project': project2.id}) url = '/api/performancestats' res = self.app.get('{}?api_key={}&project_id={}'.format( url, owner.api_key, project1.id)) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['project'] == project1.id res = self.app.get('{}?api_key={}&project_id={}'.format( url, owner.api_key, project2.id)) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['project'] == project2.id
def test_project_query(self): """ Test API project query""" project1 = ProjectFactory.create(updated='2015-01-01T14:37:30.642119', info={ 'total': 150, 'task_presenter': 'foo' }) projects = ProjectFactory.create_batch(8, info={ 'total': 150, 'task_presenter': 'foo' }) project2 = ProjectFactory.create(updated='2019-01-01T14:37:30.642119', info={ 'total': 150, 'task_presenter': 'foo' }) projects.insert(0, project1) projects.append(project2) res = self.app.get('/api/project') data = json.loads(res.data) dataNoDesc = data assert len(data) == 10, data project = data[0] assert project['info']['task_presenter'] == 'foo', data assert 'total' not in project['info'].keys(), data # The output should have a mime-type: application/json assert res.mimetype == 'application/json', res # Test a non-existant ID res = self.app.get('/api/project/0') err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'project', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err # Limits res = self.app.get("/api/project?limit=5") data = json.loads(res.data) assert len(data) == 5, data # Related res = self.app.get("/api/project?limit=1&related=True") data = json.loads(res.data) assert len(data) == 1, data keys = ['tasks', 'task_runs', 'results'] for key in keys: assert key not in data[0].keys() # Keyset pagination url = "/api/project?limit=5&last_id=%s" % (projects[4].id) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, len(data) assert data[0]['id'] == projects[5].id, (data[0]['id'], projects[5].id) # Desc filter url = "/api/project?orderby=updated&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." assert data[0]['updated'] == projects[len(projects) - 1].updated, err_msg # Orderby filter url = "/api/project?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." assert data[0]['id'] == projects[len(projects) - 1].id, err_msg # Orderby filter non attribute url = "/api/project?orderby=wrongattribute&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should return 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/project?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." projects_by_id = sorted(projects, key=lambda x: x.id, reverse=False) for i in range(len(projects_by_id)): assert projects_by_id[i].id == data[i]['id'], ( projects_by_id[i].id, data[i]['id']) url = "/api/project?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." projects_by_id = sorted(projects, key=lambda x: x.id, reverse=True) for i in range(len(projects_by_id)): assert projects_by_id[i].id == data[i]['id'], ( projects_by_id[i].id, data[i]['id'])
def test_project_query_with_context(self): """ Test API project query with context.""" user = UserFactory.create() project_oc = ProjectFactory.create(owner=user, info={'total': 150}) ProjectFactory.create() res = self.app.get('/api/project?api_key=' + user.api_key) data = json.loads(res.data) assert len(data) == 1, len(data) project = data[0] assert project['info']['total'] == 150, data assert project_oc.id == project['id'], project assert project['owner_id'] == user.id, project res = self.app.get('/api/project?api_key=' + user.api_key + '&offset=1') data = json.loads(res.data) assert len(data) == 0, data # The output should have a mime-type: application/json assert res.mimetype == 'application/json', res # Test a non-existant ID res = self.app.get('/api/project/0?api_key=' + user.api_key) err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'project', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err # Limits user_two = UserFactory.create() projects = ProjectFactory.create_batch(9, owner=user) res = self.app.get("/api/project?limit=5&api_key=" + user.api_key) data = json.loads(res.data) assert len(data) == 5, data for d in data: d['owner_id'] == user.id, d res = self.app.get("/api/project?limit=5&api_key=" + user_two.api_key) data = json.loads(res.data) assert len(data) == 0, data res = self.app.get("/api/project?all=1&limit=5&api_key=" + user_two.api_key) data = json.loads(res.data) assert len(data) == 5, data for d in data: d['owner_id'] == user.id, d # Keyset pagination url = "/api/project?limit=5&last_id=%s&api_key=%s" % (projects[3].id, user.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, len(data) assert data[0]['id'] == projects[4].id, data for d in data: d['owner_id'] == user.id, d # Keyset pagination url = "/api/project?limit=5&last_id=%s&api_key=%s" % (projects[3].id, user_two.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 0, data # Keyset pagination url = "/api/project?all=1&limit=5&last_id=%s&api_key=%s" % ( projects[3].id, user_two.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, data assert data[0]['id'] == projects[4].id, data for d in data: d['owner_id'] == user.id, d
def test_limits_query(self, auth): """Test API GET limits works""" admin, owner, user = UserFactory.create_batch(3) projects = ProjectFactory.create_batch(30, owner=owner) project_created = ProjectFactory.create( created='2000-01-01T12:08:47.134025') auth.return_value = True for project in projects: task = TaskFactory.create(project=project) TaskRunFactory.create(task=task) res = self.app.get('/api/project') data = json.loads(res.data) assert data[ 'status_code'] == 401, "anonymous user should not have acess to project api" res = self.app.get('/api/project?all=1&api_key=' + user.api_key) data = json.loads(res.data) assert len(data) == 20, len(data) assert res.mimetype == 'application/json' res = self.app.get('/api/project?limit=10&all=1&api_key=' + user.api_key) data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/project?all=1&limit=10&offset=10&api_key=' + user.api_key) data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] # Keyset pagination url = '/api/project?all=1&limit=10&last_id=%s&api_key=%s' % ( projects[9].id, user.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] res = self.app.get('/api/task?api_key=' + owner.api_key) data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/taskrun?api_key=' + owner.api_key) data = json.loads(res.data) assert len(data) == 20, len(data) UserFactory.create_batch(30) res = self.app.get('/api/user') data = json.loads(res.data) assert data[ 'status_code'] == 401, "anonymous user should not have acess to user api" # now access with admin user res = self.app.get('/api/user?api_key=' + admin.api_key) data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/user?limit=10&api_key=' + admin.api_key) data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/user?limit=10&offset=10&api_key=' + admin.api_key) data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data res = self.app.get('/api/user?limit=10&last_id=10&api_key=' + admin.api_key) data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data # By date created res = self.app.get('/api/project?created=2000-01') data = json.loads(res.data) assert data[ 'status_code'] == 401, "anonymous user should not have acess to project api" res = self.app.get('/api/project?all=1&created=2000-01&api_key=%s' % admin.api_key) data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0].get('id') == project_created.id year = datetime.datetime.now().year res = self.app.get('/api/project?all=1&created=%s&api_key=%s' % (year, owner.api_key)) data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get( '/api/project?all=1&created=%s&limit=100&api_key=%s' % (year, owner.api_key)) data = json.loads(res.data) assert len(data) == 30, len(data)
def test_project_query_with_context(self): """ Test API project query with context.""" user = UserFactory.create() project_oc = ProjectFactory.create(owner=user, info={'total': 150}) ProjectFactory.create() res = self.app.get('/api/project?api_key=' + user.api_key) data = json.loads(res.data) assert len(data) == 1, len(data) project = data[0] assert project['info']['total'] == 150, data assert project_oc.id == project['id'], project assert project['owner_id'] == user.id, project res = self.app.get('/api/project?api_key=' + user.api_key + '&offset=1') data = json.loads(res.data) assert len(data) == 0, data # The output should have a mime-type: application/json assert res.mimetype == 'application/json', res # Test a non-existant ID res = self.app.get('/api/project/0?api_key=' + user.api_key) err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'project', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err # Limits user_two = UserFactory.create() projects = ProjectFactory.create_batch(9, owner=user) res = self.app.get("/api/project?limit=5&api_key=" + user.api_key) data = json.loads(res.data) assert len(data) == 5, data for d in data: d['owner_id'] == user.id, d res = self.app.get("/api/project?limit=5&api_key=" + user_two.api_key) data = json.loads(res.data) assert len(data) == 0, data res = self.app.get("/api/project?all=1&limit=5&api_key=" + user_two.api_key) data = json.loads(res.data) assert len(data) == 5, data for d in data: d['owner_id'] == user.id, d # Keyset pagination url = "/api/project?limit=5&last_id=%s&api_key=%s" % (projects[3].id, user.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, len(data) assert data[0]['id'] == projects[4].id, data for d in data: d['owner_id'] == user.id, d # Keyset pagination url = "/api/project?limit=5&last_id=%s&api_key=%s" % (projects[3].id, user_two.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 0, data # Keyset pagination url = "/api/project?all=1&limit=5&last_id=%s&api_key=%s" % (projects[3].id, user_two.api_key) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, data assert data[0]['id'] == projects[4].id, data for d in data: d['owner_id'] == user.id, d
def test_query_projectstats(self): """Test API query for project stats endpoint works""" project_stats = [] projects = ProjectFactory.create_batch(3) for project in projects: for task in TaskFactory.create_batch(4, project=project, n_answers=3): TaskRunFactory.create(task=task) stats.update_stats(project.id) ps = stats.get_stats(project.id, full=True) project_stats.append(ps) extra_stat_types = ['hours_stats', 'dates_stats', 'users_stats'] # As anon url = '/api/projectstats' res = self.app_get_json(url) data = json.loads(res.data) assert len(data) == 3, data # Limits res = self.app.get(url + "?limit=1") data = json.loads(res.data) assert len(data) == 1, data # Keyset pagination res = self.app.get(url + '?limit=1&last_id=' + str(projects[1].id)) data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0]['id'] == project.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/projectstats?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 # Order by url = "/api/projectstats?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." ps_by_id = sorted(project_stats, key=lambda x: x.id, reverse=False) for i in range(len(project_stats)): assert ps_by_id[i].id == data[i]['id'] # Desc filter url = "/api/projectstats?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." ps_by_id = sorted(project_stats, key=lambda x: x.id, reverse=True) for i in range(len(project_stats)): assert ps_by_id[i].id == data[i]['id'] # Without full filter url = "/api/projectstats" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should not return the full stats." extra = [row['info'].get(_type) for _type in extra_stat_types for row in data if row['info'].get(_type)] assert not extra # With full filter url = "/api/projectstats?full=1" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should return full stats." for i, row in enumerate(data): for _type in extra_stat_types: assert row['info'][_type] == project_stats[i].info[_type]
def test_limits_query(self): """Test API GET limits works""" owner = UserFactory.create() projects = ProjectFactory.create_batch(30, owner=owner) project_created = ProjectFactory.create(created='2000-01-01T12:08:47.134025') for project in projects: task = TaskFactory.create(project=project) TaskRunFactory.create(task=task) res = self.app.get('/api/project') data = json.loads(res.data) assert len(data) == 20, len(data) assert res.mimetype == 'application/json' res = self.app.get('/api/project?limit=10') data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/project?limit=10&offset=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] # Keyset pagination url = '/api/project?limit=10&last_id=%s' % projects[9].id res = self.app.get(url) data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] res = self.app.get('/api/task') data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/taskrun') data = json.loads(res.data) assert len(data) == 20, len(data) UserFactory.create_batch(30) res = self.app.get('/api/user') data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/user?limit=10') data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/user?limit=10&offset=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data res = self.app.get('/api/user?limit=10&last_id=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data # By date created res = self.app.get('/api/project?created=2000-01') data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0].get('id') == project_created.id year = datetime.datetime.now().year res = self.app.get('/api/project?created=%s' % year) data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/project?created=%s&limit=100' % year) data = json.loads(res.data) assert len(data) == 30, len(data)
def test_limits_query(self): """Test API GET limits works""" owner = UserFactory.create() projects = ProjectFactory.create_batch(30, owner=owner) project_created = ProjectFactory.create( created='2000-01-01T12:08:47.134025') for project in projects: task = TaskFactory.create(project=project) TaskRunFactory.create(task=task) res = self.app.get('/api/project') data = json.loads(res.data) assert len(data) == 20, len(data) assert res.mimetype == 'application/json' res = self.app.get('/api/project?limit=10') data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/project?limit=10&offset=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] # Keyset pagination url = '/api/project?limit=10&last_id=%s' % projects[9].id res = self.app.get(url) data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == projects[10].name, data[0] res = self.app.get('/api/task') data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/taskrun') data = json.loads(res.data) assert len(data) == 20, len(data) UserFactory.create_batch(30) res = self.app.get('/api/user') data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/user?limit=10') data = json.loads(res.data) assert len(data) == 10, len(data) # DEPRECATED res = self.app.get('/api/user?limit=10&offset=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data res = self.app.get('/api/user?limit=10&last_id=10') data = json.loads(res.data) assert len(data) == 10, len(data) assert data[0].get('name') == 'user11', data # By date created res = self.app.get('/api/project?created=2000-01') data = json.loads(res.data) assert len(data) == 1, len(data) assert data[0].get('id') == project_created.id year = datetime.datetime.now().year res = self.app.get('/api/project?created=%s' % year) data = json.loads(res.data) assert len(data) == 20, len(data) res = self.app.get('/api/project?created=%s&limit=100' % year) data = json.loads(res.data) assert len(data) == 30, len(data)
def test_project_query(self): """ Test API project query""" project1 = ProjectFactory.create(updated='2015-01-01T14:37:30.642119', info={'total': 150, 'task_presenter': 'foo'}) projects = ProjectFactory.create_batch(8, info={'total': 150, 'task_presenter': 'foo'}) project2 = ProjectFactory.create(updated='2019-01-01T14:37:30.642119', info={'total': 150, 'task_presenter': 'foo'}) projects.insert(0, project1) projects.append(project2) res = self.app.get('/api/project') data = json.loads(res.data) dataNoDesc = data assert len(data) == 10, data project = data[0] assert project['info']['task_presenter'] == 'foo', data assert 'total' not in project['info'].keys(), data # The output should have a mime-type: application/json assert res.mimetype == 'application/json', res # Test a non-existant ID res = self.app.get('/api/project/0') err = json.loads(res.data) assert res.status_code == 404, err assert err['status'] == 'failed', err assert err['target'] == 'project', err assert err['exception_cls'] == 'NotFound', err assert err['action'] == 'GET', err # Limits res = self.app.get("/api/project?limit=5") data = json.loads(res.data) assert len(data) == 5, data # Related res = self.app.get("/api/project?limit=1&related=True") data = json.loads(res.data) assert len(data) == 1, data keys = ['tasks', 'task_runs', 'results'] for key in keys: assert key not in data[0].keys() # Keyset pagination url = "/api/project?limit=5&last_id=%s" % (projects[4].id) res = self.app.get(url) data = json.loads(res.data) assert len(data) == 5, len(data) assert data[0]['id'] == projects[5].id, (data[0]['id'], projects[5].id) # Desc filter url = "/api/project?orderby=updated&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." assert data[0]['updated'] == projects[len(projects)-1].updated, err_msg # Orderby filter url = "/api/project?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." assert data[0]['id'] == projects[len(projects)-1].id, err_msg # Orderby filter non attribute url = "/api/project?orderby=wrongattribute&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should return 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/project?orderby=id" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." projects_by_id = sorted(projects, key=lambda x: x.id, reverse=False) for i in range(len(projects_by_id)): assert projects_by_id[i].id == data[i]['id'], (projects_by_id[i].id, data[i]['id']) url = "/api/project?orderby=id&desc=true" res = self.app.get(url) data = json.loads(res.data) err_msg = "It should get the last item first." projects_by_id = sorted(projects, key=lambda x: x.id, reverse=True) for i in range(len(projects_by_id)): assert projects_by_id[i].id == data[i]['id'], (projects_by_id[i].id, data[i]['id'])