def test_query_projectstats_filter_field(self): owner = UserFactory.create() project = ProjectFactory.create(owner=owner) stats = PerformanceStatsFactory.create(field='field_a', user_id=owner.id, project_id=project.id, info={'field': 'A'}) stats = PerformanceStatsFactory.create(field='field_b', user_id=owner.id, project_id=project.id, info={'field': 'B'}) url = '/api/performancestats' res = self.app.get('{}?api_key={}&field={}'.format( url, owner.api_key, 'field_a')) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['field'] == 'A' res = self.app.get('{}?api_key={}&field={}'.format( url, owner.api_key, 'field_b')) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['field'] == 'B'
def test_query_projectstats_filter_type(self): owner = UserFactory.create() project = ProjectFactory.create(owner=owner) stats = PerformanceStatsFactory.create(stat_type='confusion_matrix', user_id=owner.id, project_id=project.id, info={'type': 'matrix'}) stats = PerformanceStatsFactory.create(stat_type='accuracy', user_id=owner.id, project_id=project.id, info={'type': 'accuracy'}) url = '/api/performancestats' res = self.app.get('{}?api_key={}&stat_type={}'.format( url, owner.api_key, 'confusion_matrix')) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['type'] == 'matrix' res = self.app.get('{}?api_key={}&stat_type={}'.format( url, owner.api_key, 'accuracy')) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['type'] == 'accuracy'
def test_query_projectstats_permissions(self): admin, owner, user = UserFactory.create_batch(3) project = ProjectFactory.create(owner=owner) url = '/api/performancestats' res = self.app.get('{}'.format(url)) assert res.status_code == 401 res = self.app.get('{}?api_key={}'.format(url, owner.api_key)) assert json.loads(res.data) == [] stats = PerformanceStatsFactory.create(user_id=user.id, project_id=project.id) res = self.app.get('{}?api_key={}'.format(url, admin.api_key)) data = json.loads(res.data) assert not data res = self.app.get('{}?api_key={}&all=1'.format(url, admin.api_key)) data = json.loads(res.data) assert len(data) == 1 res = self.app.get('{}?api_key={}'.format(url, owner.api_key)) data = json.loads(res.data) assert len(data) == 1 res = self.app.get('{}?api_key={}'.format(url, user.api_key)) data = json.loads(res.data) assert not data res = self.app.get('{}?api_key={}&all=1'.format(url, user.api_key)) data = json.loads(res.data) assert not data
def test_update_integrity_error(self): user = UserFactory.create() project = ProjectFactory.create() stat = PerformanceStatsFactory.create(project_id=project.id, user_id=user.id) stat.stat_type = None assert_raises(DBIntegrityError, self.repo.update, stat)
def test_update_row(self): answer_fields = { 'hello': { 'type': 'categorical', 'config': { 'labels': ['A', 'B'] } } } project = ProjectFactory.create( info={ 'answer_fields': answer_fields, 'data_classification': dict(input_data="L4 - public", output_data="L4 - public") }) task = TaskFactory.create(project=project, calibration=1, gold_answers={'hello': 'A'}) task_run = TaskRunFactory.create(task=task, info={'hello': 'B'}) stat = PerformanceStatsFactory.create( user_id=task_run.user_id, project_id=project.id, field='hello', info={'matrix': [[1, 4], [2, 3]]}) update_gold_stats(task_run.user_id, task.id, task_run.dictize()) stats = performance_repo.filter_by(project_id=project.id) assert len(stats) == 1 assert stats[0].info['matrix'] == [[1, 5], [2, 3]]
def test_delete(self): user = UserFactory.create() project = ProjectFactory.create() stat = PerformanceStatsFactory.create(project_id=project.id, user_id=user.id) self.repo.bulk_delete(project.id, stat.field, stat.stat_type) deleted = self.repo.get(stat.id) assert deleted is None
def test_query_by_id(self): user = UserFactory.create() project = ProjectFactory.create(owner=user) info = {'matrix': [[1, 0], [0, 1]]} stat = PerformanceStatsFactory.create(user_id=user.id, project_id=project.id, info=info) url = '/api/performancestats/{}'.format(stat.id) res = self.app.get('{}?api_key={}'.format(url, user.api_key)) assert json.loads(res.data)['info'] == info
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 test_query_projectstats_filter_user(self): owner, user = UserFactory.create_batch(2) project = ProjectFactory.create(owner=owner) stats = PerformanceStatsFactory.create(user_id=user.id, project_id=project.id, info={'user': user.id}) stats = PerformanceStatsFactory.create(user_id=owner.id, project_id=project.id, info={'user': owner.id}) url = '/api/performancestats' res = self.app.get('{}?api_key={}&user_id={}'.format( url, owner.api_key, user.id)) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['user'] == user.id res = self.app.get('{}?api_key={}&user_id={}'.format( url, owner.api_key, owner.id)) data = json.loads(res.data) assert len(data) == 1 assert data[0]['info']['user'] == owner.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_delete_stats(self): project = ProjectFactory.create() user = UserFactory.create() stat = PerformanceStatsFactory.create(project_id=project.id, user_id=user.id) url = '/project/%s/performancestats?api_key=%s' % ( project.short_name, project.owner.api_key) res = self.app_get_json(url) data = json.loads(res.data) csrf = data['csrf'] url = '/project/%s/performancestats?api_key=%s&field=%s&user_id=%s' % ( project.short_name, project.owner.api_key, stat.field, stat.user_id) res = self.app.delete(url, content_type='application/json', data=json.dumps({'answer_fields': {}}), headers={'X-CSRFToken': csrf}) assert res.status_code == 204 stats = perf_repo.filter_by() assert not stats
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_save_integrity_error(self): stat = PerformanceStatsFactory.build() assert_raises(DBIntegrityError, self.repo.save, stat)