File: Pickle.py

package info (click to toggle)
pygithub 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,476 kB
  • sloc: python: 38,409; sh: 7; makefile: 6
file content (92 lines) | stat: -rw-r--r-- 4,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
############################ Copyrights and license ############################
#                                                                              #
# Copyright 2023 Andrew Dawes <53574062+AndrewJDawes@users.noreply.github.com> #
# Copyright 2023 Enrico Minack <github@enrico.minack.dev>                      #
# Copyright 2023 Hemslo Wang <hemslo.wang@gmail.com>                           #
# Copyright 2024 Enrico Minack <github@enrico.minack.dev>                      #
# Copyright 2025 Enrico Minack <github@enrico.minack.dev>                      #
#                                                                              #
# This file is part of PyGithub.                                               #
# http://pygithub.readthedocs.io/                                              #
#                                                                              #
# PyGithub is free software: you can redistribute it and/or modify it under    #
# the terms of the GNU Lesser General Public License as published by the Free  #
# Software Foundation, either version 3 of the License, or (at your option)    #
# any later version.                                                           #
#                                                                              #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details.                                                                     #
#                                                                              #
# You should have received a copy of the GNU Lesser General Public License     #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
#                                                                              #
################################################################################

import inspect
import pickle
import sys
from abc import ABC

import github
from github.Auth import AppAuth, AppAuthToken, AppInstallationAuth, AppUserAuth, Auth, Login, NetrcAuth, Token
from github.PaginatedList import PaginatedList
from github.Repository import Repository

from . import Framework

REPO_NAME = "PyGithub/PyGithub"


class Pickle(Framework.TestCase):
    def testPickleGithub(self):
        gh = github.Github()
        gh2 = pickle.loads(pickle.dumps(gh))
        self.assertIsInstance(gh2, github.Github)
        self.assertIsNotNone(gh2._Github__requester._Requester__connection_lock)
        self.assertIsNone(gh2._Github__requester._Requester__connection)
        self.assertEqual(len(gh2._Github__requester._Requester__custom_connections), 0)

    def testPickleRepository(self):
        gh = github.Github(lazy=True)
        repo = gh.get_repo(REPO_NAME)
        repo2 = pickle.loads(pickle.dumps(repo))
        self.assertIsInstance(repo2, Repository)
        self.assertIsNotNone(repo2._requester._Requester__connection_lock)
        self.assertIsNone(repo2._requester._Requester__connection)
        self.assertEqual(len(repo2._requester._Requester__custom_connections), 0)

    def testPicklePaginatedList(self):
        gh = github.Github()
        repo = gh.get_repo(REPO_NAME, lazy=True)
        branches = repo.get_branches()
        branches2 = pickle.loads(pickle.dumps(branches))
        self.assertIsInstance(branches2, PaginatedList)

    auths = [
        Login("login", "password"),
        Token("token"),
        AppAuth("id", "key"),
        AppAuthToken("token"),
        AppInstallationAuth(AppAuth("id", "key"), 123),
        AppUserAuth("client_id", "client_secret", "access_token"),
        NetrcAuth(),
    ]

    def testPickleAuthSetup(self):
        # check we are testing *all* exiting auth classes
        auth_module = sys.modules[github.Auth.__name__]
        existing_auths = [
            clazz_type.__name__
            for clazz, clazz_type in inspect.getmembers(auth_module, inspect.isclass)
            if Auth in clazz_type.mro() and ABC not in clazz_type.__bases__
        ]
        tested_auths = [type(auth).__name__ for auth in self.auths]
        self.assertSequenceEqual(sorted(existing_auths), sorted(tested_auths))

    def testPickleAuth(self):
        for auth in self.auths:
            with self.subTest(auth=type(auth).__name__):
                auth2 = pickle.loads(pickle.dumps(auth))
                self.assertIsInstance(auth2, type(auth))