Ejemplo n.º 1
0
    def test_contains_in_watchlist_empty_keyword(self):
        FAKE_USER_ID = 'userid'
        FAKE_KEYWORD = ''

        wm = db.WatchModel()
        with pytest.raises(exceptions.EmptyKeywordException):
            wm.is_watched(FAKE_USER_ID, FAKE_KEYWORD)
Ejemplo n.º 2
0
    def test_watchlist(self, mocker):
        FAKE_USER_ID = 'userid'

        wm = db.WatchModel()
        find = mocker.patch.object(wm.table, 'find')
        wm.watchlist(FAKE_USER_ID)
        find.assert_called_once_with({'user_id': FAKE_USER_ID})
Ejemplo n.º 3
0
    def test_contains_in_watchlist_empty_user_id(self):
        FAKE_USER_ID = ''
        FAKE_KEYWORD = 'phone'

        wm = db.WatchModel()
        with pytest.raises(exceptions.NoUserException):
            wm.contains_in_watchlist(FAKE_USER_ID, FAKE_KEYWORD)
Ejemplo n.º 4
0
    def test_unwatch_no_user_id(self, mocker):
        FAKE_USER_ID = ''
        FAKE_KEYWORD = 'phone'

        wm = db.WatchModel()
        with pytest.raises(exceptions.NoUserException):
            wm.unwatch(FAKE_USER_ID, FAKE_KEYWORD)
Ejemplo n.º 5
0
    def test_unwatch_no_keyword(self, mocker):
        FAKE_USER_ID = 'userid'
        FAKE_KEYWORD = ''

        wm = db.WatchModel()
        with pytest.raises(exceptions.EmptyKeywordException):
            wm.unwatch(FAKE_USER_ID, FAKE_KEYWORD)
Ejemplo n.º 6
0
    def updates(bot, update):
        """Returns list of updated posts"""
        user_id = utils.get_user_id(update)
        cm = db.CityModel()
        wm = db.WatchModel()
        pm = db.PostsModel()

        user_city = cm.get_city(user_id)
        watchlist = wm.watchlist(user_id)

        posts = []
        for item in watchlist:
            for post in feed.get_posts(user_city, item['keyword']):
                if not pm.is_post_seen(user_id, post.post_id):
                    posts.append(post)
                    pm.mark_as_seen(user_id, post.post_id)

        if not posts:
            utils.send_message_with_keyboard(bot, update, "No updates so far.")
            return

        for ten_posts in utils.chunks(posts, 10):
            updates = ""
            for post in ten_posts:
                updates += post.oneline
            utils.send_message_with_keyboard(bot, update, updates)
Ejemplo n.º 7
0
    def test_unwatch(self, mocker):
        FAKE_USER_ID = 'userid'
        FAKE_KEYWORD = 'phone'

        wm = db.WatchModel()
        delete_one = mocker.patch.object(wm.table, 'delete_one')
        wm.unwatch(FAKE_USER_ID, FAKE_KEYWORD)
        delete_one.assert_called_once_with(
            {'keyword': FAKE_KEYWORD, 'user_id': FAKE_USER_ID})
Ejemplo n.º 8
0
    def test_contains_in_watchlist(self, mocker):
        FAKE_USER_ID = 'userid'
        FAKE_KEYWORD = 'phone'

        wm = db.WatchModel()
        find_one = mocker.patch.object(wm.table, 'find_one')
        wm.contains_in_watchlist(FAKE_USER_ID, FAKE_KEYWORD)

        find_one.assert_called_once_with(
            {'keyword': FAKE_KEYWORD, 'user_id': FAKE_USER_ID})
Ejemplo n.º 9
0
    def test_watch(self, mocker):
        FAKE_USER_ID = 'userid'
        FAKE_KEYWORD = 'phone'

        wm = db.WatchModel()
        insert_one = mocker.patch.object(wm.table, 'insert_one')
        mocker.patch.object(wm, 'is_watched', return_value=False)
        wm.watch(FAKE_USER_ID, FAKE_KEYWORD)
        insert_one.assert_called_once_with({'keyword': FAKE_KEYWORD,
                                            'user_id': FAKE_USER_ID})
Ejemplo n.º 10
0
    def watchlist(bot, update):
        user_id = utils.get_user_id(update)
        wm = db.WatchModel()
        watchlist = wm.watchlist(user_id)

        if watchlist:
            message = "\n".join([item['keyword'] for item in watchlist])
        else:
            message = "empty"

        utils.send_message_with_keyboard(bot, update,
                                         "Your watchlist is:\n%s" % message)
Ejemplo n.º 11
0
    def unwatch(bot, update):
        user_id = utils.get_user_id(update)
        keyword = utils.extract_command_value(update)

        wm = db.WatchModel()

        if wm.is_watched(user_id, keyword):
            wm.unwatch(user_id, keyword)
            message = "%s removed from watching list." % keyword
        else:
            message = "Keyword %s not found in you're watching list." % keyword

        utils.send_message_with_keyboard(bot, update, message)
Ejemplo n.º 12
0
    def watch(bot, update):
        """Add new item to watchlist"""
        user_id = utils.get_user_id(update)
        keyword = utils.extract_command_value(update)

        wm = db.WatchModel()

        if not wm.is_watched(user_id, keyword):
            wm.watch(user_id, keyword)
            message = "You are now watching %s" % keyword
        else:
            message = "You already watching %s" % keyword

        utils.send_message_with_keyboard(bot, update, message)
Ejemplo n.º 13
0
    def test_watchlist_empty_user_id(self):
        FAKE_USER_ID = ''

        wm = db.WatchModel()
        with pytest.raises(exceptions.NoUserException):
            wm.watchlist(FAKE_USER_ID)
Ejemplo n.º 14
0
 def restart(bot, update):
     db.PostsModel().delete_all()
     db.WatchModel().delete_all()
     db.CityModel().delete_all()
     utils.send_message_with_keyboard(bot, update, "Total reset")