def setup_repositories(app): """Setup repositories.""" from pybossa.repositories import UserRepository from pybossa.repositories import ProjectRepository from pybossa.repositories import ProjectStatsRepository from pybossa.repositories import AnnouncementRepository from pybossa.repositories import BlogRepository from pybossa.repositories import TaskRepository from pybossa.repositories import AuditlogRepository from pybossa.repositories import WebhookRepository from pybossa.repositories import ResultRepository from pybossa.repositories import HelpingMaterialRepository from pybossa.repositories import PerformanceStatsRepository global user_repo global project_repo global project_stats_repo global announcement_repo global blog_repo global task_repo global auditlog_repo global webhook_repo global result_repo global helping_repo global performance_stats_repo language = app.config.get('FULLTEXTSEARCH_LANGUAGE') rdancy_upd_exp = app.config.get('REDUNDANCY_UPDATE_EXPIRATION', 30) user_repo = UserRepository(db) project_repo = ProjectRepository(db) project_stats_repo = ProjectStatsRepository(db) announcement_repo = AnnouncementRepository(db) blog_repo = BlogRepository(db) task_repo = TaskRepository(db, language, rdancy_upd_exp) auditlog_repo = AuditlogRepository(db) webhook_repo = WebhookRepository(db) result_repo = ResultRepository(db) helping_repo = HelpingMaterialRepository(db) performance_stats_repo = PerformanceStatsRepository(db)
class TestPerformanceStatsRepository(Test): def setUp(self): super(TestPerformanceStatsRepository, self).setUp() self.repo = PerformanceStatsRepository(db) @with_context def test_save_integrity_error(self): stat = PerformanceStatsFactory.build() assert_raises(DBIntegrityError, self.repo.save, stat) @with_context 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) @with_context 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 @with_context 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) @with_context 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 setUp(self): super(TestPerformanceStatsRepository, self).setUp() self.repo = PerformanceStatsRepository(db)
import json from mock import patch from default import db, with_context from factories import ProjectFactory, UserFactory, TaskRunFactory, PerformanceStatsFactory from helper import web from pybossa.repositories import ProjectRepository, UserRepository, PerformanceStatsRepository project_repo = ProjectRepository(db) user_repo = UserRepository(db) perf_repo = PerformanceStatsRepository(db) class TestPerfStats(web.Helper): @with_context def test_owner_has_access(self): project = ProjectFactory.create(published=True) url = '/project/%s/performancestats?api_key=%s' % ( project.short_name, project.owner.api_key) res = self.app.get(url) assert 'Performance Statistics' in res.data, res.data @with_context def test_not_owner_has_not_access(self): owner, user = UserFactory.create_batch(2) project = ProjectFactory.create(owner=owner, published=True) url = '/project/%s/performancestats?api_key=%s' % (project.short_name, user.api_key) res = self.app.get(url) assert 'You do not have the permission to access the requested resource.' in res.data, res.data