def test_http_wrong_status(self): """Test if a error is raised when the http status was not 200""" issue = "" httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=500, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) client = GitHubClient("zhquan_example", "repo", "aaa", None) with self.assertRaises(requests.exceptions.HTTPError): _ = [issues for issues in client.get_issues()] # Check requests expected = { 'per_page': ['30'], 'state': ['all'], 'direction': ['asc'], 'sort': ['updated'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_get_empty_issues(self): """ Test when issue is empty API call """ issue = read_file('data/github_empty_request') httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) client = GitHubClient("zhquan_example", "repo", "aaa", None) raw_issues = [issues for issues in client.get_issues()] self.assertEqual(raw_issues[0], issue) # Check requests expected = { 'per_page': ['30'], 'state': ['all'], 'direction': ['asc'], 'sort': ['updated'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_get_from_date_issues(self): """ Test get_from_issues API call """ issue = read_file('data/github_request_from_2016_03_01') httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) from_date = datetime.datetime(2016, 3, 1) client = GitHubClient("zhquan_example", "repo", "aaa", None) raw_issues = [issues for issues in client.get_issues(from_date)] self.assertEqual(len(raw_issues), 1) self.assertEqual(raw_issues[0], issue) # Check requests expected = { 'per_page': ['30'], 'state': ['all'], 'direction': ['asc'], 'since': ['2016-03-01T00:00:00'], 'sort': ['updated'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_rate_limit_error(self): """ Test get_page_issue API call """ issue = read_file('data/github_empty_request') httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL, body=issue, status=200, forcing_headers={ 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': '0', 'Link': '<' + GITHUB_ISSUES_URL + '/?&page=2>; rel="next", <' + GITHUB_ISSUES_URL + '/?&page=3>; rel="last"' }) client = GitHubClient("zhquan_example", "repo", "aaa", sleep_for_rate=False) with self.assertRaises(RateLimitError): _ = [issues for issues in client.get_issues()] # Check requests expected = { 'per_page': ['30'], 'state': ['all'], 'direction': ['asc'], 'sort': ['updated'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_sleep_for_rate(self): """ Test get_page_issue API call """ issue_1 = read_file('data/github_empty_request') issue_2 = read_file('data/github_empty_request') wait = 1 reset = int(time.time() + wait) httpretty.register_uri( httpretty.GET, GITHUB_ISSUES_URL, body=issue_1, status=200, forcing_headers={ 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': reset, 'Link': '<' + GITHUB_ISSUES_URL + '/?&page=2>; rel="next", <' + GITHUB_ISSUES_URL + '/?&page=3>; rel="last"' }) httpretty.register_uri(httpretty.GET, GITHUB_ISSUES_URL + '/?&page=2', body=issue_2, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) client = GitHubClient("zhquan_example", "repo", "aaa", sleep_for_rate=True) before = int(time.time()) issues = [issues for issues in client.get_issues()] after = int(time.time()) dif = after - before self.assertGreaterEqual(dif, wait) self.assertEqual(len(issues), 2) self.assertEqual(issues[0], issue_1) self.assertEqual(issues[1], issue_2) # Check requests expected = { 'per_page': ['30'], 'page': ['2'], 'state': ['all'], 'direction': ['asc'], 'sort': ['updated'] } self.assertDictEqual(httpretty.last_request().querystring, expected) self.assertEqual(httpretty.last_request().headers["Authorization"], "token aaa")
def test_get_user(self): """ Test get_user API call """ login = read_file('data/github_login') httpretty.register_uri(httpretty.GET, GITHUB_USER_URL, body=login, status=200, forcing_headers={ 'X-RateLimit-Remaining': '20', 'X-RateLimit-Reset': '15' }) client = GitHubClient("zhquan_example", "repo", "aaa", None) response = client.get_user("zhquan_example") self.assertEqual(response, login)