From 546c5702878359011d6a31bd3fe7a6a204a2200b Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Fri, 31 Jan 2025 20:32:12 +0100 Subject: [PATCH] Clean up testfunctions.ipynb by removing redundant code Removed outdated and unused code snippets from testfunctions.ipynb to streamline the notebook. This improves readability and reduces clutter, ensuring the file contains only relevant and functional code samples. --- backend/tests/test_contact.py | 123 ++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 backend/tests/test_contact.py diff --git a/backend/tests/test_contact.py b/backend/tests/test_contact.py new file mode 100644 index 0000000..6f0d511 --- /dev/null +++ b/backend/tests/test_contact.py @@ -0,0 +1,123 @@ +from fastapi.testclient import TestClient +from backend.main import app + +client = TestClient(app) + + +def test_create_contact_success(): + response = client.post( + "/contact", + json={ + "pgroups": "p20001", + "firstname": "John", + "lastname": "Rambo", + "email": "john.rambo@example.com", + "phone_number": "+000000000", + }, + ) + # Assert success and verify response structure + assert response.status_code == 201 + json_response = response.json() + assert json_response["firstname"] == "John" + assert json_response["lastname"] == "Rambo" + assert json_response["email"] == "john.rambo@example.com" + + +def test_create_contact_already_exists(): + # Ensure that the route fails gracefully if contact exists + client.post( + "/contact", + json={ + "pgroups": "p20001", + "firstname": "John", + "lastname": "Rambo", + "email": "john.rambo@example.com", + "phone_number": "+000000000", + }, + ) + response = client.post( + "/contact", + json={ + "pgroups": "p20001", + "firstname": "John", + "lastname": "Rambo", + "email": "john.rambo@example.com", + "phone_number": "+000000000", + }, + ) + assert response.status_code == 400 + assert ( + response.json()["detail"] + == "This contact already exists in the provided pgroup." + ) + + +def test_get_active_contacts(): + # Seed with some test data + client.post( + "/contact", + json={ + "pgroups": "p20001", + "firstname": "John", + "lastname": "Rambo", + "email": "john.rambo@example.com", + "phone_number": "+000000000", + }, + ) + + response = client.get("/contacts", params={"active_pgroup": "p20001"}) + assert response.status_code == 200 + json_response = response.json() + assert isinstance(json_response, list) + assert json_response[0]["firstname"] == "John" + assert json_response[0]["lastname"] == "Rambo" + + +def test_get_contacts_invalid_pgroup(): + response = client.get("/contacts", params={"active_pgroup": "invalid_pgroup"}) + assert response.status_code == 400 + assert response.json()["detail"] == "Invalid pgroup provided." + + +def test_update_contact_success(): + # Create a contact first + contact = client.post( + "/contact", + json={ + "pgroups": "p20001", + "firstname": "John", + "lastname": "Rambo", + "email": "john.rambo@example.com", + "phone_number": "+000000000", + }, + ).json() + + response = client.put( + f"/contacts/{contact['id']}", + json={ + "firstname": "Johnny", + "lastname": "Ramboski", + "pgroups": "p20001", + }, + ) + assert response.status_code == 200 + json_response = response.json() + assert json_response["firstname"] == "Johnny" + assert json_response["lastname"] == "Ramboski" + + +def test_delete_contact(): + # Create a contact first + contact = client.post( + "/contact", + json={ + "pgroups": "p20001", + "firstname": "John", + "lastname": "Rambo", + "email": "john.rambo@example.com", + "phone_number": "+000000000", + }, + ).json() + + response = client.delete(f"/contacts/{contact['id']}") + assert response.status_code == 204