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 <pabelanger@redhat.com>
This commit is contained in:
parent
353cf6b3e3
commit
b91de6e8e9
@ -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)
|
||||
|
@ -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))
|
||||
|
6
tests/schema/fixtures/dashboard-0001.json
Normal file
6
tests/schema/fixtures/dashboard-0001.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"dashboard": {
|
||||
"rows": [],
|
||||
"title": "New dashboard"
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user