Skip to content

Commit

Permalink
add tests. remove unused
Browse files Browse the repository at this point in the history
  • Loading branch information
dchhabda committed Dec 2, 2021
1 parent d629464 commit eb195b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pybossa/redis_lock.py
Expand Up @@ -233,7 +233,7 @@ def get_task_category_lock(self, project_id, user_id=None, category=None, exclud


def acquire_reserve_task_lock(self, project_id, task_id, user_id, category):
if not(project_id or user_id or task_id or category):
if not(project_id and user_id and task_id and category):
raise BadRequest('Missing required parameters')

# check task category reserved by user
Expand Down
6 changes: 0 additions & 6 deletions pybossa/sched.py
Expand Up @@ -252,12 +252,6 @@ def get_candidate_task_ids(project_id, user_id=None, user_ip=None,
data = query.limit(limit).offset(offset).all()
return _handle_tuples(data)

def task_contains_category(task_id, category):
task = task_repo.get_task(task_id)
if not (task and category):
return False
return all([field in task.info for field in category])


def locked_scheduler(query_factory):
@wraps(query_factory)
Expand Down
34 changes: 34 additions & 0 deletions test/test_reserve_task_category.py
Expand Up @@ -29,6 +29,9 @@
release_reserve_task_lock_by_keys
)
import time
from nose.tools import assert_raises
from werkzeug.exceptions import BadRequest


class TestReserveTaskCategory(sched.Helper):

Expand Down Expand Up @@ -157,3 +160,34 @@ def test_acquire_and_release_reserve_task_lock(self):
release_reserve_task_lock_by_keys([expected_reserve_task_key], timeout, expiry=expiry)
time.sleep(expiry)
assert expected_reserve_task_key not in sentinel.master.keys(), "reserve task key should not exist in redis cache"


@with_context
def test_reserve_task_category_lock_raises_exceptions(self):
# missing project_id raises exception
with assert_raises(BadRequest):
get_reserve_task_category_info(["x", "y"], None, 1, 1)

# missing user id and passing exclude user raises exception
with assert_raises(BadRequest):
get_reserve_task_category_info(["x", "y"], 1, 1, user_id=None, exclude_user=True)

_, category_keys = get_reserve_task_category_info(["x", "y"], 1, 1, 1)
assert not category_keys, "reserve task category keys should not be present"

user = UserFactory.create()
project = ProjectFactory.create(
owner=user,
info=dict(
sched="task_queue_scheduler",
reserve_tasks=dict(
category=["field_1", "field_2"]
)
)
)
task = TaskFactory.create_batch(
1, project=project, n_answers=1,
info=dict(field_1="abc", field_2=123)
)[0]
with assert_raises(BadRequest):
acquire_reserve_task_lock(project.id, task.id, None, 1)

0 comments on commit eb195b1

Please sign in to comment.