Пример #1
0
    def test_query_expression_rendering(self):
        """Test that query expresssion can be query and rendered correctly."""
        CollectedDatum(user_id=self.user_1.id, key='data',
                       value='value1').add()
        DatabaseManager.commit()
        time.sleep(1)
        CollectedDatum(user_id=self.user_1.id, key='data',
                       value='value2').add()
        DatabaseManager.commit()
        time.sleep(1)
        CollectedDatum(user_id=self.user_1.id, key='data',
                       value='value3').add()
        CollectedDatum(user_id=self.user_1.id, key='aaa',
                       value='aaa').add()
        CollectedDatum(user_id=self.user_2.id, key='data',
                       value='value4').add()
        DatabaseManager.commit()

        g.user = self.user_1
        m = Message("{{data('data').first|upper}}")
        self.assertEquals(m.as_dict()['text'], 'VALUE1')

        m = Message("{{data('data').get(1)}}")
        self.assertEquals(m.as_dict()['text'], 'value2')

        m = Message("{{data('data').last}}")
        self.assertEquals(m.as_dict()['text'], 'value3')

        m = Message("{{data('data').lru(0)}}")
        self.assertEquals(m.as_dict()['text'], 'value3')

        m = Message("{{data('data').lru(1)}}")
        self.assertEquals(m.as_dict()['text'], 'value2')

        m = Message("{{data('data').fallback('valuef').get(5)}}")
        self.assertEquals(m.as_dict()['text'], 'valuef')

        m = Message("{{data('data').order_by('-created_at').first}}")
        self.assertEquals(m.as_dict()['text'], 'value3')

        m = Message("{{data('data').count}}")
        self.assertEquals(m.as_dict()['text'], '3')

        # Test error
        with self.assertRaises(Exception):
            Message("{{data('data')|some_filter}}")

        wrong_tmpl = "{{data('some_key').first}}"
        m = Message(wrong_tmpl)
        self.assertEquals(m.as_dict()['text'], wrong_tmpl)
Пример #2
0
 def lru(self, index):
     result = CollectedDatum.query(
         CollectedDatum.value,
         func.max(CollectedDatum.created_at).label('latest')).filter_by(
             key=self.key,
             user_id=g.user.id).group_by(CollectedDatum.value).order_by(
                 desc('latest')).offset(index).limit(1).first()
     return result[0] if result else self.fallback_value
Пример #3
0
 def GetLast(cls, key, default=None):
     """Get the last collected result given *key*."""
     result = _CollectedDatum.get_by(query=[_CollectedDatum.value],
                                     user_id=g.user.id,
                                     key=key,
                                     order_by=[desc('created_at')],
                                     single=True)
     return result[0] if result else default
Пример #4
0
    def test_CollectedData_API(self):
        for i in range(3):
            CollectedDatum(user_id=self.user_1.id,
                           key='K', value='V%d' % i).add()
            time.sleep(1)
            DatabaseManager.commit()

        g.user = self.user_1
        self.assertEquals(CollectedData.GetLast('K'), 'V2')
        self.assertEquals(CollectedData.Get('K', 3), ['V2', 'V1', 'V0'])
        self.assertEquals(CollectedData.Get('K', 2, 1), ['V1', 'V0'])

        # pylint: disable=W0212
        for i in range(CollectedData._MAX_RETURN_RESULTS + 10):
            CollectedDatum(user_id=self.user_2.id,
                           key='K', value='V%d' % i).add()
        DatabaseManager.commit()

        g.user = self.user_2
        self.assertEquals(len(CollectedData.Get('K', 110)), 100)
        self.assertEquals(CollectedData.Count('K'), 110)
Пример #5
0
    def Get(cls, key, limit=1, offset=0):
        """Get *limit* result starting from *offset* given *key*.

        The max number of results is constraint to _MAX_RETURN_RESULTS.
        """
        if limit > cls._MAX_RETURN_RESULTS:
            limit = cls._MAX_RETURN_RESULTS

        result = _CollectedDatum.get_by(query=[_CollectedDatum.value],
                                        user_id=g.user.id,
                                        key=key,
                                        order_by=[desc('created_at')],
                                        offset=offset,
                                        limit=limit)
        return [x[0] for x in result] if result else []
Пример #6
0
 def __init__(self, key):
     self.key = key
     self.fallback_value = ''
     self.query = CollectedDatum.query(CollectedDatum.value,
                                       CollectedDatum.created_at).filter_by(
                                           key=key, user_id=g.user.id)
Пример #7
0
 def Count(cls, key):
     """Get number of entries."""
     return _CollectedDatum.count_by(user_id=g.user.id, key=key)
Пример #8
0
 def insert_data(self, user, data):
     """Insert collected data into database."""
     for key in data:
         CollectedDatum(user_id=user.id, key=key, value=data[key]).add()
Пример #9
0
    def test_schema_sanity(self):
        """Populate data into all tables and make sure there are no error."""
        DatabaseManager.reset()

        account = Account(name=u'Test Account',
                          email='*****@*****.**', passwd='test_hashed').add()

        bot = Bot(name=u'test', description=u'test', interaction_timeout=120,
                  session_timeout=86400).add()
        account.bots.append(bot)

        content = ContentModule(id='test', name='Content1', description='desc',
                                module_name='', ui_module_name='').add()
        parser = ParserModule(id='test', name='Parser1', description='desc',
                              module_name='passthrough', ui_module_name='',
                              variables={}).add()

        # Test for oauth schema
        oauth1 = OAuthInfo(provider=OAuthProviderEnum.Facebook,
                           provider_ident='mock-facebook-id').add()

        oauth2 = OAuthInfo(provider=OAuthProviderEnum.Github,
                           provider_ident='mock-github-id').add()

        account.oauth_infos.append(oauth1)
        account.oauth_infos.append(oauth2)
        DatabaseManager.commit()

        account_ = Account.get_by(id=account.id, single=True)
        self.assertNotEquals(account_, None)
        self.assertEquals(len(account_.oauth_infos), 2)

        oauth_ = OAuthInfo.get_by(provider_ident='mock-facebook-id',
                                  single=True)
        self.assertNotEquals(oauth_, None)
        self.assertNotEquals(oauth_.account, None)
        self.assertEquals(oauth_.account.id, account.id)

        # Test for bot
        account.bots.append(bot)

        DatabaseManager.commit()

        self.assertNotEquals(Account.get_by(id=account.id, single=True), None)
        self.assertNotEquals(Bot.get_by(id=bot.id, single=True), None)
        self.assertNotEquals(ContentModule.get_by(id=content.id, single=True),
                             None)
        self.assertNotEquals(ParserModule.get_by(id=parser.id, single=True),
                             None)

        # Check acccount_bot association table
        self.assertEquals(len(account.bots), 1)
        self.assertEquals(account.bots[0].id, bot.id)

        platform = Platform(name=u'Test platform',
                            bot_id=bot.id,
                            type_enum=PlatformTypeEnum.Facebook,
                            provider_ident='facebook_page_id',
                            config={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(Platform.get_by(id=platform.id, single=True),
                             None)
        self.assertEquals(len(bot.platforms), 1)
        self.assertEquals(bot.platforms[0].id, platform.id)

        node1 = Node(stable_id='node1', name=u'1', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        node2 = Node(stable_id='node2', name=u'2', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        node3 = Node(stable_id='node3', name=u'3', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        bot.orphan_nodes.append(node3)

        DatabaseManager.commit()

        self.assertNotEquals(Node.get_by(id=node1.id, single=True), None)
        self.assertNotEquals(Node.get_by(id=node2.id, single=True), None)
        self.assertNotEquals(Node.get_by(id=node3.id, single=True), None)

        # Test bot_node association table
        self.assertEquals(bot.orphan_nodes[0].id, node3.id)

        user = User(platform_id=platform.id,
                    platform_user_ident='',
                    last_seen=datetime.datetime.now()).add()
        DatabaseManager.commit()

        self.assertNotEquals(User.get_by(id=user.id, single=True), None)

        event = Event(bot_id=bot.id, user_id=user.id, event_name='event',
                      event_value={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(Event.get_by(id=event.id, single=True), None)

        collected_datum = CollectedDatum(user_id=user.id,
                                         key='key', value={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(CollectedDatum.get_by(id=collected_datum.id,
                                                   single=True), None)
        self.assertEquals(len(user.colleted_data), 1)
        self.assertEquals(user.colleted_data[0].id, collected_datum.id)

        conversation = Conversation(bot_id=bot.id, user_id=user.id,
                                    sender_enum=SenderEnum.Bot, msg={}).add()
        DatabaseManager.commit()
        self.assertNotEquals(Conversation.get_by(id=conversation.id,
                                                 single=True), None)

        # Broadcast
        bc = Broadcast(account_id=account.id, bot_id=bot.id,
                       name=u'New broadcast', messages=[],
                       scheduled_time=datetime.datetime.utcnow()).add()

        DatabaseManager.commit()
        self.assertNotEquals(Broadcast.get_by(id=bc.id, single=True), None)

        # PublicFeed, Feed
        account = Account(name=u'Test Account - 1',
                          email='*****@*****.**', passwd='test_hashed').add()
        feed1 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'foo.com', image_url='foo.com/logo').add()
        feed2 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'bar.com', image_url='bar.com/logo').add()
        feed3 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'baz.com', image_url='baz.com/logo').add()

        account.feeds.append(feed1)
        account.feeds.append(feed2)
        account.feeds.append(feed3)

        DatabaseManager.commit()
        self.assertNotEquals(Feed.get_by(id=feed1.id, single=True), None)

        feeds = Feed.search_title('ba')
        self.assertEquals(len(list(feeds)), 2)

        pfeed = PublicFeed(url='example.com/rss', type=FeedEnum.RSS,
                           title=u'example.com',
                           image_url='example.com/logo').add()

        DatabaseManager.commit()
        self.assertNotEquals(PublicFeed.get_by(id=pfeed.id, single=True), None)