8000 Merge pull request #803 from sigmavirus24/bug/801 · domdfcoding/github3.py@de04628 · GitHub
[go: up one dir, main page]

Skip to content

Commit de04628

Browse files
authored
Merge pull request sigmavirus24#803 from sigmavirus24/bug/801
Generate the _api attribute for ShortBranch objects
2 parents 2aa1748 + f4152cc commit de04628

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

github3/repos/branch.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class _Branch(models.GitHubCore):
2525
def _update_attributes(self, branch):
2626
self.commit = commit.MiniCommit(branch['commit'], self)
2727
self.name = branch['name']
28+
base = self.commit.url.split('/commit', 1)[0]
29+
self._api = self._build_url('branches', self.name, base_url=base)
2830

2931
def _repr(self):
3032
return '<{0} [{1}]>'.format(self.class_name, self.name)
3133

3234
def latest_sha(self, differs_from=''):
33-
"""Check if SHA-1 is the same as remote branch.
35+
"""Check if SHA-1 is the same as the remote branch.
3436
3537
See: https://git.io/vaqIw
3638
@@ -139,10 +141,6 @@ def _update_attributes(self, branch):
139141
self.protection_url = branch['protection_url']
140142
if self.links and 'self' in self.links:
141143
self._api = self.links['self']
142-
elif isinstance(self.commit, commit.ShortCommit):
143-
# Branches obtained via `repo.branches` don't have links.
144-
base = self.commit.url.split('/commit', 1)[0]
145-
self._api = self._build_url('branches', self.name, base_url=base)
146144

147145

148146
class ShortBranch(_Branch):

tests/unit/helper.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,32 @@ def build_url(self, *args, **kwargs):
4444
return github3.session.GitHubSession().build_url(*args, **kwargs)
4545

4646

47-
class UnitHelper(unittest.TestCase):
47+
def enterprise_build_url_builder(enterprise_url):
48+
"""Build a URL builder function."""
49+
def enterprise_build_url(self, *args, **kwargs):
50+
"""A function to proxy to the actual GitHubSession#build_url method."""
51+
# We want to assert what is happening with the actual calls to the
52+
# Internet. We can proxy this.
53+
return github3.session.GitHubSession().build_url(
54+
*args,
55+
base_url=enterprise_url,
56+
**kwargs
57+
)
58+
return enterprise_build_url
4859

60+
61+
class UnitHelper(unittest.TestCase):
4962
"""Base class for unittests."""
5063

5164
# Sub-classes must assign the class to this during definition
5265
described_class = None
5366
# Sub-classes must also assign a dictionary to this during definition
5467
example_data = {}
68+
enterprise_url = None
69+
70+
@staticmethod
71+
def get_build_url_proxy():
72+
return build_url
5573

5674
def create_mocked_session(self):
5775
"""Use mock to auto-spec a GitHubSession and return an instance."""
@@ -89,7 +107,10 @@ def create_instance_of_described_class(self):
89107
instance = self.described_class(self.example_data, session)
90108

91109
else:
92-
instance = self.described_class()
110+
if self.enterprise_url is None:
111+
instance = self.described_class()
112+
else:
113+
instance = self.described_class(self.enterprise_url)
93114
instance.session = self.session
94115

95116
return instance
@@ -154,13 +175,18 @@ def put_called_with(self, *args, **kwargs):
154175
def setUp(self):
155176
"""Use to set up attributes on self before each test."""
156177
self.session = self.create_session_mock()
157-
self.instance = self.create_instance_of_described_class()
158178
# Proxy the build_url method to the class so it can build the URL and
159179
# we can assert things about the call that will be attempted to the
160180
# internet
161-
self.described_class._build_url = build_url
181+
self.old_build_url = self.described_class._build_url
182+
self.described_class._build_url = self.get_build_url_proxy()
183+
self.instance = self.create_instance_of_described_class()
162184
self.after_setup()
163185

186+
def tearDown(self):
187+
"""Reset attributes on items under test."""
188+
self.described_class._build_url = self.old_build_url
189+
164190
def after_setup(self):
165191
"""No-op method to avoid people having to override setUp."""
166192
pass
@@ -234,42 +260,17 @@ def assert_requires_auth(self, func, *args, **kwargs):
234260

235261

236262
class UnitGitHubObjectHelper(UnitHelper):
237-
238263
"""Base class for GitHubObject unit tests."""
264+
# TODO(sigmavirus24): delete me
239265

240-
def setUp(self):
241-
self.session = None
242-
self.instance = self.create_instance_of_described_class()
243-
# Proxy the build_url method to the class so it can build the URL and
244-
# we can assert things about the call that will be attempted to the
245-
# internet
246-
self.described_class._build_url = build_url
247-
self.after_setup()
248-
pass
266+
pass
249267

250268

251269
@pytest.mark.usefixtures('enterprise_url')
252270
class UnitGitHubEnterpriseHelper(UnitHelper):
253271

254-
def build_url(self, *args, **kwargs):
255-
"""A function to proxy to the actual GitHubSession#build_url method."""
256-
# We want to assert what is happening with the actual calls to the
257-
# Internet. We can proxy this.
258-
return github3.session.GitHubSession().build_url(
259-
*args,
260-
base_url=self.enterprise_url,
261-
**kwargs
262-
)
263-
264-
def setUp(self):
265-
self.session = self.create_session_mock()
266-
self.instance = github3.GitHubEnterprise(self.enterprise_url)
267-
self.instance.session = self.session
268-
# Proxy the build_url method to the class so it can build the URL and
269-
# we can assert things about the call that will be attempted to the
270-
# internet
271-
self.instance._build_url = self.build_url
272-
self.after_setup()
272+
def get_build_url_proxy(self):
273+
return enterprise_build_url_builder(self.enterprise_url)
273274

274275

275276
is_py3 = (3, 0) <= sys.version_info < (4, 0)

tests/unit/json/repos_branch_example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"commit": {
44
"sha": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
55
"commit": {
6+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
67
"author": {
78
"name": "The Octocat",
89
"date": "2012-03-06T15:06:50-08:00",

0 commit comments

Comments
 (0)
0