def testShouldParseLocationsFromResponseBody(self): # Given... prefs_body = r'''<html><body><table><thead><tr id="dpref_driver_pk__preferences_pk__driver_locations_pk__header"><th>Default</th><th>Name</th><th>Description</th><th></th></tr></thead><tbody id="dpref_driver_pk__preferences_pk__driver_locations_pk__noprofiles" style="display: none; "><tr><td colspan="4">You have no saved locations.</td></tr></tbody><tbody id="dpref_driver_pk__preferences_pk__driver_locations_pk__profiles"><tr class=""><td><input type="radio" class="profile_default" value="18065565" checked="checked"></td><td class="profile_name">My House</td><td class="profile_descr"></td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr><tr class="zebra"><td><input type="radio" class="profile_default" value="25782103"></td><td class="profile_name">My Job</td><td class="profile_descr">Walnut St & S 33rd St, Philadelphia, PA 19104, USA</td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr><tr class=""><td><input type="radio" class="profile_default" value="17966898"></td><td class="profile_name">Sprucemont</td><td class="profile_descr"></td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr><tr class="zebra"><td><input type="radio" class="profile_default" value="25618502"></td><td class="profile_name">UPenn Library</td><td class="profile_descr">Walnut St & S 36th St, Philadelphia, PA 19104, USA</td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr></tbody><tbody id="dpref_driver_pk__preferences_pk__driver_locations_pk__favourites"><tr class=""><td><input type="radio" class="profile_default" value="0"></td><td colspan="2">Favorites</td><td></td></tr><tr class="zebra"><td><input type="hidden" value="2041034"></td><td colspan="2"> 47th & Baltimore - Scion xB</td><td><a href="javascript:void(0);" class="delete_favourite">Delete</a></td></tr><tr class=""><td><input type="hidden" value="4756298"></td><td colspan="2"> 47th & Baltimore - Sienna Minivan</td><td><a href="javascript:void(0);" class="delete_favourite">Delete</a></td></tr></tbody></table></body></html>''' source = LocationsScreenscrapeSource() # When... locations = source.parse_locations_from_preferences_body(prefs_body) # Then... self.assertEqual(len(locations), 4)
def testShouldReturnRequestedCustomLocation(self): # Given... source = LocationsScreenscrapeSource() # When... location = source.fetch_custom_location('Custom Location', (123,456)) # Then... self.assertEqual(location.name, 'Custom Location') self.assertEqual(location.latitude, 123) self.assertEqual(location.longitude, 456)
def testShouldRaiseAnErrorWhenNoProfilesTbodyInResponse(self): # Given... prefs_body = r'Nobody' source = LocationsScreenscrapeSource() # When... try: locations = source.parse_locations_from_preferences_body(prefs_body) # Then... except ScreenscrapeParseError: return self.fail('Should have raised an exception')
def testShouldRecognizeWhenSessionHasExpiredWhileFetchingLocProfiles(self): from strings_for_testing import EXPIRED_PASSWORD_LOGIN_FORM source = LocationsScreenscrapeSource() @patch(source) def get_preferences_response(self, conn, sessionid): return EXPIRED_PASSWORD_LOGIN_FORM, None expected_code = 'invalid_session' try: sessionid = None source.fetch_location_profiles(sessionid) except ScreenscrapeFetchError, sfe: self.assertEqual(sfe.code, expected_code, 'Fetch has incorrect code: %r, expected %r' % (sfe.code, expected_code)) return
def testPreferencesResponseShouldBeAsExpectedFromConnection(self): # Given... class StubConnection (object): def request(self, url, method, data, headers): import StringIO response = StringIO.StringIO('MyBody') response.getheaders = lambda: {'h1':1} return response StubConnection = Stub(PcsConnection)(StubConnection) conn = StubConnection() source = LocationsScreenscrapeSource() # When... response_body, response_headers = \ source.get_preferences_response(conn=conn, sessionid='123abc') # Then... self.assertEqual(response_body, 'MyBody') self.assertEqual(response_headers, {'h1':1})
def testShouldReturnRequestedLocationProfile(self): # Given... class StubConnection (object): def request(self, url, method, data, headers): import StringIO response = StringIO.StringIO(r'''<table><thead><tr id="dpref_driver_pk__preferences_pk__driver_locations_pk__header"><th>Default</th><th>Name</th><th>Description</th><th></th></tr></thead><tbody id="dpref_driver_pk__preferences_pk__driver_locations_pk__noprofiles" style="display: none; "><tr><td colspan="4">You have no saved locations.</td></tr></tbody><tbody id="dpref_driver_pk__preferences_pk__driver_locations_pk__profiles"><tr class=""><td><input type="radio" class="profile_default" value="18065565" checked="checked"></td><td class="profile_name">My House</td><td class="profile_descr"></td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr><tr class="zebra"><td><input type="radio" class="profile_default" value="25782103"></td><td class="profile_name">My Job</td><td class="profile_descr">Walnut St & S 33rd St, Philadelphia, PA 19104, USA</td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr><tr class=""><td><input type="radio" class="profile_default" value="17966898"></td><td class="profile_name">Sprucemont</td><td class="profile_descr"></td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr><tr class="zebra"><td><input type="radio" class="profile_default" value="25618502"></td><td class="profile_name">UPenn Library</td><td class="profile_descr">Walnut St & S 36th St, Philadelphia, PA 19104, USA</td><td><a href="javascript:void(0);" class="profile_name">Edit</a> <a href="javascript:void(0);" class="delete_profile">Delete</a></td></tr></tbody><tbody id="dpref_driver_pk__preferences_pk__driver_locations_pk__favourites"><tr class=""><td><input type="radio" class="profile_default" value="0"></td><td colspan="2">Favorites</td><td></td></tr><tr class="zebra"><td><input type="hidden" value="2041034"></td><td colspan="2"> 47th & Baltimore - Scion xB</td><td><a href="javascript:void(0);" class="delete_favourite">Delete</a></td></tr><tr class=""><td><input type="hidden" value="4756298"></td><td colspan="2"> 47th & Baltimore - Sienna Minivan</td><td><a href="javascript:void(0);" class="delete_favourite">Delete</a></td></tr></tbody></table>''') response.getheaders = lambda: {'h1':1} return response StubConnection = Stub(PcsConnection)(StubConnection) source = LocationsScreenscrapeSource() @patch(source) def create_connection(self): return StubConnection() # When... location = source.fetch_location_profile('123abc', '25782103') # Then... self.assertEqual(location.name, 'My Job') self.assertEqual(location.desc, 'Walnut St & S 33rd St, Philadelphia, PA 19104, USA') self.assertEqual(location.id, '25782103')