From 0c20c020984785e9067267c0e144a25d31aea24d Mon Sep 17 00:00:00 2001 From: Holger Nahrstaedt <holger@nahrstaedt.de> Date: Sat, 28 Apr 2018 07:09:12 +0200 Subject: [PATCH] Fix unittest for python 2.7 --- beem/asciichart.py | 20 +++++++++--------- beem/cli.py | 24 ++++++++++++++++----- tests/beem/test_asciichart.py | 40 +++++++++++++++++++++++++++++++++++ tests/beem/test_cli.py | 2 +- 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 tests/beem/test_asciichart.py diff --git a/beem/asciichart.py b/beem/asciichart.py index 6f0921aa..0836bf7b 100644 --- a/beem/asciichart.py +++ b/beem/asciichart.py @@ -22,7 +22,7 @@ class AsciiChart(object): :param int offset: Offset between tick strings and y-axis (default is 3) :param str placeholder: Defines how the numbers on the y-axes are formated (default is '{:8.2f} ') """ - def __init__(self, height=None, width=None, offset=3, placeholder='{:8.2f} '): + def __init__(self, height=None, width=None, offset=3, placeholder=u'{:8.2f} '): self.height = height self.width = width self.offset = offset @@ -137,7 +137,7 @@ class AsciiChart(object): if n is not None: self.n = n self._calc_plot_parameter() - self.canvas = [[' '] * (int(self.n / self.skip) + self.offset) for i in range(self.rows + 1)] + self.canvas = [[u' '] * (int(self.n / self.skip) + self.offset) for i in range(self.rows + 1)] def add_axis(self): """Adds a y-axis to the canvas @@ -164,7 +164,7 @@ class AsciiChart(object): def _set_y_axis_elem(self, y, label): intmin2 = int(self.min2) self.canvas[y - intmin2][max(self.offset - len(label), 0)] = label - self.canvas[y - intmin2][self.offset - 1] = '┼' if y == 0 else '┤' + self.canvas[y - intmin2][self.offset - 1] = u'┼' if y == 0 else u'┤' def _map_y(self, y_float): intmin2 = int(self.min2) @@ -191,7 +191,7 @@ class AsciiChart(object): if len(self.canvas) == 0: self.new_chart() y0 = self._map_y(series[0]) - self._set_elem(y0, -1, '┼') + self._set_elem(y0, -1, u'┼') for x in range(0, len(series[::self.skip]) - 1): y0 = self._map_y(series[::self.skip][x + 0]) y1 = self._map_y(series[::self.skip][x + 1]) @@ -206,20 +206,20 @@ class AsciiChart(object): def _draw_diag(self, y0, y1, x): """Plot diagonal element""" if y0 > y1: - c1 = 'â•°' - c0 = 'â•®' + c1 = u'â•°' + c0 = u'â•®' else: - c1 = 'â•' - c0 = '╯' + c1 = u'â•' + c0 = u'╯' self._set_elem(y1, x, c1) self._set_elem(y0, x, c0) - def _draw_h_line(self, y, x_start, x_end, line='─'): + def _draw_h_line(self, y, x_start, x_end, line=u'─'): """Plot horizontal line""" for x in range(x_start, x_end): self._set_elem(y, x, line) - def _draw_v_line(self, y_start, y_end, x, line='│'): + def _draw_v_line(self, y_start, y_end, x, line=u'│'): """Plot vertical line""" for y in range(y_start, y_end): self._set_elem(y, x, line) diff --git a/beem/cli.py b/beem/cli.py index 04c5482c..17cf48b7 100644 --- a/beem/cli.py +++ b/beem/cli.py @@ -29,6 +29,7 @@ from beemgraphenebase.account import PrivateKey, PublicKey import os import ast import json +import sys from prettytable import PrettyTable import math import random @@ -1164,9 +1165,12 @@ def pricehistory(width, height): chart.adapt_on_series(price) chart.new_chart() chart.add_axis() - chart._draw_h_line(chart._map_y(float(current_base) / float(current_quote)), 1, int(chart.n / chart.skip), line='┈') + chart._draw_h_line(chart._map_y(float(current_base) / float(current_quote)), 1, int(chart.n / chart.skip), line=u'┈') chart.add_curve(price) - print(str(chart)) + if sys.version_info[0] < 3: + print(str(chart).encode('utf-8')) + else: + print(str(chart)) @cli.command() @@ -1195,7 +1199,14 @@ def tradehistory(days, hours, limit, width, height): price.append(base / quote) chart = AsciiChart(height=height, width=width, offset=3, placeholder='{:6.2f} ') print("\n Trade history %s - %s \n\nSTEEM/SBD" % (formatTimeString(start), formatTimeString(stop))) - chart.plot(price) + chart.adapt_on_series(price) + chart.new_chart() + chart.add_axis() + chart.add_curve(price) + if sys.version_info[0] < 3: + print(str(chart).encode('utf-8')) + else: + print(str(chart)) @cli.command() @@ -1253,9 +1264,12 @@ def orderbook(chart, limit, show_date, width, height): chart.add_axis() y0 = chart._map_y(chart.minimum) y1 = chart._map_y(chart.maximum) - chart._draw_v_line(y0 + 1, y1, int(chart.n / chart.skip / 2), line='┊') + chart._draw_v_line(y0 + 1, y1, int(chart.n / chart.skip / 2), line=u'┊') chart.add_curve(sumsum_asks[::-1] + sumsum_bids) - print(str(chart)) + if sys.version_info[0] < 3: + print(str(chart).encode('utf-8')) + else: + print(str(chart)) return for i in range(n): row = [] diff --git a/tests/beem/test_asciichart.py b/tests/beem/test_asciichart.py new file mode 100644 index 00000000..536c81a9 --- /dev/null +++ b/tests/beem/test_asciichart.py @@ -0,0 +1,40 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals +from builtins import bytes +from builtins import range +from builtins import super +import string +import random +import unittest +import base64 +from pprint import pprint +from beem.asciichart import AsciiChart + + +class Testcases(unittest.TestCase): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.curve = [1.2, 4.3, 2.0, -1.3, 6.4, 0.] + + def test_plot(self): + ac = AsciiChart(height=3, width=3) + self.assertEqual(len(ac.canvas), 0) + ret = ac.plot(self.curve, return_str=True) + ac.plot(self.curve, return_str=False) + self.assertTrue(len(ret) > 0) + ac.clear_data() + self.assertEqual(len(ac.canvas), 0) + + def test_plot2(self): + ac = AsciiChart(height=3, width=3) + ac.clear_data() + ac.adapt_on_series(self.curve) + self.assertEqual(ac.maximum, max(self.curve)) + self.assertEqual(ac.minimum, min(self.curve)) + self.assertEqual(ac.n, len(self.curve)) + ac.new_chart() + ac.add_axis() + ac.add_curve(self.curve) diff --git a/tests/beem/test_cli.py b/tests/beem/test_cli.py index 71dfffcb..d3829d8e 100644 --- a/tests/beem/test_cli.py +++ b/tests/beem/test_cli.py @@ -401,8 +401,8 @@ class Testcases(unittest.TestCase): runner = CliRunner() runner.invoke(cli, ['-o', 'set', 'nodes', '']) result = runner.invoke(cli, ['pricehistory']) - self.assertEqual(result.exit_code, 0) runner.invoke(cli, ['-o', 'set', 'nodes', 'wss://testnet.steem.vc']) + self.assertEqual(result.exit_code, 0) def test_tradehistory(self): runner = CliRunner() -- GitLab