Skip to content

Commit

Permalink
tests: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalEgn authored and pamfilos committed Oct 9, 2023
1 parent 7f589b4 commit 1302030
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 165 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/backend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,6 @@ jobs:
uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
- name: Create index
run: >
docker run
--pull always
--network=host
--entrypoint poetry
--env DJANGO_SETTINGS_MODULE=config.settings.test
--env DISABLE_SECURITY_PLUGIN=true
--env POSTGRES_DB=scoap3
--env POSTGRES_USER=scoap3
--env POSTGRES_PASSWORD=scoap3
--env POSTGRES_HOST=127.0.0.1
--env OPENSEARCH_HOST=127.0.0.1:9200
${{ inputs.image }}
run python manage.py opensearch index --force create
- name: Test
run: >
docker run
Expand Down
5 changes: 4 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@

# Opensearch
# ------------------------------------------------------------------------------
OPENSEARCH_INDEX_PREFIX = env("OPENSEARCH_INDEX_PREFIX")
# Name of the Opensearch index
OPENSEARCH_INDEX_NAMES = {
"scoap3.articles.documents": f'{env("OPENSEARCH_INDEX_PREFIX")}-articles',
}

OPENSEARCH_DSL = {
"default": {"hosts": env("OPENSEARCH_HOST")},
Expand Down
11 changes: 9 additions & 2 deletions config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"DJANGO_SECRET_KEY",
default="wpQt7H4zQGseXwknqw5RFWSpBXxKwr3FqavY6iEzFMmRLlUJWJ8U3eftfJlh88Ie",
)
ALLOWED_HOSTS = ["127.0.0.1"]


# https://docs.djangoproject.com/en/dev/ref/settings/#test-runner
TEST_RUNNER = "django.test.runner.DiscoverRunner"

Expand All @@ -36,9 +39,13 @@

# Opensearch
# ------------------------------------------------------------------------------
# OPENSEARCH_HOST overrides for the default OpenSearch host and port
ALLOWED_HOSTS = ["127.0.0.1"]
# Name of the Opensearch index
OPENSEARCH_INDEX_NAMES = {
"scoap3.articles.documents": "scoap3-backend-test-articles",
}
# Force an index refresh with every save.
OPENSEARCH_DSL_AUTO_REFRESH = True

OPENSEARCH_DSL = {
"default": {
"hosts": [env("OPENSEARCH_HOST")],
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ build-backend = "poetry.core.masonry.api"
[tool.pytest.ini_options]
addopts = ["--ds=config.settings.test", "--reuse-db"]
python_files = ["test_*.py", "*_test.py"]
filterwarnings = [
"ignore::DeprecationWarning",
"ignore::UserWarning"
]
4 changes: 2 additions & 2 deletions scoap3/articles/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def prepare_publication_info(self, instance):
return serialized_publication_infos

class Index:
name = f"{settings.OPENSEARCH_INDEX_PREFIX}-articles"
settings = {"number_of_shards": 1, "number_of_replicas": 0}
name = settings.OPENSEARCH_INDEX_NAMES[__name__]
settings = {"number_of_shards": 1, "number_of_replicas": 1}

class Django:
model = Article
Expand Down
3 changes: 0 additions & 3 deletions scoap3/articles/tests.py

This file was deleted.

77 changes: 14 additions & 63 deletions scoap3/articles/tests/test_article_indexing.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,27 @@
import json

import pytest
from django.contrib.auth import get_user_model
from django.test.client import Client
from rest_framework.authtoken.models import Token
from django.urls import reverse

pytestmark = pytest.mark.django_db
User = get_user_model()


@pytest.fixture
def admin_user_token(user):
client = Client()
password = "admin"
my_admin = User.objects.create_superuser("admin", "[email protected]", password)
client.login(username=my_admin.username, password="admin")
user_token = Token.objects.create(user=user)
return {"client": client, "user_token": user_token}


@pytest.fixture
def license_id(admin_user_token):
license = {"url": "https://creativecommons.org/about/cclicenses/", "name": "cc"}
response = admin_user_token["client"].post(
"/api/license/",
data=license,
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {admin_user_token['user_token']}",
)
return json.loads(response.content.decode("utf-8"))["id"]


def test_article_post_and_delete(admin_user_token, license_id):
response = admin_user_token["client"].get(
"/api/license/",
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {admin_user_token['user_token']}",
)
@pytest.mark.django_db
def test_article_post_and_delete(client, user, license):
client.force_login(user)
article = {
"reception_date": "2023-07-11",
"acceptance_date": "2023-07-11",
"publication_date": "2023-07-11",
"first_online_date": "2023-07-11",
"title": "string",
"subtitle": "string",
"abstract": "string",
"related_licenses": [license_id],
"related_materials": [],
"_files": [],
"related_licenses": [license.id],
}
url = "http://localhost:8000/api/articles/"
response = admin_user_token["client"].post(
url,
response = client.post(
reverse("api:article-list"),
data=article,
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {admin_user_token['user_token']}",
)
article_id = json.loads(response.content.decode("utf-8"))["id"]
assert response.status_code == 201
opensearch_url = f"http://localhost:8000/search/article/{article_id}/"
response = admin_user_token["client"].get(
opensearch_url,
data=article,
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {admin_user_token['user_token']}",
)

article_id = json.loads(response.content)["id"]
article_detail_url = reverse("api:article-detail", kwargs={"pk": article_id})

response = client.get(article_detail_url)
assert response.status_code == 200
url_delete_article = f"http://localhost:8000/api/articles/{article_id}/"
response = admin_user_token["client"].delete(
url_delete_article,
data=article,
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {admin_user_token['user_token']}",
)

response = client.delete(article_detail_url)
assert response.status_code == 204
16 changes: 5 additions & 11 deletions scoap3/articles/tests/test_article_views.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient

pytestmark = pytest.mark.django_db


@pytest.fixture
def api_client():
return APIClient()


class TestArticleViewSet:
def test_get_article(self, api_client):
def test_get_article(self, client):
url = reverse("api:article-list")
response = api_client.get(url)
response = client.get(url)
assert response.status_code == status.HTTP_200_OK


class TestArticleIdentifierViewSet:
def test_get_article_identifier(self, api_client):
def test_get_article_identifier(self, client):
url = reverse("api:articleidentifier-list")
response = api_client.get(url)
response = client.get(url)
assert response.status_code == status.HTTP_200_OK

url = reverse("api:articleidentifier-detail", kwargs={"pk": 0})
response = api_client.get(url)
response = client.get(url)
assert response.status_code == status.HTTP_404_NOT_FOUND
24 changes: 6 additions & 18 deletions scoap3/articles/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

pytestmark = pytest.mark.django_db


@pytest.fixture
def api_client():
return APIClient()


def test_search_article(user, api_client):
user_token = Token.objects.create(user=user)
response = api_client.get(
"/search/article",
content_type="application/json",
HTTP_AUTHORIZATION=f"Token {user_token}",
follow=True,
)

@pytest.mark.django_db
@pytest.mark.usefixtures("rebuild_opensearch_index")
def test_article_search(user, client):
client.force_login(user)
response = client.get(reverse("search:article-list"))
assert response.status_code == status.HTTP_200_OK
16 changes: 5 additions & 11 deletions scoap3/authors/tests/test_author_views.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient

pytestmark = pytest.mark.django_db


@pytest.fixture
def api_client():
return APIClient()


class TestAuthorViewSet:
def test_get_article(self, api_client):
def test_get_article(self, client):
url = reverse("api:author-list")
response = api_client.get(url)
response = client.get(url)
assert response.status_code == status.HTTP_200_OK


class TestAuthorIdentifierViewSet:
def test_get_article_identifier(self, api_client):
def test_get_article_identifier(self, client):
url = reverse("api:authoridentifier-list")
response = api_client.get(url)
response = client.get(url)
assert response.status_code == status.HTTP_200_OK

url = reverse("api:authoridentifier-detail", kwargs={"pk": 0})
response = api_client.get(url)
response = client.get(url)
assert response.status_code == status.HTTP_404_NOT_FOUND
15 changes: 15 additions & 0 deletions scoap3/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import pytest
from django.core.management import call_command

from scoap3.misc.models import License
from scoap3.misc.tests.factories import LicenseFactory
from scoap3.users.models import User
from scoap3.users.tests.factories import UserFactory


@pytest.fixture
def rebuild_opensearch_index():
call_command("opensearch", "index", "rebuild", "--force")
yield
call_command("opensearch", "index", "delete", "--force")


@pytest.fixture(autouse=True)
def media_storage(settings, tmpdir):
settings.MEDIA_ROOT = tmpdir.strpath
Expand All @@ -12,3 +22,8 @@ def media_storage(settings, tmpdir):
@pytest.fixture
def user(db) -> User:
return UserFactory()


@pytest.fixture
def license(db) -> License:
return LicenseFactory()
Empty file added scoap3/misc/tests/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions scoap3/misc/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from factory import Faker
from factory.django import DjangoModelFactory

from scoap3.misc.models import License


class LicenseFactory(DjangoModelFactory):
url = Faker("url")
name = Faker("domain_word")

class Meta:
model = License
django_get_or_create = ["url"]
Loading

0 comments on commit 1302030

Please sign in to comment.