From 8903e1d3863872fc34a38a9f1cc7ee9c3c16f290 Mon Sep 17 00:00:00 2001 From: Romain Dorgueil Date: Mon, 26 Dec 2016 08:57:39 +0100 Subject: [PATCH] still testing --- bonobo/core/plugins.py | 9 +++++++++ bonobo/ext/console/plugin.py | 4 +++- bonobo/ext/jupyter/plugin.py | 2 +- tests/ext/test_ods.py | 35 ++++++++++++++++++++++++++++++++++ tests/io/test_json.py | 37 ++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 bonobo/core/plugins.py create mode 100644 tests/ext/test_ods.py create mode 100644 tests/io/test_json.py diff --git a/bonobo/core/plugins.py b/bonobo/core/plugins.py new file mode 100644 index 0000000..4ebc629 --- /dev/null +++ b/bonobo/core/plugins.py @@ -0,0 +1,9 @@ +class Plugin: + def initialize(self, context): + pass + + def run(self, context): + pass + + def finalize(self, context): + pass diff --git a/bonobo/ext/console/plugin.py b/bonobo/ext/console/plugin.py index 9cbbe95..5ef6469 100644 --- a/bonobo/ext/console/plugin.py +++ b/bonobo/ext/console/plugin.py @@ -20,6 +20,8 @@ import blessings import psutil +from bonobo.core.plugins import Plugin + t = blessings.Terminal() @@ -34,7 +36,7 @@ def memory_usage(): # return datetime.datetime.now() - harness._started_at -class ConsoleOutputPlugin: +class ConsoleOutputPlugin(Plugin): """ Outputs status information to the connected stdout. Can be a TTY, with or without support for colors/cursor movements, or a non tty (pipe, file, ...). The features are adapted to terminal capabilities. diff --git a/bonobo/ext/jupyter/plugin.py b/bonobo/ext/jupyter/plugin.py index 7fb48ec..16626d0 100644 --- a/bonobo/ext/jupyter/plugin.py +++ b/bonobo/ext/jupyter/plugin.py @@ -3,7 +3,7 @@ from IPython.core.display import display from bonobo.ext.jupyter.widget import BonoboWidget -class JupyterOutputPlugin: +class JupyterOutputPlugin(Plugin): def initialize(self, context): self.widget = BonoboWidget() display(self.widget) diff --git a/tests/ext/test_ods.py b/tests/ext/test_ods.py new file mode 100644 index 0000000..e585637 --- /dev/null +++ b/tests/ext/test_ods.py @@ -0,0 +1,35 @@ +from mock import patch + +from bonobo.ext.ods import extract_ods + + +class ResponseMock: + def __init__(self, json_value): + self.json_value = json_value + self.count = 0 + + def json(self): + if self.count: + return {} + else: + self.count += 1 + return {'records': self.json_value, } + + +def test_read_from_opendatasoft_api(): + extract = extract_ods('http://example.com/', 'test-a-set') + with patch( + 'requests.get', return_value=ResponseMock([ + { + 'fields': { + 'foo': 'bar' + } + }, + { + 'fields': { + 'foo': 'zab' + } + }, + ])): + for line in extract(): + assert 'foo' in line diff --git a/tests/io/test_json.py b/tests/io/test_json.py new file mode 100644 index 0000000..1c4716b --- /dev/null +++ b/tests/io/test_json.py @@ -0,0 +1,37 @@ +import pytest + +from bonobo import to_json +from bonobo.util.lifecycle import get_initializer, get_finalizer + + +class ContextMock: + pass + + +def test_write_json_to_file(tmpdir): + file = tmpdir.join('output.json') + json_writer = to_json(file) + context = ContextMock() + + get_initializer(json_writer)(context) + json_writer(context, {'foo': 'bar'}) + get_finalizer(json_writer)(context) + + assert file.read() == '''[ +{"foo": "bar"} +]''' + + with pytest.raises(AttributeError): + getattr(context, 'fp') + + with pytest.raises(AttributeError): + getattr(context, 'first') + + +def test_write_json_without_initializer_should_not_work(tmpdir): + file = tmpdir.join('output.json') + json_writer = to_json(file) + + context = ContextMock() + with pytest.raises(AttributeError): + json_writer(context, {'foo': 'bar'})