1. Marc-Alexandre Chan
  2. DailyPromptBot

Commits

Marc-Alexandre Chan  committed 9ce9aec

[TEST] Added EventMock, a couple notes

  • Participants
  • Parent commits 169b6fd
  • Branches dev-minibot

Comments (0)

Files changed (2)

File minibot/config/localtest/minibot-setup.py

View file
  • Ignore whitespace
 #-------------------------------------------------------------------------------
 
 import sys
+import random
 from os.path import dirname, abspath
 from os import chdir
 
 db = bot.db
 dbs = db.get_new_session()
 
+random.seed()
+
+class EventMock(object):
+    S_INIT = 0
+    S_START = 1
+    S_RUN = 2
+    S_END = 3
+
+    ERR_NONE = 0
+    ERR_RETRY = 1
+    ERR_FINAL = 2
+    ERR_UNHANDLED = 3
+
+    start_time = 0
+    duration = 0
+    interval = 0
+
+    required_res = []
+
+    def __init__(self):
+        # all of these variables keep track of the event state for debugging
+        self.run_count = 0
+        self.state = self.S_INIT
+        self.error_raised = self.ERR_NONE
+        self.error_handled = self.ERR_NONE
+
+        # these are settings you can invoke
+        self.raise_error = self.ERR_NONE
+
+    def start(self):
+        self.state = self.S_START
+
+    def run(self):
+        self.run_count += 1
+        self.state = self.S_RUN
+        self.error_raised = self.raise_error
+
+        if self.raise_error == self.ERR_RETRY:
+            raise ValueError('retry possible')
+        elif self.raise_error == self.ERR_FINAL:
+            raise KeyError('final error')
+        elif self.raise_error == self.ERR_UNHANDLED:
+            raise OverflowError('oh god no')
+
+    def end(self):
+        self.state = self.S_END
+
+    def handle_exception(self, e):
+        if self.error_raised == self.ERR_RETRY:
+            self.delay = 4
+            return self.owner.EXC_HANDLED_RETRY
+        elif self.error_raised == self.ERR_FINAL:
+            return self.owner.EXC_HANDLED_FINAL
+        elif self.error_raised == self.ERR_UNHANDLED:
+            return self.owner.EXC_UNHANDLED
+
 class SchedulerMock(object):
-    def __init__(self):
+    id_chars = [chr(i) for i in xrange(ord('a'), ord('z')+1)] +\
+        [str(i) for i in xrange(0, 10)]
+
+    def __init__(self, include_msg_id=None):
         self.events = []
         self.last_event = None
+        self._include_msg_id = include_msg_id
 
     def queue_event(self, event):
         self.last_event = event
         self.events.append(event)
 
+    def get_events(self, cls=None): # yields 10 eventmocks with .msg.id attrib
+        class MessageMock(object):
+            def __init__(self, id_='abc123'):
+                self.id = id_
+
+        for i in xrange(0, 10):
+            ev = EventMock()
+            if i != 6 or not self._include_msg_id:
+                id_parts = [random.choice(self.id_chars) for _ in xrange(0, 5)]
+                ev.msg = MessageMock(''.join(id_parts))
+                yield ev
+            else:
+                ev.msg = MessageMock(self._include_msg_id)
+                yield ev
+
     def print_event(self, index=-1):
         print repr(self.events[index])
 

File minibot/config/localtest/notes.txt

View file
  • Ignore whitespace
 execfile('D:\Projects\Software\DailyPrompt\minibot\config\localtest\minibot-setup.py');
-res = {'reddit' : reddit, 'dbsession' : db.get_new_session(), 'config.minibot' : config.minibot, 'config.reddit' : config.reddit, 'logger' : log};
+res = {'reddit' : reddit, 'dbsession' : db.get_new_session(), 'config.minibot' : config.minibot, 'config.reddit' : config.reddit, 'logger' : log, 'approved' : ['Laogeodritt', 'awkisopen']};
 msg=list(reddit.user.get_unread())[0];
-owner=SchedulerMock()
+msg2=list(reddit.user.get_unread())[1];
+owner=SchedulerMock(msg2.id)
 import minibot.events, minibot.db, minibot.util
 
 # PromptCommandBase
 
 ev._find_prev_thread()
 print ev._get_title()
-print ev._get_text()
+print ev._get_text()
+
+# CheckMessageEvent
+ev = minibot.events.CheckMessageEvent()
+(ev.res, ev.owner, ev.log, ev.reddit, ev.users) = (res, owner, log, reddit, ['Laogeodritt', 'awkisopen'])
+
+ev._exc_msg_data(msg)
+ev._is_msg_queued(msg, {'action' : 'add'})
+# do _parse_field tests here
+ev._parse_message(msg)
+ev._parse_message(msg2)
+for x in list(reddit.user.get_inbox()):
+	print x.body
+	print ev._is_valid_message(x)