def test_task_priority(self): """Test SCHED respects priority_0 field""" redis_flushall() # Del previous TaskRuns self.del_task_runs() # Register self.register() self.signin() # By default, tasks without priority should be ordered by task.id (FIFO) tasks = db.session.query(model.Task).filter_by(app_id=1).order_by('id').all() res = self.app.get('api/app/1/newtask') task1 = json.loads(res.data) # Check that we received a Task err_msg = "Task.id should be the same" assert task1.get('id') == tasks[0].id, err_msg # Now let's change the priority to a random task import random t = random.choice(tasks) # Increase priority to maximum t.priority_0 = 1 db.session.add(t) db.session.commit() # Request again a new task res = self.app.get('api/app/1/newtask') task1 = json.loads(res.data) # Check that we received a Task err_msg = "Task.id should be the same" assert task1.get('id') == t.id, err_msg err_msg = "Task.priority_0 should be the 1" assert task1.get('priority_0') == 1, err_msg
def test_task_priority(self): """Test SCHED respects priority_0 field""" redis_flushall() # Del previous TaskRuns self.del_task_runs() # Register self.register() self.signin() # By default, tasks without priority should be ordered by task.id (FIFO) tasks = db.session.query( model.Task).filter_by(app_id=1).order_by('id').all() res = self.app.get('api/app/1/newtask') task1 = json.loads(res.data) # Check that we received a Task err_msg = "Task.id should be the same" assert task1.get('id') == tasks[0].id, err_msg # Now let's change the priority to a random task import random t = random.choice(tasks) # Increase priority to maximum t.priority_0 = 1 db.session.add(t) db.session.commit() # Request again a new task res = self.app.get('api/app/1/newtask') task1 = json.loads(res.data) # Check that we received a Task err_msg = "Task.id should be the same" assert task1.get('id') == t.id, err_msg err_msg = "Task.priority_0 should be the 1" assert task1.get('priority_0') == 1, err_msg
def test_anonymous_01_newtask(self): """ Test SCHED newtask returns a Task for the Anonymous User""" redis_flushall() # Del previous TaskRuns self.del_task_runs() res = self.app.get('api/app/1/newtask') data = json.loads(res.data) assert data['info'], data
def test_incremental_tasks(self): """ Test incremental SCHED strategy - second TaskRun receives first gaven answer""" redis_flushall() Fixtures.create_2(sched='incremental') # Del previous TaskRuns self.del_task_runs() # Register self.register(fullname=self.user.fullname, username=self.user.username, password=self.user.password) self.register(fullname="Marie Doe", username="******", password="******") self.signin() # Get the only task with no runs! res = self.app.get('api/app/1/newtask') data = json.loads(res.data) # Check that we received a clean Task assert data.get('info'), data assert not data.get('info').get('last_answer') # Submit an Answer for the assigned task tr = dict(app_id=data['app_id'], task_id=data['id'], info={'answer': 'No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) # No more tasks available for this user! res = self.app.get('api/app/1/newtask') data = json.loads(res.data) assert not data #### Get the only task now with an answer as Anonimous! self.signout() res = self.app.get('api/app/1/newtask') data = json.loads(res.data) # Check that we received a Task with answer assert data.get('info'), data assert data.get('info').get('last_answer').get('answer') == 'No' # Submit a second Answer as Anonimous tr = dict(app_id=data['app_id'], task_id=data['id'], info={'answer': 'No No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) #### Get the only task now with an answer as User2! self.signin(email="*****@*****.**", password="******") res = self.app.get('api/app/1/newtask') data = json.loads(res.data) # Check that we received a Task with answer assert data.get('info'), data assert data.get('info').get('last_answer').get('answer') == 'No No'
def test_task_preloading(self): """Test TASK Pre-loading works""" redis_flushall() # Del previous TaskRuns self.del_task_runs() # Register self.register() self.signin() assigned_tasks = [] # Get Task until scheduler returns None res = self.app.get('api/app/1/newtask') task1 = json.loads(res.data) # Check that we received a Task assert task1.get('info'), task1 # Pre-load the next task for the user res = self.app.get('api/app/1/newtask?offset=1') task2 = json.loads(res.data) # Check that we received a Task assert task2.get('info'), task2 # Check that both tasks are different assert task1.get('id') != task2.get('id'), "Tasks should be different" ## Save the assigned task assigned_tasks.append(task1) assigned_tasks.append(task2) # Submit an Answer for the assigned and pre-loaded task for t in assigned_tasks: tr = dict(app_id=t['app_id'], task_id=t['id'], info={'answer': 'No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) # Get two tasks again res = self.app.get('api/app/1/newtask') task3 = json.loads(res.data) # Check that we received a Task assert task3.get('info'), task1 # Pre-load the next task for the user res = self.app.get('api/app/1/newtask?offset=1') task4 = json.loads(res.data) # Check that we received a Task assert task4.get('info'), task2 # Check that both tasks are different assert task3.get('id') != task4.get('id'), "Tasks should be different" assert task1.get('id') != task3.get('id'), "Tasks should be different" assert task2.get('id') != task4.get('id'), "Tasks should be different" # Check that a big offset returns None res = self.app.get('api/app/1/newtask?offset=11') print json.loads(res.data) assert json.loads(res.data) == {}, res.data
def test_user_01_newtask(self): """ Test SCHED newtask returns a Task for John Doe User""" redis_flushall() # Del previous TaskRuns self.del_task_runs() # Register self.register() self.signin() res = self.app.get('api/app/1/newtask') data = json.loads(res.data) assert data['info'], data self.signout()
def test_user_03_respects_limit_tasks(self): """ Test SCHED newtask respects the limit of 30 TaskRuns per Task""" redis_flushall() # Del previous TaskRuns self.del_task_runs() assigned_tasks = [] # We need one extra loop to allow the scheduler to mark a task as completed for i in range(11): self.register(fullname=self.user.username + str(i), username=self.user.username + str(i), password=self.user.username + str(i)) print "Number of users %s" % len( db.session.query(model.User).all()) print "Giving answers as User: %s" % self.user.username + str(i) self.signin() # Get Task until scheduler returns None res = self.app.get('api/app/1/newtask') data = json.loads(res.data) while data.get('info') is not None: # Check that we received a Task assert data.get('info'), data # Save the assigned task assigned_tasks.append(data) # Submit an Answer for the assigned task tr = dict(app_id=data['app_id'], task_id=data['id'], info={'answer': 'No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) res = self.app.get('api/app/1/newtask') data = json.loads(res.data) self.signout() # Check if there are 30 TaskRuns per Task tasks = db.session.query(model.Task).filter_by(app_id=1).all() for t in tasks: print len(t.task_runs) assert len(t.task_runs) == 10, t.task_runs # Check that all the answers are from different IPs err_msg = "There are two or more Answers from same User" for t in tasks: for tr in t.task_runs: assert self.is_unique(tr.user_id, t.task_runs), err_msg # Check that task.state is updated to completed for t in tasks: assert t.state == "completed", t.state
def test_user_03_respects_limit_tasks(self): """ Test SCHED newtask respects the limit of 30 TaskRuns per Task""" redis_flushall() # Del previous TaskRuns self.del_task_runs() assigned_tasks = [] # We need one extra loop to allow the scheduler to mark a task as completed for i in range(11): self.register(fullname=self.user.username + str(i), username=self.user.username + str(i), password=self.user.username + str(i)) print "Number of users %s" % len(db.session.query(model.User).all()) print "Giving answers as User: %s" % self.user.username + str(i) self.signin() # Get Task until scheduler returns None res = self.app.get('api/app/1/newtask') data = json.loads(res.data) while data.get('info') is not None: # Check that we received a Task assert data.get('info'), data # Save the assigned task assigned_tasks.append(data) # Submit an Answer for the assigned task tr = dict(app_id=data['app_id'], task_id=data['id'], info={'answer': 'No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) res = self.app.get('api/app/1/newtask') data = json.loads(res.data) self.signout() # Check if there are 30 TaskRuns per Task tasks = db.session.query(model.Task).filter_by(app_id=1).all() for t in tasks: print len(t.task_runs) assert len(t.task_runs) == 10, t.task_runs # Check that all the answers are from different IPs err_msg = "There are two or more Answers from same User" for t in tasks: for tr in t.task_runs: assert self.is_unique(tr.user_id, t.task_runs), err_msg # Check that task.state is updated to completed for t in tasks: assert t.state == "completed", t.state
def test_user_02_gets_different_tasks(self): """ Test SCHED newtask returns N different Tasks for John Doe User""" redis_flushall() # Del previous TaskRuns self.del_task_runs() # Register self.register() self.signin() assigned_tasks = [] # Get Task until scheduler returns None res = self.app.get('api/app/1/newtask') data = json.loads(res.data) while data.get('info') is not None: # Check that we received a Task assert data.get('info'), data # Save the assigned task assigned_tasks.append(data) # Submit an Answer for the assigned task tr = dict(app_id=data['app_id'], task_id=data['id'], info={'answer': 'No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) res = self.app.get('api/app/1/newtask') data = json.loads(res.data) # Check if we received the same number of tasks that the available ones tasks = db.session.query(model.Task).filter_by(app_id=1).all() assert len(assigned_tasks) == len(tasks), assigned_tasks # Check if all the assigned Task.id are equal to the available ones tasks = db.session.query(model.Task).filter_by(app_id=1).all() err_msg = "Assigned Task not found in DB Tasks" for at in assigned_tasks: assert self.is_task(at['id'], tasks), err_msg # Check that there are no duplicated tasks err_msg = "One Assigned Task is duplicated" for at in assigned_tasks: assert self.is_unique(at['id'], assigned_tasks), err_msg
def check_limit(self, url, action, obj, data=None): # Reset keys in Redis redis_flushall() # Set the limit limit = 299 # Start check for i in range(limit, -1, -1): if action == 'get': res = self.app.get(url) elif action == 'post': if obj == 'app': data = dict(name=i, short_name=i, long_description=u'something') data = json.dumps(data) res = self.app.post(url, data=data) elif action == 'put': _url = '/api/%s/%s' % (obj, i) if obj == 'app': data = dict(name=i, short_name=i, long_description=u'something') data = json.dumps(data) res = self.app.put(_url + url, data) elif action == 'delete': _url = '/api/%s/%s' % (obj, i) res = self.app.delete(_url + url) else: raise Exception("action not found") # Error message err_msg = "GET X-RateLimit-Remaining not working" # Tests assert int(res.headers['X-RateLimit-Remaining']) == i, err_msg if res.headers['X-RateLimit-Remaining'] == 0: error = json.loads(res.data) err_msg = "The status_code should be 429" assert error['status_code'] == 429, err_msg err_msg = "The status should be failed" assert error['status'] == 'failed', err_msg err_msg = "The exception_cls should be TooManyRequests" assert error['exception_cls'] == 'TooManyRequests', err_msg
def test_anonymous_02_gets_different_tasks(self): """ Test SCHED newtask returns N different Tasks for the Anonymous User""" redis_flushall() # Del previous TaskRuns self.del_task_runs() assigned_tasks = [] # Get a Task until scheduler returns None res = self.app.get('api/app/1/newtask') data = json.loads(res.data) while data.get('info') is not None: # Check that we have received a Task assert data.get('info'), data # Save the assigned task assigned_tasks.append(data) # Submit an Answer for the assigned task tr = model.TaskRun(app_id=data['app_id'], task_id=data['id'], user_ip="127.0.0.1", info={'answer': 'Yes'}) db.session.add(tr) db.session.commit() res = self.app.get('api/app/1/newtask') data = json.loads(res.data) # Check if we received the same number of tasks that the available ones tasks = db.session.query(model.Task).filter_by(app_id=1).all() assert len(assigned_tasks) == len(tasks), len(assigned_tasks) # Check if all the assigned Task.id are equal to the available ones tasks = db.session.query(model.Task).filter_by(app_id=1).all() err_msg = "Assigned Task not found in DB Tasks" for at in assigned_tasks: assert self.is_task(at['id'], tasks), err_msg # Check that there are no duplicated tasks err_msg = "One Assigned Task is duplicated" for at in assigned_tasks: assert self.is_unique(at['id'], assigned_tasks), err_msg
def test_anonymous_03_respects_limit_tasks(self): """ Test SCHED newtask respects the limit of 30 TaskRuns per Task""" redis_flushall() # Del previous TaskRuns self.del_task_runs() assigned_tasks = [] # Get Task until scheduler returns None for i in range(10): res = self.app.get('api/app/1/newtask') data = json.loads(res.data) while data.get('info') is not None: # Check that we received a Task assert data.get('info'), data # Save the assigned task assigned_tasks.append(data) # Submit an Answer for the assigned task tr = model.TaskRun(app_id=data['app_id'], task_id=data['id'], user_ip="127.0.0." + str(i), info={'answer': 'Yes'}) db.session.add(tr) db.session.commit() res = self.app.get('api/app/1/newtask') data = json.loads(res.data) # Check if there are 30 TaskRuns per Task tasks = db.session.query(model.Task).filter_by(app_id=1).all() for t in tasks: assert len(t.task_runs) == 10, len(t.task_runs) # Check that all the answers are from different IPs err_msg = "There are two or more Answers from same IP" for t in tasks: for tr in t.task_runs: assert self.is_unique(tr.user_ip, t.task_runs), err_msg
def tearDown(self): db.session.remove() redis_flushall()
def teardown_package(cls): model.rebuild_db() redis_flushall()
def tearDown(self): model.rebuild_db() redis_flushall()
def teardown_module(): db.session.remove() model.rebuild_db() redis_flushall()
def test_tasks_for_user_ip_id(self): """ Test SCHED newtask to see if sends the same ammount of Task to user_id and user_ip """ redis_flushall() # Del Fixture Task self.del_task_runs() assigned_tasks = [] for i in range(10): signin = False if random.random >= 0.5: signin = True self.register(fullname=self.user.username + str(i), username=self.user.username + str(i), password=self.user.username + str(i)) if signin: print "Giving answers as User: %s" % self.user.username + str(i) else: print "Giving answers as User IP: 127.0.0.%s" % str(i) if signin: self.signin() # Get Task until scheduler returns None res = self.app.get('api/app/1/newtask') data = json.loads(res.data) while data.get('info') is not None: # Check that we received a Task assert data.get('info'), data # Save the assigned task assigned_tasks.append(data) # Submit an Answer for the assigned task if signin: tr = dict(app_id=data['app_id'], task_id=data['id'], info={'answer': 'No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) else: tr = model.TaskRun(app_id=data['app_id'], task_id=data['id'], user_ip="127.0.0." + str(i), info={'answer': 'Yes'}) db.session.add(tr) db.session.commit() res = self.app.get('api/app/1/newtask') data = json.loads(res.data) if signin: self.signout() # Check if there are 30 TaskRuns per Task tasks = db.session.query(model.Task).filter_by(app_id=1).all() for t in tasks: print len(t.task_runs) assert len(t.task_runs) == 10, t.task_runs # Check that all the answers are from different IPs and IDs err_msg1 = "There are two or more Answers from same User ID" err_msg2 = "There are two or more Answers from same User IP" for t in tasks: for tr in t.task_runs: if tr.user_id: assert self.is_unique(tr.user_id, t.task_runs), err_msg1 else: assert self.is_unique(tr.user_ip, t.task_runs), err_msg2
def setUp(self): self.app = web.app.test_client() model.rebuild_db() redis_flushall() Fixtures.create()
def test_tasks_for_user_ip_id(self): """ Test SCHED newtask to see if sends the same ammount of Task to user_id and user_ip """ redis_flushall() # Del Fixture Task self.del_task_runs() assigned_tasks = [] for i in range(10): signin = False if random.random >= 0.5: signin = True self.register(fullname=self.user.username + str(i), username=self.user.username + str(i), password=self.user.username + str(i)) if signin: print "Giving answers as User: %s" % self.user.username + str( i) else: print "Giving answers as User IP: 127.0.0.%s" % str(i) if signin: self.signin() # Get Task until scheduler returns None res = self.app.get('api/app/1/newtask') data = json.loads(res.data) while data.get('info') is not None: # Check that we received a Task assert data.get('info'), data # Save the assigned task assigned_tasks.append(data) # Submit an Answer for the assigned task if signin: tr = dict(app_id=data['app_id'], task_id=data['id'], info={'answer': 'No'}) tr = json.dumps(tr) self.app.post('/api/taskrun', data=tr) else: tr = model.TaskRun(app_id=data['app_id'], task_id=data['id'], user_ip="127.0.0." + str(i), info={'answer': 'Yes'}) db.session.add(tr) db.session.commit() res = self.app.get('api/app/1/newtask') data = json.loads(res.data) if signin: self.signout() # Check if there are 30 TaskRuns per Task tasks = db.session.query(model.Task).filter_by(app_id=1).all() for t in tasks: print len(t.task_runs) assert len(t.task_runs) == 10, t.task_runs # Check that all the answers are from different IPs and IDs err_msg1 = "There are two or more Answers from same User ID" err_msg2 = "There are two or more Answers from same User IP" for t in tasks: for tr in t.task_runs: if tr.user_id: assert self.is_unique(tr.user_id, t.task_runs), err_msg1 else: assert self.is_unique(tr.user_ip, t.task_runs), err_msg2
def teardown_class(cls): model.rebuild_db() redis_flushall()