Skip to content

Commit

Permalink
chore: Add unit tests for Client Search (#370)
Browse files Browse the repository at this point in the history
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
artkolpakov authored May 17, 2024
1 parent 6c8ac9e commit fa5b949
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/api/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
59 changes: 59 additions & 0 deletions tests/views/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from backend.models import Client
from tests.handler import ViewTestCase
from model_bakery import baker


class ClientsViewTestCase(ViewTestCase):
Expand Down Expand Up @@ -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']}")

0 comments on commit fa5b949

Please sign in to comment.