From 1dbb73a1c806f128f3d7239567e3c5e22b3945a4 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 11 Dec 2017 09:23:24 -0600 Subject: [PATCH] Extract test_file_utils.py. This isolates all the code that depends on our current directory structure, and it de-clutters test_lib.py a bit. --- zulip_bots/zulip_bots/test_file_utils.py | 35 ++++++++++++++++++++++++ zulip_bots/zulip_bots/test_lib.py | 27 ++++-------------- 2 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 zulip_bots/zulip_bots/test_file_utils.py diff --git a/zulip_bots/zulip_bots/test_file_utils.py b/zulip_bots/zulip_bots/test_file_utils.py new file mode 100644 index 0000000..c79e1d9 --- /dev/null +++ b/zulip_bots/zulip_bots/test_file_utils.py @@ -0,0 +1,35 @@ +import json +import os + +from importlib import import_module + +from typing import Any, Dict + +''' +This module helps us find files in the bots directory. Our +directory structure is currently: + + zulip_bots/zulip_bots/bots/ + / + .py + fixtures/ +''' + +def get_bot_message_handler(bot_name): + # type: (str) -> Any + # message_handler is of type 'Any', since it can contain any bot's + # handler class. Eventually, we want bot's handler classes to + # inherit from a common prototype specifying the handle_message + # function. + lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name)) # type: Any + return lib_module.handler_class() + +def read_bot_fixture_data(bot_name, test_name): + # type: (str, str) -> Dict[str, Any] + base_path = os.path.realpath(os.path.join(os.path.dirname( + os.path.abspath(__file__)), 'bots', bot_name, 'fixtures')) + http_data_path = os.path.join(base_path, '{}.json'.format(test_name)) + with open(http_data_path) as f: + content = f.read() + http_data = json.loads(content) + return http_data diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index a265619..82ea4d4 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -1,10 +1,7 @@ -import json import mock -import os from mock import patch -from importlib import import_module from unittest import TestCase from typing import List, Dict, Any, Tuple @@ -19,6 +16,11 @@ from zulip_bots.simple_lib import ( SimpleMessageServer, ) +from zulip_bots.test_file_utils import ( + get_bot_message_handler, + read_bot_fixture_data, +) + class StubBotHandler: def __init__(self): # type: () -> None @@ -169,22 +171,3 @@ class StubBotTestCase(TestCase): def mock_config_info(self, config_info): # type: (Dict[str, str]) -> Any return patch('zulip_bots.test_lib.StubBotHandler.get_config_info', return_value=config_info) - -def get_bot_message_handler(bot_name): - # type: (str) -> Any - # message_handler is of type 'Any', since it can contain any bot's - # handler class. Eventually, we want bot's handler classes to - # inherit from a common prototype specifying the handle_message - # function. - lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name)) # type: Any - return lib_module.handler_class() - -def read_bot_fixture_data(bot_name, test_name): - # type: (str, str) -> Dict[str, Any] - base_path = os.path.realpath(os.path.join(os.path.dirname( - os.path.abspath(__file__)), 'bots', bot_name, 'fixtures')) - http_data_path = os.path.join(base_path, '{}.json'.format(test_name)) - with open(http_data_path) as f: - content = f.read() - http_data = json.loads(content) - return http_data