Esempio n. 1
0
    def test_routes(self):
        b = general.General(db=None, config={}, dir='')
        act = Interaction()
        b.logging = Mock()
        b.logging.info = Mock(return_value=True)

        b.log_expense = Mock()
        b.config_set = Mock()
        b.time = Mock()
        b.datetime = Mock()

        response = b.handle(act)
        self.assertEqual(response, None)

        act.command = {'text': 'time'}
        b.handle(act)
        b.time.assert_called_once()

        act.command = {'text': 'set config something=avalue'}
        b.handle(act)
        b.config_set.assert_called_once()
        self.assertEqual(b.match.group(1), 'something')
        self.assertEqual(b.match.group(2), 'avalue')

        b.config_set = Mock()
        act.command = {'text': 'config something=anothervalue'}
        b.handle(act)
        b.config_set.assert_called_once()
        self.assertEqual(b.match.group(1), 'something')
        self.assertEqual(b.match.group(2), 'anothervalue')
Esempio n. 2
0
 def test_behaviour_set_history(self):
     g = general.General(
         db=None, config={},
         dir='')  # Use general because Behaviour is abstract base class
     g.logging = Mock()
     g.info = Mock(return_value=True)
     g.set_history(1, {'msg': 'test'})
     expected = {1: {'msg': 'test', 'time': datetime.datetime.now()}}
     self.assertEqual(g.history, expected)
Esempio n. 3
0
 def test_behaviour_handle(self):
     g = general.General(
         db=None, config={},
         dir='')  # Use general because Behaviour is abstract base class
     act = Interaction()
     g.logging = Mock()
     g.info = Mock(return_value=True)
     response = g.handle(act)
     self.assertEqual(response, None)
     act.command = {'text': 'time'}
     response = g.handle(act)
     self.assertNotEqual(response, None)
Esempio n. 4
0
    def test_set_config(self):
        b = general.General(db=None, config=Config(), dir='')
        act = Interaction()
        b.logging = Mock()
        b.logging.info = Mock(return_value=True)
        b.config.db = Mock()
        b.config.db.insert = Mock(return_value=1234)

        b.match = re.search('^(?:set )?config (.*)=(.*)$', 'set config configItem=value',
                            re.IGNORECASE)

        self.assertEquals(b.config_set(), 'Config set: configItem = value')
        b.config.db.insert.assert_called_with('config', {'key': 'configItem', 'value': 'value'})
Esempio n. 5
0
    def test_get_config(self):
        b = general.General(db=None, config=Config(), dir='')
        act = Interaction()
        b.logging = Mock()
        b.logging.info = Mock(return_value=True)
        b.config.db = Mock()
        b.config.db.find_one = Mock(return_value=None)
        try:
            self.assertEquals(b.config_get('something'), None)
        except ValueError as e:
            self.assertEqual(str(e), 'something is not set in config')

        b.config.db.find_one = Mock(return_value={'key': 'configItem', 'value': '1234'})
        self.assertEquals(b.config_get('something'), '1234')
Esempio n. 6
0
 def test_behaviour_init(self):
     # self.assertEqual(type(general.General.routes), type(collections.OrderedDict()))
     g = general.General(
         db=None, config={},
         dir='')  # Use general because Behaviour is abstract base class
     self.assertEqual(g.act, {})
     self.assertEqual(g.db, None)
     self.assertEqual(g.dir, '')
     self.assertEqual(g.files, g.dir + '/files')
     self.assertEqual(g.config, {})
     self.assertEqual(g.collection, '')
     self.assertEqual(g.match, None)
     self.assertEqual(g.execution_order, 1)
     self.assertEqual(g.logging, None)
     self.assertEqual(g.history, {})
Esempio n. 7
0
    def test_behaviour_get_recent_history(self):
        g = general.General(
            db=None, config={},
            dir='')  # Use general because Behaviour is abstract base class
        g.logging = Mock()
        g.info = Mock(return_value=True)
        response = g.get_recent_history(1)
        self.assertEqual(response, None)  # If no history

        g.history = {
            1: {
                'msg': 'test',
                'time':
                datetime.datetime.now() - datetime.timedelta(minutes=10)
            }
        }
        response = g.get_recent_history(1)
        self.assertEqual(response, None)  # If history expired

        expected = {'msg': 'test', 'time': datetime.datetime.now()}
        g.history = {1: expected}
        response = g.get_recent_history(1)
        self.assertEqual(response, expected)  # If history valid
Esempio n. 8
0
 def test_datetime(self):
     g = general.General(db=None, config={}, dir='')
     self.assertEqual(g.date_time(), datetime.datetime.now().strftime('%d-%m-%y %I:%M %p'))