Skip to content

Commit

Permalink
Render built-in dashboard locally during tests
Browse files Browse the repository at this point in the history
You can now see the dashboard built with mock data in dashboard_dev/ by
running the test_dashboard_template.py module. This enables you to
quickly refine the built-in template.
  • Loading branch information
jonathansick committed Jun 21, 2022
1 parent 16d0bca commit e02282f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pgdb/

integration_tests/ltd_keeper_doc_examples.txt

# Dashboard development
dashboard_dev

# Kubernetes deployment
kubernetes/cloudsql-secrets.yaml
kubernetes/keeper-secrets.yaml
Expand Down
36 changes: 36 additions & 0 deletions keeper/dashboard/templateproviders.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import shutil
from pathlib import Path

import jinja2
Expand Down Expand Up @@ -54,3 +55,38 @@ def render_build_dashboard(
builds=build_contexts,
asset_dir="../_dashboard-assets",
)

def render_locally(
self,
*,
directory: Path,
project_context: ProjectContext,
edition_contexts: EditionContextList,
build_contexts: BuildContextList,
clobber: bool = True,
) -> None:
"""Render the dashboard into a local directory for testing."""
if directory.exists():
shutil.rmtree(directory)
directory.mkdir()
assets_dir = directory.joinpath("_dashboard-assets")
# assets_dir.mkdir()
v_dir = directory.joinpath("v")
v_dir.mkdir()
builds_dir = directory.joinpath("builds")
builds_dir.mkdir()

shutil.copytree(self.static_dir, assets_dir)

edition_dashboard = self.render_edition_dashboard(
project_context=project_context,
edition_contexts=edition_contexts,
)
v_html_path = v_dir.joinpath("index.html")
v_html_path.write_text(edition_dashboard)

build_dashboard = self.render_build_dashboard(
project_context=project_context, build_contexts=build_contexts
)
build_html_path = builds_dir.joinpath("index.html")
build_html_path.write_text(build_dashboard)
58 changes: 58 additions & 0 deletions tests/test_dashboard_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Test the built-in dashboard template by rendering to a local directory."""

from __future__ import annotations

from datetime import datetime, timezone
from pathlib import Path
from typing import List

from keeper.dashboard.context import (
BuildContext,
BuildContextList,
EditionContext,
EditionContextList,
ProjectContext,
)
from keeper.dashboard.templateproviders import BuiltinTemplateProvider
from keeper.models import EditionKind


def test_templates() -> None:
output_dir = Path(__file__).parent.parent.joinpath("dashboard_dev")

# Create mock data
project = ProjectContext(
title="LTD Test Project",
source_repo_url="https://github.com/lsst-sqre/ltd-keeper",
url="https://example.com/ltd-test/",
)
editions: List[EditionContext] = []
builds: List[BuildContext] = []

editions.append(
EditionContext(
title="Current",
url="https://example.com/ltd-test/",
date_updated=datetime(2022, 6, 21, tzinfo=timezone.utc),
kind=EditionKind.main,
slug="__main",
git_ref="main",
)
)

builds.append(
BuildContext(
slug="1",
url="https://example.com/ltd-test/builds/1",
git_ref="main",
date=datetime(2022, 6, 21, tzinfo=timezone.utc),
)
)

template = BuiltinTemplateProvider()
template.render_locally(
directory=output_dir,
project_context=project,
edition_contexts=EditionContextList(editions),
build_contexts=BuildContextList(builds),
)

0 comments on commit e02282f

Please sign in to comment.