Skip to content

Commit 90941c0

Browse files
committed
Added tests for requests for merge (github only)
1 parent 410e737 commit 90941c0

File tree

6 files changed

+385
-21
lines changed

6 files changed

+385
-21
lines changed

tests/helpers.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def __init__(self, *args, **kwarg):
2525
self._did_create = None
2626
self._did_fork = None
2727
self._did_user = False
28+
self._did_request_list = None
29+
self._did_request_fetch = None
2830

2931
def pull(self, *args, **kwarg):
3032
self._did_pull = (args, kwarg)
@@ -50,6 +52,18 @@ def create(self, *args, **kwarg):
5052
def fork(self, *args, **kwarg):
5153
self._did_fork = (args, kwarg)
5254

55+
def request_list(self, *args, **kwarg):
56+
self._did_request_list = (args, kwarg)
57+
return [('1', 'desc1', 'http://request/1'),
58+
('2', 'desc2', 'http://request/2'),
59+
('3', 'desc3', 'http://request/3')]
60+
61+
def request_fetch(self, *args, **kwarg):
62+
self._did_request_fetch = (args, kwarg)
63+
if args[-1] == 'bad':
64+
raise Exception('bad request for merge!')
65+
return "pr/42"
66+
5367
@property
5468
def user(self):
5569
self._did_user = True
@@ -100,6 +114,7 @@ def setup_args(self, d, args={}):
100114
'fetch': False,
101115
'fork': False,
102116
'list': False,
117+
'ls': False,
103118
'open': False,
104119
'request': False,
105120
'<request>': None,
@@ -157,10 +172,25 @@ def main_fork(self, repo, rc=0, args={}):
157172
return RepositoryService._current._did_fork
158173

159174
def main_request_list(self, repo, rc=0, args={}):
160-
assert True
175+
assert rc == main(self.setup_args({
176+
'request': True,
177+
'list': True,
178+
'<user>/<repo>': repo,
179+
'--clone': True,
180+
'--path': self.tempdir.name
181+
}, args)), "Non {} result for request list".format(rc)
182+
return RepositoryService._current._did_request_list
161183

162184
def main_request_fetch(self, repo, rc=0, args={}):
163-
assert True
185+
assert rc == main(self.setup_args({
186+
'request': True,
187+
'fetch': True,
188+
'<user>/<repo>': repo,
189+
'--clone': True,
190+
'--path': self.tempdir.name
191+
}, args)), "Non {} result for request fetch".format(rc)
192+
return RepositoryService._current._did_request_fetch
193+
164194
def main_open(self, repo, rc=0, args={}):
165195
os.mkdir(os.path.join(self.tempdir.name, repo.split('/')[-1]))
166196
Repo.init(os.path.join(self.tempdir.name, repo.split('/')[-1]))
@@ -410,6 +440,20 @@ def action_add(self, cassette_name, namespace, repository, alone=False, name=Non
410440
self.assert_added_remote(name)
411441
self.assert_tracking_remote(name, tracking)
412442

443+
def action_request_list(self, cassette_name, namespace, repository, rq_list_data=[]):
444+
with self.recorder.use_cassette('_'.join(['test', self.service.name, cassette_name])):
445+
self.service.connect()
446+
requests = list(self.service.request_list(user=namespace, repo=repository))
447+
for i, rq in enumerate(rq_list_data):
448+
assert requests[i] == rq
449+
450+
def action_request_fetch(self, cassette_name, namespace, repository, request, pull=False):
451+
with self.recorder.use_cassette('_'.join(['test', self.service.name, cassette_name])):
452+
self.service.connect()
453+
self.service.clone(namespace, repository)
454+
self.service.request_fetch(repository, namespace, request)
455+
assert self.repository.branches[-1].name == 'request-{}'.format(request)
456+
413457
def action_open(self, cassette_name, namespace, repository):
414458
self.set_mock_popen_commands([
415459
('xdg-open {}'.format(self.service.format_path(namespace=namespace, repository=repository)), b'', b'', 0),

tests/integration/cassettes/test_github_test_14_request_list.json

Lines changed: 175 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"http_interactions": [
3+
{
4+
"recorded_at": "2016-05-22T21:20:21",
5+
"request": {
6+
"body": {
7+
"encoding": "utf-8",
8+
"string": ""
9+
},
10+
"headers": {
11+
"Accept": "application/vnd.github.v3.full+json",
12+
"Accept-Charset": "utf-8",
13+
"Accept-Encoding": "identity",
14+
"Authorization": "token <PRIVATE_KEY_GITHUB>",
15+
"Connection": "keep-alive",
16+
"Content-Type": "application/json",
17+
"User-Agent": "github3.py/0.9.5"
18+
},
19+
"method": "GET",
20+
"uri": "https://api.github.com/user"
21+
},
22+
"response": {
23+
"body": {
24+
"encoding": "utf-8",
25+
"string": "{\"login\":\"<GITHUB_NAMESPACE>\",\"id\":254441,\"avatar_url\":\"https://avatars.githubusercontent.com/u/254441?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>\",\"html_url\":\"https://github.com/<GITHUB_NAMESPACE>\",\"followers_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/followers\",\"following_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/subscriptions\",\"organizations_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/orgs\",\"repos_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/repos\",\"events_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/received_events\",\"type\":\"User\",\"site_admin\":false,\"name\":\"<GITHUB_NAMESPACE>\",\"company\":null,\"blog\":\"http://<GITHUB_NAMESPACE>.got.nothing.to/blog/\",\"location\":\"Paris\",\"email\":null,\"hireable\":true,\"bio\":null,\"public_repos\":73,\"public_gists\":14,\"followers\":44,\"following\":13,\"created_at\":\"2010-04-27T14:04:09Z\",\"updated_at\":\"2016-03-26T00:48:05Z\"}"
26+
},
27+
"headers": {
28+
"Access-Control-Allow-Origin": "*",
29+
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
30+
"Cache-Control": "private, max-age=60, s-maxage=60",
31+
"Content-Length": "1118",
32+
"Content-Security-Policy": "default-src 'none'",
33+
"Content-Type": "application/json; charset=utf-8",
34+
"Date": "Sun, 22 May 2016 21:20:22 GMT",
35+
"ETag": "\"a03b1823bdc81ce18c8fad535632335e\"",
36+
"Last-Modified": "Sat, 26 Mar 2016 00:48:05 GMT",
37+
"Server": "GitHub.com",
38+
"Status": "200 OK",
39+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
40+
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
41+
"X-Accepted-OAuth-Scopes": "",
42+
"X-Content-Type-Options": "nosniff",
43+
"X-Frame-Options": "deny",
44+
"X-GitHub-Media-Type": "github.v3; param=full; format=json",
45+
"X-GitHub-Request-Id": "4EC11365:1271B:DE6536F:57422296",
46+
"X-OAuth-Scopes": "delete_repo, gist, repo",
47+
"X-RateLimit-Limit": "5000",
48+
"X-RateLimit-Remaining": "4970",
49+
"X-RateLimit-Reset": "1463952313",
50+
"X-Served-By": "52437fedc85beec8da3449496900fb9a",
51+
"X-XSS-Protection": "1; mode=block"
52+
},
53+
"status": {
54+
"code": 200,
55+
"message": "OK"
56+
},
57+
"url": "https://api.github.com/user"
58+
}
59+
}
60+
],
61+
"recorded_with": "betamax/0.5.1"
62+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"http_interactions": [
3+
{
4+
"recorded_at": "2016-05-22T21:21:06",
5+
"request": {
6+
"body": {
7+
"encoding": "utf-8",
8+
"string": ""
9+
},
10+
"headers": {
11+
"Accept": "application/vnd.github.v3.full+json",
12+
"Accept-Charset": "utf-8",
13+
"Accept-Encoding": "identity",
14+
"Authorization": "token <PRIVATE_KEY_GITHUB>",
15+
"Connection": "keep-alive",
16+
"Content-Type": "application/json",
17+
"User-Agent": "github3.py/0.9.5"
18+
},
19+
"method": "GET",
20+
"uri": "https://api.github.com/user"
21+
},
22+
"response": {
23+
"body": {
24+
"encoding": "utf-8",
25+
"string": "{\"login\":\"<GITHUB_NAMESPACE>\",\"id\":254441,\"avatar_url\":\"https://avatars.githubusercontent.com/u/254441?v=3\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>\",\"html_url\":\"https://github.com/<GITHUB_NAMESPACE>\",\"followers_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/followers\",\"following_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/subscriptions\",\"organizations_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/orgs\",\"repos_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/repos\",\"events_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/<GITHUB_NAMESPACE>/received_events\",\"type\":\"User\",\"site_admin\":false,\"name\":\"<GITHUB_NAMESPACE>\",\"company\":null,\"blog\":\"http://<GITHUB_NAMESPACE>.got.nothing.to/blog/\",\"location\":\"Paris\",\"email\":null,\"hireable\":true,\"bio\":null,\"public_repos\":73,\"public_gists\":14,\"followers\":44,\"following\":13,\"created_at\":\"2010-04-27T14:04:09Z\",\"updated_at\":\"2016-03-26T00:48:05Z\"}"
26+
},
27+
"headers": {
28+
"Access-Control-Allow-Origin": "*",
29+
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
30+
"Cache-Control": "private, max-age=60, s-maxage=60",
31+
"Content-Length": "1118",
32+
"Content-Security-Policy": "default-src 'none'",
33+
"Content-Type": "application/json; charset=utf-8",
34+
"Date": "Sun, 22 May 2016 21:21:07 GMT",
35+
"ETag": "\"a03b1823bdc81ce18c8fad535632335e\"",
36+
"Last-Modified": "Sat, 26 Mar 2016 00:48:05 GMT",
37+
"Server": "GitHub.com",
38+
"Status": "200 OK",
39+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
40+
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
41+
"X-Accepted-OAuth-Scopes": "",
42+
"X-Content-Type-Options": "nosniff",
43+
"X-Frame-Options": "deny",
44+
"X-GitHub-Media-Type": "github.v3; param=full; format=json",
45+
"X-GitHub-Request-Id": "4EC11365:12719:CB2E110:574222C3",
46+
"X-OAuth-Scopes": "delete_repo, gist, repo",
47+
"X-RateLimit-Limit": "5000",
48+
"X-RateLimit-Remaining": "4969",
49+
"X-RateLimit-Reset": "1463952313",
50+
"X-Served-By": "5aeb3f30c9e3ef6ef7bcbcddfd9a68f7",
51+
"X-XSS-Protection": "1; mode=block"
52+
},
53+
"status": {
54+
"code": 200,
55+
"message": "OK"
56+
},
57+
"url": "https://api.github.com/user"
58+
}
59+
}
60+
],
61+
"recorded_with": "betamax/0.5.1"
62+
}

tests/integration/test_github.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from tests.helpers import GitRepoTestCase
1717

1818
from git_repo.services.service import github
19-
from git_repo.exceptions import ResourceExistsError
19+
from git_repo.exceptions import ResourceExistsError, ResourceNotFoundError
2020

2121

2222
class Test_Github(GitRepoTestCase):
@@ -118,7 +118,12 @@ def test_12_add__alone_default_name(self):
118118
name='test0r',
119119
tracking='github')
120120

121-
def test_13_request_list(self):
121+
def test_13_open(self):
122+
self.action_open(cassette_name=sys._getframe().f_code.co_name,
123+
namespace='guyzmo',
124+
repository='git-repo')
125+
126+
def test_14_request_list(self):
122127
self.action_request_list(
123128
cassette_name=sys._getframe().f_code.co_name,
124129
namespace='guyzmo',
@@ -128,22 +133,17 @@ def test_13_request_list(self):
128133
(2, 'prefer gitrepo.<target>.token > privatekey, docs', 'https://api.github.com/repos/guyzmo/git-repo/issues/2'),
129134
])
130135

131-
def test_14_request_fetch(self):
136+
def test_15_request_fetch(self):
132137
self.action_request_fetch(cassette_name=sys._getframe().f_code.co_name,
133138
namespace='guyzmo',
134139
repository='git-repo',
135140
request='2')
136141

137-
def test_15_request_fetch__bad_request(self):
142+
def test_16_request_fetch__bad_request(self):
138143
with pytest.raises(ResourceNotFoundError):
139144
self.action_request_fetch(cassette_name=sys._getframe().f_code.co_name,
140145
namespace='guyzmo',
141146
repository='git-repo',
142147
request='1')
143148

144-
def test_31_open(self):
145-
self.action_open(cassette_name=sys._getframe().f_code.co_name,
146-
namespace='guyzmo',
147-
repository='git-repo')
148-
149149

tests/integration/test_main.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,38 @@ def test_fork__branch_clone(self):
154154
assert ('guyzmo', 'git-repo') == repo_slug
155155
assert {'branch': 'foobar', 'clone': True} == seen_args
156156

157-
def test_request_list(self):
158-
repo_slug, seen_args = self.main_request_list('guyzmo/git-repo', 0,
159-
args={})
157+
def test_request_list(self, capsys, caplog):
158+
from subprocess import call
159+
call(['git', 'init', '-q', self.tempdir.name])
160+
repo_slug, seen_args = self.main_request_list('guyzmo/git-repo', 0, args={})
161+
out, err = capsys.readouterr()
162+
assert out == ' 1\tdesc1 \thttp://request/1\n 2\tdesc2 \thttp://request/2\n 3\tdesc3 \thttp://request/3\n'
163+
assert 'id' in caplog.text and 'title' in caplog.text and 'URL' in caplog.text
164+
165+
def test_request_fetch__request(self, capsys, caplog):
166+
from subprocess import call
167+
call(['git', 'init', '-q', self.tempdir.name])
168+
seen_args, extra_args = self.main_request_fetch('guyzmo/git-repo', 0,
169+
args={'<request>': '42'})
170+
out, err = capsys.readouterr()
171+
assert ('guyzmo', 'git-repo', '42') == seen_args
172+
assert {} == extra_args
173+
assert out == ''
174+
assert 'Successfully fetched request id `42` of `guyzmo/git-repo` into `pr/42`!' in caplog.text
175+
176+
def test_request_fetch__bad_request(self, capsys, caplog):
177+
from subprocess import call
178+
call(['git', 'init', '-q', self.tempdir.name])
179+
seen_args, extra_args = self.main_request_fetch('guyzmo/git-repo', 2,
180+
args={'<request>': 'bad', '--verbose': 0})
181+
out, err = capsys.readouterr()
182+
assert ('guyzmo', 'git-repo', 'bad') == seen_args
183+
assert {} == extra_args
184+
print('out:', out)
185+
print('err:', caplog.text)
186+
assert out == ''
187+
assert 'Fatal error: bad request for merge!' in caplog.text
160188

161-
def test_request_fetch__request(self):
162-
repo_slug, seen_args = self.main_request_fetch('guyzmo/git-repo', 0,
163-
args={})
164-
165-
def test_request_fetch__bad_request(self):
166-
repo_slug, seen_args = self.main_request_fetch('guyzmo/git-repo', 0,
167-
args={})
168189
def test_open(self):
169190
repo_slug, seen_args = self.main_open('guyzmo/git-repo', 0)
170191
assert ('guyzmo', 'git-repo') == repo_slug

0 commit comments

Comments
 (0)