def test_patient_urlopen(self, mocked_time): sleeps = [] def mocked_sleeper(seconds): sleeps.append(seconds) mocked_time.sleep = mocked_sleeper mock_calls = [] @stringioify def mocked_urlopener(url): mock_calls.append(url) if len(mock_calls) == 1: raise urllib2.HTTPError(url, 500, "Server Error", {}, None) if len(mock_calls) == 2: raise urllib2.HTTPError(url, 504, "Timeout", {}, None) if len(mock_calls) == 3: raise urllib2.URLError("BadStatusLine") return "<html>content</html>" self.urllib2.side_effect = mocked_urlopener content = ftpscraper.patient_urlopen('http://doesntmatt.er', sleep_time=25) self.assertEqual(content, "<html>content</html>") self.assertEqual(sleeps, [25, 25, 25])
def test_patient_urlopen(self, mocked_time): sleeps = [] def mocked_sleeper(seconds): sleeps.append(seconds) mocked_time.sleep = mocked_sleeper mock_calls = [] @stringioify def mocked_urlopener(url): mock_calls.append(url) if len(mock_calls) == 1: raise urllib2.HTTPError(url, 500, "Server Error", {}, None) if len(mock_calls) == 2: raise urllib2.HTTPError(url, 504, "Timeout", {}, None) if len(mock_calls) == 3: raise urllib2.URLError("BadStatusLine") return "<html>content</html>" self.urllib2.side_effect = mocked_urlopener content = ftpscraper.patient_urlopen( 'http://doesntmatt.er', sleep_time=25 ) eq_(content, "<html>content</html>") eq_(sleeps, [25, 25, 25])
def test_patient_urlopen_pass_404_errors(self): mock_calls = [] @stringioify def mocked_urlopener(url): mock_calls.append(url) raise urllib2.HTTPError(url, 404, "Not Found", {}, None) self.urllib2.side_effect = mocked_urlopener response = ftpscraper.patient_urlopen('http://doesntmatt.er') self.assertEqual(response, None) assert len(mock_calls) == 1, mock_calls
def test_patient_urlopen_pass_404_errors(self): mock_calls = [] @stringioify def mocked_urlopener(url): mock_calls.append(url) raise urllib2.HTTPError(url, 404, "Not Found", {}, None) self.urllib2.side_effect = mocked_urlopener response = ftpscraper.patient_urlopen('http://doesntmatt.er') eq_(response, None) assert len(mock_calls) == 1, mock_calls