Skip to content

Commit

Permalink
Make Pickle test use recorded data (#3137)
Browse files Browse the repository at this point in the history
Otherwise fetching repo data might be defered by rate limit and CI is
blocked.
  • Loading branch information
EnricoMi authored Jan 8, 2025
1 parent c5ab18f commit 1990eb9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
10 changes: 6 additions & 4 deletions tests/Connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@


class RecordingMockConnection(Framework.RecordingConnection):
def __init__(self, file, protocol, host, port, realConnection):
def __init__(self, protocol, host, port, realConnection):
self._realConnection = realConnection
super().__init__(file, protocol, host, port)
super().__init__(protocol, host, port)


@pytest.mark.parametrize(
Expand All @@ -88,7 +88,8 @@ def testRecordAndReplay(replaying_connection_class, protocol, response_body, exp
connection.getresponse.return_value = response

# write mock response to buffer
recording_connection = RecordingMockConnection(file, protocol, host, None, lambda *args, **kwds: connection)
RecordingMockConnection.setOpenFile(lambda slf, mode: file)
recording_connection = RecordingMockConnection(protocol, host, None, lambda *args, **kwds: connection)
recording_connection.request(verb, url, None, headers)
recording_connection.getresponse()
recording_connection.close()
Expand All @@ -106,7 +107,8 @@ def testRecordAndReplay(replaying_connection_class, protocol, response_body, exp

# rewind buffer and attempt to replay response from it
file.seek(0)
replaying_connection = replaying_connection_class(file, host=host, port=None)
replaying_connection_class.setOpenFile(lambda slf, mode: file)
replaying_connection = replaying_connection_class(host=host, port=None)
replaying_connection.request(verb, url, None, headers)
replaying_connection.getresponse()

Expand Down
48 changes: 31 additions & 17 deletions tests/Framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,16 @@ def fixAuthorizationHeader(headers):


class RecordingConnection:
def __init__(self, file, protocol, host, port, *args, **kwds):
__openFile = None

@staticmethod
def setOpenFile(func):
RecordingConnection.__openFile = func

def __init__(self, protocol, host, port, *args, **kwds):
self.__file = self.__openFile("w")
# write operations make the assumption that the file is not in binary mode
assert isinstance(file, io.TextIOBase)
self.__file = file
assert isinstance(self.__file, io.TextIOBase)
self.__protocol = protocol
self.__host = host
self.__port = port
Expand Down Expand Up @@ -194,20 +200,26 @@ def __writeLine(self, line):
class RecordingHttpConnection(RecordingConnection):
_realConnection = github.Requester.HTTPRequestsConnectionClass

def __init__(self, file, *args, **kwds):
super().__init__(file, "http", *args, **kwds)
def __init__(self, *args, **kwds):
super().__init__("http", *args, **kwds)


class RecordingHttpsConnection(RecordingConnection):
_realConnection = github.Requester.HTTPSRequestsConnectionClass

def __init__(self, file, *args, **kwds):
super().__init__(file, "https", *args, **kwds)
def __init__(self, *args, **kwds):
super().__init__("https", *args, **kwds)


class ReplayingConnection:
def __init__(self, file, protocol, host, port, *args, **kwds):
self.__file = file
__openFile = None

@staticmethod
def setOpenFile(func):
ReplayingConnection.__openFile = func

def __init__(self, protocol, host, port, *args, **kwds):
self.__file = self.__openFile("r")
self.__protocol = protocol
self.__host = host
self.__port = port
Expand Down Expand Up @@ -283,15 +295,15 @@ def close(self):
class ReplayingHttpConnection(ReplayingConnection):
_realConnection = github.Requester.HTTPRequestsConnectionClass

def __init__(self, file, *args, **kwds):
super().__init__(file, "http", *args, **kwds)
def __init__(self, *args, **kwds):
super().__init__("http", *args, **kwds)


class ReplayingHttpsConnection(ReplayingConnection):
_realConnection = github.Requester.HTTPSRequestsConnectionClass

def __init__(self, file, *args, **kwds):
super().__init__(file, "https", *args, **kwds)
def __init__(self, *args, **kwds):
super().__init__("https", *args, **kwds)


class BasicTestCase(unittest.TestCase):
Expand All @@ -312,9 +324,10 @@ def setUp(self):
if (
self.recordMode
): # pragma no cover (Branch useful only when recording new tests, not used during automated tests)
RecordingConnection.setOpenFile(self.__openFile)
github.Requester.Requester.injectConnectionClasses(
lambda ignored, *args, **kwds: RecordingHttpConnection(self.__openFile("w"), *args, **kwds),
lambda ignored, *args, **kwds: RecordingHttpsConnection(self.__openFile("w"), *args, **kwds),
RecordingHttpConnection,
RecordingHttpsConnection,
)
import GithubCredentials # type: ignore

Expand All @@ -333,9 +346,10 @@ def setUp(self):
else None
)
else:
ReplayingConnection.setOpenFile(self.__openFile)
github.Requester.Requester.injectConnectionClasses(
lambda ignored, *args, **kwds: ReplayingHttpConnection(self.__openFile("r"), *args, **kwds),
lambda ignored, *args, **kwds: ReplayingHttpsConnection(self.__openFile("r"), *args, **kwds),
ReplayingHttpConnection,
ReplayingHttpsConnection,
)
self.login = github.Auth.Login("login", "password")
self.oauth_token = github.Auth.Token("oauth_token")
Expand Down
5 changes: 3 additions & 2 deletions tests/Pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
################################################################################

import pickle
import unittest

import github
from github.PaginatedList import PaginatedList
from github.Repository import Repository

from . import Framework

REPO_NAME = "PyGithub/PyGithub"


class Pickle(unittest.TestCase):
class Pickle(Framework.TestCase):
def testPickleGithub(self):
gh = github.Github()
gh2 = pickle.loads(pickle.dumps(gh))
Expand Down
10 changes: 10 additions & 0 deletions tests/ReplayData/Pickle.testPickleRepository.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
https
GET
api.github.com
None
/repos/PyGithub/PyGithub
{'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Fri, 25 Aug 2023 22:18:41 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Cache-Control', 'public, max-age=60, s-maxage=60'), ('Vary', 'Accept, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"30c1df371a44c445b7125f2d17df44062f98c1725ddf212dd09cabdd8fde9389"'), ('Last-Modified', 'Fri, 25 Aug 2023 15:12:40 GMT'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-RateLimit-Limit', '60'), ('X-RateLimit-Remaining', '52'), ('X-RateLimit-Reset', '1693004247'), ('X-RateLimit-Resource', 'core'), ('X-RateLimit-Used', '8'), ('Accept-Ranges', 'bytes'), ('Content-Length', '1427'), ('X-GitHub-Request-Id', '9DE4:9CCE:5DA8631:5E9093E:64E928C0')]
{"id":3544490,"node_id":"MDEwOlJlcG9zaXRvcnkzNTQ0NDkw","name":"PyGithub","full_name":"PyGithub/PyGithub","private":false,"owner":{"login":"PyGithub","id":11288996,"node_id":"MDEyOk9yZ2FuaXphdGlvbjExMjg4OTk2","avatar_url":"https://avatars.githubusercontent.com/u/11288996?v=4","gravatar_id":"","url":"https://api.github.com/users/PyGithub","html_url":"https://github.com/PyGithub","followers_url":"https://api.github.com/users/PyGithub/followers","following_url":"https://api.github.com/users/PyGithub/following{/other_user}","gists_url":"https://api.github.com/users/PyGithub/gists{/gist_id}","starred_url":"https://api.github.com/users/PyGithub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PyGithub/subscriptions","organizations_url":"https://api.github.com/users/PyGithub/orgs","repos_url":"https://api.github.com/users/PyGithub/repos","events_url":"https://api.github.com/users/PyGithub/events{/privacy}","received_events_url":"https://api.github.com/users/PyGithub/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/PyGithub/PyGithub","description":"Typed interactions with the GitHub API v3","fork":false,"url":"https://api.github.com/repos/PyGithub/PyGithub","forks_url":"https://api.github.com/repos/PyGithub/PyGithub/forks","keys_url":"https://api.github.com/repos/PyGithub/PyGithub/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PyGithub/PyGithub/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PyGithub/PyGithub/teams","hooks_url":"https://api.github.com/repos/PyGithub/PyGithub/hooks","issue_events_url":"https://api.github.com/repos/PyGithub/PyGithub/issues/events{/number}","events_url":"https://api.github.com/repos/PyGithub/PyGithub/events","assignees_url":"https://api.github.com/repos/PyGithub/PyGithub/assignees{/user}","branches_url":"https://api.github.com/repos/PyGithub/PyGithub/branches{/branch}","tags_url":"https://api.github.com/repos/PyGithub/PyGithub/tags","blobs_url":"https://api.github.com/repos/PyGithub/PyGithub/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PyGithub/PyGithub/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PyGithub/PyGithub/git/refs{/sha}","trees_url":"https://api.github.com/repos/PyGithub/PyGithub/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PyGithub/PyGithub/statuses/{sha}","languages_url":"https://api.github.com/repos/PyGithub/PyGithub/languages","stargazers_url":"https://api.github.com/repos/PyGithub/PyGithub/stargazers","contributors_url":"https://api.github.com/repos/PyGithub/PyGithub/contributors","subscribers_url":"https://api.github.com/repos/PyGithub/PyGithub/subscribers","subscription_url":"https://api.github.com/repos/PyGithub/PyGithub/subscription","commits_url":"https://api.github.com/repos/PyGithub/PyGithub/commits{/sha}","git_commits_url":"https://api.github.com/repos/PyGithub/PyGithub/git/commits{/sha}","comments_url":"https://api.github.com/repos/PyGithub/PyGithub/comments{/number}","issue_comment_url":"https://api.github.com/repos/PyGithub/PyGithub/issues/comments{/number}","contents_url":"https://api.github.com/repos/PyGithub/PyGithub/contents/{+path}","compare_url":"https://api.github.com/repos/PyGithub/PyGithub/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PyGithub/PyGithub/merges","archive_url":"https://api.github.com/repos/PyGithub/PyGithub/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PyGithub/PyGithub/downloads","issues_url":"https://api.github.com/repos/PyGithub/PyGithub/issues{/number}","pulls_url":"https://api.github.com/repos/PyGithub/PyGithub/pulls{/number}","milestones_url":"https://api.github.com/repos/PyGithub/PyGithub/milestones{/number}","notifications_url":"https://api.github.com/repos/PyGithub/PyGithub/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PyGithub/PyGithub/labels{/name}","releases_url":"https://api.github.com/repos/PyGithub/PyGithub/releases{/id}","deployments_url":"https://api.github.com/repos/PyGithub/PyGithub/deployments","created_at":"2012-02-25T12:53:47Z","updated_at":"2023-08-25T15:12:40Z","pushed_at":"2023-08-25T22:01:27Z","git_url":"git://github.com/PyGithub/PyGithub.git","ssh_url":"[email protected]:PyGithub/PyGithub.git","clone_url":"https://github.com/PyGithub/PyGithub.git","svn_url":"https://github.com/PyGithub/PyGithub","homepage":"https://pygithub.readthedocs.io/","size":13634,"stargazers_count":6209,"watchers_count":6209,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"has_discussions":true,"forks_count":1662,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":286,"license":{"key":"lgpl-3.0","name":"GNU Lesser General Public License v3.0","spdx_id":"LGPL-3.0","url":"https://api.github.com/licenses/lgpl-3.0","node_id":"MDc6TGljZW5zZTEy"},"allow_forking":true,"is_template":false,"web_commit_signoff_required":false,"topics":["github","github-api","pygithub","python"],"visibility":"public","forks":1662,"open_issues":286,"watchers":6209,"default_branch":"main","temp_clone_token":null,"organization":{"login":"PyGithub","id":11288996,"node_id":"MDEyOk9yZ2FuaXphdGlvbjExMjg4OTk2","avatar_url":"https://avatars.githubusercontent.com/u/11288996?v=4","gravatar_id":"","url":"https://api.github.com/users/PyGithub","html_url":"https://github.com/PyGithub","followers_url":"https://api.github.com/users/PyGithub/followers","following_url":"https://api.github.com/users/PyGithub/following{/other_user}","gists_url":"https://api.github.com/users/PyGithub/gists{/gist_id}","starred_url":"https://api.github.com/users/PyGithub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PyGithub/subscriptions","organizations_url":"https://api.github.com/users/PyGithub/orgs","repos_url":"https://api.github.com/users/PyGithub/repos","events_url":"https://api.github.com/users/PyGithub/events{/privacy}","received_events_url":"https://api.github.com/users/PyGithub/received_events","type":"Organization","site_admin":false},"network_count":1662,"subscribers_count":112}

0 comments on commit 1990eb9

Please sign in to comment.