From 8f5ececb986cfcadf57559c992d3ae670bdfca64 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Fri, 21 Sep 2012 11:43:19 +0200 Subject: [PATCH] Fix 'No data' when the serie only contains Nones + add some tests --- pygal/__init__.py | 2 +- pygal/graph/base.py | 2 +- pygal/test/test_config.py | 6 ++--- pygal/test/test_graph.py | 49 +++++++++++++++++++++++++++++++++++++-- pygal/test/test_util.py | 4 ++-- pygal/util.py | 2 ++ 6 files changed, 56 insertions(+), 9 deletions(-) diff --git a/pygal/__init__.py b/pygal/__init__.py index b186008..3af5136 100644 --- a/pygal/__init__.py +++ b/pygal/__init__.py @@ -21,7 +21,7 @@ Pygal - A python svg graph plotting library """ -__version__ = '0.12.1' +__version__ = '0.12.2' import sys from pygal.config import Config from pygal.ghost import Ghost diff --git a/pygal/graph/base.py b/pygal/graph/base.py index d93edde..9139a49 100644 --- a/pygal/graph/base.py +++ b/pygal/graph/base.py @@ -150,7 +150,7 @@ class BaseGraph(object): def _has_data(self): """Check if there is any data""" - return sum(map(len, map(lambda s: s.values, self.series))) != 0 + return sum(map(len, map(lambda s: s.safe_values, self.series))) != 0 def render(self, is_unicode): """Render the graph, and return the svg string""" diff --git a/pygal/test/test_config.py b/pygal/test/test_config.py index 869136f..0b5ecd7 100644 --- a/pygal/test/test_config.py +++ b/pygal/test/test_config.py @@ -180,9 +180,9 @@ def test_logarithmic_small_scale(): def test_human_readable(): line = Line() line.add('_', [10 ** 4, 10 ** 5, 23 * 10 ** 4]) - # q = line.render_pyquery() - # assert q(".axis.y text").map(texts) == map( - # str, range(20000, 240000, 20000)) + q = line.render_pyquery() + assert q(".axis.y text").map(texts) == map( + str, map(float, range(20000, 240000, 20000))) line.human_readable = True q = line.render_pyquery() assert q(".axis.y text").map(texts) == map( diff --git a/pygal/test/test_graph.py b/pygal/test/test_graph.py index 38071dd..ae1c7ff 100644 --- a/pygal/test/test_graph.py +++ b/pygal/test/test_graph.py @@ -93,14 +93,16 @@ def test_empty_lists(Chart): chart.add('B', []) chart.x_labels = ('red', 'green', 'blue') q = chart.render_pyquery() - assert len(q(".legend")) == 2 + assert len(q(".legend")) == 1 + +def test_empty_lists_with_nones(Chart): chart = Chart() chart.add('A', [None, None]) chart.add('B', [None, 4, 4]) chart.x_labels = ('red', 'green', 'blue') q = chart.render_pyquery() - assert len(q(".legend")) == 2 + assert len(q(".legend")) == 1 def test_non_iterable_value(Chart): @@ -151,3 +153,46 @@ def test_values_by_dict(Chart): chart.x_labels = ('red', 'green', 'blue') chart2 = chart.render() assert chart1 == chart2 + + +def test_no_data_with_no_values(Chart): + chart = Chart() + q = chart.render_pyquery() + assert q("text").text() == "No data" + + +def test_no_data_with_empty_serie(Chart): + chart = Chart() + chart.add('Serie', []) + q = chart.render_pyquery() + assert q("text").text() == "No data" + + +def test_no_data_with_empty_series(Chart): + chart = Chart() + chart.add('Serie1', []) + chart.add('Serie2', []) + q = chart.render_pyquery() + assert q("text").text() == "No data" + + +def test_no_data_with_none(Chart): + chart = Chart() + chart.add('Serie', None) + q = chart.render_pyquery() + assert q("text").text() == "No data" + + +def test_no_data_with_list_of_none(Chart): + chart = Chart() + chart.add('Serie', [None]) + q = chart.render_pyquery() + assert q("text").text() == "No data" + + +def test_no_data_with_lists_of_nones(Chart): + chart = Chart() + chart.add('Serie1', [None, None, None, None]) + chart.add('Serie2', [None, None, None]) + q = chart.render_pyquery() + assert q("text").text() == "No data" diff --git a/pygal/test/test_util.py b/pygal/test/test_util.py index 543893d..441365d 100644 --- a/pygal/test/test_util.py +++ b/pygal/test/test_util.py @@ -125,7 +125,7 @@ def test_truncate(): def test_minify_css(): css = ''' -/* +/* * Font-sizes from config, override with care */ @@ -136,7 +136,7 @@ def test_minify_css(): } .legends .legend text { - font-family: monospace; + font-family: monospace; font-size: 14 ;} ''' assert minify_css(css) == ( diff --git a/pygal/util.py b/pygal/util.py index 6c0ffe1..9b62379 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -316,6 +316,8 @@ def prepare_values(raw, config, cls): else: raw_values = list(raw_values) + if not filter(lambda x: x is not None, raw_values): + continue for index, raw_value in enumerate( raw_values + ( (width - len(raw_values)) * [None] # aligning values