From b91de6e8e9da11ae0c0a7d9e07578f6c8edb9cbd Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Mon, 11 May 2015 14:38:24 -0400 Subject: [PATCH] Rework unit tests With help from JJB we now compare yaml to json, making sure we generating the proper JSON from the schema Signed-off-by: Paul Belanger --- grafana_dashboards/schema/dashboard.py | 2 +- tests/base.py | 62 ++++++++++++++++++++++- tests/schema/fixtures/dashboard-0001.json | 6 +++ tests/schema/fixtures/dashboard-0001.yaml | 20 -------- tests/schema/test_dashboard.py | 27 +++------- 5 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 tests/schema/fixtures/dashboard-0001.json diff --git a/grafana_dashboards/schema/dashboard.py b/grafana_dashboards/schema/dashboard.py index 92bacac..6572f43 100644 --- a/grafana_dashboards/schema/dashboard.py +++ b/grafana_dashboards/schema/dashboard.py @@ -27,7 +27,7 @@ class Dashboard(object): rows = Row().get_schema() dashboard.update(rows.schema) schema = v.Schema({ - 'dashboard': dashboard, + v.Required('dashboard'): dashboard, }) return schema(data) diff --git a/tests/base.py b/tests/base.py index 1c30cdb..0601cfc 100644 --- a/tests/base.py +++ b/tests/base.py @@ -2,6 +2,7 @@ # Copyright 2010-2011 OpenStack Foundation # Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# Copyright 2015 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -15,9 +16,66 @@ # License for the specific language governing permissions and limitations # under the License. -from oslotest import base +import doctest +import json +import os +import re +import testtools +import yaml + +from grafana_dashboards.schema import dashboard -class TestCase(base.BaseTestCase): +def get_scenarios(fixtures_path, in_ext='yaml', out_ext='json'): + scenarios = [] + files = [] + for dirpath, dirs, fs in os.walk(fixtures_path): + files.extend([os.path.join(dirpath, f) for f in fs]) + + input_files = [f for f in files if re.match(r'.*\.{0}$'.format(in_ext), f)] + + for input_filename in input_files: + output_candidate = re.sub( + r'\.{0}$'.format(in_ext), '.{0}'.format(out_ext), input_filename) + if output_candidate not in files: + output_candidate = None + + scenarios.append((input_filename, { + 'in_filename': input_filename, + 'out_filename': output_candidate, + })) + + return scenarios + + +class TestCase(object): """Test case base class for all unit tests.""" + + def _read_raw_content(self): + # if None assume empty file + if self.out_filename is None: + return "" + + content = open(self.out_filename, 'r').read() + + return content + + def _read_yaml_content(self, filename): + with open(filename, 'r') as yaml_file: + content = yaml.load(yaml_file) + + return content + + def test_yaml_snippet(self): + expected_json = self._read_raw_content() + yaml_content = self._read_yaml_content(self.in_filename) + + schema = dashboard.Dashboard() + valid_yaml = schema.validate(yaml_content) + pretty_json = json.dumps( + valid_yaml, indent=4, separators=(',', ': '), sort_keys=True) + + self.assertThat(pretty_json, testtools.matchers.DocTestMatches( + expected_json, doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | + doctest.REPORT_NDIFF)) diff --git a/tests/schema/fixtures/dashboard-0001.json b/tests/schema/fixtures/dashboard-0001.json new file mode 100644 index 0000000..ba680d7 --- /dev/null +++ b/tests/schema/fixtures/dashboard-0001.json @@ -0,0 +1,6 @@ +{ + "dashboard": { + "rows": [], + "title": "New dashboard" + } +} diff --git a/tests/schema/fixtures/dashboard-0001.yaml b/tests/schema/fixtures/dashboard-0001.yaml index 31fda20..2d47eec 100644 --- a/tests/schema/fixtures/dashboard-0001.yaml +++ b/tests/schema/fixtures/dashboard-0001.yaml @@ -1,22 +1,2 @@ dashboard: title: New dashboard - rows: - - title: New row - height: 250px - panels: - - type: text - title: no title (click here) - error: false - editable: true - span: 12 - mode: markdown - content: '' - style: {} - - type: dashlist - title: Starred Dashboards - error: false - editable: true - span: 12 - mode: starred - query: '' - limit: 1 diff --git a/tests/schema/test_dashboard.py b/tests/schema/test_dashboard.py index 36723b3..87a4113 100644 --- a/tests/schema/test_dashboard.py +++ b/tests/schema/test_dashboard.py @@ -13,29 +13,14 @@ # under the License. import os -import re -import yaml +from testscenarios.testcase import TestWithScenarios from testtools import TestCase -from grafana_dashboards.schema import dashboard - -FIXTURE_DIR = os.path.join(os.path.dirname(__file__), - 'fixtures') -LAYOUT_RE = re.compile(r'^(dashboard)-.*\.yaml$') +from tests.base import get_scenarios +from tests.base import TestCase as BaseTestCase -class TestCaseSchemaDashboard(TestCase): - def test_layouts(self): - for fn in os.listdir(os.path.join(FIXTURE_DIR)): - schema = None - m = LAYOUT_RE.match(fn) - if not m: - continue - layout = os.path.join(FIXTURE_DIR, fn) - data = yaml.load(open(layout)) - - if m.group(1) == 'dashboard': - schema = dashboard.Dashboard() - - schema.validate(data) +class TestCaseSchemaDashboard(TestWithScenarios, TestCase, BaseTestCase): + fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') + scenarios = get_scenarios(fixtures_path)