initial commit
This commit is contained in:
6
tests/conftest.py
Normal file
6
tests/conftest.py
Normal file
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
import pytest
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def set_test_db_env():
|
||||
os.environ['DB_ENV'] = 'test'
|
||||
179
tests/test_child_api.py
Normal file
179
tests/test_child_api.py
Normal file
@@ -0,0 +1,179 @@
|
||||
import pytest
|
||||
import os
|
||||
from flask import Flask
|
||||
from api.child_api import child_api
|
||||
from db.db import child_db, reward_db, task_db
|
||||
from tinydb import Query
|
||||
from models.child import Child
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app = Flask(__name__)
|
||||
app.register_blueprint(child_api)
|
||||
app.config['TESTING'] = True
|
||||
with app.test_client() as client:
|
||||
yield client
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def cleanup_db():
|
||||
yield
|
||||
child_db.close()
|
||||
reward_db.close()
|
||||
task_db.close()
|
||||
if os.path.exists('children.json'):
|
||||
os.remove('children.json')
|
||||
if os.path.exists('rewards.json'):
|
||||
os.remove('rewards.json')
|
||||
if os.path.exists('tasks.json'):
|
||||
os.remove('tasks.json')
|
||||
|
||||
def test_add_child(client):
|
||||
response = client.put('/child/add', json={'name': 'Alice', 'age': 8})
|
||||
assert response.status_code == 201
|
||||
assert b'Child Alice added.' in response.data
|
||||
children = child_db.all()
|
||||
assert any(c.get('name') == 'Alice' and c.get('age') == 8 and c.get('image_id') == 'boy01' for c in children)
|
||||
|
||||
response = client.put('/child/add', json={'name': 'Mike', 'age': 4, 'image_id': 'girl02'})
|
||||
assert response.status_code == 201
|
||||
assert b'Child Mike added.' in response.data
|
||||
children = child_db.all()
|
||||
assert any(c.get('name') == 'Mike' and c.get('age') == 4 and c.get('image_id') == 'girl02' for c in children)
|
||||
|
||||
def test_list_children(client):
|
||||
child_db.truncate()
|
||||
child_db.insert(Child(name='Alice', age=8, image_id="boy01").to_dict())
|
||||
child_db.insert(Child(name='Mike', age=4, image_id="boy01").to_dict())
|
||||
response = client.get('/child/list')
|
||||
assert response.status_code == 200
|
||||
data = response.json
|
||||
assert len(data['children']) == 2
|
||||
|
||||
def test_assign_and_remove_task(client):
|
||||
client.put('/child/add', json={'name': 'Bob', 'age': 10})
|
||||
children = client.get('/child/list').get_json()['children']
|
||||
child_id = children[0]['id']
|
||||
response = client.post(f'/child/{child_id}/assign-task', json={'task_id': 'task123'})
|
||||
assert response.status_code == 200
|
||||
ChildQuery = Query()
|
||||
child = child_db.search(ChildQuery.id == child_id)[0]
|
||||
assert 'task123' in child.get('tasks', [])
|
||||
response = client.post(f'/child/{child_id}/remove-task', json={'task_id': 'task123'})
|
||||
assert response.status_code == 200
|
||||
child = child_db.search(ChildQuery.id == child_id)[0]
|
||||
assert 'task123' not in child.get('tasks', [])
|
||||
|
||||
def test_get_child_not_found(client):
|
||||
response = client.get('/child/nonexistent-id')
|
||||
assert response.status_code == 404
|
||||
assert b'Child not found' in response.data
|
||||
|
||||
def test_remove_task_not_found(client):
|
||||
response = client.post('/child/nonexistent-id/remove-task', json={'task_id': 'task123'})
|
||||
assert response.status_code == 404
|
||||
assert b'Child not found' in response.data
|
||||
|
||||
def test_remove_reward_not_found(client):
|
||||
response = client.post('/child/nonexistent-id/remove-reward', json={'reward_id': 'reward123'})
|
||||
assert response.status_code == 404
|
||||
assert b'Child not found' in response.data
|
||||
|
||||
def test_affordable_rewards(client):
|
||||
reward_db.insert({'id': 'r_cheep', 'name': 'Sticker', 'cost': 5})
|
||||
reward_db.insert({'id': 'r_exp', 'name': 'Bike', 'cost': 20})
|
||||
client.put('/child/add', json={'name': 'Charlie', 'age': 9})
|
||||
children = client.get('/child/list').get_json()['children']
|
||||
child_id = next((c['id'] for c in children if c.get('name') == 'Charlie'), children[0]['id'])
|
||||
ChildQuery = Query()
|
||||
child_db.update({'points': 10}, ChildQuery.id == child_id)
|
||||
client.post(f'/child/{child_id}/assign-reward', json={'reward_id': 'r_cheep'})
|
||||
client.post(f'/child/{child_id}/assign-reward', json={'reward_id': 'r_exp'})
|
||||
resp = client.get(f'/child/{child_id}/affordable-rewards')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
affordable_ids = [r['id'] for r in data['affordable_rewards']]
|
||||
assert 'r_cheep' in affordable_ids and 'r_exp' not in affordable_ids
|
||||
|
||||
def test_reward_status(client):
|
||||
reward_db.insert({'id': 'r1', 'name': 'Candy', 'cost': 3, 'image': 'candy.png'})
|
||||
reward_db.insert({'id': 'r2', 'name': 'Game', 'cost': 8, 'image': 'game.png'})
|
||||
reward_db.insert({'id': 'r3', 'name': 'Trip', 'cost': 15, 'image': 'trip.png'})
|
||||
client.put('/child/add', json={'name': 'Dana', 'age': 11})
|
||||
children = client.get('/child/list').get_json()['children']
|
||||
child_id = next((c['id'] for c in children if c.get('name') == 'Dana'), children[0]['id'])
|
||||
ChildQuery = Query()
|
||||
child_db.update({'points': 7}, ChildQuery.id == child_id)
|
||||
for rid in ['r1', 'r2', 'r3']:
|
||||
client.post(f'/child/{child_id}/assign-reward', json={'reward_id': rid})
|
||||
resp = client.get(f'/child/{child_id}/reward-status')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
mapping = {s['id']: s['points_needed'] for s in data['reward_status']}
|
||||
assert mapping['r1'] == 0 and mapping['r2'] == 1 and mapping['r3'] == 8
|
||||
|
||||
def test_list_child_tasks_returns_tasks(client):
|
||||
task_db.insert({'id': 't_list_1', 'name': 'Task One', 'points': 2, 'is_good': True})
|
||||
task_db.insert({'id': 't_list_2', 'name': 'Task Two', 'points': 3, 'is_good': False})
|
||||
child_db.insert({
|
||||
'id': 'child_list_1',
|
||||
'name': 'Eve',
|
||||
'age': 8,
|
||||
'points': 0,
|
||||
'tasks': ['t_list_1', 't_list_2', 't_missing'],
|
||||
'rewards': []
|
||||
})
|
||||
resp = client.get('/child/child_list_1/list-tasks')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
returned_ids = {t['id'] for t in data['child_tasks']}
|
||||
assert returned_ids == {'t_list_1', 't_list_2'}
|
||||
for t in data['child_tasks']:
|
||||
assert 'name' in t and 'points' in t and 'is_good' in t
|
||||
|
||||
def test_list_assignable_tasks_returns_expected_ids(client):
|
||||
child_db.truncate()
|
||||
task_db.truncate()
|
||||
task_db.insert({'id': 'tA', 'name': 'Task A', 'points': 1, 'is_good': True})
|
||||
task_db.insert({'id': 'tB', 'name': 'Task B', 'points': 2, 'is_good': True})
|
||||
task_db.insert({'id': 'tC', 'name': 'Task C', 'points': 3, 'is_good': False})
|
||||
client.put('/child/add', json={'name': 'Zoe', 'age': 7})
|
||||
child_id = client.get('/child/list').get_json()['children'][0]['id']
|
||||
client.post(f'/child/{child_id}/assign-task', json={'task_id': 'tA'})
|
||||
client.post(f'/child/{child_id}/assign-task', json={'task_id': 'tC'})
|
||||
resp = client.get(f'/child/{child_id}/list-assignable-tasks')
|
||||
assert resp.status_code == 200
|
||||
data = resp.get_json()
|
||||
assert len(data['assignable_tasks']) == 1
|
||||
assert data['assignable_tasks'][0]['id'] == 'tB'
|
||||
assert data['count'] == 1
|
||||
|
||||
def test_list_assignable_tasks_when_none_assigned(client):
|
||||
child_db.truncate()
|
||||
task_db.truncate()
|
||||
ids = ['t1', 't2', 't3']
|
||||
for i, tid in enumerate(ids, 1):
|
||||
task_db.insert({'id': tid, 'name': f'Task {i}', 'points': i, 'is_good': True})
|
||||
client.put('/child/add', json={'name': 'Liam', 'age': 6})
|
||||
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()
|
||||
returned_ids = {t['id'] for t in data['assignable_tasks']}
|
||||
assert returned_ids == set(ids)
|
||||
assert data['count'] == len(ids)
|
||||
|
||||
def test_list_assignable_tasks_empty_task_db(client):
|
||||
child_db.truncate()
|
||||
task_db.truncate()
|
||||
client.put('/child/add', json={'name': 'Mia', 'age': 5})
|
||||
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()
|
||||
assert data['assignable_tasks'] == []
|
||||
assert data['count'] == 0
|
||||
|
||||
def test_list_assignable_tasks_child_not_found(client):
|
||||
resp = client.get('/child/does-not-exist/list-assignable-tasks')
|
||||
assert resp.status_code == 404
|
||||
assert b'Child not found' in resp.data
|
||||
180
tests/test_image_api.py
Normal file
180
tests/test_image_api.py
Normal file
@@ -0,0 +1,180 @@
|
||||
# python
|
||||
import io
|
||||
import os
|
||||
from PIL import Image as PILImage
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from api.image_api import image_api, UPLOAD_FOLDER
|
||||
from db.db import image_db
|
||||
|
||||
IMAGE_TYPE_PROFILE = 1
|
||||
IMAGE_TYPE_ICON = 2
|
||||
MAX_DIMENSION = 512
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app = Flask(__name__)
|
||||
app.register_blueprint(image_api)
|
||||
app.config['TESTING'] = True
|
||||
with app.test_client() as c:
|
||||
yield c
|
||||
for f in os.listdir(UPLOAD_FOLDER):
|
||||
os.remove(os.path.join(UPLOAD_FOLDER, f))
|
||||
image_db.truncate()
|
||||
|
||||
def make_image_bytes(w, h, mode='RGB', color=(255, 0, 0, 255), fmt='PNG'):
|
||||
img = PILImage.new(mode, (w, h), color)
|
||||
bio = io.BytesIO()
|
||||
img.save(bio, format=fmt)
|
||||
bio.seek(0)
|
||||
return bio
|
||||
|
||||
def list_saved_files():
|
||||
return [f for f in os.listdir(UPLOAD_FOLDER) if os.path.isfile(os.path.join(UPLOAD_FOLDER, f))]
|
||||
|
||||
def test_upload_missing_type(client):
|
||||
img = make_image_bytes(100, 100, fmt='PNG')
|
||||
data = {'file': (img, 'test.png')}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 400
|
||||
assert b'Image type is required' in resp.data
|
||||
|
||||
def test_upload_invalid_type_value(client):
|
||||
img = make_image_bytes(50, 50, fmt='PNG')
|
||||
data = {'file': (img, 'test.png'), 'type': '3'}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 400
|
||||
assert b'Invalid image type. Must be 1 or 2' in resp.data
|
||||
|
||||
def test_upload_non_integer_type(client):
|
||||
img = make_image_bytes(50, 50, fmt='PNG')
|
||||
data = {'file': (img, 'test.png'), 'type': 'abc'}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 400
|
||||
assert b'Image type must be an integer' in resp.data
|
||||
|
||||
def test_upload_valid_png_with_id(client):
|
||||
image_db.truncate()
|
||||
img = make_image_bytes(120, 80, fmt='PNG')
|
||||
data = {'file': (img, 'sample.png'), 'type': str(IMAGE_TYPE_ICON), 'permanent': 'true'}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 200
|
||||
j = resp.get_json()
|
||||
assert 'id' in j and j['id']
|
||||
assert j['message'] == 'Image uploaded successfully'
|
||||
# DB entry
|
||||
records = image_db.all()
|
||||
assert len(records) == 1
|
||||
rec = records[0]
|
||||
assert rec['id'] == j['id']
|
||||
assert rec['permanent'] is True
|
||||
|
||||
def test_upload_valid_jpeg_extension_mapping(client):
|
||||
image_db.truncate()
|
||||
img = make_image_bytes(100, 60, mode='RGB', fmt='JPEG')
|
||||
data = {'file': (img, 'photo.jpg'), 'type': str(IMAGE_TYPE_PROFILE)}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 200
|
||||
j = resp.get_json()
|
||||
# Expected extension should be .jpg (this will fail with current code if format lost)
|
||||
filename = j['filename']
|
||||
assert filename.endswith('.jpg'), "JPEG should be saved with .jpg extension (code may be using None format)"
|
||||
path = os.path.join(UPLOAD_FOLDER, filename)
|
||||
assert os.path.exists(path)
|
||||
|
||||
def test_upload_png_alpha_preserved(client):
|
||||
image_db.truncate()
|
||||
img = make_image_bytes(64, 64, mode='RGBA', color=(10, 20, 30, 128), fmt='PNG')
|
||||
data = {'file': (img, 'alpha.png'), 'type': str(IMAGE_TYPE_ICON)}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 200
|
||||
j = resp.get_json()
|
||||
path = os.path.join(UPLOAD_FOLDER, j['filename'])
|
||||
with PILImage.open(path) as saved:
|
||||
# Alpha should exist (mode RGBA); if conversion changed it incorrectly this fails
|
||||
assert saved.mode in ('RGBA', 'LA')
|
||||
|
||||
def test_upload_large_image_resized(client):
|
||||
image_db.truncate()
|
||||
img = make_image_bytes(2000, 1500, fmt='PNG')
|
||||
data = {'file': (img, 'large.png'), 'type': str(IMAGE_TYPE_PROFILE)}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 200
|
||||
j = resp.get_json()
|
||||
path = os.path.join(UPLOAD_FOLDER, j['filename'])
|
||||
with PILImage.open(path) as saved:
|
||||
assert saved.width <= MAX_DIMENSION
|
||||
assert saved.height <= MAX_DIMENSION
|
||||
|
||||
def test_upload_invalid_image_content(client):
|
||||
bogus = io.BytesIO(b'notanimage')
|
||||
data = {'file': (bogus, 'bad.png'), 'type': str(IMAGE_TYPE_ICON)}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 400
|
||||
# Could be 'Uploaded file is not a valid image' or 'Failed to process image'
|
||||
assert b'valid image' in resp.data or b'Failed to process image' in resp.data
|
||||
|
||||
def test_upload_invalid_extension(client):
|
||||
image_db.truncate()
|
||||
img = make_image_bytes(40, 40, fmt='PNG')
|
||||
data = {'file': (img, 'note.gif'), 'type': str(IMAGE_TYPE_ICON)}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 400
|
||||
assert b'Invalid file type' in resp.data or b'Invalid file type or no file selected' in resp.data
|
||||
|
||||
def test_request_image_success(client):
|
||||
image_db.truncate()
|
||||
img = make_image_bytes(30, 30, fmt='PNG')
|
||||
data = {'file': (img, 'r.png'), 'type': str(IMAGE_TYPE_ICON)}
|
||||
up = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert up.status_code == 200
|
||||
recs = image_db.all()
|
||||
image_id = recs[0]['id']
|
||||
resp = client.get(f'/image/request/{image_id}')
|
||||
assert resp.status_code == 200
|
||||
|
||||
def test_request_image_not_found(client):
|
||||
resp = client.get('/image/request/missing-id')
|
||||
assert resp.status_code == 404
|
||||
assert b'Image not found' in resp.data
|
||||
|
||||
def test_list_images_filter_type(client):
|
||||
image_db.truncate()
|
||||
# Upload type 1
|
||||
for _ in range(2):
|
||||
img = make_image_bytes(20, 20, fmt='PNG')
|
||||
client.post('/image/upload', data={'file': (img, 'a.png'), 'type': '1'}, content_type='multipart/form-data')
|
||||
# Upload type 2
|
||||
for _ in range(3):
|
||||
img = make_image_bytes(25, 25, fmt='PNG')
|
||||
client.post('/image/upload', data={'file': (img, 'b.png'), 'type': '2'}, content_type='multipart/form-data')
|
||||
|
||||
resp = client.get('/image/list?type=2')
|
||||
assert resp.status_code == 200
|
||||
j = resp.get_json()
|
||||
assert j['count'] == 3
|
||||
|
||||
def test_list_images_invalid_type_query(client):
|
||||
resp = client.get('/image/list?type=99')
|
||||
assert resp.status_code == 400
|
||||
assert b'Invalid image type' in resp.data
|
||||
|
||||
def test_list_images_all(client):
|
||||
image_db.truncate()
|
||||
for _ in range(4):
|
||||
img = make_image_bytes(10, 10, fmt='PNG')
|
||||
client.post('/image/upload', data={'file': (img, 'x.png'), 'type': '2'}, content_type='multipart/form-data')
|
||||
resp = client.get('/image/list')
|
||||
assert resp.status_code == 200
|
||||
j = resp.get_json()
|
||||
assert j['count'] == 4
|
||||
assert len(j['ids']) == 4
|
||||
|
||||
def test_permanent_flag_false_default(client):
|
||||
image_db.truncate()
|
||||
img = make_image_bytes(32, 32, fmt='PNG')
|
||||
data = {'file': (img, 't.png'), 'type': '1'}
|
||||
resp = client.post('/image/upload', data=data, content_type='multipart/form-data')
|
||||
assert resp.status_code == 200
|
||||
recs = image_db.all()
|
||||
assert recs[0]['permanent'] is False
|
||||
83
tests/test_reward_api.py
Normal file
83
tests/test_reward_api.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import pytest
|
||||
import os
|
||||
from flask import Flask
|
||||
from api.reward_api import reward_api
|
||||
from db.db import reward_db, child_db
|
||||
from tinydb import Query
|
||||
from models.reward import Reward
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app = Flask(__name__)
|
||||
app.register_blueprint(reward_api)
|
||||
app.config['TESTING'] = True
|
||||
with app.test_client() as client:
|
||||
yield client
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def cleanup_db():
|
||||
yield
|
||||
reward_db.close()
|
||||
if os.path.exists('rewards.json'):
|
||||
os.remove('rewards.json')
|
||||
|
||||
def test_add_reward(client):
|
||||
response = client.put('/reward/add', json={'name': 'Vacation', 'cost': 10, 'description': "A test reward"})
|
||||
assert response.status_code == 201
|
||||
assert b'Reward Vacation added.' in response.data
|
||||
# verify in database
|
||||
rewards = reward_db.all()
|
||||
assert any(reward.get('name') == 'Vacation' and reward.get('cost') == 10 and reward.get('description') == "A test reward" and reward.get('image_id') == '' for reward in rewards)
|
||||
|
||||
response = client.put('/reward/add', json={'name': 'Ice Cream', 'cost': 4, 'description': "A test ice cream", 'image_id': 'ice_cream'})
|
||||
assert response.status_code == 201
|
||||
assert b'Reward Ice Cream added.' in response.data
|
||||
# verify in database
|
||||
rewards = reward_db.all()
|
||||
assert any(reward.get('name') == 'Ice Cream' and reward.get('cost') == 4 and reward.get('description') == "A test ice cream" and reward.get('image_id') == 'ice_cream' for reward in rewards)
|
||||
|
||||
|
||||
def test_list_rewards(client):
|
||||
reward_db.truncate()
|
||||
reward_db.insert(Reward(name='Reward1', description='Desc1', cost=5).to_dict())
|
||||
reward_db.insert(Reward(name='Reward2', description='Desc2', cost=15, image_id='ice_cream').to_dict())
|
||||
response = client.get('/reward/list')
|
||||
assert response.status_code == 200
|
||||
assert b'rewards' in response.data
|
||||
data = response.json
|
||||
assert len(data['rewards']) == 2
|
||||
|
||||
|
||||
def test_get_reward_not_found(client):
|
||||
response = client.get('/reward/nonexistent-id')
|
||||
assert response.status_code == 404
|
||||
assert b'Reward not found' in response.data
|
||||
|
||||
def test_delete_reward_not_found(client):
|
||||
response = client.delete('/reward/nonexistent-id')
|
||||
assert response.status_code == 404
|
||||
assert b'Reward not found' in response.data
|
||||
|
||||
def test_delete_assigned_reward_removes_from_child(client):
|
||||
# create task and child with the task already assigned
|
||||
reward_db.insert({'id': 'r_delete_assigned', 'name': 'Temp Task', 'cost': 5})
|
||||
child_db.insert({
|
||||
'id': 'child_for_reward_delete',
|
||||
'name': 'Frank',
|
||||
'age': 7,
|
||||
'points': 0,
|
||||
'rewards': ['r_delete_assigned'],
|
||||
'tasks': []
|
||||
})
|
||||
|
||||
ChildQuery = Query()
|
||||
# precondition: child has the task
|
||||
assert 'r_delete_assigned' in child_db.search(ChildQuery.id == 'child_for_reward_delete')[0].get('rewards', [])
|
||||
|
||||
# call the delete endpoint
|
||||
resp = client.delete('/reward/r_delete_assigned')
|
||||
assert resp.status_code == 200
|
||||
|
||||
# verify the task id is no longer in the child's tasks
|
||||
child = child_db.search(ChildQuery.id == 'child_for_reward_delete')[0]
|
||||
assert 'r_delete_assigned' not in child.get('rewards', [])
|
||||
83
tests/test_task_api.py
Normal file
83
tests/test_task_api.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import pytest
|
||||
import os
|
||||
from flask import Flask
|
||||
from api.task_api import task_api
|
||||
from db.db import task_db, child_db
|
||||
from tinydb import Query
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app = Flask(__name__)
|
||||
app.register_blueprint(task_api)
|
||||
app.config['TESTING'] = True
|
||||
with app.test_client() as client:
|
||||
yield client
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def cleanup_db():
|
||||
yield
|
||||
task_db.close()
|
||||
if os.path.exists('tasks.json'):
|
||||
os.remove('tasks.json')
|
||||
|
||||
def test_add_task(client):
|
||||
response = client.put('/task/add', json={'name': 'Clean Room', 'points': 10, 'is_good': True})
|
||||
assert response.status_code == 201
|
||||
assert b'Task Clean Room added.' in response.data
|
||||
# verify in database
|
||||
tasks = task_db.all()
|
||||
assert any(task.get('name') == 'Clean Room' and task.get('points') == 10 and task.get('is_good') is True and task.get('image_id') == '' for task in tasks)
|
||||
|
||||
response = client.put('/task/add', json={'name': 'Eat Dinner', 'points': 5, 'is_good': False, 'image_id': 'meal'})
|
||||
assert response.status_code == 201
|
||||
assert b'Task Eat Dinner added.' in response.data
|
||||
# verify in database
|
||||
tasks = task_db.all()
|
||||
assert any(task.get('name') == 'Eat Dinner' and task.get('points') == 5 and task.get('is_good') is False and task.get('image_id') == 'meal' for task in tasks)
|
||||
|
||||
|
||||
|
||||
def test_list_tasks(client):
|
||||
task_db.truncate()
|
||||
task_db.insert({'id': 't1', 'name': 'Task1', 'points': 5, 'is_good': True})
|
||||
task_db.insert({'id': 't2', 'name': 'Task2', 'points': 15, 'is_good': False, 'image_id': 'meal'})
|
||||
response = client.get('/task/list')
|
||||
assert response.status_code == 200
|
||||
assert b'tasks' in response.data
|
||||
data = response.json
|
||||
assert len(data['tasks']) == 2
|
||||
|
||||
|
||||
def test_get_task_not_found(client):
|
||||
response = client.get('/task/nonexistent-id')
|
||||
assert response.status_code == 404
|
||||
assert b'Task not found' in response.data
|
||||
|
||||
def test_delete_task_not_found(client):
|
||||
response = client.delete('/task/nonexistent-id')
|
||||
assert response.status_code == 404
|
||||
assert b'Task not found' in response.data
|
||||
|
||||
def test_delete_assigned_task_removes_from_child(client):
|
||||
# create task and child with the task already assigned
|
||||
task_db.insert({'id': 't_delete_assigned', 'name': 'Temp Task', 'points': 5, 'is_good': True})
|
||||
child_db.insert({
|
||||
'id': 'child_for_task_delete',
|
||||
'name': 'Frank',
|
||||
'age': 7,
|
||||
'points': 0,
|
||||
'tasks': ['t_delete_assigned'],
|
||||
'rewards': []
|
||||
})
|
||||
|
||||
ChildQuery = Query()
|
||||
# precondition: child has the task
|
||||
assert 't_delete_assigned' in child_db.search(ChildQuery.id == 'child_for_task_delete')[0].get('tasks', [])
|
||||
|
||||
# call the delete endpoint
|
||||
resp = client.delete('/task/t_delete_assigned')
|
||||
assert resp.status_code == 200
|
||||
|
||||
# verify the task id is no longer in the child's tasks
|
||||
child = child_db.search(ChildQuery.id == 'child_for_task_delete')[0]
|
||||
assert 't_delete_assigned' not in child.get('tasks', [])
|
||||
Reference in New Issue
Block a user