Skip to content

Commit

Permalink
feat: move mako logic to init method (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-canon committed Sep 20, 2023
1 parent 13ed2ff commit e145f56
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 49 deletions.
25 changes: 1 addition & 24 deletions eox_nelp/course_experience/frontend/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
Classes:
CourseExperienceFrontendTestCase: Test CourseExperienceFrontendView template.
"""
from django.apps import apps
from django.http import HttpResponse
from django.shortcuts import render
from django.test import TestCase
from django.urls import reverse
from mock import patch
from rest_framework.test import APIClient


Expand All @@ -21,24 +17,7 @@ def setUp(self): # pylint: disable=invalid-name
self.client = APIClient()
self.url_endpoint = reverse("course-experience-frontend:feedback-courses")

@patch("eox_nelp.templates_config.edxmako")
def test_edxmako_render_correct_call(self, edxmako_mock):
""" Test edxmako functions from edxapp_wrapper are called with the right values.
Expected behavior:
- `edxmako_mock.paths.add_lookup` is called with course_experience_template_path.
(The path of course xp templates is used)
- The get request to the url_endpoint is using the template `feedback_courses.html`.
"""
course_experience_template_path = apps.get_app_config('eox_nelp').path + "/course_experience/frontend/templates"
edxmako_mock.shortcuts.render_to_response.return_value = HttpResponse(content='Template mock')

self.client.get(self.url_endpoint)

edxmako_mock.paths.add_lookup.assert_called_with('main', course_experience_template_path)
edxmako_mock.shortcuts.render_to_response.assert_called_with("feedback_courses.html", {}, 'main', None)

@patch("eox_nelp.course_experience.frontend.views.render_to_response")
def test_feedback_course_template_behaviour(self, render_to_response_mock):
def test_feedback_course_template_behaviour(self):
""" The correct rendering of the feedback courses template using the url_endpoint
for frontend feedback courses.
Expected behavior:
Expand All @@ -48,8 +27,6 @@ def test_feedback_course_template_behaviour(self, render_to_response_mock):
- Response has the correct path to load styles with feedback carrousel css.
- Response has the correct path to load script with feedback carrousel js.
"""
render_to_response_mock.return_value = render(None, "feedback_courses.html")

response = self.client.get(self.url_endpoint)

self.assertContains(response, '<title>Feedback courses general</title>', status_code=200)
Expand Down
4 changes: 2 additions & 2 deletions eox_nelp/course_experience/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""
from django.views import View

from eox_nelp.templates_config import render_to_response
from eox_nelp.edxapp_wrapper.edxmako import edxmako


class FeedbackCoursesTemplate(View):
Expand All @@ -19,4 +19,4 @@ class FeedbackCoursesTemplate(View):

def get(self, request): # pylint: disable=unused-argument
"""Render start html"""
return render_to_response("feedback_courses.html", {})
return edxmako.shortcuts.render_to_response("feedback_courses.html", {}, "main", request)
24 changes: 24 additions & 0 deletions eox_nelp/init_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
patch_user_gender_choices: Change the current openedx gender options (Male, Female, Other)
"""
import os

from django.utils.translation import gettext_noop


Expand All @@ -15,6 +17,7 @@ def run_init_pipeline():
Executes multiple processes that must run before starting the django application.
"""
patch_user_gender_choices()
set_mako_templates()


def patch_user_gender_choices():
Expand All @@ -30,3 +33,24 @@ def patch_user_gender_choices():
('m', gettext_noop('Male')),
('f', gettext_noop('Female')),
)


def set_mako_templates():
"""This method adds the plugin templates to mako configuration."""
# pylint: disable=import-error, import-outside-toplevel
# This cannot be at the top of the file since this file is imported the plugin initialization
# and therefore the settings has not been set yet
from eox_nelp.course_experience.frontend import templates as course_experience_templates
from eox_nelp.edxapp_wrapper.edxmako import edxmako
from eox_nelp.stats import templates as stats_templates

templates_modules_to_include = [
stats_templates,
course_experience_templates,
]

for module in templates_modules_to_include:
path_to_templates = os.path.dirname(module.__file__)

if path_to_templates not in edxmako.LOOKUP['main'].directories:
edxmako.paths.add_lookup('main', path_to_templates)
4 changes: 2 additions & 2 deletions eox_nelp/stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
classes:
get_tenant_stats: function based view.
"""
from eox_nelp.templates_config import render_to_response
from eox_nelp.edxapp_wrapper.edxmako import edxmako

STATS_QUERY_PARAMS = [
"show_courses",
Expand Down Expand Up @@ -47,4 +47,4 @@ def get_tenant_stats(request):
context = {query_param: "true" for query_param in STATS_QUERY_PARAMS}
context.update(request.GET.dict())

return render_to_response("tenant_stats.html", context)
return edxmako.shortcuts.render_to_response("tenant_stats.html", context, "main", request)
21 changes: 0 additions & 21 deletions eox_nelp/templates_config.py

This file was deleted.

51 changes: 51 additions & 0 deletions eox_nelp/tests/test_init_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""This file contains all the test for the init_pipeline.py file.
Classes:
RunInitPipelineTestCase: Tests cases for run_init_pipeline method.
SetMakoTemplatesTestCase: Tests cases for set_mako_templates method.
"""
import os
from unittest.mock import call

from django.test import TestCase
from mock import patch

from eox_nelp.course_experience.frontend import templates as course_experience_templates
from eox_nelp.edxapp_wrapper.edxmako import edxmako
from eox_nelp.init_pipeline import run_init_pipeline, set_mako_templates
from eox_nelp.stats import templates as stats_templates


class RunInitPipelineTestCase(TestCase):
"""Test class for run_init_pipeline method."""

@patch("eox_nelp.init_pipeline.patch_user_gender_choices")
@patch("eox_nelp.init_pipeline.set_mako_templates")
def test_pipeline_execute_expected_methods(self, set_mako_templates_mock, patch_user_gender_choices_mock):
""" Test that method calls the expected methods during the pipeline execution.
Expected behavior:
- set_mako_templates_mock is called once.
- patch_user_gender_choices_mock is called once.
"""
run_init_pipeline()

set_mako_templates_mock.assert_called_once()
patch_user_gender_choices_mock.assert_called_once()


class SetMakoTemplatesTestCase(TestCase):
"""Test class for set_mako_templates method."""

def test_edxmako_adds_expected_paths(self):
""" Test that method adds the expected template paths.
Expected behavior:
- `edxmako.paths.add_lookup` is called with the expected paths.
"""
set_mako_templates()

edxmako.paths.add_lookup.assert_has_calls([
call('main', os.path.dirname(stats_templates.__file__)),
call('main', os.path.dirname(course_experience_templates.__file__)),
])

0 comments on commit e145f56

Please sign in to comment.