This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for favourites pictures: dashboard and favourite pictures t…
…emplates.
- Loading branch information
Showing
10 changed files
with
151 additions
and
47 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
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,17 @@ | ||
from random import SystemRandom | ||
|
||
import pytest | ||
from mimesis import Field, Locale | ||
|
||
|
||
@pytest.fixture() | ||
def faker_seed() -> int: | ||
"""Returns seed for generating fake data.""" | ||
cryptogen = SystemRandom() | ||
return cryptogen.randrange(100) | ||
|
||
|
||
@pytest.fixture() | ||
def fake(faker_seed: int) -> Field: | ||
"""Returns mimesis field.""" | ||
return Field(locale=Locale.RU, seed=faker_seed) |
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
Empty file.
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,45 @@ | ||
from dataclasses import asdict, dataclass | ||
|
||
import pytest | ||
from mimesis import Field | ||
|
||
from server.apps.identity.models import User | ||
from server.apps.pictures.models import FavouritePicture | ||
|
||
|
||
@dataclass(slots=True) | ||
class FavouritePictureData: | ||
"""Essential favourite picture data model.""" | ||
|
||
foreign_id: int | ||
url: str | ||
|
||
def as_dict(self) -> dict[str, str | int]: | ||
"""Return as a dictionary.""" | ||
return asdict(self) | ||
|
||
|
||
@pytest.fixture() | ||
def picture_data(fake: Field) -> FavouritePictureData: | ||
"""Returns favourite picture data.""" | ||
return FavouritePictureData( | ||
foreign_id=fake('numeric.increment'), | ||
url=fake('internet.uri'), | ||
) | ||
|
||
|
||
@pytest.mark.django_db() | ||
@pytest.fixture() | ||
def created_fav_picture( | ||
created_new_user: User, | ||
picture_data: 'FavouritePictureData', | ||
) -> FavouritePicture: | ||
"""Create new favourite picture.""" | ||
created_picture = FavouritePicture.objects.create( | ||
user=created_new_user, | ||
**picture_data.as_dict(), | ||
) | ||
assert str(created_picture).startswith( | ||
'<Picture {0}'.format(picture_data.foreign_id), | ||
) | ||
return created_picture |
Empty file.
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,29 @@ | ||
from http import HTTPStatus | ||
|
||
import pytest | ||
from django.test import Client | ||
from django.urls import reverse | ||
|
||
from server.apps.identity.models import User | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_valid_login( | ||
client: Client, | ||
created_new_user: User, | ||
) -> None: | ||
"""Tests that login works with correct user data.""" | ||
client.force_login(created_new_user) | ||
|
||
response = client.get(reverse('index')) | ||
assert 'Выход'.encode() in response.content | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_login_view_uses_login_template(client: Client) -> None: | ||
"""Tests that login view uses correct template.""" | ||
response = client.get(reverse('identity:login')) | ||
|
||
assert response.status_code == HTTPStatus.OK | ||
assert 'Войти в личный кабинет'.encode() in response.content | ||
assert 'Have fun!'.encode() in response.content |
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
Empty file.
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,49 @@ | ||
import pytest | ||
from django.test import Client | ||
from django.urls import reverse | ||
|
||
from server.apps.identity.models import User | ||
from server.apps.pictures.logic.usecases.favourites_list import FavouritesList | ||
from server.apps.pictures.models import FavouritePicture | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_fetch_favourite_picture_if_exists( | ||
created_new_user: User, | ||
created_fav_picture: FavouritePicture, | ||
) -> None: | ||
"""Test getting correct favourite pictures.""" | ||
favourite_pictures = FavouritesList() | ||
user_fav_pictures = favourite_pictures(user_id=created_new_user.id) | ||
|
||
assert len(user_fav_pictures) == 1 | ||
assert created_fav_picture in user_fav_pictures | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_logged_in_user_access_dashboard( | ||
created_new_user: User, | ||
client: Client, | ||
) -> None: | ||
"""Test that logged in user can access all views.""" | ||
client.force_login(created_new_user) | ||
|
||
response = client.get(reverse('pictures:dashboard')) | ||
assert 'Профиль'.encode() in response.content | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_logged_in_user_access_favourite_pictures( | ||
created_new_user: User, | ||
client: Client, | ||
created_fav_picture: FavouritePicture, | ||
) -> None: | ||
"""Test that logged in user can access all views.""" | ||
client.force_login(created_new_user) | ||
|
||
response = client.get(reverse('pictures:favourites')) | ||
assert 'Список любимых картинок'.encode() in response.content | ||
assert ( | ||
'Номер {0}'.format(created_fav_picture.foreign_id).encode() | ||
in response.content | ||
) |