|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +from faker import Faker |
| 5 | +import azure.functions as func |
| 6 | + |
| 7 | +from time_tracker.projects._application import _projects as azure_projects |
| 8 | +from time_tracker.projects import _domain as domain |
| 9 | +from time_tracker.projects import _infrastructure as infrastructure |
| 10 | +from time_tracker._infrastructure import DB |
| 11 | + |
| 12 | +PROJECT_URL = '/api/projects/' |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(name='insert_project') |
| 16 | +def _insert_project() -> domain.Project: |
| 17 | + def _new_project(project: domain.Project, database: DB): |
| 18 | + dao = infrastructure.ProjectsSQLDao(database) |
| 19 | + new_project = dao.create(project) |
| 20 | + return new_project |
| 21 | + return _new_project |
| 22 | + |
| 23 | + |
| 24 | +def test__project_azure_endpoint__returns_all_projects( |
| 25 | + test_db, project_factory, insert_project |
| 26 | +): |
| 27 | + project_to_insert = [project_factory(), project_factory()] |
| 28 | + inserted_projects = [ |
| 29 | + insert_project(project_to_insert[0], test_db).__dict__, |
| 30 | + insert_project(project_to_insert[1], test_db).__dict__ |
| 31 | + ] |
| 32 | + |
| 33 | + req = func.HttpRequest(method='GET', body=None, url=PROJECT_URL) |
| 34 | + response = azure_projects._get_projects.get_projects(req) |
| 35 | + projects_json_data = response.get_body().decode("utf-8") |
| 36 | + |
| 37 | + assert response.status_code == 200 |
| 38 | + assert projects_json_data == json.dumps(inserted_projects) |
| 39 | + |
| 40 | + |
| 41 | +def test__project_azure_endpoint__returns_an_project__when_project_matches_its_id( |
| 42 | + test_db, project_factory, insert_project |
| 43 | +): |
| 44 | + project_to_insert = project_factory() |
| 45 | + inserted_project = insert_project(project_to_insert, test_db).__dict__ |
| 46 | + |
| 47 | + req = func.HttpRequest( |
| 48 | + method='GET', |
| 49 | + body=None, |
| 50 | + url=PROJECT_URL, |
| 51 | + route_params={"id": inserted_project["id"]}, |
| 52 | + ) |
| 53 | + |
| 54 | + response = azure_projects._get_projects.get_projects(req) |
| 55 | + activitiy_json_data = response.get_body().decode("utf-8") |
| 56 | + |
| 57 | + assert response.status_code == 200 |
| 58 | + assert activitiy_json_data == json.dumps(inserted_project) |
| 59 | + |
| 60 | + |
| 61 | +def test__projects_azure_endpoint__returns_a_status_code_400__when_project_recive_invalid_id( |
| 62 | +): |
| 63 | + req = func.HttpRequest( |
| 64 | + method="GET", |
| 65 | + body=None, |
| 66 | + url=PROJECT_URL, |
| 67 | + route_params={"id": "invalid id"}, |
| 68 | + ) |
| 69 | + |
| 70 | + response = azure_projects._get_projects.get_projects(req) |
| 71 | + |
| 72 | + assert response.status_code == 400 |
| 73 | + assert response.get_body() == b'Invalid Format ID' |
| 74 | + |
| 75 | + |
| 76 | +def test__project_azure_endpoint__returns_an_project_with_inactive_status__when_an_project_matching_its_id_is_found( |
| 77 | + test_db, project_factory, insert_project |
| 78 | +): |
| 79 | + project_to_insert = project_factory() |
| 80 | + inserted_project = insert_project(project_to_insert, test_db).__dict__ |
| 81 | + |
| 82 | + req = func.HttpRequest( |
| 83 | + method='DELETE', |
| 84 | + body=None, |
| 85 | + url=PROJECT_URL, |
| 86 | + route_params={"id": inserted_project["id"]}, |
| 87 | + ) |
| 88 | + |
| 89 | + response = azure_projects._delete_project.delete_project(req) |
| 90 | + project_json_data = json.loads(response.get_body().decode("utf-8")) |
| 91 | + |
| 92 | + assert response.status_code == 200 |
| 93 | + assert project_json_data['status'] == 0 |
| 94 | + assert project_json_data['deleted'] is True |
| 95 | + |
| 96 | + |
| 97 | +def test__delete_projects_azure_endpoint__returns_a_status_code_400__when_project_recive_invalid_id( |
| 98 | +): |
| 99 | + req = func.HttpRequest( |
| 100 | + method="DELETE", |
| 101 | + body=None, |
| 102 | + url=PROJECT_URL, |
| 103 | + route_params={"id": "invalid id"}, |
| 104 | + ) |
| 105 | + |
| 106 | + response = azure_projects._delete_project.delete_project(req) |
| 107 | + |
| 108 | + assert response.status_code == 400 |
| 109 | + assert response.get_body() == b'Invalid Format ID' |
| 110 | + |
| 111 | + |
| 112 | +def test__update_project_azure_endpoint__returns_an_project__when_found_an_project_to_update( |
| 113 | + test_db, project_factory, insert_project |
| 114 | +): |
| 115 | + project_to_insert = project_factory() |
| 116 | + inserted_project = insert_project(project_to_insert, test_db).__dict__ |
| 117 | + |
| 118 | + project_body = {"description": Faker().sentence()} |
| 119 | + req = func.HttpRequest( |
| 120 | + method='PUT', |
| 121 | + body=json.dumps(project_body).encode("utf-8"), |
| 122 | + url=PROJECT_URL, |
| 123 | + route_params={"id": inserted_project["id"]}, |
| 124 | + ) |
| 125 | + |
| 126 | + response = azure_projects._update_project.update_project(req) |
| 127 | + activitiy_json_data = response.get_body().decode("utf-8") |
| 128 | + inserted_project.update(project_body) |
| 129 | + |
| 130 | + assert response.status_code == 200 |
| 131 | + assert activitiy_json_data == json.dumps(inserted_project) |
| 132 | + |
| 133 | + |
| 134 | +def test__update_projects_azure_endpoint__returns_a_status_code_400__when_project_recive_invalid_id( |
| 135 | +): |
| 136 | + req = func.HttpRequest( |
| 137 | + method="PUT", |
| 138 | + body=None, |
| 139 | + url=PROJECT_URL, |
| 140 | + route_params={"id": "invalid id"}, |
| 141 | + ) |
| 142 | + |
| 143 | + response = azure_projects._update_project.update_project(req) |
| 144 | + |
| 145 | + assert response.status_code == 400 |
| 146 | + assert response.get_body() == b'Invalid Format ID' |
| 147 | + |
| 148 | + |
| 149 | +def test__project_azure_endpoint__creates_an_project__when_project_has_all_attributes( |
| 150 | + project_factory |
| 151 | +): |
| 152 | + project_body = project_factory().__dict__ |
| 153 | + body = json.dumps(project_body).encode("utf-8") |
| 154 | + req = func.HttpRequest( |
| 155 | + method='POST', |
| 156 | + body=body, |
| 157 | + url=PROJECT_URL, |
| 158 | + ) |
| 159 | + |
| 160 | + response = azure_projects._create_project.create_project(req) |
| 161 | + project_json_data = json.loads(response.get_body()) |
| 162 | + project_body['id'] = project_json_data['id'] |
| 163 | + |
| 164 | + assert response.status_code == 201 |
| 165 | + assert project_json_data == project_body |
0 commit comments