Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit826f7f0

Browse files
committed
I'm feeling far more comfortable with mock now.
1 parent5d82a7a commit826f7f0

File tree

6 files changed

+96
-46
lines changed

6 files changed

+96
-46
lines changed

‎mock_tests/json/authorization

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"scopes": ["public_repo"], "url": "https://api.github.com/authorizations/10", "app": {"url": "https://travis-ci.org", "name": "Travis"}, "updated_at": "2012-10-13T19:18:29Z", "id":241576, "note": null, "token": "...", "note_url": null, "created_at": "2012-02-28T01:45:49Z"}
1+
{"scopes": ["public_repo"], "url": "https://api.github.com/authorizations/10", "app": {"url": "https://travis-ci.org", "name": "Travis"}, "updated_at": "2012-10-13T19:18:29Z", "id":10, "note": null, "token": "...", "note_url": null, "created_at": "2012-02-28T01:45:49Z"}

‎mock_tests/json/gist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"files": {"recipe.py": {"size": 1549, "raw_url": "https://gist.github.com/raw/3813862/ff60a9f54f16d651c8ba40441cace6259c1d8c1a/recipe.py", "type": "application/python", "language": "Python", "filename": "recipe.py"}, "gistfile1.txt": {"size": 1918, "raw_url": "https://gist.github.com/raw/3813862/e59fc7d730b04ef1b2b224897b72d284ec87facf/gistfile1.txt", "type": "text/plain", "language": null, "filename": "gistfile1.txt"}}, "description": "List of places to replace list functions with iter functions", "url": "https://api.github.com/gists/3813862", "created_at": "2012-10-01T19:23:44Z", "html_url": "https://gist.github.com/3813862", "comments": 0, "updated_at": "2012-10-01T19:23:44Z", "id": "3813862", "user": {"url": "https://api.github.com/users/sigmavirus24", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "_links": {"self": {"href": "https://api.github.com/users/sigmavirus24"}}, "gravatar_id": "c148356d89f925e692178bee1d93acf7", "login": "sigmavirus24", "id": 240830}, "git_pull_url": "git://gist.github.com/3813862.git", "git_push_url": "git@gist.github.com:3813862.git", "public": true}

‎mock_tests/json/issue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"body": "We use the `_api` attribute to make constructing urls for objects. After a discussion today with GitHub support, it seems as if they'll be using the `url` parameter in the json objects for the same url with parameters in the future. This change was recently made (possibly accidentally) breaking what functionality I had relied on (although they fixed it once I reported it).\n\nFrom now on, to keep with that functionality, I'll have to construct these urls myself (the form I was intending on relying upon).\n\nAnother option I have is to just strip off the parameters and save those elsewhere in case the API starts to require that.", "labels": [{"url": "https://api.github.com/repos/sigmavirus24/github3.py/labels/bug", "color": "fc2929", "name": "bug"}, {"url": "https://api.github.com/repos/sigmavirus24/github3.py/labels/enhancement", "color": "84b6eb", "name": "enhancement"}], "body_text": "We use the _api attribute to make constructing urls for objects. After a discussion today with GitHub support, it seems as if they'll be using the url parameter in the json objects for the same url with parameters in the future. This change was recently made (possibly accidentally) breaking what functionality I had relied on (although they fixed it once I reported it).\n\nFrom now on, to keep with that functionality, I'll have to construct these urls myself (the form I was intending on relying upon).\n\nAnother option I have is to just strip off the parameters and save those elsewhere in case the API starts to require that.", "title": "Construct _api attribute on our own", "url": "https://api.github.com/repos/sigmavirus24/github3.py/issues/1", "created_at": "2012-06-15T17:54:49Z", "milestone": {"description": null, "title": "0.1", "url": "https://api.github.com/repos/sigmavirus24/github3.py/milestones/1", "created_at": "2012-06-15T17:54:49Z", "creator": {"url": "https://api.github.com/users/sigmavirus24", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "_links": {"self": {"href": "https://api.github.com/users/sigmavirus24"}}, "gravatar_id": "c148356d89f925e692178bee1d93acf7", "login": "sigmavirus24", "id": 240830}, "number": 1, "open_issues": 3, "state": "open", "closed_issues": 3, "due_on": null, "id": 132462}, "body_html": "<p>We use the <code>_api</code> attribute to make constructing urls for objects. After a discussion today with GitHub support, it seems as if they'll be using the <code>url</code> parameter in the json objects for the same url with parameters in the future. This change was recently made (possibly accidentally) breaking what functionality I had relied on (although they fixed it once I reported it).</p>\n\n<p>From now on, to keep with that functionality, I'll have to construct these urls myself (the form I was intending on relying upon).</p>\n\n<p>Another option I have is to just strip off the parameters and save those elsewhere in case the API starts to require that.</p>", "comments": 2, "number": 1, "updated_at": "2012-09-17T23:32:48Z", "assignee": {"url": "https://api.github.com/users/sigmavirus24", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "_links": {"self": {"href": "https://api.github.com/users/sigmavirus24"}}, "gravatar_id": "c148356d89f925e692178bee1d93acf7", "login": "sigmavirus24", "id": 240830}, "html_url": "https://github.com/sigmavirus24/github3.py/issues/1", "state": "closed", "user": {"url": "https://api.github.com/users/sigmavirus24", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "_links": {"self": {"href": "https://api.github.com/users/sigmavirus24"}}, "gravatar_id": "c148356d89f925e692178bee1d93acf7", "login": "sigmavirus24", "id": 240830}, "pull_request": {"diff_url": null, "html_url": null, "patch_url": null}, "id": 5097345, "closed_at": "2012-09-17T23:32:48Z", "closed_by": {"url": "https://api.github.com/users/sigmavirus24", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "_links": {"self": {"href": "https://api.github.com/users/sigmavirus24"}}, "gravatar_id": "c148356d89f925e692178bee1d93acf7", "login": "sigmavirus24", "id": 240830}}

‎mock_tests/json/repo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"has_wiki": false, "mirror_url": null, "forks_count": 6, "updated_at": "2012-10-16T22:01:31Z", "svn_url": "https://github.com/sigmavirus24/github3.py", "full_name": "sigmavirus24/github3.py", "owner": {"url": "https://api.github.com/users/sigmavirus24", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "_links": {"self": {"href": "https://api.github.com/users/sigmavirus24"}}, "gravatar_id": "c148356d89f925e692178bee1d93acf7", "login": "sigmavirus24", "id": 240830}, "id": 3710711, "size": 564, "network_count": 6, "_links": {"self": {"href": "https://api.github.com/repos/sigmavirus24/github3.py"}}, "watchers_count": 31, "clone_url": "https://github.com/sigmavirus24/github3.py.git", "forks": 6, "homepage": "http://github3py.readthedocs.org/", "fork": false, "description": "Python library for interfacing with the GitHub APIv3", "has_downloads": true, "html_url": "https://github.com/sigmavirus24/github3.py", "git_url": "git://github.com/sigmavirus24/github3.py.git", "ssh_url": "git@github.com:sigmavirus24/github3.py.git", "has_issues": true, "master_branch": "master", "permissions": {"push": true, "pull": true, "admin": true}, "open_issues_count": 5, "watchers": 31, "name": "github3.py", "language": "Python", "url": "https://api.github.com/repos/sigmavirus24/github3.py", "created_at": "2012-03-13T19:58:53Z", "pushed_at": "2012-10-16T22:01:31Z", "private": false, "open_issues": 5}

‎mock_tests/test_github.py

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,62 @@
11
importgithub3
2-
importrequests
3-
fromjsonimportdumps
4-
frommockimportpatch
2+
fromjsonimportload
3+
frommockimportcall,patch
54
fromunittestimportTestCase
6-
fromexpecterimportexpect
7-
frommock_tests.utilsimportgenerate_response,path
5+
frommock_tests.utilsimportgenerate_response,expect,patch_request,path
86

97

108
classTestGitHub(TestCase):
119
defsetUp(self):
12-
self.get_r=generate_response(open(path('authorization')))
13-
self.post_r=generate_response(open(path('authorization')),201)
14-
self.g_anon=github3.GitHub()
15-
16-
@patch.object(requests.sessions.Session,'request')
17-
deftest_authorization(self,request_mock):
18-
request_mock.return_value=self.get_r
19-
#url = 'https://api.github.com/authorizations/10'
10+
self.g=github3.GitHub()
11+
12+
@patch_request
13+
deftest_authorization(self,request):
14+
request.return_value=generate_response('authorization')
15+
url='https://api.github.com/authorizations/10'
2016
withexpect.raises(github3.GitHubError):
21-
self.g_anon.authorization(10)
22-
23-
self.g_anon.login('user','password')
24-
a=self.g_anon.authorization(10)
25-
assertisinstance(a,github3.auths.Authorization)
26-
#self.g_auth.authorization(10)
27-
#self.mock_auth.get.assert_called_with(url)
28-
29-
@patch.object(requests.sessions.Session,'request')
30-
deftest_authorize(self,request_mock):
31-
request_mock.return_value=self.post_r
32-
url='https://api.github.com/authorizations'
17+
self.g.authorization(10)
18+
19+
self.g.login('user','password')
20+
a=self.g.authorization(10)
21+
expect(a).isinstance(github3.auths.Authorization)
22+
assertrequest.calledisTrue
23+
assertcall('get',url,allow_redirects=True)inrequest.mock_calls
24+
25+
@patch_request
26+
deftest_authorize(self,request):
27+
request.return_value=generate_response('authorization',201)
3328
scopes= ['scope1','scope2']
34-
json_scopes=dumps(scopes)
3529

36-
defassertions(args):
37-
asserturlinargs
38-
assertjson_scopesinargs[1]
39-
assert'"note": ""'inargs[1]
40-
assert'"note_url": ""'inargs[1]
30+
self.g.authorize(None,None,scopes)
31+
assertrequest.calledisFalse
32+
33+
a=self.g.authorize('user','password',scopes)
34+
expect(a).isinstance(github3.auths.Authorization)
35+
assertrequest.calledisTrue
36+
37+
@patch_request
38+
deftest_create_gist(self,request):
39+
request.return_value=generate_response('gist',201)
40+
41+
g=self.g.create_gist('description','files')
42+
expect(g).isinstance(github3.gists.Gist)
43+
assertrequest.calledisTrue
44+
45+
@patch_request
46+
deftest_create_issue(self,request):
47+
request.return_value=generate_response('issue',201)
48+
49+
i=self.g.create_issue(None,None,None)
50+
assertiisNone
51+
assertrequest.calledisFalse
4152

42-
self.g_anon.authorize(None,None,scopes)
43-
#assert self.mock_anon.post.call_count == 0
53+
i=self.g.create_issue('user','repo','')
54+
assertiisNone
55+
assertrequest.calledisFalse
4456

45-
a=self.g_anon.authorize('user','password',scopes)
46-
#calls = self.sess_mock.mock_calls
47-
#expect(len(calls)) == 4
48-
assertisinstance(a,github3.auths.Authorization)
57+
withpatch.object(github3.GitHub,'repository')asrepo:
58+
repo.return_value=github3.repos.Repository(load(path('repo')))
59+
i=self.g.create_issue('user','repo','Title')
4960

50-
#self.g_auth.authorize(None, None, scopes)
51-
#assert self.mock_auth.post.call_count == 1
52-
#assertions(self.mock_auth.post.call_args[0])
61+
expect(i).isinstance(github3.issues.Issue)
62+
assertrequest.calledisTrue

‎mock_tests/utils.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
1-
fromrequestsimportResponse
1+
importrequests
2+
frommockimportpatch
23
fromioimportBytesIO
4+
importexpecter
35

46

5-
defgenerate_response(content,status_code=200,encoding='utf-8'):
6-
r=Response()
7+
defgenerate_response(path_name,status_code=200,encoding='utf-8'):
8+
r=requests.Response()
79
r.status_code=status_code
810
r.encoding=encoding
11+
content=path(path_name)
912
r.raw=BytesIO(content.read().encode())
1013
returnr
1114

1215

13-
defpath(name):
14-
return'mock_tests/json/{0}'.format(name)
16+
defpath(name,mode='r'):
17+
returnopen('mock_tests/json/{0}'.format(name),mode)
18+
19+
20+
defpatch_request(method='request'):
21+
defdecorator(func):
22+
returnpatch.object(requests.sessions.Session,method)(func)
23+
returndecorator
24+
25+
26+
classCustomExpecter(expecter.expect):
27+
defis_not_None(self):
28+
assertself._actualisnotNone, (
29+
'Expected anything but None but got it.'
30+
)
31+
32+
defis_None(self):
33+
assertself._actualisNone, (
34+
'Expected None but got %s'%repr(self._actual)
35+
)
36+
37+
defis_True(self):
38+
assertself._actualisTrue, (
39+
'Expected True but got %s'%repr(self._actual)
40+
)
41+
42+
defis_False(self):
43+
assertself._actualisFalse, (
44+
'Expected False but got %s'%repr(self._actual)
45+
)
46+
47+
deflist_of(self,cls):
48+
foractualinself._actual:
49+
CustomExpecter(actual).isinstance(cls)
50+
51+
expect=CustomExpecter

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp