diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..076dfd2 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +DJANGO_SETTINGS_MODULE = oc_lettings_site.settings diff --git a/tests/.coverage b/tests/.coverage new file mode 100644 index 0000000..e39aa5c Binary files /dev/null and b/tests/.coverage differ diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..6b006cb --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,83 @@ +import pytest +from lettings.models import Address, Letting +from profiles.models import Profile +from django.contrib.auth.models import User + + +@pytest.fixture +@pytest.mark.django_db +def sample_address(): + """ + creates temporary Addresses objects in test DB + :return: tuple of Address + """ + address1 = Address.objects.create( + number = 22, + street = "Quality Street", + city = "New-York", + state = "New-York", + zip_code = 10010, + country_iso_code = "US" + ) + address2 = Address.objects.create( + number = 18, + street = "Rue Cocotte", + city = "Paris", + state = "Ile-de-France", + zip_code = 75000, + country_iso_code = "FR" + ) + return address1, address2 + + +@pytest.fixture +@pytest.mark.django_db +def sample_letting(sample_address): + """ + creates temporary Letting objects in test DB + :return: tuple of Lettings + """ + letting = Letting.objects.create( + title = "Pretty thing", + address = sample_address[0], + ) + letting2 = Letting.objects.create( + title = "Ugly thing", + address = sample_address[1], + ) + + return letting, letting2 + + +@pytest.fixture +@pytest.mark.django_db +def sample_profile(): + """ + creates temporary Profile objects in test DB + :return: tuple of Profiles + """ + user = User.objects.create( + username = 'TestUser', + password = 'password', + ) + user2 = User.objects.create( + username = 'TestUser2', + password = 'password2', + ) + user3 = User.objects.create( + username = 'TestUser3', + password = 'password2', + ) + profile = Profile.objects.create( + user=user, + favorite_city="Paris", + ) + profile2 = Profile.objects.create( + user=user2, + favorite_city="Marseilles", + ) + profile3 = Profile.objects.create( + user=user3, + favorite_city="Rennes", + ) + return profile, profile2, profile3 \ No newline at end of file diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/lettings/__init__.py b/tests/unit/lettings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/lettings/test_models.py b/tests/unit/lettings/test_models.py new file mode 100644 index 0000000..16224f3 --- /dev/null +++ b/tests/unit/lettings/test_models.py @@ -0,0 +1,15 @@ +import pytest + + +@pytest.mark.django_db +def test_address_str(sample_address): + """ test if Address objects are well created """ + assert str(sample_address[0]) == "22 Quality Street" + assert str(sample_address[1]) == "18 Rue Cocotte" + + +@pytest.mark.django_db +def test_letting_str2(sample_letting): + """ test if Letting objects are well created """ + assert str(sample_letting[0]) == "Pretty thing" + assert str(sample_letting[1]) == "Ugly thing" \ No newline at end of file diff --git a/tests/unit/lettings/test_urls.py b/tests/unit/lettings/test_urls.py new file mode 100644 index 0000000..85172fc --- /dev/null +++ b/tests/unit/lettings/test_urls.py @@ -0,0 +1,21 @@ +import pytest +from django.urls import reverse +from django.test import Client + + +@pytest.mark.django_db +def test_should_get_200_on_lettings_index(): + """ test the server's response """ + c = Client() + url = reverse('lettings_index') + response = c.get(url) + assert response.status_code == 200 + + +@pytest.mark.django_db +def test_should_get_200_on_letting_detail(sample_letting): + c = Client() + url = reverse('letting', kwargs={'letting_id':1}) + response = c.get(url) + assert response.status_code == 200 + diff --git a/tests/unit/lettings/test_views.py b/tests/unit/lettings/test_views.py new file mode 100644 index 0000000..9ca7beb --- /dev/null +++ b/tests/unit/lettings/test_views.py @@ -0,0 +1,19 @@ +import pytest +from lettings.models import Address, Letting + +from django.test import Client +from django.urls import reverse +from bs4 import BeautifulSoup + +@pytest.mark.django_db +def test_view_should_display_right_len_list(sample_letting): + """ + test if the list displayed contains the right amount of objects + created in fixture + """ + c = Client() + url = reverse('lettings_index') + response = c.get(url) + soup = BeautifulSoup(response.content, 'html.parser') + li_tags = soup.find_all('li') + assert len(li_tags) == 2 diff --git a/tests/unit/oc_lettings_site/__init__.py b/tests/unit/oc_lettings_site/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/oc_lettings_site/test_urls.py b/tests/unit/oc_lettings_site/test_urls.py new file mode 100644 index 0000000..e1640e4 --- /dev/null +++ b/tests/unit/oc_lettings_site/test_urls.py @@ -0,0 +1,18 @@ +from django.urls import reverse, resolve +from oc_lettings_site.views import index +from django.test import Client + + +def test_server_should_answer_200(): + """ test the server's response """ + c = Client() + url = reverse('index') + response = c.get(url) + assert response.status_code == 200 + + +def test_home_url(): + """ test the home page""" + url = reverse('index') + assert resolve(url).view_name == 'index' + assert resolve(url).func, index() \ No newline at end of file diff --git a/tests/unit/oc_lettings_site/test_view.py b/tests/unit/oc_lettings_site/test_view.py new file mode 100644 index 0000000..0178d0d --- /dev/null +++ b/tests/unit/oc_lettings_site/test_view.py @@ -0,0 +1,10 @@ +from django.test import Client +from django.urls import reverse + + +def test_view_should_reply_title_on_home(): + """ test the content display (title) """ + c = Client() + url = reverse('index') + response = c.get(url) + assert "Welcome to Holiday Homes" in response.content.decode() \ No newline at end of file diff --git a/tests/unit/profiles/__init__.py b/tests/unit/profiles/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/profiles/test_models.py b/tests/unit/profiles/test_models.py new file mode 100644 index 0000000..a8a1cb3 --- /dev/null +++ b/tests/unit/profiles/test_models.py @@ -0,0 +1,17 @@ +import pytest +from django.contrib.auth.models import User +from profiles.models import Profile + + +@pytest.mark.django_db +def test_str_profile(): + user = User.objects.create( + username = 'TestUser', + password = 'password', + ) + profile = Profile.objects.create( + user=user, + favorite_city="Paris", + + ) + assert str(profile) == "TestUser" diff --git a/tests/unit/profiles/test_urls.py b/tests/unit/profiles/test_urls.py new file mode 100644 index 0000000..5468059 --- /dev/null +++ b/tests/unit/profiles/test_urls.py @@ -0,0 +1,20 @@ +import pytest +from django.urls import reverse +from django.test import Client + + +@pytest.mark.django_db +def test_should_get_200_on_profile_index(): + """ test the server's response """ + c = Client() + url = reverse('profiles_index') + response = c.get(url) + assert response.status_code == 200 + +@pytest.mark.django_db +def test_should_get_200_on_profile_detail(sample_profile): + c = Client() + url = reverse('profile', kwargs={'username': "TestUser"}) + response = c.get(url) + assert response.status_code == 200 + diff --git a/tests/unit/profiles/test_views.py b/tests/unit/profiles/test_views.py new file mode 100644 index 0000000..fadadb5 --- /dev/null +++ b/tests/unit/profiles/test_views.py @@ -0,0 +1,13 @@ +import pytest +from django.test import Client +from django.urls import reverse +from bs4 import BeautifulSoup + +@pytest.mark.django_db +def test_view_should_display_right_len_list(sample_profile): + c = Client() + url = reverse('profiles_index') + response = c.get(url) + soup = BeautifulSoup(response.content, 'html.parser') + li_tags = soup.find_all('li') + assert len(li_tags) == 3 \ No newline at end of file