<s> """<STR_LIT>""" <EOL> import sys <EOL> import unittest <EOL> import requests <EOL> from mock import patch <EOL> import six <EOL> import warnings <EOL> from ads . tests . mocks import MockSolrResponse <EOL> from ads . search import SearchQuery , SolrResponse , APIResponse , Article , query <EOL> from ads . exceptions import APIResponseError , SolrResponseParseError <EOL> from ads . config import SEARCH_URL <EOL> class TestArticle ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . article = Article ( <EOL> bibcode = "<STR_LIT>" , <EOL> year = <NUM_LIT> , <EOL> first_author = "<STR_LIT>" , <EOL> author = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] , <EOL> ) <EOL> def test_equals ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertNotEqual ( Article ( bibcode = "<STR_LIT>" ) , self . article ) <EOL> self . assertEqual ( Article ( bibcode = "<STR_LIT>" ) , self . article ) <EOL> with self . assertRaises ( TypeError ) : <EOL> Article ( ) == self . article <EOL> def test_init ( self ) : <EOL> """<STR_LIT>""" <EOL> for key , value in six . iteritems ( self . article . _raw ) : <EOL> self . assertEqual ( <EOL> self . article . __getattribute__ ( key ) , <EOL> value , <EOL> msg = "<STR_LIT>" . format ( key ) <EOL> ) <EOL> def test_print_methods ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> self . article . __str__ ( ) <EOL> ) <EOL> self . assertEqual ( self . article . __unicode__ ( ) , self . article . __str__ ( ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_cached_properties ( self , patched ) : <EOL> """<STR_LIT>""" <EOL> patched . return_value = "<STR_LIT>" <EOL> self . article . year <EOL> self . assertEqual ( patched . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . article . aff , '<STR_LIT>' ) <EOL> self . assertEqual ( self . article . aff , '<STR_LIT>' ) <EOL> patched . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( patched . call_count , <NUM_LIT:1> ) <EOL> def test_get_field ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaisesRegexp ( <EOL> APIResponseError , <EOL> "<STR_LIT>" ) : <EOL> self . article . _get_field ( '<STR_LIT>' ) <EOL> self . article . id = <NUM_LIT> <EOL> with MockSolrResponse ( SEARCH_URL ) : <EOL> self . assertEqual ( self . article . pubdate , '<STR_LIT>' ) <EOL> self . assertEqual ( self . article . read_count , <NUM_LIT:0.0> ) <EOL> self . assertIsNone ( self . article . issue ) <EOL> class TestSearchQuery ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_rows_rewrite ( self ) : <EOL> """<STR_LIT>""" <EOL> sq = SearchQuery ( q = "<STR_LIT>" , rows = <NUM_LIT> ) <EOL> with MockSolrResponse ( sq . HTTP_ENDPOINT ) : <EOL> self . assertEqual ( sq . query [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> next ( sq ) <EOL> if six . PY3 : <EOL> msg = w [ - <NUM_LIT:1> ] . message . args [ <NUM_LIT:0> ] <EOL> elif six . PY2 : <EOL> msg = w [ - <NUM_LIT:1> ] . message . message <EOL> self . assertEqual ( <EOL> msg , <EOL> "<STR_LIT>" <EOL> ) <EOL> self . assertEqual ( sq . query [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> def test_iter ( self ) : <EOL> """<STR_LIT>""" <EOL> sq = SearchQuery ( q = "<STR_LIT>" , rows = <NUM_LIT:1> , max_pages = <NUM_LIT:20> ) <EOL> with MockSolrResponse ( sq . HTTP_ENDPOINT ) : <EOL> self . assertEqual ( sq . _query [ '<STR_LIT:start>' ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( next ( sq ) . bibcode , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( sq . articles ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( sq . _query [ '<STR_LIT:start>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( next ( sq ) . bibcode , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( list ( sq ) ) , <NUM_LIT> ) <EOL> with self . assertRaisesRegexp ( <EOL> StopIteration , <EOL> "<STR_LIT>" ) : <EOL> next ( sq ) <EOL> sq . max_pages = <NUM_LIT> <EOL> self . assertEqual ( len ( list ( sq ) ) , <NUM_LIT> - <NUM_LIT> - <NUM_LIT:2> ) <EOL> with self . assertRaisesRegexp ( <EOL> StopIteration , <EOL> "<STR_LIT>" ) : <EOL> next ( sq ) <EOL> sq = SearchQuery ( q = "<STR_LIT>" , rows = <NUM_LIT:3> ) <EOL> with MockSolrResponse ( sq . HTTP_ENDPOINT ) : <EOL> self . assertEqual ( len ( list ( sq ) ) , <NUM_LIT:3> ) <EOL> def test_init ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaisesRegexp ( AssertionError , "<STR_LIT>" ) : <EOL> SearchQuery ( ) <EOL> sq = SearchQuery ( q = "<STR_LIT>" ) <EOL> self . assertIn ( "<STR_LIT>" , sq . query [ '<STR_LIT:q>' ] ) <EOL> sq = SearchQuery ( title = "<STR_LIT:t>" , author = "<STR_LIT>" ) <EOL> assertItemsEqual = self . assertItemsEqual if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> else self . assertCountEqual <EOL> assertItemsEqual ( <EOL> sq . query [ '<STR_LIT:q>' ] . split ( ) , <EOL> '<STR_LIT>' . split ( ) , <EOL> ) <EOL> sq = SearchQuery ( q = "<STR_LIT>" , aff = "<STR_LIT>" ) <EOL> assertItemsEqual ( <EOL> sq . query [ '<STR_LIT:q>' ] . split ( ) , <EOL> '<STR_LIT>' . split ( ) , <EOL> ) <EOL> sq = SearchQuery ( q = "<STR_LIT:q>" , token = "<STR_LIT>" ) <EOL> self . assertEqual ( sq . token , "<STR_LIT>" ) <EOL> self . assertEqual ( sq . _token , "<STR_LIT>" ) <EOL> class TestSolrResponse ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> with MockSolrResponse ( '<STR_LIT>' ) : <EOL> self . response = requests . get ( <EOL> '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : [ "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" ] } <EOL> ) <EOL> def test_init ( self ) : <EOL> """<STR_LIT>""" <EOL> sr = SolrResponse ( self . response . text ) <EOL> self . assertIn ( '<STR_LIT>' , sr . json ) <EOL> self . assertIn ( '<STR_LIT>' , sr . json ) <EOL> self . assertEqual ( sr . numFound , <NUM_LIT> ) <EOL> self . assertEqual ( sr . fl , [ "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> malformed_text = self . response . text . replace ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> with self . assertRaises ( SolrResponseParseError ) : <EOL> SolrResponse ( malformed_text ) <EOL> def test_articles ( self ) : <EOL> """<STR_LIT>""" <EOL> sr = SolrResponse ( self . response . text ) <EOL> self . assertIsNone ( sr . _articles ) <EOL> self . assertEqual ( len ( sr . articles ) , <NUM_LIT> ) <EOL> self . assertEqual ( sr . _articles , sr . articles ) <EOL> self . assertEqual ( sr . articles [ <NUM_LIT:0> ] . doi , [ u'<STR_LIT>' ] ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> sr . articles = '<STR_LIT>' <EOL> def test_load_http_response ( self ) : <EOL> """<STR_LIT>""" <EOL> sr = SolrResponse . load_http_response ( self . response ) <EOL> self . assertIsInstance ( sr , SolrResponse ) <EOL> self . assertIsInstance ( sr , APIResponse ) <EOL> self . assertEqual ( sr . response , self . response ) <EOL> self . response . status_code = <NUM_LIT> <EOL> with self . assertRaises ( APIResponseError ) : <EOL> SolrResponse . load_http_response ( self . response ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_default_article_fields ( self , patched ) : <EOL> """<STR_LIT>""" <EOL> sr = SolrResponse ( self . response . text ) <EOL> sr . docs = [ { "<STR_LIT:id>" : <NUM_LIT:1> } ] <EOL> sr . fl = [ '<STR_LIT:id>' , '<STR_LIT>' ] <EOL> self . assertEqual ( sr . articles [ <NUM_LIT:0> ] . id , <NUM_LIT:1> ) <EOL> self . assertEqual ( sr . articles [ <NUM_LIT:0> ] . bibstem , None ) <EOL> self . assertEqual ( patched . call_count , <NUM_LIT:0> ) <EOL> class Testquery ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_init ( self ) : <EOL> """<STR_LIT>""" <EOL> _ = query ( "<STR_LIT>" ) <EOL> self . assertEqual ( _ . _query [ '<STR_LIT:q>' ] , "<STR_LIT>" ) <EOL> _ = query ( { "<STR_LIT:q>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( _ . _query [ '<STR_LIT:q>' ] , "<STR_LIT>" ) <EOL> self . assertIsInstance ( _ , SearchQuery ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( verbosity = <NUM_LIT:2> ) </s>
