From 3b5ba508f774daa0386af1fd1b608d517a5bf3b0 Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Wed, 4 Nov 2015 15:25:36 -0500 Subject: [PATCH] Add unit test coverage for panels This also fixes a bug with our valueName field in a singlestat panel. Change-Id: I4df8d130fce45cf58b01808997fc561cf8c4b42d Signed-off-by: Paul Belanger --- grafana_dashboards/schema/panel/singlestat.py | 2 +- tests/schema/panels/__init__.py | 0 tests/schema/panels/test_base.py | 35 +++++++++++++ tests/schema/panels/test_dashlist.py | 39 ++++++++++++++ tests/schema/panels/test_graph.py | 47 +++++++++++++++++ tests/schema/panels/test_singlestat.py | 52 +++++++++++++++++++ tests/schema/panels/test_text.py | 37 +++++++++++++ 7 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 tests/schema/panels/__init__.py create mode 100644 tests/schema/panels/test_base.py create mode 100644 tests/schema/panels/test_dashlist.py create mode 100644 tests/schema/panels/test_graph.py create mode 100644 tests/schema/panels/test_singlestat.py create mode 100644 tests/schema/panels/test_text.py diff --git a/grafana_dashboards/schema/panel/singlestat.py b/grafana_dashboards/schema/panel/singlestat.py index cd0e202..5accc18 100644 --- a/grafana_dashboards/schema/panel/singlestat.py +++ b/grafana_dashboards/schema/panel/singlestat.py @@ -60,7 +60,7 @@ class Singlestat(Base): v.Required( 'valueFontSize', default='80%'): v.All( v.Match(r'^[1-9]?[0]{1}%$|^1[0-9]?[0]{1}%$|^200%$')), - v.Required('valueName', default='avg'): v.All( + v.Required('valueName', default='avg'): v.Any( 'avg', 'current', 'max', 'min', 'total'), v.Optional('decimals'): v.All(int, v.Range(min=0, max=12)), } diff --git a/tests/schema/panels/__init__.py b/tests/schema/panels/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/schema/panels/test_base.py b/tests/schema/panels/test_base.py new file mode 100644 index 0000000..420fab6 --- /dev/null +++ b/tests/schema/panels/test_base.py @@ -0,0 +1,35 @@ +# 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 a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from testtools import TestCase + +from grafana_dashboards.schema.panel.base import Base + + +class TestCaseBase(TestCase): + + def setUp(self): + super(TestCaseBase, self).setUp() + self.schema = Base().get_schema() + + def test_defaults(self): + # Ensure default values get parsed correctly. + defaults = { + 'editable': True, + 'error': False, + 'span': 12, + 'title': 'foobar', + 'type': 'text', + } + self.assertEqual(self.schema(defaults), defaults) diff --git a/tests/schema/panels/test_dashlist.py b/tests/schema/panels/test_dashlist.py new file mode 100644 index 0000000..953f3ad --- /dev/null +++ b/tests/schema/panels/test_dashlist.py @@ -0,0 +1,39 @@ +# 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 a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from testtools import TestCase + +from grafana_dashboards.schema.panel.dashlist import Dashlist + + +class TestCaseDashlist(TestCase): + + def setUp(self): + super(TestCaseDashlist, self).setUp() + self.schema = Dashlist().get_schema() + + def test_defaults(self): + # Ensure default values get parsed correctly. + defaults = { + 'editable': True, + 'error': False, + 'limit': 10, + 'mode': 'starred', + 'query': '', + 'span': 12, + 'tag': '', + 'title': 'foobar', + 'type': 'dashlist', + } + self.assertEqual(self.schema(defaults), defaults) diff --git a/tests/schema/panels/test_graph.py b/tests/schema/panels/test_graph.py new file mode 100644 index 0000000..a54e11f --- /dev/null +++ b/tests/schema/panels/test_graph.py @@ -0,0 +1,47 @@ +# 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 a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from testtools import TestCase + +from grafana_dashboards.schema.panel.graph import Graph + + +class TestCaseGraph(TestCase): + + def setUp(self): + super(TestCaseGraph, self).setUp() + self.schema = Graph().get_schema() + + def test_defaults(self): + # Ensure default values get parsed correctly. + defaults = { + 'bars': False, + 'editable': True, + 'error': False, + 'fill': 1, + 'lines': True, + 'linewidth': 2, + 'percentage': False, + 'pointradius': 5, + 'points': False, + 'span': 12, + 'stack': False, + 'steppedLine': False, + 'targets': [], + 'title': 'foobar', + 'type': 'graph', + 'x-axis': True, + 'y-axis': True, + } + self.assertEqual(self.schema(defaults), defaults) diff --git a/tests/schema/panels/test_singlestat.py b/tests/schema/panels/test_singlestat.py new file mode 100644 index 0000000..01fd019 --- /dev/null +++ b/tests/schema/panels/test_singlestat.py @@ -0,0 +1,52 @@ +# 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 a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from testtools import TestCase + +from grafana_dashboards.schema.panel.singlestat import Singlestat + + +class TestCaseSinglestat(TestCase): + + def setUp(self): + super(TestCaseSinglestat, self).setUp() + self.schema = Singlestat().get_schema() + + def test_defaults(self): + # Ensure default values get parsed correctly. + defaults = { + 'colorBackground': False, + 'colorValue': False, + 'editable': True, + 'error': False, + 'maxDataPoints': 100, + 'postfix': '', + 'postfixFontSize': '50%', + 'prefix': '', + 'prefixFontSize': '50%', + 'span': 12, + 'sparkline': { + 'fillColor': 'rgba(31, 118, 189, 0.18)', + 'full': False, + 'lineColor': 'rgb(31, 120, 193)', + 'show': False + }, + 'targets': [], + 'thresholds': '', + 'title': 'foobar', + 'type': 'singlestat', + 'valueFontSize': '80%', + 'valueName': 'avg', + } + self.assertEqual(self.schema(defaults), defaults) diff --git a/tests/schema/panels/test_text.py b/tests/schema/panels/test_text.py new file mode 100644 index 0000000..73abea2 --- /dev/null +++ b/tests/schema/panels/test_text.py @@ -0,0 +1,37 @@ +# 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 a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from testtools import TestCase + +from grafana_dashboards.schema.panel.text import Text + + +class TestCaseText(TestCase): + + def setUp(self): + super(TestCaseText, self).setUp() + self.schema = Text().get_schema() + + def test_defaults(self): + # Ensure default values get parsed correctly. + defaults = { + 'content': 'junk', + 'editable': True, + 'error': False, + 'mode': 'markdown', + 'span': 12, + 'title': 'foobar', + 'type': 'text', + } + self.assertEqual(self.schema(defaults), defaults)