feat: Enhance task and reward assignment logic to prioritize user items over system items with the same name; add corresponding tests
All checks were successful
Gitea Actions Demo / build-and-push (push) Successful in 15s
All checks were successful
Gitea Actions Demo / build-and-push (push) Successful in 15s
This commit is contained in:
@@ -270,3 +270,81 @@ def test_set_child_tasks_child_not_found(client):
|
||||
# New backend returns 400 for missing child
|
||||
assert resp.status_code in (400, 404)
|
||||
assert b'error' in resp.data
|
||||
|
||||
def test_assignable_tasks_user_overrides_system(client):
|
||||
"""If a user task exists with the same name as a system task, only the user task should be shown in assignable list."""
|
||||
child_db.truncate()
|
||||
task_db.truncate()
|
||||
# System task (user_id=None)
|
||||
task_db.insert({'id': 'sys1', 'name': 'Duplicate', 'points': 1, 'is_good': True, 'user_id': None})
|
||||
# User task (same name)
|
||||
task_db.insert({'id': 'user1', 'name': 'Duplicate', 'points': 2, 'is_good': True, 'user_id': 'testuserid'})
|
||||
client.put('/child/add', json={'name': 'Sam', 'age': 8})
|
||||
child_id = client.get('/child/list').get_json()['children'][0]['id']
|
||||
resp = client.get(f'/child/{child_id}/list-assignable-tasks')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
names = [t['name'] for t in data['tasks']]
|
||||
ids = [t['id'] for t in data['tasks']]
|
||||
# Only the user task should be present
|
||||
assert names == ['Duplicate']
|
||||
assert ids == ['user1']
|
||||
|
||||
def test_assignable_tasks_multiple_user_same_name(client):
|
||||
"""If two user tasks exist with the same name as a system task, both user tasks are shown, not the system one."""
|
||||
child_db.truncate()
|
||||
task_db.truncate()
|
||||
# System task (user_id=None)
|
||||
task_db.insert({'id': 'sys1', 'name': 'Duplicate', 'points': 1, 'is_good': True, 'user_id': None})
|
||||
# User tasks (same name, different user_ids)
|
||||
task_db.insert({'id': 'user1', 'name': 'Duplicate', 'points': 2, 'is_good': True, 'user_id': 'testuserid'})
|
||||
task_db.insert({'id': 'user2', 'name': 'Duplicate', 'points': 3, 'is_good': True, 'user_id': 'otheruserid'})
|
||||
client.put('/child/add', json={'name': 'Sam', 'age': 8})
|
||||
child_id = client.get('/child/list').get_json()['children'][0]['id']
|
||||
resp = client.get(f'/child/{child_id}/list-assignable-tasks')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
names = [t['name'] for t in data['tasks']]
|
||||
ids = [t['id'] for t in data['tasks']]
|
||||
# Both user tasks should be present, not the system one
|
||||
assert set(names) == {'Duplicate'}
|
||||
assert set(ids) == {'user1', 'user2'}
|
||||
|
||||
def test_assignable_rewards_user_overrides_system(client):
|
||||
"""If a user reward exists with the same name as a system reward, only the user reward should be shown in assignable list."""
|
||||
child_db.truncate()
|
||||
reward_db.truncate()
|
||||
# System reward (user_id=None)
|
||||
reward_db.insert({'id': 'sysr1', 'name': 'Prize', 'cost': 5, 'user_id': None})
|
||||
# User reward (same name)
|
||||
reward_db.insert({'id': 'userr1', 'name': 'Prize', 'cost': 10, 'user_id': 'testuserid'})
|
||||
client.put('/child/add', json={'name': 'Sam', 'age': 8})
|
||||
child_id = client.get('/child/list').get_json()['children'][0]['id']
|
||||
resp = client.get(f'/child/{child_id}/list-assignable-rewards')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
names = [r['name'] for r in data['rewards']]
|
||||
ids = [r['id'] for r in data['rewards']]
|
||||
# Only the user reward should be present
|
||||
assert names == ['Prize']
|
||||
assert ids == ['userr1']
|
||||
|
||||
def test_assignable_rewards_multiple_user_same_name(client):
|
||||
"""If two user rewards exist with the same name as a system reward, both user rewards are shown, not the system one."""
|
||||
child_db.truncate()
|
||||
reward_db.truncate()
|
||||
# System reward (user_id=None)
|
||||
reward_db.insert({'id': 'sysr1', 'name': 'Prize', 'cost': 5, 'user_id': None})
|
||||
# User rewards (same name, different user_ids)
|
||||
reward_db.insert({'id': 'userr1', 'name': 'Prize', 'cost': 10, 'user_id': 'testuserid'})
|
||||
reward_db.insert({'id': 'userr2', 'name': 'Prize', 'cost': 15, 'user_id': 'otheruserid'})
|
||||
client.put('/child/add', json={'name': 'Sam', 'age': 8})
|
||||
child_id = client.get('/child/list').get_json()['children'][0]['id']
|
||||
resp = client.get(f'/child/{child_id}/list-assignable-rewards')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
names = [r['name'] for r in data['rewards']]
|
||||
ids = [r['id'] for r in data['rewards']]
|
||||
# Both user rewards should be present, not the system one
|
||||
assert set(names) == {'Prize'}
|
||||
assert set(ids) == {'userr1', 'userr2'}
|
||||
Reference in New Issue
Block a user