def test_csv_serialization(): csv_filename = get_temp_test_file_name('csv') tweets_collector = st.CollectorTweetOutput() get_tweets_to_tweet_output_test( [st.CsvTweetOutput(csv_filename), tweets_collector]) tweets_from_csv = st.read_tweets_from_csv_file(csv_filename) two_lists_assert_equal(tweets_from_csv, tweets_collector.get_raw_list())
def test_file_json_lines_serialization(): jl_filename = get_temp_test_file_name('jl') tweets_collector = st.CollectorTweetOutput() get_tweets_to_tweet_output_test( [st.JsonLineFileTweetOutput(jl_filename), tweets_collector]) tweets_from_jl = st.read_tweets_from_json_lines_file(jl_filename) two_lists_assert_equal(tweets_from_jl, tweets_collector.get_raw_list())
def test_print_all_tweet_output(): captured_output = StringIO() sys.stdout = captured_output tweets_collector = st.CollectorTweetOutput() get_tweets_to_tweet_output_test([st.PrintTweetOutput(), tweets_collector]) sys.stdout = sys.__stdout__ assert captured_output.getvalue().count('Tweet(') == len( tweets_collector.get_raw_list())
def test_print_each_n_tweet_tweet_output(): captured_output = StringIO() each_n = 7 sys.stdout = captured_output tweet_output_counter = TweetOutputTweetsCounter() get_tweets_to_tweet_output_test( [st.PrintEveryNTweetOutput(each_n), tweet_output_counter]) sys.stdout = sys.__stdout__ print_tweet_count = captured_output.getvalue().count('Tweet(') assert print_tweet_count == int( tweet_output_counter.get_output_call_count() / each_n)
def test_print_batch_single_tweet_tweet_output(): captured_output = StringIO() sys.stdout = captured_output tweet_output_counter = TweetOutputExportCallCounter() get_tweets_to_tweet_output_test([ st.PrintFirstInRequestTweetOutput(), tweet_output_counter ]) sys.stdout = sys.__stdout__ print_tweet_count = captured_output.getvalue().count('Tweet(') print_no_tweets_line = captured_output.getvalue().count('PrintFirstInRequestTweetOutput -- no tweets to print') assert (print_tweet_count + print_no_tweets_line) == tweet_output_counter.get_output_call_count()
def test_tweet_csv_read_iterator(): file_name = get_temp_test_file_name('csv') collector = st.CollectorTweetOutput() get_tweets_to_tweet_output_test([collector, st.CsvTweetOutput(file_name)]) iterator = st.TweetCsvFileIterator(file_name, 4) list_from_iterator = [] iterator.open() while True: try: list_from_iterator.extend(next(iterator)) except StopIteration: break two_lists_assert_equal(list_from_iterator, collector.get_raw_list())
def test_tweet_json_lines_read_iterator(): file_name = get_temp_test_file_name('jl') collector = st.CollectorTweetOutput() get_tweets_to_tweet_output_test([collector, st.JsonLineFileTweetOutput(file_name)]) iterator = st.TweetJsonLineFileIterator(file_name, 4) list_from_iterator = [] iterator.open() while True: try: list_from_iterator.extend(next(iterator)) except StopIteration: break iterator.close() two_lists_assert_equal(list_from_iterator, collector.get_raw_list())