From efeac92d4279fcab1a733ef335e26ca670e37343 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Wed, 29 Nov 2017 13:30:09 -0800 Subject: [PATCH] Add tools/test-lib. This starts to add direct test coverage on zulip_bots/zulip_bots/lib.py. It is not yet integrated into tools/test-main. --- tools/run-mypy | 2 + tools/test-lib | 35 +++++++++++++++++ zulip_bots/zulip_bots/lib_tests.py | 60 ++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100755 tools/test-lib create mode 100644 zulip_bots/zulip_bots/lib_tests.py diff --git a/tools/run-mypy b/tools/run-mypy index 692f9c5..423966d 100755 --- a/tools/run-mypy +++ b/tools/run-mypy @@ -28,6 +28,8 @@ exclude = [ # Excluded out of laziness: "zulip_bots/zulip_bots/terminal.py", "zulip_bots/zulip_bots/simple_lib.py", + "zulip_bots/zulip_bots/lib_tests.py", + "tools/test-lib", ] parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.") diff --git a/tools/test-lib b/tools/test-lib new file mode 100755 index 0000000..8b6bceb --- /dev/null +++ b/tools/test-lib @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +import coverage +import os +import sys +import unittest + +from importlib import import_module + +def run_all(): + TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) + ROOT_DIR = os.path.abspath(os.path.join(TOOLS_DIR, '..')) + BOTS_DIR = os.path.join(ROOT_DIR, 'zulib_bots') + sys.path.insert(0, BOTS_DIR) + + cov = coverage.Coverage(config_file="tools/.coveragerc") + cov.start() + + module = import_module('zulip_bots.lib_tests') + suite = unittest.defaultTestLoader.loadTestsFromModule(module) + + suite = unittest.TestSuite([suite]) + runner = unittest.TextTestRunner(verbosity=2) + result = runner.run(suite) + if result.failures or result.errors: + sys.exit(1) + + cov.stop() + cov.data_suffix = False # Disable suffix so that filename is .coverage + cov.save() + cov.html_report() + print("HTML report saved under directory 'htmlcov'.") + +if __name__ == '__main__': + run_all() diff --git a/zulip_bots/zulip_bots/lib_tests.py b/zulip_bots/zulip_bots/lib_tests.py new file mode 100644 index 0000000..ec674e0 --- /dev/null +++ b/zulip_bots/zulip_bots/lib_tests.py @@ -0,0 +1,60 @@ +from unittest import TestCase +from zulip_bots.lib import ( + ExternalBotHandler, + StateHandler, +) + +class FakeClient: + def __init__(self): + self.storage = dict() + + def get_profile(self): + return dict( + user_id='alice', + full_name='Alice', + email='alice@example.com', + ) + + def update_storage(self, payload): + new_data = payload['storage'] + self.storage.update(new_data) + + return dict( + result='success', + ) + + def get_storage(self): + return dict( + result='success', + storage=self.storage, + ) + + def send_message(self, message): + pass + +class LibTest(TestCase): + def test_basics(self): + client = FakeClient() + + handler = ExternalBotHandler( + client=client, + root_dir=None, + bot_details=None, + bot_config_file=None + ) + + message = None + handler.send_message(message) + + def test_state_handler(self): + client = FakeClient() + + state_handler = StateHandler(client) + state_handler.put('key', [1, 2, 3]) + val = state_handler.get('key') + self.assertEqual(val, [1, 2, 3]) + + # force us to get non-cached values + state_handler = StateHandler(client) + val = state_handler.get('key') + self.assertEqual(val, [1, 2, 3])