Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Johnha/feature/create unavailability testing #174

Merged
merged 7 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion application.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask import Flask
from flask_cors import CORS

from controllers import *

# Register the application
Expand Down
2 changes: 1 addition & 1 deletion services/optimiser/input_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def get_position_qualification(session, position_id):
return qualification_id


# This can be called by api from postman, to test easily
# This can be called by api from postman, to tests easily
def test_vehicle_list(session, request_id):
v_l = get_vehicle_list(session, request_id)
for v in v_l:
Expand Down
64 changes: 0 additions & 64 deletions services/optimiser/testing/test_shift.py

This file was deleted.

61 changes: 0 additions & 61 deletions services/optimiser/testing_for_scheduler.py

This file was deleted.

File renamed without changes.
39 changes: 39 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

os.environ.setdefault('username', 'user')
os.environ.setdefault('password', 'password')
os.environ.setdefault('host', '127.0.0.1')
os.environ.setdefault('port', '3306')
os.environ.setdefault('dbname', 'db')
import pytest
from application import app


@pytest.fixture(scope='module')
def test_client():
# Set the Testing configuration prior to creating the Flask application

with app.test_client() as testing_client:
# Establish an application context
with app.app_context():
yield testing_client


@pytest.fixture(scope='module')
def auth_token(test_client):
# Login with predefined admin credentials
login_payload = { # admin account details
'email': 'admin',
'password': 'admin'
}
response = test_client.post('/authentication/login', json=login_payload)
assert response.status_code == 200, "Failed to log in with the given credential"
print(response.json)
token = response.json['access_token']
return token


# append jwt token to header of all the testing requests
@pytest.fixture(autouse=True)
def set_auth_header(test_client, auth_token):
test_client.environ_base['HTTP_AUTHORIZATION'] = f'Bearer {auth_token}'
Empty file added tests/functional/__init__.py
Empty file.
137 changes: 137 additions & 0 deletions tests/functional/test_unavailability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
def test_create_unavailability_consecutive_event_id(test_client):
user_id = 49 # admin id
payload_1 = {
"title": "All Day Event",
"periodicity": 0,
"start": "2024-03-02T00:00:00Z",
"end": "2024-03-02T23:59:59Z"
}
payload_2 = {
"title": "All Day Event",
"periodicity": 0,
"start": "2024-03-03T00:00:00Z",
"end": "2024-03-03T23:59:59Z"
}
response = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload_1
)
response_2 = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload_2
)
assert response.status_code == 200
assert response_2.status_code == 200
assert response_2.json["eventId"] - response.json["eventId"] == 1


def test_create_unavailability_same_time_interval(test_client):
user_id = 49
payload = {
"title": "All Day Event",
"periodicity": 0,
"start": "2024-03-02T00:00:00Z",
"end": "2024-03-02T23:59:59Z"
}
response = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload
)
response_2 = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload
)
assert response.status_code == 200
assert response_2.status_code == 400


def test_create_unavailability_nonexistent_user_id(test_client):
user_id = -1
payload = {
"title": "All Day Event",
"periodicity": 0,
"start": "2024-03-02T00:00:00Z",
"end": "2024-03-02T23:59:59Z"
}
response = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload
)
response.status_code = 404


def test_create_unavailability_end_before_start(test_client):
user_id = 49
payload = {
"title": "All Day Event",
"periodicity": 0,
"start": "2024-03-02T00:00:00Z",
"end": "2024-03-01T23:59:59Z"
}
response = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload
)
assert response.status_code == 400


def test_create_unavailability_overlapped_time(test_client):
user_id = 49
payload_1 = {
"title": "All Day Event",
"periodicity": 0,
"start": "2024-03-03T00:00:00Z",
"end": "2024-03-04T23:59:59Z"
}
payload_2 = {
"title": "All Day Event",
"periodicity": 0,
"start": "2024-03-01T00:00:00Z",
"end": "2024-03-05T23:59:59Z"
}
response_1 = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload_1
)
response_2 = test_client.post(f"/v2/volunteers/{user_id}/unavailability",
json=payload_2
)
assert response_1.status_code == 200
assert response_2.status_code == 400


def test_merge_overlapping_unavailability_intervals(test_client):
user_id = 49
payload_1 = {
"title": "Morning Event",
"periodicity": 0,
"start": "2024-03-05T08:00:00Z",
"end": "2024-03-05T12:00:00Z"
}
payload_2 = {
"title": "Afternoon Event",
"periodicity": 0,
"start": "2024-03-05T11:00:00Z",
"end": "2024-03-05T15:00:00Z"
}
test_client.post(f"/v2/volunteers/{user_id}/unavailability", json=payload_1)
response = test_client.post(f"/v2/volunteers/{user_id}/unavailability", json=payload_2)
assert response.status_code == 200
assert len(response.json["mergedIntervals"]) == 1 # json response must have mergedIntervals field if it is merged
assert response.json["mergedIntervals"][0]["start"] == "2024-03-05T08:00:00Z"
assert response.json["mergedIntervals"][0]["end"] == "2024-03-05T15:00:00Z"


def test_merge_adjacent_unavailability_intervals(test_client):
user_id = 49
payload_1 = {
"title": "Morning Shift",
"periodicity": 0,
"start": "2024-03-06T08:00:00Z",
"end": "2024-03-06T12:00:00Z"
}
payload_2 = {
"title": "Afternoon Shift",
"periodicity": 0,
"start": "2024-03-06T12:00:00Z",
"end": "2024-03-06T16:00:00Z"
}
test_client.post(f"/v2/volunteers/{user_id}/unavailability", json=payload_1)
response = test_client.post(f"/v2/volunteers/{user_id}/unavailability", json=payload_2)
assert response.status_code == 200
assert len(response.json["mergedIntervals"]) == 1 # json response must have mergedIntervals field if it is merged
assert response.json["mergedIntervals"][0]["start"] == "2024-03-06T08:00:00Z"
assert response.json["mergedIntervals"][0]["end"] == "2024-03-06T16:00:00Z"
Empty file added tests/unit/__init__.py
Empty file.
Loading