Commits

Marc-Alexandre Chan committed c73f23f

[TEST] Added modular EventMock.get_events() mocking; added more bootstrap testing code;
added some more notes/code for manual event testing

Comments (0)

Files changed (2)

minibot/config/localtest/minibot-setup.py

 sys.path.append(botroot)
 
 from minibot import DailyPromptMinibot
+from datetime import datetime
 
 bot = DailyPromptMinibot('minibot/config/localtest/minibot.ini')
 
         self.run_count += 1
         self.state = self.S_RUN
         self.error_raised = self.raise_error
+        print "run at {}".format(str(datetime.now()))
+
+        if run_count > 10:
+            self.owner.exit()
 
         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')
+            raise IndexError('oh god no')
 
     def end(self):
         self.state = self.S_END
         elif self.error_raised == self.ERR_UNHANDLED:
             return self.owner.EXC_UNHANDLED
 
+def scheduler_msg_events(include_msg_id=None): # yields event mocks with ev.msg.id set
+    def generator():
+        class MessageMock(object):
+            def __init__(self, id_='abc123'):
+                self.id = id_
+
+        for i in xrange(0, 10):
+            ev = EventMock()
+            if i != 6 or not 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(include_msg_id)
+                yield ev
+    return generator
+
+def scheduler_postid_events(include_id=None, exclude_id=None): # yields 10 event mocks with ev.post_id set
+    def generator():
+        for i in xrange(0, 10):
+            ev = EventMock()
+            if i != 4 or not include_id:
+                ev.post_id = random.randrange(1, 101)
+                while exclude_id and ev.post_id == exclude_id: # make sure not the excluded id
+                    ev.post_id = random.randrange(1, 101)
+                yield ev
+            else:
+                ev.post_id = include_id
+                yield ev
+    return generator
+
 class SchedulerMock(object):
     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):
+    def __init__(self, events_list=None, events_generator=None):
         self.events = []
         self.last_event = None
-        self._include_msg_id = include_msg_id
+
+        if events_generator:
+            self.events_generator = events_generator
+            self.events_list = None
+        else:
+            self.events_generator = None
+            self.events_list = events_list
 
     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))
+        if self.events_generator is not None:
+            for ev in self.events_generator():
                 yield ev
-            else:
-                ev.msg = MessageMock(self._include_msg_id)
-                yield ev
+        else:
+            try:
+                for ev in self.events_list:
+                    yield ev
+            except TypeError:
+                return
 
     def print_event(self, index=-1):
         print repr(self.events[index])
 def listQuery(query):
     for x in query:
         print x
+
+# for events testing
+res = {'reddit' : reddit, 'dbsession' : db.get_new_session(),
+       'config.minibot' : config.minibot, 'config.reddit' : config.reddit,
+       'logger' : log, 'approved' : ['Laogeodritt', 'awkisopen']}
+owner = SchedulerMock()
+# these can be redefined if arguments must be passed to the scheduler_*_events() generator
+owner_msg = SchedulerMock(None, scheduler_msg_events())
+owner_postid = SchedulerMock(None, scheduler_postid_events())
+try:
+    msg=list(reddit.user.get_unread())[0]
+    msg2=list(reddit.user.get_unread())[1]
+except IndexError:
+    pass
+
+import minibot.events, minibot.db, minibot.util

minibot/config/localtest/notes.txt

 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, 'approved' : ['Laogeodritt', 'awkisopen']};
-msg=list(reddit.user.get_unread())[0];
-msg2=list(reddit.user.get_unread())[1];
-owner=SchedulerMock(msg2.id)
-import minibot.events, minibot.db, minibot.util
 
 # PromptCommandBase
 ev=minibot.events.PromptCommandBase(msg, title='title', text='text');
 
 # CheckMessageEvent
 ev = minibot.events.CheckMessageEvent()
+owner=SchedulerMock(scheduler_msg_events(msg2.id))
+(ev.res, ev.owner) = (res, owner)
 (ev.res, ev.owner, ev.log, ev.reddit, ev.users) = (res, owner, log, reddit, ['Laogeodritt', 'awkisopen'])
 
 ev._exc_msg_data(msg)
 ev._parse_message(msg2)
 for x in list(reddit.user.get_inbox()):
 	print x.body
-	print ev._is_valid_message(x)
+	print ev._is_valid_message(x)
+
+# CheckPostQueueEvent
+ev = minibot.events.CheckPostQueueEvent()
+(ev.res, ev.owner) = (res, owner)
+ev.start()
+
+# CheckSuggestionQueueEvent
+ev = minibot.events.CheckSuggestionQueueEvent()
+(ev.res, ev.owner) = (res, owner)
+ev.start()
+
+# SuggestionThreadQueueMaintainer
+ev = minibot.events.SuggestionThreadQueueMaintainer()
+(ev.res, ev.owner) = (res, owner)
+ev.start()