Skip to content

Commit

Permalink
Use drf client instead django
Browse files Browse the repository at this point in the history
  • Loading branch information
yalef committed Nov 30, 2023
1 parent 3a6d748 commit 28c530a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile

from rest_framework import test

import pytest

from tests.fake_app.models import Artist, Band, Membership
Expand Down Expand Up @@ -127,3 +130,19 @@ def temp_directory_for_media(tmpdir_factory):
)
media = tmpdir_factory.mktemp("tmp_media")
settings.MEDIA_ROOT = media


@pytest.fixture
def api_client() -> test.APIClient:
"""Create api client."""
return test.APIClient()


@pytest.fixture
def admin_api_client(
superuser: User,
api_client: test.APIClient,
) -> test.APIClient:
"""Authenticate admin_user and return api client."""
api_client.force_authenticate(user=superuser)
return api_client
23 changes: 9 additions & 14 deletions tests/test_api/test_import.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test.client import Client
from django.urls import reverse

from rest_framework import status
from rest_framework.test import APIClient

import pytest

Expand All @@ -12,13 +12,11 @@

@pytest.mark.django_db(transaction=True)
def test_import_api_creates_import_job(
client: Client,
superuser: User,
admin_api_client: APIClient,
uploaded_file: SimpleUploadedFile,
):
"""Ensure import start api creates new import job."""
client.force_login(superuser)
response = client.post(
response = admin_api_client.post(
path=reverse("import-artist-start"),
data={
"file": uploaded_file,
Expand All @@ -32,13 +30,11 @@ def test_import_api_creates_import_job(

@pytest.mark.django_db(transaction=True)
def test_import_api_detail(
client: Client,
superuser: User,
admin_api_client: APIClient,
artist_import_job: ImportJob,
):
"""Ensure import detail api shows current import job status."""
client.force_login(superuser)
response = client.get(
response = admin_api_client.get(
path=reverse(
"import-artist-detail",
kwargs={"pk": artist_import_job.id},
Expand All @@ -49,7 +45,7 @@ def test_import_api_detail(
artist_import_job.refresh_from_db()
artist_import_job.confirm_import()

response = client.get(
response = admin_api_client.get(
path=reverse(
"import-artist-detail",
kwargs={"pk": artist_import_job.id},
Expand All @@ -60,7 +56,7 @@ def test_import_api_detail(

@pytest.mark.django_db(transaction=True)
def test_force_import_api_detail(
client: Client,
admin_api_client: APIClient,
superuser: User,
force_import_artist_job: ImportJob,
):
Expand All @@ -71,8 +67,7 @@ def test_force_import_api_detail(
and create right ones.
"""
client.force_login(superuser)
response = client.get(
response = admin_api_client.get(
path=reverse(
"import-artist-detail",
kwargs={"pk": force_import_artist_job.id},
Expand All @@ -83,7 +78,7 @@ def test_force_import_api_detail(
force_import_artist_job.refresh_from_db()
force_import_artist_job.confirm_import()

response = client.get(
response = admin_api_client.get(
path=reverse(
"import-artist-detail",
kwargs={"pk": force_import_artist_job.id},
Expand Down

0 comments on commit 28c530a

Please sign in to comment.