Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Fix check first time and default values test
Browse files Browse the repository at this point in the history
  • Loading branch information
ahelal committed Aug 30, 2017
1 parent 4d2476a commit 6eaeb2b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/lib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
16 changes: 12 additions & 4 deletions src/lib/check_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand All @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions src/lib/in_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand Down
3 changes: 3 additions & 0 deletions src/lib/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions src/tests/test_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 6eaeb2b

Please sign in to comment.