-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/dev add document download view #71
Changes from 4 commits
51a0f03
59225d1
0b932a8
763e7af
2ae7074
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import pytest | ||
from faker import Faker | ||
from rest_framework.test import APIClient | ||
from rest_framework_simplejwt.tokens import RefreshToken | ||
|
||
from users.models import CustomUser | ||
|
||
fake = Faker() | ||
|
||
|
||
@pytest.fixture | ||
def user(django_user_model): | ||
|
@@ -17,4 +20,24 @@ def authorized_client(user): | |
client = APIClient() | ||
refresh = RefreshToken.for_user(user) | ||
client.credentials(HTTP_AUTHORIZATION=f"Bearer {str(refresh.access_token)}") | ||
client.user = user | ||
return client | ||
|
||
|
||
@pytest.fixture | ||
def test_data(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. фикстура-фабрика должна начинаться с make_, желательно переименовать |
||
return { | ||
"delivery_type": fake.random_element(elements=("auto", "manual")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Значения которые мы передаем - ("auto", "rw", "self-deliery") |
||
"client_id": fake.random_int(min=1, max=100), | ||
"items": [ | ||
{"product_id": fake.random_int(min=1, max=100), | ||
"quantity": fake.random_int(min=1, max=10), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Количество у нас в тоннах, это FloatField также в сериализаторе. 10 - слишком мало, здесь можно и 1000 поставить. |
||
"discount": fake.random_int(min=0, max=50)}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. До 100 можно в тестах проверять. Может быть даже разнести на несколько кейсов, с 0, 100 и всеми остальными вариантами. |
||
{"product_id": fake.random_int(min=1, max=100), | ||
"quantity": fake.random_int(min=1, max=10), | ||
"discount": fake.random_int(min=0, max=50)}, | ||
], | ||
"factory_id": fake.random_int(min=1, max=100), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Фабрик пока у нас 3, 100 слишком много. |
||
"destination": fake.city(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в destination мы получаем либо "0", либо две склеенных через запятую строки (можно и 2 pystr). Наверное здесь лучше разнести тесты на 2 кейса. |
||
"delivery_cost": fake.random_int(min=100, max=2000), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В сериализаторе у нас минимальная стоимость рейса 0, максимальная на практике (пока) доходит до 15000. Тоже наверное лучше на разные кейсы разнести: 0, все остальное. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,57 @@ | ||
import json | ||
|
||
import pytest | ||
from django.core.cache import cache | ||
from django.urls import reverse | ||
from rest_framework.test import APIClient | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__data_doc_view__unauthorized_user_cannot_post_data() -> None: | ||
def test__data_doc_view__unauthorized_user_cannot_post_data(test_data): | ||
client = APIClient() | ||
url = reverse("data") | ||
data = { | ||
"delivery_type": "auto", | ||
"client_id": 1, | ||
"items": [ | ||
{"product_id": 1, "quantity": 2, "discount": 10}, | ||
{"product_id": 2, "quantity": 5, "discount": 4}, | ||
], | ||
"factory_id": 1, | ||
"destination": "New York", | ||
"delivery_cost": 1500, | ||
} | ||
response = client.post(url, data, format="json") | ||
assert response.status_code == 401 # Unauthorized | ||
response = client.post(url, test_data, format="json") | ||
assert response.status_code == 401 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__data_doc_view__authorized_user_can_post_data(authorized_client) -> None: | ||
def test__data_doc_view__authorized_user_can_post_data(authorized_client, test_data): | ||
url = reverse("data") | ||
data = { | ||
"delivery_type": "auto", | ||
"client_id": 1, | ||
"items": [ | ||
{"product_id": 1, "quantity": 2, "discount": 10}, | ||
{"product_id": 2, "quantity": 5, "discount": 4}, | ||
], | ||
"factory_id": 1, | ||
"destination": "New York", | ||
"delivery_cost": 1500, | ||
} | ||
response = authorized_client.post(url, data, format="json") | ||
response = authorized_client.post(url, test_data, format="json") | ||
assert response.status_code == 200 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__data_doc_view__authorized_user_post_data_response_is_correct(authorized_client) -> None: | ||
def test__data_doc_view__authorized_user_post_data_response_is_correct(authorized_client, | ||
test_data): | ||
url = reverse("data") | ||
data = { | ||
"delivery_type": "auto", | ||
"client_id": 1, | ||
"items": [ | ||
{"product_id": 1, "quantity": 2, "discount": 10}, | ||
{"product_id": 2, "quantity": 5, "discount": 4}, | ||
], | ||
"factory_id": 1, | ||
"destination": "New York", | ||
"delivery_cost": 1500, | ||
} | ||
response = authorized_client.post(url, data, format="json") | ||
assert response.json() == {"success": True, "data": data} | ||
response = authorized_client.post(url, test_data, format="json") | ||
assert response.status_code == 200 | ||
assert response.json() == {"Success": True} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__create_docs_view__authorized_user_get_valid_data(authorized_client, test_data): | ||
url = reverse("file") | ||
cache_key = f"validated_data_{authorized_client.user.id}" | ||
cache.set(cache_key, json.dumps(test_data), timeout=120) | ||
response = authorized_client.get(url) | ||
assert response.status_code == 200 | ||
assert response.json() == {"message": "Documents saved"} | ||
cache.delete(cache_key) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__create_docs_view__authorized_user_get_no_data(authorized_client): | ||
url = reverse("file") | ||
response = authorized_client.get(url) | ||
assert response.status_code == 400 | ||
assert response.json() == {"error": "No data found in cache"} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__download_doc_view__returns_404_when_no_file_exists(authorized_client): | ||
url = reverse("downloadfile") | ||
response = authorized_client.get(url) | ||
assert response.status_code == 404 | ||
assert response.json() == {"error": "No file found"} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from django.urls import path | ||
|
||
from .views import DataDocView, CreateDocsView | ||
from .views import DataDocView, CreateDocsView, DownloadDocView | ||
|
||
urlpatterns = [ | ||
path("filemake/", CreateDocsView.as_view(), name="file"), | ||
path("downloadfile/", DownloadDocView.as_view(), name="downloadfile"), | ||
path("data/", DataDocView.as_view(), name="data"), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Создавать его нет необходимости, можно использовать имеющуюся фикстуру faker