From 82776a8c75a1cd15d764926b1647b1ea24c18a5f Mon Sep 17 00:00:00 2001 From: David Medina Date: Tue, 3 Apr 2012 07:55:43 +0200 Subject: "services.gists.Gist" tests and resources --- pygithub3/tests/services/test_gists.py | 93 ++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 pygithub3/tests/services/test_gists.py (limited to 'pygithub3/tests') diff --git a/pygithub3/tests/services/test_gists.py b/pygithub3/tests/services/test_gists.py new file mode 100644 index 0000000..af88e1e --- /dev/null +++ b/pygithub3/tests/services/test_gists.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +import requests +from mock import patch, Mock + +from pygithub3.tests.utils.core import TestCase +from pygithub3.resources.base import json +from pygithub3.services.gists import Gist +from pygithub3.tests.utils.base import (mock_response, mock_response_result, + mock_json) +from pygithub3.tests.utils.services import _ + +json.dumps = Mock(side_effect=mock_json) +json.loads = Mock(side_effect=mock_json) + + +@patch.object(requests.sessions.Session, 'request') +class TestGistService(TestCase): + + def setUp(self): + self.gs = Gist() + + def test_LIST_without_user(self, request_method): + request_method.return_value = mock_response_result() + self.gs.list().all() + self.assertEqual(request_method.call_args[0], ('get', _('gists'))) + + def test_LIST_with_user(self, request_method): + request_method.return_value = mock_response_result() + self.gs.list('octocat').all() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat/gists'))) + + def test_LIST_public(self, request_method): + request_method.return_value = mock_response_result() + self.gs.list_public().all() + self.assertEqual(request_method.call_args[0], + ('get', _('gists/public'))) + + def test_LIST_starred(self, request_method): + request_method.return_value = mock_response_result() + self.gs.list_starred.all() + self.assertEqual(request_method.call_args[0], + ('get', _('gists/starred'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.gs.get(1) + self.assertEqual(request_method.call_args[0], + ('get', _('gists/1'))) + + def test_CREATE(self, request_method): + request_method.return_value = mock_response('post') + self.gs.create(dict(public=True, files={ + 'file1.txt': {'content': 'Some\ncontent'}})) + self.assertEqual(request_method.call_args[0], + ('post', _('gists'))) + + def test_UPDATE(self, request_method): + request_method.return_value = mock_response('patch') + self.gs.update(1, {'description': 'edited'}) + self.assertEqual(request_method.call_args[0], + ('patch', _('gists/1'))) + + def test_STAR(self, request_method): + self.gs.star(1) + self.assertEqual(request_method.call_args[0], + ('put', _('gists/1/star'))) + + def test_UNSTAR(self, request_method): + request_method.return_value = mock_response('delete') + self.gs.unstar(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('gists/1/star'))) + + def test_IS_STARRED(self, request_method): + request_method.return_value = mock_response() + self.gs.is_starred(1) + self.assertEqual(request_method.call_args[0], + ('get', _('gists/1/star'))) + + def test_FORK(self, request_method): + request_method.return_value = mock_response('post') + self.gs.fork(1) + self.assertEqual(request_method.call_args[0], + ('post', _('gists/1/fork'))) + + def test_DELETE(self, request_method): + request_method.return_value = mock_response('delete') + self.gs.delete(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('gists/1'))) -- cgit v1.2.3-59-g8ed1b From 3a52231c943bc59b1ddc8800f96179cbe0d1523d Mon Sep 17 00:00:00 2001 From: David Medina Date: Tue, 3 Apr 2012 12:37:18 +0200 Subject: services.gists.Gist done --- pygithub3/github.py | 9 +++ pygithub3/requests/gists/__init__.py | 77 +++++++++++++++++++ pygithub3/services/gists/__init__.py | 132 +++++++++++++++++++++++++++++++++ pygithub3/tests/services/test_gists.py | 6 +- 4 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 pygithub3/requests/gists/__init__.py create mode 100644 pygithub3/services/gists/__init__.py (limited to 'pygithub3/tests') diff --git a/pygithub3/github.py b/pygithub3/github.py index 23b5b0b..1cf9b05 100644 --- a/pygithub3/github.py +++ b/pygithub3/github.py @@ -15,8 +15,10 @@ class Github(object): def __init__(self, **config): from pygithub3.services.users import User from pygithub3.services.repos import Repos + from pygithub3.services.gists import Gist self._users = User(**config) self._repos = Repos(**config) + self._gists = Gist(**config) @property def remaining_requests(self): @@ -37,3 +39,10 @@ class Github(object): :ref:`Repos service ` """ return self._repos + + @property + def gists(self): + """ + :ref:`Gist service ` + """ + return self._gists diff --git a/pygithub3/requests/gists/__init__.py b/pygithub3/requests/gists/__init__.py new file mode 100644 index 0000000..4dbc6e6 --- /dev/null +++ b/pygithub3/requests/gists/__init__.py @@ -0,0 +1,77 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request, ValidationError +from pygithub3.resources.gists import Gist + +class List(Request): + + uri = 'users/{user}/gists' + resource = Gist + + def clean_uri(self): + if not self.user: + return 'gists' + + +class Public(Request): + + uri = 'gists/public' + resource = Gist + + +class Starred(Request): + + uri = 'gists/starred' + resource = Gist + + +class Get(Request): + + uri = 'gists/{id}' + resource = Gist + + +class Create(Request): + + uri = 'gists' + resource = Gist + body_schema = { + 'schema': ('description', 'public', 'files'), + 'required': ('public', 'files') + } + + +class Update(Request): + + uri = 'gists/{id}' + resource = Gist + body_schema = { + 'schema': ('description', 'public', 'files'), + 'required': (), + } + + +class Star(Request): + + uri = 'gists/{id}/star' + + +class Unstar(Request): + + uri = 'gists/{id}/star' + + +class Is_starred(Request): + + uri = 'gists/{id}/star' + + +class Fork(Request): + + uri = 'gists/{id}/fork' + resource = Gist + + +class Delete(Request): + + uri = 'gists/{id}' diff --git a/pygithub3/services/gists/__init__.py b/pygithub3/services/gists/__init__.py new file mode 100644 index 0000000..b983fea --- /dev/null +++ b/pygithub3/services/gists/__init__.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service + + +class Gist(Service): + """ Consume `Gists API `_ """ + + def __init__(self, **config): + super(Gist, self).__init__(**config) + + def list(self, user=None): + """ Get user's gists + + :param str user: Username + :returns: A :doc:`result` + + If you call it without user and you are authenticated, get the + authenticated user's gists. but if you aren't authenticated get the + public gists + + :: + + gist_service.list('copitux') + gist_service.list() + """ + request = self.request_builder('gists.list', user=user) + return self._get_result(request) + + def public(self): + """ Get public gists + + :returns: A :doc:`result` + + .. note :: + Be careful iterating the result + """ + request = self.request_builder('gists.public') + return self._get_result(request) + + def starred(self): + """ Get authenticated user's starred gists + + :returns: A :doc:`result` + + .. warning:: + You must be authenticated + """ + request = self.request_builder('gists.starred') + return self._get_result(request) + + def get(self, id): + """ Get a single gist + + :param int id: Gist id + """ + request = self.request_builder('gists.get', id=id) + return self._get(request) + + def create(self, data): + """ Create a gist + + :param dict data: Input. See `github gists doc`_ + + :: + + gist_service.create(dict(description='some gist', public=True, + files={'xample.py': {'content': 'import code'}})) + """ + request = self.request_builder('gists.create', body=data) + return self._post(request) + + def update(self, id, data): + """ Update a single gist + + :param int id: Gist id + :param dict data: Input. See `github gists doc`_ + + .. warning :: + You must be authenticated + + :: + + gist_service.update(dict(description='edited', + files={'xample.py': { + 'filename': 'new_xample.py', + 'content': 'import new_code'}})) + """ + request = self.request_builder('gists.update', id=id, body=data) + return self._patch(request) + + def star(self, id): + """ Star a gist + + :param int id: Gist id + """ + request = self.request_builder('gists.star', id=id) + self._put(request) + + def unstar(self, id): + """ Unstar a gist + + :param int id: Gist id + """ + request = self.request_builder('gists.unstar', id=id) + return self._delete(request) + + def is_starred(self, id): + """ Check if a gist is starred + + :param int id: Gist id + """ + request = self.request_builder('gists.is_starred', id=id) + return self._bool(request) + + def fork(self, id): + """ Fork a gist + + :param int id: Gist id + """ + + request = self.request_builder('gists.fork', id=id) + return self._post(request) + + def delete(self, id): + """ Delete a gist + + :param int id: Gist id + """ + request = self.request_builder('gists.delete', id=id) + return self._delete(request) diff --git a/pygithub3/tests/services/test_gists.py b/pygithub3/tests/services/test_gists.py index af88e1e..66ff537 100644 --- a/pygithub3/tests/services/test_gists.py +++ b/pygithub3/tests/services/test_gists.py @@ -34,13 +34,13 @@ class TestGistService(TestCase): def test_LIST_public(self, request_method): request_method.return_value = mock_response_result() - self.gs.list_public().all() + self.gs.public().all() self.assertEqual(request_method.call_args[0], ('get', _('gists/public'))) def test_LIST_starred(self, request_method): request_method.return_value = mock_response_result() - self.gs.list_starred.all() + self.gs.starred().all() self.assertEqual(request_method.call_args[0], ('get', _('gists/starred'))) @@ -78,7 +78,7 @@ class TestGistService(TestCase): request_method.return_value = mock_response() self.gs.is_starred(1) self.assertEqual(request_method.call_args[0], - ('get', _('gists/1/star'))) + ('head', _('gists/1/star'))) def test_FORK(self, request_method): request_method.return_value = mock_response('post') -- cgit v1.2.3-59-g8ed1b From 30033cb33d828d96492c429407977a9f7912bf28 Mon Sep 17 00:00:00 2001 From: David Medina Date: Tue, 3 Apr 2012 19:06:02 +0200 Subject: services.gists.Comment done --- pygithub3/requests/gists/comments.py | 40 +++++++++++++++++++++ pygithub3/resources/gists.py | 8 +++++ pygithub3/services/gists/__init__.py | 2 ++ pygithub3/services/gists/comments.py | 64 ++++++++++++++++++++++++++++++++++ pygithub3/tests/services/test_gists.py | 39 ++++++++++++++++++++- 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 pygithub3/requests/gists/comments.py create mode 100644 pygithub3/services/gists/comments.py (limited to 'pygithub3/tests') diff --git a/pygithub3/requests/gists/comments.py b/pygithub3/requests/gists/comments.py new file mode 100644 index 0000000..32f01ec --- /dev/null +++ b/pygithub3/requests/gists/comments.py @@ -0,0 +1,40 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request +from pygithub3.resources.gists import Comment + +class List(Request): + + uri = 'gists/{gist_id}/comments' + resource = Comment + + +class Get(Request): + + uri = 'gists/comments/{id}' + resource = Comment + + +class Create(Request): + + uri = 'gists/{gist_id}/comments' + resource = Comment + body_schema = { + 'schema': ('body', ), + 'required': ('body', ) + } + + +class Update(Request): + + uri = 'gists/comments/{id}' + resource = Comment + body_schema = { + 'schema': ('body', ), + 'required': ('body', ) + } + + +class Delete(Request): + + uri = 'gists/comments/1' diff --git a/pygithub3/resources/gists.py b/pygithub3/resources/gists.py index 8345f84..7e9550a 100644 --- a/pygithub3/resources/gists.py +++ b/pygithub3/resources/gists.py @@ -35,3 +35,11 @@ class Gist(Resource): def __str__(self): return '' % getattr(self, 'description', '') + +class Comment(Resource): + + _dates = ('created_at', ) + _maps = {'user': User} + + def __str__(self): + return '' % getattr(self, 'user', '') diff --git a/pygithub3/services/gists/__init__.py b/pygithub3/services/gists/__init__.py index b983fea..5fbfe7a 100644 --- a/pygithub3/services/gists/__init__.py +++ b/pygithub3/services/gists/__init__.py @@ -2,12 +2,14 @@ # -*- encoding: utf-8 -*- from pygithub3.services.base import Service +from comments import Comments class Gist(Service): """ Consume `Gists API `_ """ def __init__(self, **config): + self.comments = Comments(**config) super(Gist, self).__init__(**config) def list(self, user=None): diff --git a/pygithub3/services/gists/comments.py b/pygithub3/services/gists/comments.py new file mode 100644 index 0000000..397d583 --- /dev/null +++ b/pygithub3/services/gists/comments.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service, MimeTypeMixin + + +class Comments(Service, MimeTypeMixin): + """ Consume `Comments API + `_ + + .. note:: + This service support :ref:`mimetypes-section` configuration + """ + + def list(self, gist_id): + """ Get gist's comments + + :param int gist_id: Gist id + :returns: A :doc:`result` + """ + request = self.request_builder('gists.comments.list', gist_id=gist_id) + return self._get_result(request, **self._get_mimetype_as_header()) + + def get(self, id): + """ Get a single comment + + :param int id: Comment id + """ + request = self.request_builder('gists.comments.get', id=id) + return self._get(request, **self._get_mimetype_as_header()) + + def create(self, gist_id, message): + """ Create a comment + + :param int gist_id: Gist id + :param str message: Comment's message + + .. warning:: + You must be authenticated + + :: + comment_service.create(1, 'comment') + """ + request = self.request_builder('gists.comments.create', + gist_id=gist_id, body={'body': message}) + return self._post(request, **self._get_mimetype_as_header()) + + def update(self, id, message): + """ Update a comment + + :param int id: Comment id + :param str message: Comment's message + """ + request = self.request_builder('gists.comments.update', id=id, + body={'body': message}) + return self._patch(request, **self._get_mimetype_as_header()) + + def delete(self, id): + """ Delete a comment + + :param int id: Comment id + """ + request = self.request_builder('gists.comments.delete', id=id) + self._delete(request) diff --git a/pygithub3/tests/services/test_gists.py b/pygithub3/tests/services/test_gists.py index 66ff537..8cf93ed 100644 --- a/pygithub3/tests/services/test_gists.py +++ b/pygithub3/tests/services/test_gists.py @@ -6,7 +6,7 @@ from mock import patch, Mock from pygithub3.tests.utils.core import TestCase from pygithub3.resources.base import json -from pygithub3.services.gists import Gist +from pygithub3.services.gists import Gist, Comments from pygithub3.tests.utils.base import (mock_response, mock_response_result, mock_json) from pygithub3.tests.utils.services import _ @@ -91,3 +91,40 @@ class TestGistService(TestCase): self.gs.delete(1) self.assertEqual(request_method.call_args[0], ('delete', _('gists/1'))) + + +@patch.object(requests.sessions.Session, 'request') +class TestCommentService(TestCase): + + def setUp(self): + self.cs = Comments() + + def test_LIST(self, request_method): + request_method.return_value = mock_response_result() + self.cs.list(1).all() + self.assertEqual(request_method.call_args[0], + ('get', _('gists/1/comments'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.cs.get(1) + self.assertEqual(request_method.call_args[0], + ('get', _('gists/comments/1'))) + + def test_CREATE(self, request_method): + request_method.return_value = mock_response('post') + self.cs.create(1, dict(body='comment')) + self.assertEqual(request_method.call_args[0], + ('post', _('gists/1/comments'))) + + def test_UPDATE(self, request_method): + request_method.return_value = mock_response('patch') + self.cs.update(1, dict(body='new_comment')) + self.assertEqual(request_method.call_args[0], + ('patch', _('gists/comments/1'))) + + def test_DELETE(self, request_method): + request_method.return_value = mock_response('delete') + self.cs.delete(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('gists/comments/1'))) -- cgit v1.2.3-59-g8ed1b From 1abcce9551c83f3ad00303a8d4ef555c9c62f8a4 Mon Sep 17 00:00:00 2001 From: David Medina Date: Tue, 3 Apr 2012 23:16:49 +0200 Subject: Fix naming --- docs/repos.rst | 4 ++-- docs/users.rst | 4 ++-- pygithub3/github.py | 9 +++++---- pygithub3/services/repos/__init__.py | 6 +++--- pygithub3/tests/services/test_repos.py | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) (limited to 'pygithub3/tests') diff --git a/docs/repos.rst b/docs/repos.rst index 79e016a..c065671 100644 --- a/docs/repos.rst +++ b/docs/repos.rst @@ -1,6 +1,6 @@ .. _Repos service: -Repos's services +Repos services =================== **Fast sample**:: @@ -50,7 +50,7 @@ You can see it better with an example: :: Repo ------- -.. autoclass:: pygithub3.services.repos.Repos +.. autoclass:: pygithub3.services.repos.Repo :members: .. attribute:: collaborators diff --git a/docs/users.rst b/docs/users.rst index 5bd2e97..139d726 100644 --- a/docs/users.rst +++ b/docs/users.rst @@ -1,6 +1,6 @@ -.. _User service: +.. _Users service: -User's services +Users services =============== **Fast sample**:: diff --git a/pygithub3/github.py b/pygithub3/github.py index 1cf9b05..0b302a1 100644 --- a/pygithub3/github.py +++ b/pygithub3/github.py @@ -2,6 +2,7 @@ # -*- encoding: utf-8 -*- +#TODO: Move the imports out. setup related class Github(object): """ You can preconfigure all services globally with a ``config`` dict. See @@ -14,10 +15,10 @@ class Github(object): def __init__(self, **config): from pygithub3.services.users import User - from pygithub3.services.repos import Repos + from pygithub3.services.repos import Repo from pygithub3.services.gists import Gist self._users = User(**config) - self._repos = Repos(**config) + self._repos = Repo(**config) self._gists = Gist(**config) @property @@ -29,7 +30,7 @@ class Github(object): @property def users(self): """ - :ref:`User service ` + :ref:`Users service ` """ return self._users @@ -43,6 +44,6 @@ class Github(object): @property def gists(self): """ - :ref:`Gist service ` + :ref:`Gists service ` """ return self._gists diff --git a/pygithub3/services/repos/__init__.py b/pygithub3/services/repos/__init__.py index 3ef1fb4..628e9d6 100644 --- a/pygithub3/services/repos/__init__.py +++ b/pygithub3/services/repos/__init__.py @@ -11,7 +11,7 @@ from .watchers import Watchers from .hooks import Hooks -class Repos(Service): +class Repo(Service): """ Consume `Repos API `_ """ def __init__(self, **config): @@ -22,7 +22,7 @@ class Repos(Service): self.keys = Keys(**config) self.watchers = Watchers(**config) self.hooks = Hooks(**config) - super(Repos, self).__init__(**config) + super(Repo, self).__init__(**config) def list(self, user=None, type='all'): """ Get user's repositories @@ -133,7 +133,7 @@ class Repos(Service): return self.__list_contributors(user, repo) def list_contributors_with_anonymous(self, user=None, repo=None): - """ Like :attr:`~pygithub3.services.repos.Repos.list_contributors` plus + """ Like :attr:`~pygithub3.services.repos.Repo.list_contributors` plus anonymous """ return self.__list_contributors(user, repo, anom=True) diff --git a/pygithub3/tests/services/test_repos.py b/pygithub3/tests/services/test_repos.py index e77cc90..e21d474 100644 --- a/pygithub3/tests/services/test_repos.py +++ b/pygithub3/tests/services/test_repos.py @@ -5,7 +5,7 @@ import requests from mock import patch, Mock from pygithub3.tests.utils.core import TestCase -from pygithub3.services.repos import (Repos, Collaborators, Commits, Downloads, +from pygithub3.services.repos import (Repo, Collaborators, Commits, Downloads, Forks, Keys, Watchers, Hooks) from pygithub3.resources.base import json from pygithub3.tests.utils.base import (mock_response, mock_response_result, @@ -20,7 +20,7 @@ json.loads = Mock(side_effect=mock_json) class TestRepoService(TestCase): def setUp(self): - self.rs = Repos() + self.rs = Repo() self.rs.set_user('octocat') self.rs.set_repo('octocat_repo') -- cgit v1.2.3-59-g8ed1b