Ejemplo n.º 1
0
    def test_update_with_no_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock,)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        mock.assert_called_once_with(**extra_kwargs)
Ejemplo n.º 2
0
    def test_update_with_no_args(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock, )

        timeline.update()

        mock.assert_called_once_with()
Ejemplo n.º 3
0
    def test_update_with_no_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock, )
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        mock.assert_called_once_with(**extra_kwargs)
Ejemplo n.º 4
0
    def test_is_messages_timeline(self):
        a_timeline = Timeline()
        self.assertFalse(is_messages_timeline(a_timeline))

        messages_timeline = Timeline(
            update_function=mock_api.get_direct_messages)
        self.assertTrue(is_messages_timeline(messages_timeline))
Ejemplo n.º 5
0
    def test_update_with_one_arg(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock, update_function_args=arg)
        timeline.update()

        mock.assert_called_once_with(arg)
Ejemplo n.º 6
0
    def test_update_with_no_args(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock,)

        timeline.update()

        mock.assert_called_once_with()
Ejemplo n.º 7
0
    def test_update_with_one_arg_extra_kwargs(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock, update_function_args=arg)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        mock.assert_called_once_with(arg, **extra_kwargs)
Ejemplo n.º 8
0
    def test_update_with_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)
        timeline.update()

        mock.assert_called_once_with(**kwargs)
Ejemplo n.º 9
0
    def test_update_with_multiple_args(self):
        mock = MagicMock(name='update')
        args = '#python', '#mock'
        timeline = Timeline(update_function=mock, update_function_args=args)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args)
Ejemplo n.º 10
0
    def test_update_with_one_arg(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock, 
                            update_function_args=arg)
        timeline.update()

        mock.assert_called_once_with(arg)
Ejemplo n.º 11
0
    def test_update_with_one_arg_extra_kwargs(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock, update_function_args=arg)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        mock.assert_called_once_with(arg, **extra_kwargs)
Ejemplo n.º 12
0
    def test_update_with_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)
        timeline.update()

        mock.assert_called_once_with(**kwargs)
Ejemplo n.º 13
0
    def test_update_with_multiple_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = ('#python', '#mock')
        timeline = Timeline(update_function=mock, update_function_args=args)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        args = list(args)
        mock.assert_called_once_with(*args, **extra_kwargs)
Ejemplo n.º 14
0
    def test_update_with_multiple_args(self):
        mock = MagicMock(name='update')
        args = '#python', '#mock'
        timeline = Timeline(update_function=mock,
                            update_function_args=args)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args)
Ejemplo n.º 15
0
    def test_update_with_multiple_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = ('#python', '#mock')
        timeline = Timeline(update_function=mock, 
                            update_function_args=args)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        args = list(args)
        mock.assert_called_once_with(*args, **extra_kwargs)
Ejemplo n.º 16
0
    def test_update_with_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock, 
                            update_function_kwargs=kwargs)
        extra_kwargs = {'text': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        args = kwargs.copy()
        args.update(extra_kwargs)
        mock.assert_called_once_with(**args)
Ejemplo n.º 17
0
    def test_update_with_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)
        extra_kwargs = {'text': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        args = kwargs.copy()
        args.update(extra_kwargs)
        mock.assert_called_once_with(**args)
Ejemplo n.º 18
0
    def test_statuses_ordered_reversely_by_date(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        # ordered
        self.timeline = Timeline(statuses=[new_status, old_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)

        # unordered
        self.timeline = Timeline(statuses=[old_status, new_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)
Ejemplo n.º 19
0
    def test_update_with_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        update_args = list(args)
        update_args.append(kwargs)
        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args, **kwargs)
Ejemplo n.º 20
0
    def test_statuses_ordered_reversely_by_date(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        # ordered
        self.timeline = Timeline(statuses=[new_status, old_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)

        # unordered
        self.timeline = Timeline(statuses=[old_status, new_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)
Ejemplo n.º 21
0
    def test_update_with_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42 
        kwargs = {'text': '#python', 'action': 'search'}
        update_args = list(args)
        update_args.append(kwargs)
        timeline = Timeline(update_function=mock, 
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args, **kwargs)
Ejemplo n.º 22
0
    def test_update_with_args_and_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        extra_kwargs = {'text': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update(**extra_kwargs)

        args = list(args)
        kwargs = kwargs.copy()
        kwargs.update(extra_kwargs)
        mock.assert_called_once_with(*args, **kwargs)
Ejemplo n.º 23
0
    def test_update_with_args_and_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        extra_kwargs = {'text': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update(**extra_kwargs)

        args = list(args)
        kwargs = kwargs.copy()
        kwargs.update(extra_kwargs)
        mock.assert_called_once_with(*args, **kwargs)
Ejemplo n.º 24
0
    def test_extract_with_no_args(self):
        mock = MagicMock(name='update')

        timeline = Timeline(update_function=mock, )

        self.assertEqual(timeline._args, [])
        self.assertEqual(timeline._kwargs, {})
Ejemplo n.º 25
0
    def thread(self, status):
        """
        Create a timeline with the conversation to which `status` belongs.
        `status` can be a regular status or a direct message.
        """
        if is_DM(status):
            participants = [
                status.sender_screen_name, status.recipient_screen_name
            ]
            name = _('DM thread: %s' % ', '.join(participants))
            update_function = self.api.get_message_thread
        else:
            participants = status.mentioned_usernames
            author = status.authors_username
            if author not in participants:
                participants.insert(0, author)

            name = _('thread: %s' % ', '.join(participants))
            update_function = self.api.get_thread

        return Timeline(
            name=name,
            update_function=update_function,
            update_function_args=status,
        )
Ejemplo n.º 26
0
    def test_extract_with_only_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'python': 'rocks'}
        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline.update_function_args, None)
        self.assertEqual(timeline.update_function_kwargs, kwargs)
Ejemplo n.º 27
0
 def test_active_returns_first_appended(self):
     # append
     name = 'Timeline'
     timeline = Timeline(name)
     self.timeline_list.append_timeline(timeline)
     # assert
     active_timeline = self.timeline_list.active
     self.assertEqual(timeline, active_timeline)
Ejemplo n.º 28
0
 def append_timeline(self,
                     name,
                     update_function,
                     update_args=None,
                     update_kwargs=None):
     """
     Given a name, function to update a timeline and optionally
     arguments to the update function, it creates the timeline and
     appends it to `timelines`.
     """
     timeline = Timeline(name=name,
                         update_function=update_function,
                         update_function_args=update_args,
                         update_function_kwargs=update_kwargs)
     timeline.update()
     timeline.activate_first()
     self.timelines.append_timeline(timeline)
Ejemplo n.º 29
0
    def test_only_args(self):
        mock = MagicMock(name='update')
        args = 'python', 42

        timeline = Timeline(update_function=mock,
                            update_function_args=args,)

        self.assertEqual(timeline._args, list(args))
        self.assertEqual(timeline._kwargs, {})
Ejemplo n.º 30
0
    def test_extract_with_both_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'python', 42
        kwargs = {'python': 'rocks'}
        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline.update_function_args, list(args))
        self.assertEqual(timeline.update_function_kwargs, kwargs)
Ejemplo n.º 31
0
    def __call__(self, timeline_string):
        timeline = timeline_string.strip()

        if timeline == HOME_TIMELINE:
            return Timeline(
                name=_('tweets'),
                update_function=self.api.get_home_timeline,
            )
        elif timeline == MENTIONS_TIMELINE:
            return Timeline(
                name=_('mentions'),
                update_function=self.api.get_mentions,
            )
        elif timeline == FAVORITES_TIMELINE:
            return Timeline(
                name=_('favorites'),
                update_function=self.api.get_favorites,
            )
        elif timeline == MESSAGES_TIMELINE:
            return Timeline(
                name=_('messages'),
                update_function=self.api.get_direct_messages,
            )
        elif timeline == OWN_TWEETS_TIMELINE:
            return Timeline(
                name=_('me'),
                update_function=self.api.get_own_timeline,
            )
        elif timeline == 'retweets_of_me':
            return Timeline(
                name=_('retweets of me'),
                update_function=self.api.get_retweets_of_me,
            )

        is_search = search_name_re.match(timeline)
        if is_search:
            query = is_search.groupdict()['query']
            return Timeline(
                name=_('Search: %s' % query),
                update_function=self.api.search,
                update_function_args=query,
            )

        is_user = user_name_re.match(timeline)
        if is_user:
            screen_name = is_user.groupdict()['screen_name']
            timeline_name = _('@{screen_name}'.format(screen_name=screen_name))
            return Timeline(
                name=timeline_name,
                update_function=self.api.get_user_timeline,
                update_function_args=screen_name,
            )
Ejemplo n.º 32
0
 def append_timeline(self,
                     name,
                     update_function,
                     update_args=None,
                     update_kwargs=None):
     """
     Given a name, function to update a timeline and optionally
     arguments to the update function, it creates the timeline and
     appends it to `timelines`.
     """
     timeline = Timeline(name=name,
                         update_function=update_function,
                         update_function_args=update_args,
                         update_function_kwargs=update_kwargs)
     timeline.update()
     timeline.activate_first()
     self.timelines.append_timeline(timeline)
Ejemplo n.º 33
0
class TimelineTest(unittest.TestCase):
    def setUp(self):
        self.timeline = Timeline()
        self.timeline.clear()
        self.assertEqual(len(self.timeline), 0)
        self.assertEqual(self.timeline.active_index, ActiveList.NULL_INDEX)

    def test_unique_statuses_in_timeline(self):
        self.assertEqual(len(self.timeline), 0)
        # create and add the status
        status = create_status()
        self.timeline.add_status(status)
        self.assertEqual(len(self.timeline), 1)
        # check that adding more than once does not duplicate element
        self.timeline.add_status(status)
        self.assertEqual(len(self.timeline), 1)

    def test_active_index_becomes_0_when_adding_first_status(self):
        status = create_status()
        self.timeline.add_status(status)
        self.assertEqual(self.timeline.active_index, 0)
        # check that adding than once does not move the active
        self.timeline.add_status(status)
        self.assertEqual(self.timeline.active_index, 0)

    def test_insert_different_statuses(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)
        self.timeline.add_statuses([old_status, new_status])
        self.assertEqual(len(self.timeline), 2)

    def assert_active(self, status):
        active_status = self.timeline.get_active()
        if active_status:
            self.assertEqual(status, active_status)
        else:
            raise Exception("There is no active status")

    def test_active_is_the_same_when_inserting_statuses(self):
        """
        Test that when inserting new statuses the active doesn't change.
        """
        active_status = create_status(created_at=datetime(1988, 12, 19))
        self.timeline.add_status(active_status)
        self.assert_active(active_status)
        
        older_status = create_status(id=2, 
                                     created_at=datetime(1978, 12, 19))
        self.timeline.add_status(older_status)
        self.assert_active(active_status)

        newer_status = create_status(id=2)
        self.timeline.add_status(newer_status)
        self.assert_active(active_status)

    def test_insert_different_statuses_individually(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)
        self.timeline.add_status(old_status)
        self.assertEqual(len(self.timeline), 1)
        self.timeline.add_status(new_status)
        self.assertEqual(len(self.timeline), 2)

    def test_statuses_ordered_reversely_by_date(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)
        self.timeline.add_statuses([old_status, new_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)

    def test_get_newer_than(self):
        old_created_at = datetime(1988, 12, 19)
        old_status = create_status(created_at=old_created_at)
        new_created_at = datetime.now()
        new_status = create_status(id=2,
                                   created_at=new_created_at)
        self.timeline.add_statuses([old_status, new_status])
        # get newers than `old_status`
        newers = self.timeline.get_newer_than(old_created_at)
        self.assertEqual(len(newers), 1)
        self.assertEqual(newers[0].id, new_status.id)
        # get newers than `new_status`
        newers = self.timeline.get_newer_than(new_created_at)
        self.assertEqual(len(newers), 0)

    def test_clear(self):
        old_created_at = datetime(1988, 12, 19)
        old_status = create_status(created_at=old_created_at)
        new_created_at = datetime.now()
        new_status = create_status(id=2, 
                                   created_at=new_created_at)
        self.timeline.add_statuses([old_status, new_status])
        self.timeline.clear()
        self.assertEqual(len(self.timeline), 0)
        # add them again and check that they are inserted back
        self.timeline.add_statuses([old_status, new_status])
        self.assertEqual(len(self.timeline), 2)

    def test_get_unread_count(self):
        self.assertEqual(self.timeline.get_unread_count(), 0)

        # a status
        status = create_status(id=1)
        self.timeline.add_status(status)
        self.assertEqual(self.timeline.get_unread_count(), 1)

        self.timeline.mark_all_as_read()
        self.assertEqual(self.timeline.get_unread_count(), 0)
        
        # new statuses
        statuses = [create_status(id=id_num) for id_num in xrange(2, 10)]
        self.timeline.add_statuses(statuses)
        self.assertEqual(self.timeline.get_unread_count(), len(statuses))

        self.timeline.mark_all_as_read()
        self.assertEqual(self.timeline.get_unread_count(), 0)

    # update function related

    def test_extract_with_no_args(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock,)

        self.assertEqual(timeline.update_function_args, None)
        self.assertEqual(timeline.update_function_kwargs, None)

    def test_extract_with_only_args(self):
        mock = MagicMock(name='update')
        args = 'python', 42
        timeline = Timeline(update_function=mock,
                            update_function_args=args,)

        self.assertEqual(timeline.update_function_args, list(args))
        self.assertEqual(timeline.update_function_kwargs, None)

    def test_extract_with_only_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'python': 'rocks'}
        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline.update_function_args, None)
        self.assertEqual(timeline.update_function_kwargs, kwargs)

    def test_extract_with_both_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'python', 42
        kwargs = {'python': 'rocks'}
        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline.update_function_args, list(args))
        self.assertEqual(timeline.update_function_kwargs, kwargs)

    def test_update_with_no_args(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock,)
        timeline.update()
        mock.assert_called_once_with()

    def test_update_with_one_arg(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock, 
                            update_function_args=arg)
        timeline.update()

        mock.assert_called_once_with(arg)

    def test_update_with_multiple_args(self):
        mock = MagicMock(name='update')
        args = '#python', '#mock'
        timeline = Timeline(update_function=mock, 
                            update_function_args=args)
        timeline.update()
        args = list(args)

        mock.assert_called_once_with(*args)

    def test_update_with_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock, 
                            update_function_kwargs=kwargs)
        timeline.update()

        mock.assert_called_once_with(**kwargs)

    def test_update_with_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42 
        kwargs = {'text': '#python', 'action': 'search'}
        update_args = list(args)
        update_args.append(kwargs)
        timeline = Timeline(update_function=mock, 
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args, **kwargs)

    def test_update_with_no_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock,)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        mock.assert_called_once_with(**extra_kwargs)

    def test_update_with_one_arg_extra_kwargs(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock, update_function_args=arg)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        mock.assert_called_once_with(arg, **extra_kwargs)

    def test_update_with_multiple_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = ('#python', '#mock')
        timeline = Timeline(update_function=mock, 
                            update_function_args=args)
        extra_kwargs = {'python': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        args = list(args)
        mock.assert_called_once_with(*args, **extra_kwargs)

    def test_update_with_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock, 
                            update_function_kwargs=kwargs)
        extra_kwargs = {'text': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        args = kwargs.copy()
        args.update(extra_kwargs)
        mock.assert_called_once_with(**args)

    def test_update_with_args_and_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock, 
                            update_function_args=args,
                            update_function_kwargs=kwargs)

        extra_kwargs = {'text': 'rocks'}
        timeline.update_with_extra_kwargs(**extra_kwargs)

        args = list(args)
        kwargs = kwargs.copy()
        kwargs.update(extra_kwargs)
        mock.assert_called_once_with(*args, **kwargs)
Ejemplo n.º 34
0
 def setUp(self):
     self.timeline = Timeline()
     self.timeline.clear()
     self.assertEqual(len(self.timeline), 0)
     self.assertEqual(self.timeline.active_index, ActiveList.NULL_INDEX)
Ejemplo n.º 35
0
    def test_active_index_is_0_when_creating_timeline_with_statuses(self):
        status = create_status()

        self.timeline = Timeline(statuses=[status])

        self.assertEqual(self.timeline.active_index, 0)
Ejemplo n.º 36
0
 def setUp(self):
     self.timeline = Timeline()
     self.assert_null_index()
Ejemplo n.º 37
0
class TimelineTest(ActiveListTest):

    def setUp(self):
        self.timeline = Timeline()
        self.assert_null_index()

    def active_index(self):
        return self.timeline.active_index

    # unique elements

    def test_unique_statuses_in_timeline(self):
        status = create_status()

        self.timeline.add_status(status)
        self.timeline.add_status(status)

        self.assertEqual(len(self.timeline), 1)

    # active index

    def test_active_index_is_0_when_creating_timeline_with_statuses(self):
        status = create_status()

        self.timeline = Timeline(statuses=[status])

        self.assertEqual(self.timeline.active_index, 0)

    def test_active_index_becomes_0_when_adding_first_status(self):
        status = create_status()

        self.timeline.add_status(status)

        self.assertEqual(self.timeline.active_index, 0)

    def test_active_index_does_not_change_when_adding_various_statuses(self):
        a_status = create_status(id=1)
        another_status = create_status(id=2)

        # first
        self.timeline.add_status(a_status)
        self.assertEqual(self.timeline.active_index, 0)

        # second
        self.timeline.add_status(another_status)
        self.assertEqual(self.timeline.active_index, 0)

    # insertion

    def test_insert_different_statuses_individually(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        # first
        self.timeline.add_status(old_status)
        self.assertEqual(len(self.timeline), 1)

        # second
        self.timeline.add_status(new_status)
        self.assertEqual(len(self.timeline), 2)

    def test_insert_different_statuses(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        self.timeline.add_statuses([old_status, new_status])

        self.assertEqual(len(self.timeline), 2)

    # order

    def test_statuses_ordered_reversely_by_date(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        # ordered
        self.timeline = Timeline(statuses=[new_status, old_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)

        # unordered
        self.timeline = Timeline(statuses=[old_status, new_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)

    # unread

    def test_unread_count(self):
        self.assertEqual(self.timeline.unread_count, 0)

        # a status
        status = create_status(id=1)
        self.timeline.add_status(status)
        self.assertEqual(self.timeline.unread_count, 1)

        self.timeline.mark_all_as_read()
        self.assertEqual(self.timeline.unread_count, 0)

        # new statuses
        statuses = [create_status(id=id_num) for id_num in xrange(2, 10)]
        self.timeline.add_statuses(statuses)
        self.assertEqual(self.timeline.unread_count, len(statuses))

        self.timeline.mark_all_as_read()
        self.assertEqual(self.timeline.unread_count, 0)

    # clear

    def test_clear(self):
        old_created_at = datetime(1988, 12, 19)
        old_status = create_status(created_at=old_created_at)
        new_created_at = datetime.now()
        new_status = create_status(id=2,
                                   created_at=new_created_at)

        self.timeline.add_statuses([old_status, new_status])

        self.timeline.clear()

        self.assertEqual(len(self.timeline), 0)

    # update function related

    def test_extract_with_no_args(self):
        mock = MagicMock(name='update')

        timeline = Timeline(update_function=mock,)

        self.assertEqual(timeline._args, [])
        self.assertEqual(timeline._kwargs, {})

    def test_only_args(self):
        mock = MagicMock(name='update')
        args = 'python', 42

        timeline = Timeline(update_function=mock,
                            update_function_args=args,)

        self.assertEqual(timeline._args, list(args))
        self.assertEqual(timeline._kwargs, {})

    def test_with_only_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline._args, [])
        self.assertEqual(timeline._kwargs, kwargs)

    def test_with_both_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'python', 42
        kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline._args, list(args))
        self.assertEqual(timeline._kwargs, kwargs)

    # update invocation

    def test_update_with_no_args(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock,)

        timeline.update()

        mock.assert_called_once_with()

    def test_update_with_one_arg(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock,
                            update_function_args=arg)

        timeline.update()

        mock.assert_called_once_with(arg)

    def test_update_with_multiple_args(self):
        mock = MagicMock(name='update')
        args = '#python', '#mock'
        timeline = Timeline(update_function=mock,
                            update_function_args=args)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args)

    def test_update_with_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)
        timeline.update()

        mock.assert_called_once_with(**kwargs)

    def test_update_with_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        update_args = list(args)
        update_args.append(kwargs)

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args, **kwargs)

    def test_update_with_no_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        extra_kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock,)
        timeline.update(**extra_kwargs)

        mock.assert_called_once_with(**extra_kwargs)

    def test_update_with_one_arg_extra_kwargs(self):
        mock = MagicMock(name='update')
        arg = '#python'
        extra_kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock, update_function_args=arg)
        timeline.update(**extra_kwargs)

        mock.assert_called_once_with(arg, **extra_kwargs)

    def test_update_with_multiple_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = ('#python', '#mock')
        extra_kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_args=args)
        timeline.update(**extra_kwargs)

        args = list(args)
        mock.assert_called_once_with(*args, **extra_kwargs)

    def test_update_with_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        extra_kwargs = {'text': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)
        timeline.update(**extra_kwargs)

        args = kwargs.copy()
        args.update(extra_kwargs)
        mock.assert_called_once_with(**args)

    def test_update_with_args_and_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        extra_kwargs = {'text': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update(**extra_kwargs)

        args = list(args)
        kwargs = kwargs.copy()
        kwargs.update(extra_kwargs)
        mock.assert_called_once_with(*args, **kwargs)
Ejemplo n.º 38
0
    def test_is_search_timeline(self):
        a_timeline = Timeline()
        self.assertFalse(is_search_timeline(a_timeline))

        search_timeline = Timeline(update_function=mock_api.search)
        self.assertTrue(is_search_timeline(search_timeline))
Ejemplo n.º 39
0
    def test_is_own_timeline(self):
        a_timeline = Timeline()
        self.assertFalse(is_own_timeline(a_timeline))

        own_timeline = Timeline(update_function=mock_api.get_own_timeline)
        self.assertTrue(is_own_timeline(own_timeline))
Ejemplo n.º 40
0
    def test_is_favorites_timeline(self):
        a_timeline = Timeline()
        self.assertFalse(is_favorites_timeline(a_timeline))

        favorites_timeline = Timeline(update_function=mock_api.get_favorites)
        self.assertTrue(is_favorites_timeline(favorites_timeline))
Ejemplo n.º 41
0
    def test_is_thread_timeline(self):
        a_timeline = Timeline()
        self.assertFalse(is_thread_timeline(a_timeline))

        thread_timeline = Timeline(update_function=mock_api.get_thread)
        self.assertTrue(is_thread_timeline(thread_timeline))
Ejemplo n.º 42
0
    def test_is_retweets_of_me_timeline(self):
        a_timeline = Timeline()
        self.assertFalse(is_retweets_of_me_timeline(a_timeline))

        rts_timeline = Timeline(update_function=mock_api.get_retweets_of_me)
        self.assertTrue(is_retweets_of_me_timeline(rts_timeline))
Ejemplo n.º 43
0
class TimelineTest(ActiveListTest):
    def setUp(self):
        self.timeline = Timeline()
        self.assert_null_index()

    def active_index(self):
        return self.timeline.active_index

    # unique elements

    def test_unique_statuses_in_timeline(self):
        status = create_status()

        self.timeline.add_status(status)
        self.timeline.add_status(status)

        self.assertEqual(len(self.timeline), 1)

    # active index

    def test_active_index_is_0_when_creating_timeline_with_statuses(self):
        status = create_status()

        self.timeline = Timeline(statuses=[status])

        self.assertEqual(self.timeline.active_index, 0)

    def test_active_index_becomes_0_when_adding_first_status(self):
        status = create_status()

        self.timeline.add_status(status)

        self.assertEqual(self.timeline.active_index, 0)

    def test_active_index_does_not_change_when_adding_various_statuses(self):
        a_status = create_status(id=1)
        another_status = create_status(id=2)

        # first
        self.timeline.add_status(a_status)
        self.assertEqual(self.timeline.active_index, 0)

        # second
        self.timeline.add_status(another_status)
        self.assertEqual(self.timeline.active_index, 0)

    # insertion

    def test_insert_different_statuses_individually(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        # first
        self.timeline.add_status(old_status)
        self.assertEqual(len(self.timeline), 1)

        # second
        self.timeline.add_status(new_status)
        self.assertEqual(len(self.timeline), 2)

    def test_insert_different_statuses(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        self.timeline.add_statuses([old_status, new_status])

        self.assertEqual(len(self.timeline), 2)

    # order

    def test_statuses_ordered_reversely_by_date(self):
        old_status = create_status(created_at=datetime(1988, 12, 19))
        new_status = create_status(id=2)

        # ordered
        self.timeline = Timeline(statuses=[new_status, old_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)

        # unordered
        self.timeline = Timeline(statuses=[old_status, new_status])
        self.assertEqual(self.timeline[0], new_status)
        self.assertEqual(self.timeline[1], old_status)

    # unread

    def test_unread_count(self):
        self.assertEqual(self.timeline.unread_count, 0)

        # a status
        status = create_status(id=1)
        self.timeline.add_status(status)
        self.assertEqual(self.timeline.unread_count, 1)

        self.timeline.mark_all_as_read()
        self.assertEqual(self.timeline.unread_count, 0)

        # new statuses
        statuses = [create_status(id=id_num) for id_num in xrange(2, 10)]
        self.timeline.add_statuses(statuses)
        self.assertEqual(self.timeline.unread_count, len(statuses))

        self.timeline.mark_all_as_read()
        self.assertEqual(self.timeline.unread_count, 0)

    # clear

    def test_clear(self):
        old_created_at = datetime(1988, 12, 19)
        old_status = create_status(created_at=old_created_at)
        new_created_at = datetime.now()
        new_status = create_status(id=2, created_at=new_created_at)

        self.timeline.add_statuses([old_status, new_status])

        self.timeline.clear()

        self.assertEqual(len(self.timeline), 0)

    # update function related

    def test_extract_with_no_args(self):
        mock = MagicMock(name='update')

        timeline = Timeline(update_function=mock, )

        self.assertEqual(timeline._args, [])
        self.assertEqual(timeline._kwargs, {})

    def test_only_args(self):
        mock = MagicMock(name='update')
        args = 'python', 42

        timeline = Timeline(
            update_function=mock,
            update_function_args=args,
        )

        self.assertEqual(timeline._args, list(args))
        self.assertEqual(timeline._kwargs, {})

    def test_with_only_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline._args, [])
        self.assertEqual(timeline._kwargs, kwargs)

    def test_with_both_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'python', 42
        kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)

        self.assertEqual(timeline._args, list(args))
        self.assertEqual(timeline._kwargs, kwargs)

    # update invocation

    def test_update_with_no_args(self):
        mock = MagicMock(name='update')
        timeline = Timeline(update_function=mock, )

        timeline.update()

        mock.assert_called_once_with()

    def test_update_with_one_arg(self):
        mock = MagicMock(name='update')
        arg = '#python'
        timeline = Timeline(update_function=mock, update_function_args=arg)

        timeline.update()

        mock.assert_called_once_with(arg)

    def test_update_with_multiple_args(self):
        mock = MagicMock(name='update')
        args = '#python', '#mock'
        timeline = Timeline(update_function=mock, update_function_args=args)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args)

    def test_update_with_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)
        timeline.update()

        mock.assert_called_once_with(**kwargs)

    def test_update_with_args_and_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        update_args = list(args)
        update_args.append(kwargs)

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update()

        args = list(args)
        mock.assert_called_once_with(*args, **kwargs)

    def test_update_with_no_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        extra_kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock, )
        timeline.update(**extra_kwargs)

        mock.assert_called_once_with(**extra_kwargs)

    def test_update_with_one_arg_extra_kwargs(self):
        mock = MagicMock(name='update')
        arg = '#python'
        extra_kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock, update_function_args=arg)
        timeline.update(**extra_kwargs)

        mock.assert_called_once_with(arg, **extra_kwargs)

    def test_update_with_multiple_args_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = ('#python', '#mock')
        extra_kwargs = {'python': 'rocks'}

        timeline = Timeline(update_function=mock, update_function_args=args)
        timeline.update(**extra_kwargs)

        args = list(args)
        mock.assert_called_once_with(*args, **extra_kwargs)

    def test_update_with_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        kwargs = {'text': '#python', 'action': 'search'}
        extra_kwargs = {'text': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_kwargs=kwargs)
        timeline.update(**extra_kwargs)

        args = kwargs.copy()
        args.update(extra_kwargs)
        mock.assert_called_once_with(**args)

    def test_update_with_args_and_kwargs_extra_kwargs(self):
        mock = MagicMock(name='update')
        args = 'twitter', 42
        kwargs = {'text': '#python', 'action': 'search'}
        extra_kwargs = {'text': 'rocks'}

        timeline = Timeline(update_function=mock,
                            update_function_args=args,
                            update_function_kwargs=kwargs)
        timeline.update(**extra_kwargs)

        args = list(args)
        kwargs = kwargs.copy()
        kwargs.update(extra_kwargs)
        mock.assert_called_once_with(*args, **kwargs)
Ejemplo n.º 44
0
 def setUp(self):
     self.timeline = Timeline()
     self.assert_null_index()
Ejemplo n.º 45
0
 def append_timeline(self):
     self.timeline_list.append_timeline(Timeline('Timeline'))
Ejemplo n.º 46
0
    def test_active_index_is_0_when_creating_timeline_with_statuses(self):
        status = create_status()

        self.timeline = Timeline(statuses=[status])

        self.assertEqual(self.timeline.active_index, 0)
Ejemplo n.º 47
0
 def setUp(self):
     self.timeline = Timeline()
     self.timeline.clear()
     self.assertEqual(len(self.timeline), 0)
     self.assertEqual(self.timeline.active_index, ActiveList.NULL_INDEX)