<s> from mock import Mock <EOL> from unittest import TestCase <EOL> from selenium . common . exceptions import WebDriverException <EOL> from bok_choy . query import Query , BrowserQuery <EOL> class TestQuery ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . query = Query ( lambda : range ( <NUM_LIT:5> ) ) <EOL> def test_initial_identify ( self ) : <EOL> self . assertEquals ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , self . query . results ) <EOL> def test_replace ( self ) : <EOL> clone = self . query . replace ( seed_fn = lambda : range ( <NUM_LIT:3> ) ) <EOL> self . assertNotEquals ( id ( clone ) , id ( self . query ) ) <EOL> self . assertEquals ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , clone . results ) <EOL> self . assertEquals ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , self . query . results ) <EOL> clone2 = self . query . replace ( transforms = [ lambda xs : ( x + <NUM_LIT:1> for x in xs ) ] ) <EOL> self . assertNotEquals ( id ( clone2 ) , id ( self . query ) ) <EOL> self . assertEquals ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , clone2 . results ) <EOL> self . assertEquals ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , self . query . results ) <EOL> with self . assertRaises ( TypeError ) : <EOL> self . query . replace ( foo = '<STR_LIT:bar>' ) <EOL> def test_transform ( self ) : <EOL> transformed = self . query . transform ( lambda xs : ( x + <NUM_LIT:1> for x in xs ) ) <EOL> self . assertNotEquals ( id ( transformed ) , id ( self . query ) ) <EOL> self . assertEquals ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , transformed . results ) <EOL> self . assertEquals ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , self . query . results ) <EOL> def test_transforms_stack ( self ) : <EOL> transformed = self . query . transform ( <EOL> lambda xs : ( x + <NUM_LIT:1> for x in xs ) <EOL> ) . transform ( <EOL> lambda xs : ( x * <NUM_LIT:2> for x in xs ) <EOL> ) <EOL> self . assertNotEquals ( id ( transformed ) , id ( self . query ) ) <EOL> self . assertEquals ( [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> , <NUM_LIT:10> ] , transformed . results ) <EOL> self . assertEquals ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , self . query . results ) <EOL> def test_map ( self ) : <EOL> mapped = self . query . map ( lambda x : x + <NUM_LIT:1> ) <EOL> transformed = self . query . transform ( lambda xs : ( x + <NUM_LIT:1> for x in xs ) ) <EOL> self . assertNotEquals ( id ( self . query ) , id ( mapped ) ) <EOL> self . assertEquals ( mapped . results , transformed . results ) <EOL> def test_filter ( self ) : <EOL> filtered = self . query . filter ( lambda x : x % <NUM_LIT:2> == <NUM_LIT:0> ) <EOL> self . assertNotEquals ( id ( self . query ) , id ( filtered ) ) <EOL> self . assertEquals ( [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ] , filtered . results ) <EOL> def test_filter_shortcut ( self ) : <EOL> mapped = self . query . map ( lambda x : Mock ( text = str ( x ) ) ) <EOL> filtered = mapped . filter ( text = "<STR_LIT:3>" ) <EOL> self . assertEquals ( <NUM_LIT:1> , len ( filtered ) ) <EOL> self . assertEquals ( mapped [ <NUM_LIT:3> ] . text , filtered [ <NUM_LIT:0> ] . text ) <EOL> def test_filter_invalid_args ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> self . query . filter ( lambda x : x % <NUM_LIT:2> == <NUM_LIT:0> , text = "<STR_LIT:3>" ) <EOL> with self . assertRaises ( TypeError ) : <EOL> self . query . filter ( ) <EOL> def test_retry_on_error ( self ) : <EOL> seed = Mock ( ) <EOL> seed . side_effect = [ WebDriverException , [ "<STR_LIT:success>" ] ] <EOL> self . assertEqual ( [ "<STR_LIT:success>" ] , Query ( seed_fn = seed ) . results ) <EOL> def test_length ( self ) : <EOL> self . assertEquals ( <NUM_LIT:5> , len ( self . query ) ) <EOL> self . assertEquals ( <NUM_LIT:3> , len ( self . query . filter ( lambda x : x % <NUM_LIT:2> == <NUM_LIT:0> ) ) ) <EOL> def test_present ( self ) : <EOL> self . assertTrue ( self . query . present ) <EOL> self . assertFalse ( self . query . filter ( lambda x : x > <NUM_LIT:10> ) . present ) <EOL> def test_getitem ( self ) : <EOL> self . assertEquals ( <NUM_LIT:3> , self . query [ <NUM_LIT:3> ] ) <EOL> self . assertEquals ( <NUM_LIT:2> , self . query . filter ( lambda x : x % <NUM_LIT:2> == <NUM_LIT:0> ) [ <NUM_LIT:1> ] ) <EOL> def test_repr ( self ) : <EOL> self . assertEquals ( u"<STR_LIT>" , repr ( self . query ) ) <EOL> def integers ( ) : <EOL> return range ( <NUM_LIT:100> ) <EOL> self . assertEquals ( u"<STR_LIT>" , repr ( Query ( integers ) ) ) <EOL> self . assertEquals ( u"<STR_LIT>" , repr ( self . query . map ( lambda x : x + <NUM_LIT:1> ) ) ) <EOL> self . assertEquals ( u"<STR_LIT>" , repr ( self . query . map ( lambda x : x + <NUM_LIT:1> , '<STR_LIT>' ) ) ) <EOL> self . assertEquals ( <EOL> u"<STR_LIT>" , <EOL> repr ( self . query . map ( lambda x : x + <NUM_LIT:1> , '<STR_LIT>' ) . filter ( lambda x : x > <NUM_LIT:2> ) ) <EOL> ) <EOL> self . assertEquals ( <EOL> u"<STR_LIT>" , <EOL> repr ( self . query . transform ( lambda xs : iter ( xs ) . next ( <NUM_LIT:0> ) ) ) <EOL> ) <EOL> self . assertEquals ( <EOL> u"<STR_LIT>" , <EOL> repr ( self . query . filter ( text = '<STR_LIT:foo>' ) ) <EOL> ) <EOL> def test_first ( self ) : <EOL> query = Query ( lambda : range ( <NUM_LIT:2> ) ) <EOL> self . assertEqual ( [ <NUM_LIT:0> ] , query . first . results ) <EOL> self . assertEqual ( [ <NUM_LIT:0> ] , query . first . first . results ) <EOL> def test_first_no_results ( self ) : <EOL> query = Query ( lambda : [ ] ) <EOL> self . assertEqual ( [ ] , query . first . results ) <EOL> def test_nth ( self ) : <EOL> query = Query ( lambda : range ( <NUM_LIT:2> ) ) <EOL> self . assertEqual ( [ ] , query . nth ( - <NUM_LIT:1> ) . results ) <EOL> self . assertEqual ( [ <NUM_LIT:0> ] , query . nth ( <NUM_LIT:0> ) . results ) <EOL> self . assertEqual ( [ <NUM_LIT:1> ] , query . nth ( <NUM_LIT:1> ) . results ) <EOL> self . assertEqual ( [ ] , query . nth ( <NUM_LIT:2> ) . results ) <EOL> class TestBrowserQuery ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . browser = Mock ( <EOL> find_elements_by_css_selector = Mock ( return_value = range ( <NUM_LIT:3> ) ) , <EOL> find_elements_by_xpath = Mock ( return_value = range ( <NUM_LIT:10> ) ) <EOL> ) <EOL> def test_error_cases ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> BrowserQuery ( self . browser , css = '<STR_LIT:foo>' , xpath = '<STR_LIT:bar>' ) <EOL> with self . assertRaises ( TypeError ) : <EOL> BrowserQuery ( self . browser ) <EOL> with self . assertRaises ( TypeError ) : <EOL> BrowserQuery ( self . browser , foo = '<STR_LIT:bar>' ) <EOL> def test_query_args ( self ) : <EOL> self . assertEquals ( <EOL> self . browser . find_elements_by_css_selector . return_value , <EOL> BrowserQuery ( self . browser , css = '<STR_LIT:foo>' ) . results <EOL> ) <EOL> self . assertEquals ( <EOL> self . browser . find_elements_by_xpath . return_value , <EOL> BrowserQuery ( self . browser , xpath = '<STR_LIT:foo>' ) . results <EOL> ) <EOL> def test_repr ( self ) : <EOL> self . assertEquals ( u"<STR_LIT>" , repr ( BrowserQuery ( self . browser , css = '<STR_LIT:foo>' ) ) ) <EOL> self . assertEquals ( u"<STR_LIT>" , repr ( BrowserQuery ( self . browser , xpath = '<STR_LIT:foo>' ) ) ) </s>
