From 915adf8fd3ebe630f9fc95c6bbb33ab8ea0e9dda Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Tue, 27 Sep 2016 19:50:33 +0200 Subject: [PATCH 1/3] fix file-printer-tests by using pytest-mock --- setup.py | 2 +- test/test_printer_file.py | 24 +++++++++++++----------- tox.ini | 1 + 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index ef7b5b1..d136f95 100755 --- a/setup.py +++ b/setup.py @@ -118,7 +118,7 @@ setup( setup_requires=[ 'setuptools_scm', ], - tests_require=['tox', 'pytest', 'pytest-cov', 'nose', 'scripttest', 'mock', 'hypothesis'], + tests_require=['tox', 'pytest', 'pytest-cov', 'pytest-mock', 'nose', 'scripttest', 'mock', 'hypothesis'], cmdclass={'test': Tox}, entry_points={ 'console_scripts': [ diff --git a/test/test_printer_file.py b/test/test_printer_file.py index bce9a0e..4d1b188 100644 --- a/test/test_printer_file.py +++ b/test/test_printer_file.py @@ -15,7 +15,7 @@ from __future__ import unicode_literals import six -import mock +import pytest from hypothesis import given from hypothesis.strategies import text @@ -27,21 +27,22 @@ else: mock_open_call = '__builtin__.open' @given(path=text()) -@mock.patch(mock_open_call) -@mock.patch('escpos.escpos.Escpos.__init__') -def test_load_file_printer(mock_escpos, mock_open, path): +def test_load_file_printer(mocker, path): """test the loading of the file-printer""" + mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__') + mock_open = mocker.patch(mock_open_call) printer.File(devfile=path) assert mock_escpos.called mock_open.assert_called_with(path, "wb") @given(txt=text()) -@mock.patch.object(printer.File, 'device') -@mock.patch(mock_open_call) -@mock.patch('escpos.escpos.Escpos.__init__') -def test_auto_flush(mock_escpos, mock_open, mock_device, txt): +def test_auto_flush(mocker, txt): """test auto_flush in file-printer""" + mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__') + mock_open = mocker.patch(mock_open_call) + mock_device = mocker.patch.object(printer.File, 'device') + p = printer.File(auto_flush=False) # inject the mocked device-object p.device = mock_device @@ -56,10 +57,11 @@ def test_auto_flush(mock_escpos, mock_open, mock_device, txt): @given(txt=text()) -@mock.patch.object(printer.File, 'device') -@mock.patch(mock_open_call) -def test_flush_on_close(mock_open, mock_device, txt): +def test_flush_on_close(mocker, txt): """test flush on close in file-printer""" + mock_open = mocker.patch(mock_open_call) + mock_device = mocker.patch.object(printer.File, 'device') + p = printer.File(auto_flush=False) # inject the mocked device-object p.device = mock_device diff --git a/tox.ini b/tox.ini index 362f2a6..bed3686 100644 --- a/tox.ini +++ b/tox.ini @@ -8,6 +8,7 @@ deps = nose mock pytest pytest-cov + pytest-mock hypothesis commands = py.test --cov escpos From b795c02dd4ca651aef7cf967e689a129a59b2d3c Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Tue, 27 Sep 2016 20:02:15 +0200 Subject: [PATCH 2/3] DOC remove incode TODOs I will add them as issues where applicable. --- test/test_magicencode.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/test/test_magicencode.py b/test/test_magicencode.py index 45b0d55..3f0dbdb 100644 --- a/test/test_magicencode.py +++ b/test/test_magicencode.py @@ -112,14 +112,3 @@ class TestKatakana: def test_result(self): assert encode_katakana('カタカナ') == b'\xb6\xc0\xb6\xc5' assert encode_katakana("あいうえお") == b'\xb1\xb2\xb3\xb4\xb5' - - -# TODO Idee für unittest: hypothesis-strings erzeugen, in encode_text werfen -# Ergebnis durchgehen: Vorkommnisse von Stuersequenzen suchen und daran den Text splitten in ein sortiertes dict mit Struktur: -# encoding: textfolge -# das alles wieder in unicode dekodieren mit den codepages und dann zusammenbauen -# fertigen String mit hypothesis-string vergleichen (Achtung bei katana-conversion. Die am besten auch auf den hypothesis-string -# anwenden) -# TODO bei nicht kodierbarem Zeichen Fehler werfen! Als Option das verhalten von jetzt hinzufügen -# TODO tests sollten eigentlich nicht gehen, wenn encode_char gerufen wird (extra_char ist nicht definiert) -# TODO verhalten bei leerem String festlegen und testen From 1b2f5097588e7bf194fae28e236ce4b8c1aae46a Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Tue, 27 Sep 2016 20:26:22 +0200 Subject: [PATCH 3/3] use jaconv instead of jcconv for japanese chars jaconv is available for more Python-versions and seems to be more professional. Apart from that I added jaconv to the test-requirements but not the requirements. (If the katakana-stuff really works we can later add it as a real dependency) --- setup.py | 2 +- src/escpos/katakana.py | 14 +++++++++----- test/test_magicencode.py | 6 +++--- tox.ini | 1 + 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index d136f95..9d00261 100755 --- a/setup.py +++ b/setup.py @@ -118,7 +118,7 @@ setup( setup_requires=[ 'setuptools_scm', ], - tests_require=['tox', 'pytest', 'pytest-cov', 'pytest-mock', 'nose', 'scripttest', 'mock', 'hypothesis'], + tests_require=['jaconv', 'tox', 'pytest', 'pytest-cov', 'pytest-mock', 'nose', 'scripttest', 'mock', 'hypothesis'], cmdclass={'test': Tox}, entry_points={ 'console_scripts': [ diff --git a/src/escpos/katakana.py b/src/escpos/katakana.py index 7c2e2c7..14b2e61 100644 --- a/src/escpos/katakana.py +++ b/src/escpos/katakana.py @@ -11,24 +11,27 @@ from __future__ import unicode_literals try: - import jcconv + import jaconv except ImportError: - jcconv = None + jaconv = None def encode_katakana(text): """I don't think this quite works yet.""" encoded = [] for char in text: - if jcconv: + if jaconv: # try to convert japanese text to half-katakanas - char = jcconv.kata2half(jcconv.hira2kata(char)) + char = jaconv.z2h(jaconv.hira2kata(char)) # TODO: "the conversion may result in multiple characters" - # When? What should we do about it? + # If that really can happen (I am not really shure), than the string would have to be split and every single + # character has to passed through the following lines. if char in TXT_ENC_KATAKANA_MAP: encoded.append(TXT_ENC_KATAKANA_MAP[char]) else: + #TODO doesn't this discard all that is not in the map? Can we be shure that the input does contain only + # encodable characters? We could at least throw an exception if encoding is not possible. pass return b"".join(encoded) @@ -36,6 +39,7 @@ def encode_katakana(text): TXT_ENC_KATAKANA_MAP = { # Maps UTF-8 Katakana symbols to KATAKANA Page Codes + # TODO: has this really to be hardcoded? # Half-Width Katakanas '。': b'\xa1', diff --git a/test/test_magicencode.py b/test/test_magicencode.py index 3f0dbdb..fc7a31e 100644 --- a/test/test_magicencode.py +++ b/test/test_magicencode.py @@ -95,12 +95,12 @@ class TestMagicEncode: try: - import jcconv + import jaconv except ImportError: - jcconv = None + jaconv = None -@pytest.mark.skipif(not jcconv, reason="jcconv not installed") +@pytest.mark.skipif(not jaconv, reason="jaconv not installed") class TestKatakana: @given(st.text()) @example("カタカナ") diff --git a/tox.ini b/tox.ini index bed3686..7fbd3b8 100644 --- a/tox.ini +++ b/tox.ini @@ -3,6 +3,7 @@ envlist = py27, py34, py35, docs [testenv] deps = nose + jaconv coverage scripttest mock