<s> from weblib . etree import parse_html <EOL> from weblib . error import DataNotFound <EOL> from grab . error import GrabMisuseError <EOL> from tempfile import mkstemp <EOL> import os <EOL> import re <EOL> from test . util import build_grab <EOL> from test . util import BaseGrabTestCase <EOL> class GrabApiTestCase ( BaseGrabTestCase ) : <EOL> def setUp ( self ) : <EOL> self . server . reset ( ) <EOL> def test_choose_form_by_element ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> g . choose_form_by_element ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> g . doc ( '<STR_LIT>' ) . node ( ) , <EOL> g . doc . form ) <EOL> def test_choose_form_by_element_noform ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertRaises ( DataNotFound , g . choose_form_by_element , <EOL> '<STR_LIT>' ) <EOL> def test_form_fields ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> fields = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( fields , g . form_fields ( ) ) <EOL> def test_submit ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> g . choose_form ( <NUM_LIT:0> ) <EOL> g . submit ( make_request = False ) <EOL> self . assertTrue ( '<STR_LIT:foo>' in dict ( g . config [ '<STR_LIT>' ] ) ) <EOL> def test_set_input_methods ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> g . set_input_by_id ( '<STR_LIT:f>' , '<STR_LIT>' ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . text ( ) , '<STR_LIT>' ) <EOL> g . set_input_by_number ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . text ( ) , '<STR_LIT>' ) <EOL> g . set_input_by_xpath ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . text ( ) , '<STR_LIT>' ) <EOL> g . set_input ( '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . text ( ) , '<STR_LIT>' ) <EOL> def test_form ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . node ( ) , g . form ) <EOL> def test_load_proxylist_text_file ( self ) : <EOL> fh , path = mkstemp ( ) <EOL> with open ( path , '<STR_LIT:w>' ) as out : <EOL> out . write ( '<STR_LIT>' ) <EOL> g = build_grab ( ) <EOL> g . load_proxylist ( path , '<STR_LIT>' , auto_init = True , auto_change = False ) <EOL> self . assertEqual ( g . config [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> os . unlink ( path ) <EOL> def test_load_proxylist_url ( self ) : <EOL> self . server . response [ '<STR_LIT:data>' ] = b'<STR_LIT>' <EOL> g = build_grab ( ) <EOL> g . load_proxylist ( self . server . get_url ( ) , '<STR_LIT:url>' , <EOL> auto_init = True , auto_change = False ) <EOL> self . assertEqual ( g . config [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_load_proxylist_invalid_input ( self ) : <EOL> g = build_grab ( ) <EOL> self . assertRaises ( GrabMisuseError , g . load_proxylist , <EOL> None , '<STR_LIT>' ) <EOL> def test_response_property ( self ) : <EOL> g = build_grab ( ) <EOL> self . assertEqual ( g . response , g . doc ) <EOL> g . response = g . doc <EOL> self . assertEqual ( g . response , g . doc ) <EOL> def test_pyquery ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . node ( ) . value , <EOL> g . pyquery ( '<STR_LIT:input>' ) [ <NUM_LIT:0> ] . value ) <EOL> def test_assert_xpath ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> g . assert_xpath ( '<STR_LIT>' ) <EOL> self . assertRaises ( DataNotFound , g . assert_xpath , '<STR_LIT>' ) <EOL> def test_assert_css ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> g . assert_css ( '<STR_LIT>' ) <EOL> self . assertRaises ( DataNotFound , g . assert_css , '<STR_LIT>' ) <EOL> def test_deprecated_filter_argument ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertRaises ( GrabMisuseError , g . xpath_number , '<STR_LIT>' , <EOL> filter = lambda : True ) <EOL> self . assertRaises ( GrabMisuseError , g . xpath_text , '<STR_LIT>' , <EOL> filter = lambda : True ) <EOL> self . assertRaises ( GrabMisuseError , g . xpath_list , '<STR_LIT>' , <EOL> filter = lambda : True ) <EOL> self . assertRaises ( GrabMisuseError , g . xpath_one , '<STR_LIT>' , <EOL> filter = lambda : True ) <EOL> self . assertRaises ( GrabMisuseError , g . xpath , '<STR_LIT>' , <EOL> filter = lambda : True ) <EOL> def test_css ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . node ( ) , g . css ( '<STR_LIT>' ) ) <EOL> def test_xpath ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertEqual ( g . doc ( '<STR_LIT>' ) . node ( ) , g . xpath ( '<STR_LIT>' ) ) <EOL> def test_find_link_rex ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> g . find_link_rex ( re . compile ( '<STR_LIT>' ) , <EOL> make_absolute = True ) ) <EOL> self . assertEqual ( None , <EOL> g . find_link_rex ( re . compile ( '<STR_LIT>' ) , <EOL> make_absolute = True ) ) <EOL> def test_find_link ( self ) : <EOL> data = b'''<STR_LIT>''' <EOL> g = build_grab ( data ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> g . find_link ( b'<STR_LIT>' , <EOL> make_absolute = True ) ) <EOL> self . assertEqual ( None , <EOL> g . find_link ( b'<STR_LIT>' , <EOL> make_absolute = True ) ) <EOL> self . assertRaises ( GrabMisuseError , g . find_link , <EOL> u'<STR_LIT>' ) <EOL> def test_build_html_tree ( self ) : <EOL> data = b'<STR_LIT>' <EOL> g = build_grab ( data ) <EOL> self . assertEqual ( g . doc . tree , g . build_html_tree ( ) ) <EOL> def test_build_xml_tree ( self ) : <EOL> data = b'<STR_LIT>' <EOL> g = build_grab ( data , content_type = '<STR_LIT>' ) <EOL> self . assertEqual ( g . doc . tree , g . build_xml_tree ( ) ) </s>
