Skip to content

Commit

Permalink
test: create unit tests for contact_repository (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
DEENUU1 committed Feb 26, 2024
1 parent 841f9a3 commit fda82d4
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions tests/test_support/test_contact_repository.py
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

0 comments on commit fda82d4

Please sign in to comment.