-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: create unit tests for contact_repository (#11)
- Loading branch information
Showing
1 changed file
with
102 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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import pytest | ||
from rest_framework.exceptions import NotFound | ||
|
||
from support.models import Contact | ||
from support.repository.contact_repository import ContactRepository | ||
|
||
|
||
@pytest.fixture | ||
def repository(): | ||
return ContactRepository() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_create(repository): | ||
contact_data = { | ||
"subject": "Test Subject", | ||
"message": "Test Message", | ||
"email": "[email protected]" | ||
} | ||
|
||
created_contact = repository.create(contact_data) | ||
|
||
assert isinstance(created_contact, Contact) | ||
assert created_contact.subject == "Test Subject" | ||
assert created_contact.message == "Test Message" | ||
assert created_contact.email == "[email protected]" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_all(repository): | ||
contacts_data = [ | ||
{"subject": "Subject 1", "message": "Message 1", "email": "[email protected]"}, | ||
{"subject": "Subject 2", "message": "Message 2", "email": "[email protected]"} | ||
] | ||
|
||
created_contacts = [repository.create(contact) for contact in contacts_data] | ||
retrieved_contacts = repository.get_all() | ||
|
||
assert len(retrieved_contacts) == 2 | ||
assert set(retrieved_contacts) == set(created_contacts) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_by_id(repository): | ||
contact_data = { | ||
"subject": "Test Subject", | ||
"message": "Test Message", | ||
"email": "[email protected]" | ||
} | ||
|
||
created_contact = repository.create(contact_data) | ||
retrieved_contact = repository.get_by_id(created_contact.id) | ||
|
||
assert retrieved_contact == created_contact | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_get_by_id_not_found(repository): | ||
with pytest.raises(NotFound): | ||
repository.get_by_id(999) # Assuming ID 999 doesn't exist | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_update(repository): | ||
contact_data = { | ||
"subject": "Test Subject", | ||
"message": "Test Message", | ||
"email": "[email protected]" | ||
} | ||
updated_data = {"subject": "Updated Subject"} | ||
|
||
created_contact = repository.create(contact_data) | ||
updated_contact = repository.update(created_contact.id, updated_data) | ||
|
||
assert updated_contact.subject == "Updated Subject" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_update_not_found(repository): | ||
with pytest.raises(NotFound): | ||
repository.update(999, {"subject": "Updated Subject"}) # Assuming ID 999 doesn't exist | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete(repository): | ||
contact_data = { | ||
"subject": "Test Subject", | ||
"message": "Test Message", | ||
"email": "[email protected]" | ||
} | ||
|
||
created_contact = repository.create(contact_data) | ||
repository.delete(created_contact.id) | ||
|
||
with pytest.raises(NotFound): | ||
repository.get_by_id(created_contact.id) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete_not_found(repository): | ||
with pytest.raises(NotFound): | ||
repository.delete(999) # Assuming ID 999 doesn't exist |