Code source de profiles.tests

 1import pytest
 2
 3from django.urls import reverse, resolve
 4from django.test import Client
 5from pytest_django.asserts import assertTemplateUsed
 6
 7from profiles.models import Profile
 8from django.contrib.auth.models import User
 9
10C = Client()
11
12
[docs] 13@pytest.mark.django_db 14def test_profiles_index_url(): 15 user = User.objects.create_user(username="TestUser") 16 Profile.objects.create(user=user, favorite_city="FavoriteCost") 17 path = reverse("profiles_index") 18 19 assert path == "/profiles/" 20 assert resolve(path).view_name == "profiles_index"
21 22
[docs] 23@pytest.mark.django_db 24def test_profiles_letting_url(): 25 user = User.objects.create_user(username="TestUser") 26 Profile.objects.create(user=user, favorite_city="FavoriteCost") 27 path = reverse("profile", kwargs={'username': "TestUser"}) 28 29 assert path == "/profiles/" + "TestUser/" 30 assert resolve(path).view_name == "profile"
31 32
[docs] 33@pytest.mark.django_db 34def test_profiles_index_view(): 35 client = C 36 user = User.objects.create_user(username="TestUser") 37 Profile.objects.create(user=user, favorite_city="FavoriteCost") 38 path = reverse("profiles_index") 39 response = client.get(path) 40 content = response.content.decode() 41 42 expected_content = '<a href="/profiles/TestUser/">TestUser</a>' 43 44 assert expected_content in content 45 assert response.status_code == 200 46 assertTemplateUsed(response, "profiles/index.html")
47 48
[docs] 49@pytest.mark.django_db 50def test_profiles_profile_view(): 51 client = C 52 user = User.objects.create_user(username="TestUser") 53 Profile.objects.create(user=user, favorite_city="FavoriteCost") 54 path = reverse("profile", kwargs={'username': 'TestUser'}) 55 response = client.get(path) 56 content = response.content.decode() 57 58 expected_content = ['<p><strong>First name :</strong> </p>', 59 '<p><strong>Last name :</strong> </p>', 60 '<p><strong>Email :</strong> </p>', 61 '<p><strong>Favorite city :</strong> FavoriteCost</p>' 62 ] 63 64 for elem in expected_content: 65 assert elem in content 66 assert response.status_code == 200 67 assertTemplateUsed(response, "profiles/profile.html")
68 69
[docs] 70@pytest.mark.django_db 71def test_profile_model(): 72 Client() 73 user = User.objects.create_user(username="TestUser") 74 profile = Profile.objects.create(user=user, 75 favorite_city="FavoriteCost") 76 77 expected_content = "TestUser" 78 79 assert str(profile) == expected_content