Skip to content

Commit

Permalink
Merge pull request #12 from ZPascal/remove-src-folder-and-add-more-un…
Browse files Browse the repository at this point in the history
…ittests

Add new tests and refactor the code
  • Loading branch information
ZPascal authored Sep 15, 2022
2 parents 5f0a041 + 0af272a commit b430d22
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 36 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import sys
import json
import logging
from typing import Dict
import tempfile

import jinja2

from src.grafana_dashboard.model import Model
from .model import Model


class Dashboard:
Expand All @@ -23,18 +25,17 @@ def __init__(self, dashboard_model: Model):
self.dashboard_model = dashboard_model
self.logging = logging.Logger

def get_dashboard_json(self, template_values: dict) -> dict:
"""The method includes a functionality to template the selected dashboard and return the corresponding dashboard
as dictionary
def get_dashboard_json(self, template_values: Dict) -> Dict:
"""The method includes a functionality to template the selected dashboard and return the corresponding dashboard as dictionary
Args:
template_values (dict): Specify the inserted templating values as dict
template_values (Dict): Specify the inserted templating values as dict
Raises:
Exception: Unspecified error by executing the functionality
jinja2.TemplateNotFound: Jinja2 template not found
Returns:
json_dashboard (dict): Returns the dashboard as dict
json_dashboard (Dict): Returns the dashboard as dict
"""

env = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))
Expand All @@ -52,26 +53,59 @@ def get_dashboard_json(self, template_values: dict) -> dict:
logging.error("Please define templating values.")
sys.exit(1)

temp_path: str = "/tmp/dashboard.json"
with tempfile.NamedTemporaryFile() as tmp_file:
self.__write_tmp_dashboard_json(
tmp_file.name, template_dashboard, template_values
)
return self.__get_dashboard_json(tmp_file.name)

@staticmethod
def __write_tmp_dashboard_json(
temp_path: str, template_dashboard: jinja2.Template, template_values: Dict
):
"""The method includes a functionality to write a templated json of the selected dashboard to a temporary file
Args:
temp_path (str): Specify the temporary path as string
template_dashboard (jinja2.Template): Specify the Jinja2 templated dashboard
template_values (Dict): Specify the template values
Raises:
FileNotFoundError: The corresponding temporary file is not available
ValueError: There is an error inside the values
AttributeError: You missed to add a specific attribute
Returns:
None
"""

try:
fw = open(temp_path, "w")
fw.write(template_dashboard.render(template_values))
fw.close()
except Exception as e:
except (FileNotFoundError, ValueError, AttributeError) as e:
logging.error(f"Please, check the error: {e} .")
raise e

try:
with open(temp_path) as file:
json_dashboard = json.load(file)
except Exception as e:
logging.error(f"Please, check the error: {e} .")
raise e
@staticmethod
def __get_dashboard_json(temp_path: str) -> Dict:
"""The method includes a functionality to get the corresponding templated dashboard JSON as Dict
Args:
temp_path (str): Specify the temporary path as string
Raises:
FileNotFoundError: The corresponding temporary file is not available
ValueError: There is an error inside the values
Returns:
json_dashboard (Dict): Returns the dashboard JSON as dict
"""

try:
os.remove(temp_path)
except Exception as e:
with open(temp_path) as file:
json_dashboard: Dict = json.load(file)
except (FileNotFoundError, ValueError) as e:
logging.error(f"Please, check the error: {e} .")
raise e

Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="grafana-dashboard-templater",
version="1.0.1",
version="1.0.2",
author="Pascal Zimmermann",
author_email="[email protected]",
description="A Grafana dashboard templater",
Expand All @@ -20,8 +20,7 @@
"License :: OSI Approved",
"Operating System :: OS Independent",
],
package_dir={"": "src"},
packages=setuptools.find_packages(where="src"),
packages=["grafana_dashboard"],
install_requires=["jinja2"],
python_requires=">=3.6",
)
35 changes: 26 additions & 9 deletions tests/test_dashboard.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import logging
import os
import unittest
import tempfile
from unittest import TestCase
from unittest.mock import MagicMock
import jinja2

from src.grafana_dashboard.model import Model
from src.grafana_dashboard.dashboard import Dashboard
from grafana_dashboard.model import Model
from grafana_dashboard.dashboard import Dashboard


class DashboardTestCase(unittest.TestCase):
class DashboardTestCase(TestCase):
@staticmethod
def __get_path_name() -> str:
if os.path.basename(os.getcwd()) == "tests":
Expand Down Expand Up @@ -55,9 +57,7 @@ def test_get_dashboard_json_successful(self):
self.assertEqual("test", dashboard["templating"]["list"][1]["current"]["text"])

def test_get_dashboard_json_no_config_template_error(self):
template_path: str = (
f"{os.path.dirname(os.getcwd())}{os.sep}dashboard"
)
template_path: str = f"{os.path.dirname(os.getcwd())}{os.sep}dashboard"
test_model: Model = Model(template_path, "database", "postgresql", "v13")
test_dashboard: Dashboard = Dashboard(test_model)
with self.assertRaises(jinja2.TemplateNotFound):
Expand All @@ -73,6 +73,23 @@ def test_get_dashboard_json_no_template_values_error(self):
with self.assertRaises(SystemExit):
test_dashboard.get_dashboard_json({})

def test__write_tmp_dashboard_json_write_not_possible(self):
template_path: str = DashboardTestCase.__get_path_name()
test_model: Model = Model(template_path, "database", "postgresql", "v13")
test_dashboard: Dashboard = Dashboard(test_model)

with self.assertRaises(AttributeError):
with tempfile.NamedTemporaryFile() as tmp_file:
test_dashboard._Dashboard__write_tmp_dashboard_json(
tmp_file.name,
MagicMock,
{"app_name": "test", "prometheus_name": "test_name"},
)

def test__get_dashboard_json_json_not_available(self):
template_path: str = DashboardTestCase.__get_path_name()
test_model: Model = Model(template_path, "database", "postgresql", "v13")
test_dashboard: Dashboard = Dashboard(test_model)

if __name__ == "__main__":
unittest.main()
with self.assertRaises(FileNotFoundError):
test_dashboard._Dashboard__get_dashboard_json("/tmp/dashboard1.json")
10 changes: 3 additions & 7 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest
from unittest import TestCase

from src.grafana_dashboard.model import Model
from grafana_dashboard.model import Model


class ModelTestCase(unittest.TestCase):
class ModelTestCase(TestCase):
def test_model_successful(self):
test_model: Model = Model("test1", "test2", "test3", "test4")
self.assertEqual("test1", test_model.dashboard_templates_path)
Expand All @@ -17,7 +17,3 @@ def test_model_error(self):
self.assertNotEqual("test2", test_model.dashboard_type)
self.assertNotEqual("test3", test_model.dashboard_version)
self.assertNotEqual("test4", test_model.dashboard_templates_path)


if __name__ == "__main__":
unittest.main()

0 comments on commit b430d22

Please sign in to comment.