From 6eaeb2b2c80e15aa260dc3f95e0167e0e2ee78b7 Mon Sep 17 00:00:00 2001 From: Adham Helal Date: Thu, 31 Aug 2017 00:59:37 +0200 Subject: [PATCH] Fix check first time and default values test --- src/lib/base.py | 12 ++++++------ src/lib/check_op.py | 16 ++++++++++++---- src/lib/in_op.py | 3 +-- src/lib/payload.py | 3 +++ src/tests/test_base.py | 2 +- src/tests/test_in.py | 7 +++++-- src/tests/test_payload.py | 2 +- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/lib/base.py b/src/lib/base.py index 5054088..b4d86da 100644 --- a/src/lib/base.py +++ b/src/lib/base.py @@ -68,12 +68,12 @@ class Base(object): # pylint: disable=too-few-public-methods,too-many-instance-a def __init__(self, **kwargs): self.slack_client = SlackClient(kwargs.get("slack_token")) - self.bot = kwargs.get("bot", "bender") - self.channel = kwargs.get("channel", "") - self.grammar = kwargs.get("grammar", False) - self.version = kwargs.get("version", False) - self.working_dir = kwargs.get("working_dir", "/") - self.slack_unread = kwargs.get("slack_unread", False) + self.bot = kwargs.get("bot_name") + self.channel = kwargs.get("channel") + self.grammar = kwargs.get("grammar") + self.version = kwargs.get("version") + self.working_dir = kwargs.get("working_dir") + self.slack_unread = kwargs.get("slack_unread") # Get all user list self.users = self._call_api("users.list", presence=0) # find my id `the bot` diff --git a/src/lib/check_op.py b/src/lib/check_op.py index 86dcf7d..5eab7d2 100644 --- a/src/lib/check_op.py +++ b/src/lib/check_op.py @@ -73,10 +73,10 @@ def check_logic_unread(self, max_api_count=100): def check_logic_concourse(self, max_api_count=1000): """Concourse resource `check` logic using version passed by concourse""" - oldest = self.version.get("id_ts", 0) has_more = True - while has_more: + limit = 5 + while has_more and limit >= 0: messages = self._call_api(self.channel_type + ".history", channel=self.channel_id, count=max_api_count, @@ -87,6 +87,15 @@ def check_logic_concourse(self, max_api_count=1000): oldest = messages["messages"][-1]["ts"] self._parse_msgs(messages["messages"], len(messages["messages"])) + if not self.checked_msg: + # Sort messages by ts chronological + self.checked_msg = sorted(self.checked_msg, key=lambda k: k['id_ts']) + + if not self.version.get("id_ts", False) and self.checked_msg: + self.checked_msg = [self.checked_msg[0]] + + limit -= 1 + def check_output(self): """Concourse resource `check` output """ print(json.dumps(self.checked_msg, indent=4, sort_keys=True)) @@ -95,8 +104,7 @@ def main(): """Concourse resource `check` main """ payload = PayLoad() slack_client = Check(**payload.args) - - if slack_client.slack_unread or not slack_client.version: + if slack_client.slack_unread: slack_client.check_logic_unread() else: slack_client.check_logic_concourse() diff --git a/src/lib/in_op.py b/src/lib/in_op.py index 076f7f9..721c838 100644 --- a/src/lib/in_op.py +++ b/src/lib/in_op.py @@ -16,8 +16,7 @@ def __init__(self, **kwargs): Base.__init__(self, **kwargs) self.metadata = [] self.template = kwargs.get("template") - - self.template_filename = os.path.basename(kwargs.get("template_filename", "")) + self.template_filename = os.path.basename(kwargs.get("template_filename")) self.templated_string = None self.original_msg = "" diff --git a/src/lib/payload.py b/src/lib/payload.py index 9921ce8..bd14ffc 100644 --- a/src/lib/payload.py +++ b/src/lib/payload.py @@ -20,6 +20,7 @@ def __init__(self): else: self.params = self.payload.get("params", {}) self.parse_payload() + # argument pass with dir self.args["working_dir"] = self._get_dir_from_argv() @staticmethod @@ -42,6 +43,8 @@ def _get_dir_from_argv(): def parse_payload(self): ''' Parse payload passed by concourse''' self.args["version"] = self.payload.get("version") + if self.args["version"] is None: + self.args["version"] = {} try: # Mandatory source configs self.args["slack_token"] = self.source["slack_token"] diff --git a/src/tests/test_base.py b/src/tests/test_base.py index 47d1b57..62508e5 100644 --- a/src/tests/test_base.py +++ b/src/tests/test_base.py @@ -21,7 +21,7 @@ def setUp(self, mock_call_api, mock_get_channel_group_info, mock_filter): mock_filter.return_value = "U01B12FDS" self.grammar = "^(superApp)\s+(deploy)\s+(live|staging)\s+(\S+)($|\s+)" - self.resource = base.Base(token="token", channel="testChannel", bot="theBender", working_dir="/test", + self.resource = base.Base(token="token", channel="testChannel", bot_name="theBender", working_dir="/test", grammar=self.grammar, path="bender_path", reply="testing 1.2.3", slack_unread=True) def test__init__(self): diff --git a/src/tests/test_in.py b/src/tests/test_in.py index 0587c43..9668fd6 100644 --- a/src/tests/test_in.py +++ b/src/tests/test_in.py @@ -22,10 +22,13 @@ def setUp(self, mock_call_api, mock_get_channel_group_info, mock_filter): mock_filter.return_value = "U01B12FDS" self.grammar = "^(superApp)\s+(deploy)\s+(live|staging)\s+(\S+)($|\s+)" - self.resource = in_op.In(token="token", channel="testChannel", bot="theBender", - template="VERSION={{ regex[4] }}", template_file="template_file.txt", + self.resource = in_op.In(template="VERSION={{ regex[4] }}", template_filename="template_filename", grammar=self.grammar, path="bender_path", reply="testing 1.2.3") + def test__init__(self): + + self.assertEqual(self.resource.template_filename, "template_filename") + # TODO test_in_logic @mock.patch('in_op.write_to_file') diff --git a/src/tests/test_payload.py b/src/tests/test_payload.py index 77947cf..a845db2 100644 --- a/src/tests/test_payload.py +++ b/src/tests/test_payload.py @@ -60,10 +60,10 @@ def test_parse_payload_defaults(self, mock_get_payload, mock_fail_unless, mock_g self.assertEqual(py.args["slack_token"], "slack_token") self.assertEqual(py.args["channel"], "channel") self.assertEqual(py.args["template_filename"], "template_file.txt") + self.assertEqual(py.args["version"], {}) self.assertFalse(py.args["slack_unread"]) self.assertIsNone(py.args["template"]) - self.assertIsNone(py.args["version"]) self.assertIsNone(py.args["grammar"]) self.assertIsNone(py.args["path"]) self.assertIsNone(py.args["reply"])