Skip to content

Commit

Permalink
test: implement test suite for superset integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Feb 20, 2024
1 parent f424221 commit 9dd263a
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 99 deletions.
17 changes: 6 additions & 11 deletions aspects/extensions/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from openedx_filters import PipelineStep
from web_fragments.fragment import Fragment

from aspects.utils import update_context
from aspects.utils import generate_superset_context

TEMPLATE_ABSOLUTE_PATH = "/instructor_dashboard/"
BLOCK_CATEGORY = "superset"
BLOCK_CATEGORY = "aspects"


class AddSupersetTab(PipelineStep):
Expand All @@ -31,32 +31,27 @@ def run_filter(
settings, "SUPERSET_INSTRUCTOR_DASHBOARD", {}
)
dashboard_uuid = instructor_dashboard_config.get("dashboard_uuid")

extra_filters_format = getattr(settings, "SUPERSET_EXTRA_FILTERS_FORMAT", [])

default_filters = [
"org = '{course.org}'",
"course_name = '{course.display_name}'",
"course_run = '{course.id.run}'",
]

filters = default_filters + extra_filters_format

context = update_context(
context, dashboard_uuid=dashboard_uuid, filters=filters
context = generate_superset_context(
context, dashboard_uuid, filters
)
template = Template(self.resource_string("static/html/superset.html"))

template = Template(self.resource_string("static/html/superset.html"))
html = template.render(Context(context))
frag = Fragment(html)

frag.add_css(self.resource_string("static/css/superset.css"))
frag.add_javascript(self.resource_string("static/js/embed_dashboard.js"))

section_data = {
"fragment": frag,
"section_key": BLOCK_CATEGORY,
"section_display_name": "Aspects",
"section_display_name": BLOCK_CATEGORY.title(),
"course_id": str(course.id),
"template_path_prefix": TEMPLATE_ABSOLUTE_PATH,
}
Expand Down
24 changes: 0 additions & 24 deletions aspects/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,8 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.22/ref/settings/
"""

from aspects import ROOT_DIRECTORY

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.22/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "secret-key"


# Application definition

INSTALLED_APPS = [
"aspects",
]


# Internationalization
# https://docs.djangoproject.com/en/2.22/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_TZ = True


def plugin_settings(settings):
"""
Expand Down
41 changes: 0 additions & 41 deletions aspects/settings/test.py

This file was deleted.

18 changes: 6 additions & 12 deletions aspects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
logger = logging.getLogger(__name__)


def update_context( # pylint: disable=dangerous-default-value
def generate_superset_context( # pylint: disable=dangerous-default-value
context,
superset_config={},
dashboard_uuid="",
filters=[],
user=None,
filters=[]
):
"""
Update context with superset token and dashboard id.
Expand All @@ -31,16 +29,13 @@ def update_context( # pylint: disable=dangerous-default-value
superset_config (dict): superset config.
dashboard_uuid (str): superset dashboard uuid.
filters (list): list of filters to apply to the dashboard.
user (User): user object.
"""
course = context["course"]
user = get_current_user()

if user is None:
user = get_current_user()
superset_token, dashboard_uuid = generate_guest_token(
user=user,
course=course,
superset_config=superset_config,
dashboard_uuid=dashboard_uuid,
filters=filters,
)
Expand All @@ -63,7 +58,7 @@ def update_context( # pylint: disable=dangerous-default-value
return context


def generate_guest_token(user, course, superset_config, dashboard_uuid, filters):
def generate_guest_token(user, course, dashboard_uuid, filters):
"""
Generate a Superset guest token for the user.
Expand All @@ -75,10 +70,9 @@ def generate_guest_token(user, course, superset_config, dashboard_uuid, filters)
tuple: Superset guest token and dashboard id.
or None, exception if Superset is missconfigured or cannot generate guest token.
"""
if not superset_config:
superset_config = getattr(settings, "SUPERSET_CONFIG", {})
superset_config = getattr(settings, "SUPERSET_CONFIG", {})

superset_internal_host = superset_config.get("service_url", "http://superset:8088/")
superset_internal_host = superset_config.get("service_url")
superset_username = superset_config.get("username")
superset_password = superset_config.get("password")

Expand Down
14 changes: 14 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def root(*args):
"""
return join(abspath(dirname(__file__)), *args)

DEBUG = True

DATABASES = {
'default': {
Expand Down Expand Up @@ -57,3 +58,16 @@ def root(*args):
],
},
}]

SUPERSET_INSTRUCTOR_DASHBOARD = {
"dashboard_slug": "test-dashboard-slug",
"dashboard_uuid": "test-dashboard-uuid",
}

SUPERSET_CONFIG = {
"url": "http://dummy-superset-url:8088",
"username": "superset",
"password": "superset",
}

SUPERSET_EXTRA_FILTERS_FORMAT = []
Empty file added tests/__init__.py
Empty file.
11 changes: 0 additions & 11 deletions tests/test_dummy.py

This file was deleted.

46 changes: 46 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Tests for the filters module.
"""

from unittest import TestCase
from unittest.mock import Mock, patch

from aspects.extensions.filters import BLOCK_CATEGORY, AddSupersetTab


class TestFilters(TestCase):
"""
Test suite for the LimeSurveyXBlock filters.
"""

def setUp(self) -> None:
"""
Set up the test suite.
"""
self.filter = AddSupersetTab(filter_type=Mock(), running_pipeline=Mock())
self.template_name = "test-template-name"
self.context = {"course": Mock()}

@patch("aspects.extensions.filters.generate_superset_context")
def test_run_filter(self, mock_generate_superset_context):
"""
Check the filter is not executed when there are no LimeSurvey blocks in the course.
Expected result:
- The context is returned without modifications.
"""
mock_generate_superset_context.return_value = {
"sections": [],
}

context = self.filter.run_filter(self.context, self.template_name)

self.assertDictContainsSubset(
{
"course_id": str(self.context["course"].id),
"section_key": BLOCK_CATEGORY,
"section_display_name": BLOCK_CATEGORY.title(),
"template_path_prefix": "/instructor_dashboard/",
},
context["context"]["sections"][0],
)
58 changes: 58 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Test plugin settings for commond, devstack and production environments
"""

from django.conf import settings
from django.test import TestCase

from aspects.settings import common as common_settings
from aspects.settings import production as production_setttings


class TestPluginSettings(TestCase):
"""
Tests plugin settings
"""

def test_common_settings(self):
"""
Test common settings
"""
settings.MAKO_TEMPLATE_DIRS_BASE = []
common_settings.plugin_settings(settings)
self.assertIn("MAKO_TEMPLATE_DIRS_BASE", settings.__dict__)
self.assertIn("url", settings.SUPERSET_CONFIG)
self.assertIn("username", settings.SUPERSET_CONFIG)
self.assertIn("password", settings.SUPERSET_CONFIG)
self.assertIn("dashboard_slug", settings.SUPERSET_INSTRUCTOR_DASHBOARD)
self.assertIn("dashboard_uuid", settings.SUPERSET_INSTRUCTOR_DASHBOARD)
self.assertIsNotNone(settings.SUPERSET_EXTRA_FILTERS_FORMAT)

def test_production_settings(self):
"""
Test production settings
"""
settings.ENV_TOKENS = {
"SUPERSET_CONFIG": {
"url": "http://superset.local.overhang.io:8088",
"username": "superset",
"password": "superset",
},
"SUPERSET_INSTRUCTOR_DASHBOARD": {
"dashboard_slug": "instructor-dashboard",
"dashboard_uuid": "1d6bf904-f53f-47fd-b1c9-6cd7e284d286",
},
"SUPERSET_EXTRA_FILTERS_FORMAT": [],
}
production_setttings.plugin_settings(settings)
self.assertEqual(
settings.SUPERSET_CONFIG, settings.ENV_TOKENS["SUPERSET_CONFIG"]
)
self.assertEqual(
settings.SUPERSET_INSTRUCTOR_DASHBOARD,
settings.ENV_TOKENS["SUPERSET_INSTRUCTOR_DASHBOARD"],
)
self.assertEqual(
settings.SUPERSET_EXTRA_FILTERS_FORMAT,
settings.ENV_TOKENS["SUPERSET_EXTRA_FILTERS_FORMAT"],
)
Loading

0 comments on commit 9dd263a

Please sign in to comment.