def test_initialization(self):
        """Test whether attributes are initializated"""

        twitter = Twitter('query',
                          'my-token',
                          max_items=5,
                          tag='test',
                          sleep_for_rate=True,
                          min_rate_to_sleep=10,
                          sleep_time=60)

        self.assertEqual(twitter.origin, 'https://twitter.com/')
        self.assertEqual(twitter.tag, 'test')
        self.assertEqual(twitter.query, 'query')
        self.assertEqual(twitter.max_items, 5)
        self.assertTrue(twitter.sleep_for_rate)
        self.assertEqual(twitter.min_rate_to_sleep, 10)
        self.assertEqual(twitter.sleep_time, 60)
        self.assertIsNone(twitter.client)
        self.assertTrue(twitter.ssl_verify)

        # When tag is empty or None it will be set to the value in URL
        twitter = Twitter('query', 'my-token', ssl_verify=False)
        self.assertEqual(twitter.origin, 'https://twitter.com/')
        self.assertEqual(twitter.tag, 'https://twitter.com/')
        self.assertFalse(twitter.ssl_verify)

        twitter = Twitter('query', 'my-token', tag='')
        self.assertEqual(twitter.origin, 'https://twitter.com/')
        self.assertEqual(twitter.tag, 'https://twitter.com/')
 def setUp(self):
     super().setUp()
     self.backend_write_archive = Twitter('query',
                                          'my-token',
                                          archive=self.archive)
     self.backend_read_archive = Twitter('query',
                                         'my-token',
                                         archive=self.archive)
    def test_fetch_no_tweets(self):
        """Test whether an empty list is returned if no tweets are available"""

        setup_http_server(no_tweets=True)

        twitter = Twitter('query', 'my-token', max_items=2)
        tweets = [tweets for tweets in twitter.fetch()]

        self.assertEqual(tweets, [])
    def test_initialization_long_query(self):
        """Test whether an exception is thrown when the search query is too long"""

        long_query = ''.join(['a' for i in range(0, 500)])
        with self.assertRaises(BackendError):
            _ = Twitter(long_query, 'my-token', max_items=5, tag='test',
                        sleep_for_rate=True, min_rate_to_sleep=10, sleep_time=60)
    def test_fetch_tweets(self):
        """Test whether a list of tweets is returned"""

        setup_http_server()
        twitter = Twitter('query', 'my-token', max_items=2)
        tweets = [tweets for tweets in twitter.fetch()]

        expected = ['1005149094560530432', '1005148958111555584', '1005277806383673344', '1005163131042193408']

        self.assertEqual(len(tweets), 4)
        for i in range(len(tweets)):
            tweet = tweets[i]
            self.assertEqual(tweet['data']['id_str'], expected[i])
            self.assertEqual(tweet['origin'], 'https://twitter.com/')
            self.assertEqual(tweet['updated_on'], str_to_datetime(tweet['data']['created_at']).timestamp())
            self.assertEqual(tweet['category'], 'tweet')
            self.assertEqual(tweet['tag'], 'https://twitter.com/')
    def test_search_fields(self):
        """Test whether the search_fields is properly set"""

        setup_http_server()
        twitter = Twitter('query', 'my-token', max_items=2)
        tweets = [tweets for tweets in twitter.fetch()]

        tweet = tweets[0]
        hashtags = [
            h['text'] for h in tweet['data']['entities'].get('hashtags', [])
        ]
        self.assertEqual(twitter.metadata_id(tweet['data']),
                         tweet['search_fields']['item_id'])
        self.assertListEqual(hashtags, ['openexpoeurope'])
        self.assertListEqual(tweet['search_fields']['hashtags'],
                             ['openexpoeurope'])

        tweet = tweets[1]
        hashtags = [
            h['text'] for h in tweet['data']['entities'].get('hashtags', [])
        ]
        self.assertEqual(twitter.metadata_id(tweet['data']),
                         tweet['search_fields']['item_id'])
        self.assertListEqual(hashtags, ['OpenExpo18'])
        self.assertListEqual(tweet['search_fields']['hashtags'],
                             ['OpenExpo18'])

        tweet = tweets[2]
        hashtags = [
            h['text'] for h in tweet['data']['entities'].get('hashtags', [])
        ]
        self.assertEqual(twitter.metadata_id(tweet['data']),
                         tweet['search_fields']['item_id'])
        self.assertListEqual(hashtags, ['structure', 'community'])
        self.assertListEqual(tweet['search_fields']['hashtags'],
                             ['structure', 'community'])
    def test_has_archiving(self):
        """Test if it returns True when has_archiving is called"""

        self.assertEqual(Twitter.has_archiving(), True)
    def test_has_resuming(self):
        """Test if it returns False when has_resuming is called"""

        self.assertEqual(Twitter.has_resuming(), False)