<s> from __future__ import print_function <EOL> from __future__ import division <EOL> import mock <EOL> from tests . compat import unittest <EOL> from haxor_news . hacker_news import HackerNews <EOL> from tests . data . comment import formatted_comment , formatted_heading , raw_comment <EOL> from tests . data . item import formatted_items <EOL> from tests . data . markdown import formatted_markdown , raw_markdown <EOL> from tests . data . regex import raw_text_for_regex <EOL> from tests . data . tip import formatted_tip <EOL> from tests . data . title import formatted_title , raw_title <EOL> from tests . mock_hacker_news_api import MockHackerNewsApi <EOL> class HackerNewsTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . hn = HackerNews ( ) <EOL> self . hn . hacker_news_api = MockHackerNewsApi ( ) <EOL> self . limit = len ( self . hn . hacker_news_api . items ) <EOL> self . valid_id = <NUM_LIT:0> <EOL> self . invalid_id = <NUM_LIT> <EOL> self . query = '<STR_LIT:foo>' <EOL> def top ( self , limit = <NUM_LIT:2> ) : <EOL> self . hn . print_items ( <EOL> message = self . headlines_message ( '<STR_LIT>' ) , <EOL> item_ids = self . hn . hacker_news_api . top_stories ( limit ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_ask ( self , mock_print_items ) : <EOL> self . hn . ask ( self . limit ) <EOL> mock_print_items . assert_called_with ( <EOL> message = self . hn . headlines_message ( '<STR_LIT>' ) , <EOL> item_ids = self . hn . hacker_news_api . ask_stories ( self . limit ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_best ( self , mock_print_items ) : <EOL> self . hn . best ( self . limit ) <EOL> mock_print_items . assert_called_with ( <EOL> message = self . hn . headlines_message ( '<STR_LIT>' ) , <EOL> item_ids = self . hn . hacker_news_api . best_stories ( self . limit ) ) <EOL> def test_format_markdown ( self ) : <EOL> result = self . hn . web_viewer . format_markdown ( raw_markdown ) <EOL> assert result == formatted_markdown <EOL> def test_headlines_message ( self ) : <EOL> message = '<STR_LIT:foo>' <EOL> headlines_message = self . hn . headlines_message ( message ) <EOL> assert message in headlines_message <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_hiring_and_freelance ( self , <EOL> mock_print_item_not_found , <EOL> mock_print_comments ) : <EOL> self . hn . hiring_and_freelance ( self . query , post_id = self . valid_id ) <EOL> item = self . hn . hacker_news_api . get_item ( self . valid_id ) <EOL> mock_print_comments . assert_called_with ( <EOL> item , self . query , comments_hide_non_matching = True ) <EOL> self . hn . hiring_and_freelance ( self . query , post_id = self . invalid_id ) <EOL> mock_print_item_not_found . assert_called_with ( self . invalid_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_jobs ( self , mock_print_items ) : <EOL> self . hn . jobs ( self . limit ) <EOL> mock_print_items . assert_called_with ( <EOL> message = self . hn . headlines_message ( '<STR_LIT>' ) , <EOL> item_ids = self . hn . hacker_news_api . ask_stories ( self . limit ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_new ( self , mock_print_items ) : <EOL> self . hn . new ( self . limit ) <EOL> mock_print_items . assert_called_with ( <EOL> message = self . hn . headlines_message ( '<STR_LIT>' ) , <EOL> item_ids = self . hn . hacker_news_api . new_stories ( self . limit ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_onion ( self , mock_click , mock_format_index_title ) : <EOL> self . hn . onion ( self . limit ) <EOL> assert len ( mock_format_index_title . mock_calls ) == self . limit <EOL> assert mock_click . mock_calls <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_show ( self , mock_print_items ) : <EOL> self . hn . show ( self . limit ) <EOL> mock_print_items . assert_called_with ( <EOL> message = self . hn . headlines_message ( '<STR_LIT>' ) , <EOL> item_ids = self . hn . hacker_news_api . show_stories ( self . limit ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_top ( self , mock_print_items ) : <EOL> self . hn . top ( self . limit ) <EOL> mock_print_items . assert_called_with ( <EOL> message = self . hn . headlines_message ( '<STR_LIT>' ) , <EOL> item_ids = self . hn . hacker_news_api . top_stories ( self . limit ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_user ( self , mock_print_item_not_found , <EOL> mock_click , mock_print_items ) : <EOL> user_id = '<STR_LIT:foo>' <EOL> self . hn . user ( user_id , self . limit ) <EOL> user = self . hn . hacker_news_api . get_user ( user_id ) <EOL> mock_print_items . assert_called_with ( <EOL> '<STR_LIT>' , user . submitted [ <NUM_LIT:0> : self . limit ] ) <EOL> assert mock_click . mock_calls <EOL> self . hn . user ( self . invalid_id , self . limit ) <EOL> mock_print_item_not_found . assert_called_with ( self . invalid_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_view_setup_query_recent ( self , mock_view ) : <EOL> index = <NUM_LIT:0> <EOL> comments = False <EOL> comments_recent = True <EOL> comments_unseen = False <EOL> comments_hide_non_matching = False <EOL> comments_clear_cache = False <EOL> browser = False <EOL> self . hn . view_setup ( <EOL> index , self . query , comments , comments_recent , <EOL> comments_unseen , comments_hide_non_matching , <EOL> comments_clear_cache , browser ) <EOL> comments_expected = True <EOL> mock_view . assert_called_with ( <EOL> index , '<STR_LIT>' , comments_expected , <EOL> comments_hide_non_matching , browser ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_view_setup_query_unseen ( self , mock_view ) : <EOL> index = <NUM_LIT:0> <EOL> comments = False <EOL> comments_recent = False <EOL> comments_unseen = True <EOL> comments_hide_non_matching = False <EOL> comments_clear_cache = False <EOL> browser = False <EOL> self . hn . view_setup ( <EOL> index , self . query , comments , comments_recent , <EOL> comments_unseen , comments_hide_non_matching , <EOL> comments_clear_cache , browser ) <EOL> comments_expected = True <EOL> mock_view . assert_called_with ( <EOL> index , self . hn . QUERY_UNSEEN , comments_expected , <EOL> comments_hide_non_matching , browser ) <EOL> def test_format_comment ( self ) : <EOL> item = self . hn . hacker_news_api . get_item ( self . valid_id ) <EOL> item . text = raw_comment <EOL> heading , comment = self . hn . format_comment ( <EOL> item , depth = <NUM_LIT:3> , header_color = '<STR_LIT>' , header_adornment = '<STR_LIT>' ) <EOL> assert heading == formatted_heading <EOL> assert comment == formatted_comment <EOL> def test_format_index_title ( self ) : <EOL> result = self . hn . format_index_title ( <EOL> index = <NUM_LIT:1> , title = raw_title ) <EOL> assert result == formatted_title <EOL> def test_format_item ( self ) : <EOL> items = self . hn . hacker_news_api . items <EOL> for index , item in enumerate ( items ) : <EOL> result = self . hn . format_item ( items [ index ] , index + <NUM_LIT:1> ) <EOL> assert result == formatted_items [ index ] <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_print_comments_unseen ( self , mock_click_echo ) : <EOL> items = self . hn . hacker_news_api . items <EOL> self . hn . print_comments ( items [ <NUM_LIT:0> ] , <EOL> regex_query = self . hn . QUERY_UNSEEN ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_print_comments_unseen_hide_non_matching ( self , <EOL> mock_click_secho , <EOL> mock_click_echo ) : <EOL> items = self . hn . hacker_news_api . items <EOL> self . hn . config . item_cache . extend ( [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] ) <EOL> self . hn . print_comments ( items [ <NUM_LIT:0> ] , <EOL> regex_query = self . hn . QUERY_UNSEEN , <EOL> comments_hide_non_matching = True ) <EOL> mock_click_secho . assert_any_call ( '<STR_LIT:.>' , nl = False ) <EOL> assert mock_click_secho . mock_calls <EOL> assert not mock_click_echo . mock_calls <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_print_comments_regex ( self , mock_click_echo ) : <EOL> items = self . hn . hacker_news_api . items <EOL> regex_query = '<STR_LIT:foo>' <EOL> self . hn . print_comments ( items [ <NUM_LIT:0> ] , regex_query ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_print_comments_regex_hide_non_matching ( self , mock_click_echo ) : <EOL> items = self . hn . hacker_news_api . items <EOL> regex_query = '<STR_LIT:foo>' <EOL> self . hn . print_comments ( items [ <NUM_LIT:0> ] , <EOL> regex_query , <EOL> comments_hide_non_matching = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_print_comments_regex_seen ( self , mock_click_echo ) : <EOL> items = self . hn . hacker_news_api . items <EOL> item = items [ <NUM_LIT:2> ] <EOL> regex_query = '<STR_LIT:foo>' <EOL> self . hn . config . item_cache . append ( str ( item . item_id ) ) <EOL> self . hn . print_comments ( item , regex_query ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> mock_click_echo . assert_any_call ( <EOL> '<STR_LIT>' , color = True ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_print_item_not_found ( self , mock_click ) : <EOL> self . hn . print_item_not_found ( self . invalid_id ) <EOL> mock_click . secho . assert_called_with ( <EOL> '<STR_LIT>' . format ( self . invalid_id ) , fg = '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_print_items ( self , mock_format_item , mock_click ) : <EOL> items = self . hn . hacker_news_api . items <EOL> item_ids = [ item . item_id for item in items ] <EOL> self . hn . print_items ( self . hn . headlines_message ( <EOL> '<STR_LIT>' ) , item_ids ) <EOL> for index , item in enumerate ( items ) : <EOL> assert mock . call ( item , index + <NUM_LIT:1> ) in mock_format_item . mock_calls <EOL> assert mock_click . secho . mock_calls <EOL> def test_print_tip_view ( self ) : <EOL> result = self . hn . tip_view ( max_index = <NUM_LIT:10> ) <EOL> assert result == formatted_tip <EOL> def test_match_comment_unseen ( self ) : <EOL> regex_query = '<STR_LIT>' <EOL> header_adornment = '<STR_LIT>' <EOL> match = self . hn . match_comment_unseen ( regex_query , header_adornment ) <EOL> assert not match <EOL> regex_query = self . hn . QUERY_UNSEEN <EOL> header_adornment = self . hn . COMMENT_UNSEEN <EOL> match = self . hn . match_comment_unseen ( regex_query , header_adornment ) <EOL> assert match <EOL> def test_match_regex ( self ) : <EOL> regex_query = '<STR_LIT>' <EOL> item = self . hn . hacker_news_api . get_item ( self . valid_id ) <EOL> assert not self . hn . match_regex ( item , regex_query ) <EOL> item . text = raw_text_for_regex <EOL> assert self . hn . match_regex ( item , regex_query ) <EOL> def test_match_regex_user ( self ) : <EOL> regex_query = '<STR_LIT:bar>' <EOL> item = self . hn . hacker_news_api . get_item ( self . valid_id ) <EOL> assert not self . hn . match_regex ( item , regex_query ) <EOL> regex_query = '<STR_LIT>' <EOL> assert self . hn . match_regex ( item , regex_query ) <EOL> def test_match_regex_item ( self ) : <EOL> regex_query = '<STR_LIT>' <EOL> item = self . hn . hacker_news_api . get_item ( self . valid_id ) <EOL> assert self . hn . match_regex ( item , regex_query ) <EOL> regex_query = '<STR_LIT>' <EOL> assert not self . hn . match_regex ( item , regex_query ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_view ( self , mock_click , mock_generate_url_contents ) : <EOL> items = self . hn . hacker_news_api . items <EOL> self . hn . config . item_ids = [ int ( item . item_id ) for item in items ] <EOL> one_based_index = self . valid_id + <NUM_LIT:1> <EOL> comments_query = '<STR_LIT>' <EOL> comments = False <EOL> comments_hide_non_matching = False <EOL> browser = False <EOL> self . hn . view ( one_based_index , comments_query , comments , <EOL> comments_hide_non_matching , browser ) <EOL> mock_generate_url_contents . assert_called_with ( <EOL> items [ self . valid_id ] . url ) <EOL> assert mock_click . secho . mock_calls <EOL> assert mock_click . echo_via_pager . mock_calls <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_view_comments ( self , mock_click , mock_print_comments ) : <EOL> items = self . hn . hacker_news_api . items <EOL> self . hn . config . item_ids = [ int ( item . item_id ) for item in items ] <EOL> one_based_index = self . valid_id + <NUM_LIT:1> <EOL> comments_query = '<STR_LIT:foo>' <EOL> comments = True <EOL> comments_hide_non_matching = False <EOL> browser = False <EOL> self . hn . view ( one_based_index , comments_query , comments , <EOL> comments_hide_non_matching , browser ) <EOL> mock_print_comments . assert_called_with ( <EOL> items [ self . valid_id ] , <EOL> comments_hide_non_matching = False , <EOL> regex_query = comments_query ) <EOL> assert mock_click . mock_calls <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_view_no_url ( self , mock_click , mock_print_comments ) : <EOL> self . hn . hacker_news_api . items [ <NUM_LIT:0> ] . url = None <EOL> items = self . hn . hacker_news_api . items <EOL> self . hn . config . item_ids = [ int ( item . item_id ) for item in items ] <EOL> one_based_index = self . valid_id + <NUM_LIT:1> <EOL> comments_query = '<STR_LIT:foo>' <EOL> comments = False <EOL> comments_hide_non_matching = False <EOL> browser = False <EOL> self . hn . view ( one_based_index , comments_query , comments , <EOL> comments_hide_non_matching , browser ) <EOL> mock_print_comments . assert_called_with ( <EOL> items [ self . valid_id ] , <EOL> comments_hide_non_matching = False , <EOL> regex_query = comments_query ) <EOL> assert mock_click . mock_calls <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_view_browser_url ( self , mock_click , mock_webbrowser ) : <EOL> items = self . hn . hacker_news_api . items <EOL> self . hn . config . item_ids = [ int ( item . item_id ) for item in items ] <EOL> one_based_index = self . valid_id + <NUM_LIT:1> <EOL> comments_query = '<STR_LIT:foo>' <EOL> comments = False <EOL> comments_hide_non_matching = False <EOL> browser = True <EOL> self . hn . view ( one_based_index , comments_query , comments , <EOL> comments_hide_non_matching , browser ) <EOL> mock_webbrowser . open . assert_called_with ( items [ self . valid_id ] . url ) <EOL> assert mock_click . mock_calls <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_view_browser_comments ( self , mock_click , mock_webbrowser ) : <EOL> items = self . hn . hacker_news_api . items <EOL> self . hn . config . item_ids = [ int ( item . item_id ) for item in items ] <EOL> one_based_index = self . valid_id + <NUM_LIT:1> <EOL> comments_query = '<STR_LIT:foo>' <EOL> comments = True <EOL> comments_hide_non_matching = False <EOL> browser = True <EOL> self . hn . view ( one_based_index , comments_query , comments , <EOL> comments_hide_non_matching , browser ) <EOL> item = items [ self . valid_id ] <EOL> comments_url = ( '<STR_LIT>' + <EOL> str ( item . item_id ) ) <EOL> mock_webbrowser . open . assert_called_with ( comments_url ) <EOL> assert mock_click . mock_calls </s>
