2016-08-02 00:54:36 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""tests for the File printer
|
|
|
|
|
|
|
|
:author: `Patrick Kanzler <patrick.kanzler@fablab.fau.de>`_
|
|
|
|
:organization: `python-escpos <https://github.com/python-escpos>`_
|
|
|
|
:copyright: Copyright (c) 2016 `python-escpos <https://github.com/python-escpos>`_
|
|
|
|
:license: GNU GPL v3
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import six
|
|
|
|
|
2016-09-27 17:50:33 +00:00
|
|
|
import pytest
|
2016-08-02 00:54:36 +00:00
|
|
|
from hypothesis import given
|
|
|
|
from hypothesis.strategies import text
|
|
|
|
|
|
|
|
import escpos.printer as printer
|
|
|
|
|
|
|
|
if six.PY3:
|
|
|
|
mock_open_call = 'builtins.open'
|
|
|
|
else:
|
|
|
|
mock_open_call = '__builtin__.open'
|
|
|
|
|
2017-01-29 23:10:14 +00:00
|
|
|
|
2016-08-02 00:54:36 +00:00
|
|
|
@given(path=text())
|
2016-09-27 17:50:33 +00:00
|
|
|
def test_load_file_printer(mocker, path):
|
2016-08-02 00:54:36 +00:00
|
|
|
"""test the loading of the file-printer"""
|
2016-09-27 17:50:33 +00:00
|
|
|
mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__')
|
|
|
|
mock_open = mocker.patch(mock_open_call)
|
2016-08-02 00:54:36 +00:00
|
|
|
printer.File(devfile=path)
|
|
|
|
assert mock_escpos.called
|
|
|
|
mock_open.assert_called_with(path, "wb")
|
|
|
|
|
|
|
|
|
|
|
|
@given(txt=text())
|
2016-09-27 17:50:33 +00:00
|
|
|
def test_auto_flush(mocker, txt):
|
2016-08-02 00:54:36 +00:00
|
|
|
"""test auto_flush in file-printer"""
|
2016-09-27 17:50:33 +00:00
|
|
|
mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__')
|
|
|
|
mock_open = mocker.patch(mock_open_call)
|
|
|
|
mock_device = mocker.patch.object(printer.File, 'device')
|
|
|
|
|
2016-08-02 00:54:36 +00:00
|
|
|
p = printer.File(auto_flush=False)
|
|
|
|
# inject the mocked device-object
|
|
|
|
p.device = mock_device
|
|
|
|
p._raw(txt)
|
|
|
|
assert not mock_device.flush.called
|
|
|
|
mock_device.reset_mock()
|
|
|
|
p = printer.File(auto_flush=True)
|
|
|
|
# inject the mocked device-object
|
|
|
|
p.device = mock_device
|
|
|
|
p._raw(txt)
|
|
|
|
assert mock_device.flush.called
|
|
|
|
|
|
|
|
|
|
|
|
@given(txt=text())
|
2016-09-27 17:50:33 +00:00
|
|
|
def test_flush_on_close(mocker, txt):
|
2016-08-02 00:54:36 +00:00
|
|
|
"""test flush on close in file-printer"""
|
2016-09-27 17:50:33 +00:00
|
|
|
mock_open = mocker.patch(mock_open_call)
|
|
|
|
mock_device = mocker.patch.object(printer.File, 'device')
|
|
|
|
|
2016-08-02 00:54:36 +00:00
|
|
|
p = printer.File(auto_flush=False)
|
|
|
|
# inject the mocked device-object
|
|
|
|
p.device = mock_device
|
|
|
|
p._raw(txt)
|
|
|
|
assert not mock_device.flush.called
|
|
|
|
p.close()
|
|
|
|
assert mock_device.flush.called
|
|
|
|
assert mock_device.close.called
|