Skip to content

Commit

Permalink
Use a mixin instead of a view
Browse files Browse the repository at this point in the history
  • Loading branch information
SebCorbin committed May 5, 2021
1 parent 254fead commit 32a4a74
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 70 deletions.
8 changes: 0 additions & 8 deletions api/urls_v1.py

This file was deleted.

File renamed without changes.
10 changes: 5 additions & 5 deletions api/views.py → contrib/mixins.py
@@ -1,17 +1,17 @@
from rest_framework.authentication import SessionAuthentication
from rest_framework.generics import ListAPIView

from history.api.serializers import HistoryEntryListSerializer
from history.models import HistoryEntry
from .serializers import HistoryEntryListSerializer
from osis_history.models import HistoryEntry

__all__ = [
"HistoryEntryListAPIView",
"HistoryEntryListAPIMixin",
]


class HistoryEntryListAPIView(ListAPIView):
class HistoryEntryListAPIMixin(ListAPIView):
serializer_class = HistoryEntryListSerializer
authentication_classes = [SessionAuthentication, ]
authentication_classes = [SessionAuthentication]

def get_queryset(self):
"""Filter the queryset with the object's uuid passed in url"""
Expand Down
2 changes: 1 addition & 1 deletion api/serializers.py → contrib/serializers.py
@@ -1,6 +1,6 @@
from rest_framework import serializers

from history.models import HistoryEntry
from osis_history.models import HistoryEntry

__all__ = [
"HistoryEntryListSerializer",
Expand Down
12 changes: 10 additions & 2 deletions models/__init__.py
@@ -1,5 +1,13 @@
from .history_entry import HistoryEntry
from .mixins import HistoryDeleteMixin
try:
from .history_entry import HistoryEntry
from .mixins import HistoryDeleteMixin
except RuntimeError as e: # pragma: no cover
# There's a weird bug when running tests, the test runner seeing a models
# package tries to import it directly, failing to do so
import sys

if 'test' not in sys.argv:
raise e

__all__ = [
"HistoryDeleteMixin",
Expand Down
Empty file removed tests/api/__init__.py
Empty file.
54 changes: 0 additions & 54 deletions tests/api/test_views.py

This file was deleted.

32 changes: 32 additions & 0 deletions tests/history_test/urls.py
@@ -0,0 +1,32 @@
# ##############################################################################
#
# OSIS stands for Open Student Information System. It's an application
# designed to manage the core business of higher education institutions,
# such as universities, faculties, institutes and professional schools.
# The core business involves the administration of students, teachers,
# courses, programs and so on.
#
# Copyright (C) 2015-2021 Université catholique de Louvain (http://www.uclouvain.be)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of this license - GNU General Public License - is available
# at the root of the source code of this program. If not,
# see http://www.gnu.org/licenses/.
#
# ##############################################################################
from django.urls import path

from .views import HistoryTestEntryListView

urlpatterns = [
path("<uuid:uuid>/", HistoryTestEntryListView.as_view(), name="history-test"),
]
5 changes: 5 additions & 0 deletions tests/history_test/views.py
@@ -0,0 +1,5 @@
from osis_history.contrib.mixins import HistoryEntryListAPIMixin


class HistoryTestEntryListView(HistoryEntryListAPIMixin):
pass
77 changes: 77 additions & 0 deletions tests/test_views.py
@@ -0,0 +1,77 @@
# ##############################################################################
#
# OSIS stands for Open Student Information System. It's an application
# designed to manage the core business of higher education institutions,
# such as universities, faculties, institutes and professional schools.
# The core business involves the administration of students, teachers,
# courses, programs and so on.
#
# Copyright (C) 2015-2021 Université catholique de Louvain (http://www.uclouvain.be)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of this license - GNU General Public License - is available
# at the root of the source code of this program. If not,
# see http://www.gnu.org/licenses/.
#
# ##############################################################################

import uuid

from django.test import TestCase, override_settings
from django.urls import reverse
from rest_framework.test import APIClient

from base.tests.factories.user import UserFactory
from osis_history.models import HistoryEntry
from osis_history.tests.history_test.models import DummyModel


@override_settings(ROOT_URLCONF='osis_history.tests.history_test.urls')
class HistoryApiTestCase(TestCase):
client_class = APIClient

@classmethod
def setUpTestData(cls):
cls.user = UserFactory()
cls.dumb_instance = DummyModel.objects.create(uuid=uuid.uuid4())
cls.dumb_instance_without_history_entries = DummyModel.objects.create(
uuid=uuid.uuid4()
)
cls.history_entry_data = {
"object_uuid": cls.dumb_instance.uuid,
"message": "a test message",
}
# Create 3 history entries related to the same instance
HistoryEntry.objects.create(**cls.history_entry_data)
HistoryEntry.objects.create(**cls.history_entry_data)
HistoryEntry.objects.create(**cls.history_entry_data)
cls.list_url = reverse("history-test", args=[cls.dumb_instance.uuid])

def test_list_api_view_returns_related_history_entries(self):
self.client.force_login(self.user)
response = self.client.get(self.list_url)
self.assertEqual(response.status_code, 200)
# We must get back the 3 history entries created in the setup
self.assertEqual(response.data["count"], 3)
self.assertEqual(
response.data["results"][0]["message"], self.history_entry_data["message"]
)

def test_list_api_view_returns_no_results_if_given_uuid_is_not_found(self):
self.client.force_login(self.user)
list_url = reverse(
"history-test",
args=[self.dumb_instance_without_history_entries.uuid],
)
response = self.client.get(list_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 0)

0 comments on commit 32a4a74

Please sign in to comment.