File: test_sparktext.py

package info (click to toggle)
python-pygal 3.0.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: python: 7,359; makefile: 9
file content (83 lines) | stat: -rw-r--r-- 2,660 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
# This file is part of pygal
#
# A python svg graph plotting library
# Copyright © 2012-2016 Kozea
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
"""Test sparktext rendering"""

from pygal import Bar, Line


def test_basic_sparktext():
    """Test basic sparktext"""
    chart = Line()
    chart.add('_', [1, 5, 22, 13, 53])
    assert chart.render_sparktext() == '▁▁▃▂█'


def test_all_sparktext():
    """Test all character sparktext"""
    chart = Line()
    chart.add('_', range(8))
    assert chart.render_sparktext() == '▁▂▃▄▅▆▇█'


def test_shifted_sparktext():
    """Test relative_to option in sparktext"""
    chart = Line()
    chart.add('_', list(map(lambda x: x + 10000, range(8))))
    assert chart.render_sparktext() == '▁▂▃▄▅▆▇█'
    assert chart.render_sparktext(relative_to=0) == '▇▇▇▇▇▇▇█'


def test_another_sparktext():
    """Test that same data produces same sparktext"""
    chart = Line()
    chart.add('_', [0, 30, 55, 80, 33, 150])
    assert chart.render_sparktext() == '▁▂▃▄▂█'
    assert chart.render_sparktext() == chart.render_sparktext()
    chart2 = Bar()
    chart2.add('_', [0, 30, 55, 80, 33, 150])
    assert chart2.render_sparktext() == chart.render_sparktext()


def test_negative_and_float__sparktext():
    """Test negative values"""
    """Test negative values"""
    chart = Line()
    chart.add('_', [0.1, 0.2, 0.9, -0.5])
    assert chart.render_sparktext() == '▁▂█▁'


def test_no_data_sparktext():
    """Test no data sparktext"""
    chart2 = Line()
    chart2.add('_', [])
    assert chart2.render_sparktext() == ''

    chart3 = Line()
    assert chart3.render_sparktext() == ''


def test_same_max_and_relative_values_sparktext():
    """Test flat sparktexts"""
    chart = Line()
    chart.add('_', [0, 0, 0, 0, 0])
    assert chart.render_sparktext() == '▁▁▁▁▁'

    chart2 = Line()
    chart2.add('_', [1, 1, 1, 1, 1])
    assert chart2.render_sparktext(relative_to=1) == '▁▁▁▁▁'