Пример #1
0
    def test_no_more_tasks(self):
        """Test that a users gets always tasks"""
        self.create()
        app = App(short_name='egil', name='egil',
                  description='egil')
        owner = db.session.query(User).get(1)
        app.owner_id = owner.id
        db.session.add(app)
        db.session.commit()

        app_id = app.id

        for i in range(20):
            task = Task(app=app, info={'i': i}, n_answers=10)
            db.session.add(task)
            db.session.commit()

        tasks = db.session.query(Task).filter_by(app_id=app.id).limit(11).all()
        for t in tasks[0:10]:
            for x in range(10):
                self._add_task_run(app, t)

        assert tasks[0].n_answers == 10

        url = 'api/app/%s/newtask' % app_id
        res = self.app.get(url)
        data = json.loads(res.data)

        err_msg = "User should get a task"
        assert 'app_id' in data.keys(), err_msg
        assert data['app_id'] == app_id, err_msg
        assert data['id'] == tasks[10].id, err_msg
Пример #2
0
    def test_blogpost_delete_errors(self):
        """Test blogposts delete for non existing apps raises errors"""
        self.register()
        user = db.session.query(User).get(1)
        app1 = App(name='app1',
                short_name='app1',
                description=u'description')
        app2 = self.create_app(info=None)
        app1.owner = user
        app2.owner = user
        blogpost = Blogpost(owner=user, app=app1, title='thisisatitle', body='body')
        db.session.add_all([app1, app2, blogpost])
        db.session.commit()

        # To a non-existing app
        url = "/app/non-existing-app/blog/%s/delete" % blogpost.id
        res = self.app.post(url, follow_redirects=True)
        assert res.status_code == 404, res.status_code

        # To a non-existing post
        url = "/app/%s/blog/999999/delete" % app1.short_name
        res = self.app.post(url, follow_redirects=True)
        assert res.status_code == 404, res.status_code

        # To an existing post but with an app in the URL it does not belong to
        url = "/app/%s/blog/%s/delete" % (app2.short_name, blogpost.id)
        res = self.app.post(url, follow_redirects=True)
        assert res.status_code == 404, res.status_code
Пример #3
0
    def test_all(self):
        """Test MODEL works"""
        username = u'test-user-1'
        user = User(name=username, fullname=username, email_addr=username)
        info = {
            'total': 150,
            'long_description': 'hello world'}
        app = App(
            name=u'My New Project',
            short_name=u'my-new-app',
            description=u'description',
            info=info)
        category = Category(name=u'cat', short_name=u'cat', description=u'cat')
        app.category = category
        app.owner = user
        task_info = {
            'question': 'My random question',
            'url': 'my url'}
        task = Task(info=task_info)
        task_run_info = {'answer': u'annakarenina'}
        task_run = TaskRun(info=task_run_info)
        task.app = app
        task_run.task = task
        task_run.app = app
        task_run.user = user
        db.session.add_all([user, app, task, task_run])
        db.session.commit()
        app_id = app.id

        db.session.remove()

        app = db.session.query(App).get(app_id)
        assert app.name == u'My New Project', app
        # year would start with 201...
        assert app.created.startswith('201'), app.created
        assert app.long_tasks == 0, app.long_tasks
        assert app.hidden == 0, app.hidden
        assert app.time_estimate == 0, app
        assert app.time_limit == 0, app
        assert app.calibration_frac == 0, app
        assert app.bolt_course_id == 0
        assert len(app.tasks) == 1, app
        assert app.owner.name == username, app
        out_task = app.tasks[0]
        assert out_task.info['question'] == task_info['question'], out_task
        assert out_task.quorum == 0, out_task
        assert out_task.state == "ongoing", out_task
        assert out_task.calibration == 0, out_task
        assert out_task.priority_0 == 0, out_task
        assert len(out_task.task_runs) == 1, out_task
        outrun = out_task.task_runs[0]
        assert outrun.info['answer'] == task_run_info['answer'], outrun
        assert outrun.user.name == username, outrun

        user = User.by_name(username)
        assert user.apps[0].id == app_id, user
Пример #4
0
    def _test_get_breadth_first_task(self, user=None):
        self.del_task_runs()
        if user:
            short_name = 'xyzuser'
        else:
            short_name = 'xyznouser'

        category = db.session.query(Category).get(1)
        app = App(short_name=short_name, name=short_name,
              description=short_name, category=category)
        owner = db.session.query(User).get(1)

        app.owner = owner
        task = Task(app=app, state='0', info={})
        task2 = Task(app=app, state='0', info={})
        task.app = app
        task2.app = app
        db.session.add(app)
        db.session.add(task)
        db.session.add(task2)
        db.session.commit()
        taskid = task.id
        appid = app.id
        # give task2 a bunch of runs
        for idx in range(2):
            self._add_task_run(app, task2)

        # now check we get task without task runs as anonymous user
        out = pybossa.sched.get_breadth_first_task(appid)
        assert out.id == taskid, out

        # now check we get task without task runs as a user
        owner = db.session.query(User).get(1)
        out = pybossa.sched.get_breadth_first_task(appid, owner.id)
        assert out.id == taskid, out


        # now check that offset works
        out1 = pybossa.sched.get_breadth_first_task(appid)
        out2 = pybossa.sched.get_breadth_first_task(appid, offset=1)
        assert out1.id != out2.id, out

        # asking for a bigger offset (max 10)
        out2 = pybossa.sched.get_breadth_first_task(appid, offset=11)
        assert out2 is None, out

        self._add_task_run(app, task)
        out = pybossa.sched.get_breadth_first_task(appid)
        assert out.id == taskid, out

        # now add 2 more taskruns. We now have 3 and 2 task runs per task
        self._add_task_run(app, task)
        self._add_task_run(app, task)
        out = pybossa.sched.get_breadth_first_task(appid)
        assert out.id == task2.id, out
Пример #5
0
    def test_app_errors(self):
        """Test project model errors."""
        app = App(name='Project',
                  short_name='proj',
                  description='desc',
                  owner_id=None)

        # App.owner_id shoult not be nullable
        db.session.add(app)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()

        # App.name shoult not be nullable
        user = User(email_addr="[email protected]",
                    name="johndoe",
                    fullname="John Doe",
                    locale="en")
        db.session.add(user)
        db.session.commit()
        user = db.session.query(User).first()
        app.owner_id = user.id
        app.name = None
        db.session.add(app)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()

        app.name = ''
        db.session.add(app)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()

        # App.short_name shoult not be nullable
        app.name = "Project"
        app.short_name = None
        db.session.add(app)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()

        app.short_name = ''
        db.session.add(app)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()

        # App.description shoult not be nullable
        db.session.add(app)
        app.short_name = "project"
        app.description = None
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()

        app.description = ''
        db.session.add(app)
        assert_raises(IntegrityError, db.session.commit)
        db.session.rollback()
Пример #6
0
 def create_app_with_tasks(self, completed_tasks, ongoing_tasks):
     app = App(name='my_app',
               short_name='my_app_shortname',
               description=u'description')
     app.owner = self.user
     db.session.add(app)
     for i in range(completed_tasks):
         task = Task(app_id = 1, state = 'completed', n_answers=3)
         db.session.add(task)
     for i in range(ongoing_tasks):
         task = Task(app_id = 1, state = 'ongoing', n_answers=3)
         db.session.add(task)
     db.session.commit()
     return app
Пример #7
0
 def create_app_with_contributors(self, anonymous, registered, two_tasks=False, name='my_app'):
     app = App(name=name,
               short_name='%s_shortname' % name,
               description=u'description')
     app.owner = self.user
     db.session.add(app)
     task = Task(app=app)
     db.session.add(task)
     if two_tasks:
         task2 = Task(app=app)
         db.session.add(task2)
     db.session.commit()
     for i in range(anonymous):
         task_run = TaskRun(app_id = app.id,
                            task_id = 1,
                            user_ip = '127.0.0.%s' % i)
         db.session.add(task_run)
         if two_tasks:
             task_run2 = TaskRun(app_id = app.id,
                            task_id = 2,
                            user_ip = '127.0.0.%s' % i)
             db.session.add(task_run2)
     for i in range(registered):
         user = User(email_addr = "%[email protected]" % i,
                     name = "user%s" % i,
                     passwd_hash = "1234%s" % i,
                     fullname = "user_fullname%s" % i)
         db.session.add(user)
         task_run = TaskRun(app_id = app.id,
                            task_id = 1,
                            user = user)
         db.session.add(task_run)
         if two_tasks:
             task_run2 = TaskRun(app_id = app.id,
                            task_id = 2,
                            user = user)
             db.session.add(task_run2)
     db.session.commit()
     return app