Skip to content

Commit

Permalink
Add more at a distance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JackMorganNZ committed Aug 23, 2022
1 parent ecea225 commit d0f1f17
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 3 deletions.
2 changes: 2 additions & 0 deletions csunplugged/at_a_distance/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
path(
"slides-file-generation-json/",
views.slides_file_generation_json,
name='slides_file_generation_json'
),
# eg: /at-a-distance/stroop-effect/
path(
Expand All @@ -43,5 +44,6 @@
path(
'<slug:lesson_slug>/slides-file-generation/',
views.LessonFileGenerationView.as_view(),
name='lesson_file_generation'
),
]
6 changes: 3 additions & 3 deletions csunplugged/at_a_distance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os.path
from django.views import generic
from django.utils import translation
from django.conf import settings
from django.http import JsonResponse
from django.utils.translation import get_language
Expand Down Expand Up @@ -109,12 +110,11 @@ def slides_file_generation_json(request, **kwargs):
else:
languages = [("en", "")]

lesson_slugs = list(Lesson.objects.values_list('slug', flat=True))

# For each language{}
data["languages"] = dict()
for language_code, _ in languages:
data["languages"][language_code] = lesson_slugs
with translation.override(language_code):
data["languages"][language_code] = list(Lesson.translated_objects.values_list('slug', flat=True))

# Other values
data["resolution"] = AT_A_DISTANCE_SLIDE_RESOLUTION
Expand Down
1 change: 1 addition & 0 deletions csunplugged/tests/at_a_distance/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Module for tests of the models in the at a distance application."""
23 changes: 23 additions & 0 deletions csunplugged/tests/at_a_distance/models/test_lesson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from tests.BaseTestWithDB import BaseTestWithDB
from tests.at_a_distance.AtADistanceTestDataGenerator import AtADistanceTestDataGenerator


class AtADistanceLessonModelTest(BaseTestWithDB):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.test_data = AtADistanceTestDataGenerator()

def test_at_a_distance_lesson_model_get_absolute_url(self):
lesson = self.test_data.create_lesson(1)
self.assertEqual(
lesson.get_absolute_url(),
"/en/at-a-distance/lesson-1/"
)

def test_at_a_distance_lesson_model_get_slides_path(self):
lesson = self.test_data.create_lesson(1)
self.assertEqual(
lesson.get_slides_path(),
"at_a_distance/lesson-slides/lesson-1.html"
)
1 change: 1 addition & 0 deletions csunplugged/tests/at_a_distance/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Module for tests of the views in the at a distance application."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<section>
<h1>Slide</h1>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<section>
<h1>Slide</h1>
</section>
<section>
<h1>Slide</h1>
</section>
<section>
<h1>Slide</h1>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<section>
<h1>Slide</h1>
</section>
<section>
<h1>Slide</h1>
</section>
<section>
<h1>Slide</h1>
</section>
<section>
<h1>Slide</h1>
</section>
<section>
<h1>Slide</h1>
</section>
<section>
<h1>Slide</h1>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.conf import settings
from django.test import override_settings
from django.urls import reverse
from tests.BaseTestWithDB import BaseTestWithDB
from tests.at_a_distance.AtADistanceTestDataGenerator import AtADistanceTestDataGenerator

TEST_TEMPLATES = settings.TEMPLATES
TEST_TEMPLATES[0]["DIRS"].append("tests/at_a_distance/views/assets/templates")


@override_settings(TEMPLATES=TEST_TEMPLATES)
class LessonFileGenerationViewTest(BaseTestWithDB):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.test_data = AtADistanceTestDataGenerator()
self.language = "en"

def test_lesson_file_generation_view_context(self):
lesson = self.test_data.create_lesson(1)
kwargs = {
"lesson_slug": lesson.slug,
}
url = reverse("at_a_distance:lesson_file_generation", kwargs=kwargs)
response = self.client.get(url)
self.assertEqual(
response.context["lesson"],
lesson
)
self.assertEqual(response.context["fragments"], "false")
self.assertEqual(response.context["slide_number"], "false")
56 changes: 56 additions & 0 deletions csunplugged/tests/at_a_distance/views/test_lesson_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from http import HTTPStatus
from django.conf import settings
from django.test import override_settings
from django.urls import reverse
from tests.BaseTestWithDB import BaseTestWithDB
from tests.at_a_distance.AtADistanceTestDataGenerator import AtADistanceTestDataGenerator

TEST_TEMPLATES = settings.TEMPLATES
TEST_TEMPLATES[0]["DIRS"].append("tests/at_a_distance/views/assets/templates")


@override_settings(TEMPLATES=TEST_TEMPLATES)
class LessonViewTest(BaseTestWithDB):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.test_data = AtADistanceTestDataGenerator()
self.language = "en"

def test_lesson_view_with_valid_slug(self):
lesson = self.test_data.create_lesson(1)
kwargs = {
"lesson_slug": lesson.slug
}
url = reverse("at_a_distance:lesson", kwargs=kwargs)
response = self.client.get(url)
self.assertEqual(HTTPStatus.OK, response.status_code)

def test_lesson_view_with_invalid_slug(self):
self.test_data.create_lesson(1)
kwargs = {
"lesson_slug": "wrong_slug",
}
url = reverse("at_a_distance:lesson", kwargs=kwargs)
response = self.client.get(url)
self.assertEqual(404, response.status_code)

def test_lesson_view_context(self):
lesson = self.test_data.create_lesson(1)
kwargs = {
"lesson_slug": lesson.slug,
}
url = reverse("at_a_distance:lesson", kwargs=kwargs)
response = self.client.get(url)
self.assertEqual(
response.context["lesson"],
lesson
)
self.assertEqual(
response.context["slides_pdf"],
"slides/en/lesson-1/lesson-1-slides.pdf"
)
self.assertEqual(
response.context["notes_pdf"],
"slides/en/lesson-1/lesson-1-speaker-notes.pdf"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from django.conf import settings
from django.test import override_settings
from django.urls import reverse
from tests.BaseTestWithDB import BaseTestWithDB
from tests.at_a_distance.AtADistanceTestDataGenerator import AtADistanceTestDataGenerator

TEST_TEMPLATES = settings.TEMPLATES
TEST_TEMPLATES[0]["DIRS"].append("tests/at_a_distance/views/assets/templates")
LANGUAGE1 = "lang1"
LANGUAGE2 = "lang2"
MULTIPLE_LANGUAGES = ((LANGUAGE1, LANGUAGE1), (LANGUAGE2, LANGUAGE2))


@override_settings(TEMPLATES=TEST_TEMPLATES)
class LessonFileGenerationViewTest(BaseTestWithDB):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.test_data = AtADistanceTestDataGenerator()
self.language = "en"

def test_slides_file_generation_view_single_lesson(self):
self.test_data.create_lesson(1)
url = reverse("at_a_distance:slides_file_generation_json")
response = self.client.get(url)
self.assertJSONEqual(
response.content.decode(),
{
"resolution": "1920x1080",
"slide_counts": {
"lesson-1": 2,
},
"languages": {
"en": [
"lesson-1",
]
},
}
)

def test_slides_file_generation_view_multiple_lessons(self):
self.test_data.create_lesson(1)
self.test_data.create_lesson(2)
self.test_data.create_lesson(3)
url = reverse("at_a_distance:slides_file_generation_json")
response = self.client.get(url)
self.assertJSONEqual(
response.content.decode(),
{
"resolution": "1920x1080",
"slide_counts": {
"lesson-1": 2,
"lesson-2": 4,
"lesson-3": 7,
},
"languages": {
"en": [
"lesson-1",
"lesson-2",
"lesson-3",
]
},
}
)

@override_settings(DEFAULT_LANGUAGES=MULTIPLE_LANGUAGES)
def test_slides_file_generation_view_single_lesson_multiple_languages(self):
lesson = self.test_data.create_lesson(1)
lesson.languages = [LANGUAGE1, LANGUAGE2]
lesson.save()
url = reverse("at_a_distance:slides_file_generation_json") + "?language=all"
response = self.client.get(url)
self.assertJSONEqual(
response.content.decode(),
{
"resolution": "1920x1080",
"slide_counts": {
"lesson-1": 2,
},
"languages": {
LANGUAGE1: [
"lesson-1",
],
LANGUAGE2: [
"lesson-1",
]
},
}
)

@override_settings(DEFAULT_LANGUAGES=MULTIPLE_LANGUAGES)
def test_slides_file_generation_view_multiple_lessons_multiple_languages(self):
lesson1 = self.test_data.create_lesson(1)
lesson1.languages = [LANGUAGE1, LANGUAGE2]
lesson1.save()
lesson2 = self.test_data.create_lesson(2)
lesson2.languages = [LANGUAGE1]
lesson2.save()
lesson3 = self.test_data.create_lesson(3)
lesson3.languages = [LANGUAGE2]
lesson3.save()
url = reverse("at_a_distance:slides_file_generation_json") + "?language=all"
response = self.client.get(url)
self.assertJSONEqual(
response.content.decode(),
{
"resolution": "1920x1080",
"slide_counts": {
"lesson-1": 2,
"lesson-2": 4,
"lesson-3": 7,
},
"languages": {
LANGUAGE1: [
"lesson-1",
"lesson-2",
],
LANGUAGE2: [
"lesson-1",
"lesson-3",
]
},
}
)

0 comments on commit d0f1f17

Please sign in to comment.