-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add unit tests for Client Search (#370)
This commit introduces unit tests for the client search functionality performed via the clients dashboard. Specifically, this commit adds two `test_search_functionality` unit tests in the `views` and `api` test folders' `test_clients.py` files to test various search queries, ensuring correct client filtering.
- Loading branch information
1 parent
6c8ac9e
commit fa5b949
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,51 @@ def test_clients_get_returned(self): | |
# Check that all created clients are in the response | ||
for client in clients: | ||
self.assertIn(client, response.context.get("clients")) | ||
|
||
def test_search_functionality(self): | ||
# Log in the user | ||
self.login_user() | ||
|
||
# Create some clients with different names, emails, and IDs | ||
client1 = baker.make("backend.Client", name="John Doe", email="[email protected]", id=1, user=self.log_in_user) | ||
client2 = baker.make("backend.Client", name="Trey", email="[email protected]", id=2, user=self.log_in_user) | ||
client3 = baker.make("backend.Client", name="Jacob Johnson", email="[email protected]", id=3, user=self.log_in_user) | ||
|
||
# Define the URL with the search query parameter | ||
url = reverse(self.url_name) | ||
headers = {"HTTP_HX-Request": "true"} | ||
|
||
# Test searching by name | ||
response = self.client.get(url, {"search": "John"}, **headers) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(client1, response.context["clients"]) | ||
self.assertNotIn(client2, response.context["clients"]) | ||
self.assertIn(client3, response.context["clients"]) # Jacob Johnson contains "John" | ||
|
||
# Test searching by email | ||
response = self.client.get(url, {"search": "[email protected]"}, **headers) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(client2, response.context["clients"]) | ||
self.assertNotIn(client1, response.context["clients"]) | ||
self.assertNotIn(client3, response.context["clients"]) | ||
|
||
# Test searching by ID | ||
response = self.client.get(url, {"search": "3"}, **headers) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(client3, response.context["clients"]) | ||
self.assertNotIn(client1, response.context["clients"]) | ||
self.assertNotIn(client2, response.context["clients"]) | ||
|
||
# Test searching with a substring | ||
response = self.client.get(url, {"search": "Tr"}, **headers) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(client2, response.context["clients"]) | ||
self.assertNotIn(client1, response.context["clients"]) | ||
self.assertNotIn(client3, response.context["clients"]) | ||
|
||
# Test searching with an empty query | ||
response = self.client.get(url, {"search": ""}, **headers) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn(client1, response.context["clients"]) | ||
self.assertIn(client2, response.context["clients"]) | ||
self.assertIn(client3, response.context["clients"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
from backend.models import Client | ||
from tests.handler import ViewTestCase | ||
from model_bakery import baker | ||
|
||
|
||
class ClientsViewTestCase(ViewTestCase): | ||
|
@@ -54,3 +55,61 @@ def test_clients_view_doesnt_create_invalid_client_no_last_name(self): | |
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(client_objects_after, client_objects_before) | ||
|
||
def test_search_functionality(self): | ||
# Log in the user | ||
self.login_user() | ||
|
||
# Create some clients with different names, emails, and IDs | ||
client_attributes = [ | ||
{"name": "Client1", "email": "[email protected]", "id": 1}, | ||
{"name": "Client2", "email": "[email protected]", "id": 2}, | ||
{"name": "Client3", "email": "[email protected]", "id": 3}, | ||
{"name": "Client4", "email": "[email protected]", "id": 4}, | ||
{"name": "Client5", "email": "[email protected]", "id": 5}, | ||
{"name": "Special_Client", "email": "[email protected]", "id": 6}, | ||
] | ||
clients = [baker.make("backend.Client", user=self.log_in_user, **attrs) for attrs in client_attributes] | ||
|
||
# Define the URL with the search query parameter | ||
url = reverse("api:clients:fetch") | ||
headers = {"HTTP_HX-Request": "true"} | ||
|
||
# Define search queries to cover various edge cases | ||
search_queries = [ | ||
# Exact matches | ||
{"query": "Client1", "expected_clients": [clients[0]]}, | ||
{"query": "[email protected]", "expected_clients": [clients[1]]}, | ||
{"query": "3", "expected_clients": [clients[2]]}, | ||
# Substring matches | ||
{"query": "Client", "expected_clients": clients}, | ||
{"query": "example.com", "expected_clients": clients}, | ||
# Case insensitivity | ||
{"query": "[email protected]", "expected_clients": [clients[2]]}, | ||
{"query": "CLIENT4", "expected_clients": [clients[3]]}, | ||
{"query": "ClieNT1", "expected_clients": [clients[0]]}, | ||
# Empty query | ||
{"query": "", "expected_clients": clients}, | ||
# Nonexistent query | ||
{"query": "NonExistentClient", "expected_clients": []}, | ||
{"query": "[email protected]", "expected_clients": []}, | ||
] | ||
|
||
for search in search_queries: | ||
response = self.client.get(url, {"search": search["query"]}, **headers) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
# Verify that the "clients" context variable is set | ||
returned_clients = response.context.get("clients") | ||
self.assertIsNotNone(returned_clients, f"Context variable 'clients' should not be None for query: {search['query']}") | ||
|
||
# Convert QuerySet to list for easy comparison | ||
returned_clients_list = list(returned_clients) | ||
|
||
# Verify that the returned clients match the expected clients | ||
expected_clients = search["expected_clients"] | ||
self.assertEqual( | ||
len(returned_clients_list), len(expected_clients), f"Mismatch in number of clients for query: {search['query']}" | ||
) | ||
for client in expected_clients: | ||
self.assertIn(client, returned_clients_list, f"Client {client} should be in the response for query: {search['query']}") |