test models, urls, views - 100%

This commit is contained in:
2025-09-09 16:20:15 +02:00
parent cb3af725b3
commit 2abad89208
16 changed files with 218 additions and 0 deletions

0
tests/unit/__init__.py Normal file
View File

View File

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

View File

@@ -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()

View File

@@ -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</h1>" in response.content.decode()

View File

View File

@@ -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"

View File

@@ -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

View File

@@ -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