Skip to content

Commit 92274ca

Browse files
committed
Update tests to support new json behaviour
Major change: *patch* json functions instead of override at module level
1 parent 9b2b5b9 commit 92274ca

File tree

18 files changed

+115
-141
lines changed

18 files changed

+115
-141
lines changed

pygithub3/services/base.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
#!/usr/bin/env python
21
# -*- encoding: utf-8 -*-
32

4-
from datetime import datetime
5-
63
from pygithub3.core.client import Client
74
from pygithub3.core.errors import NotFound
85
from pygithub3.core.result import smart, normal
96
from pygithub3.requests.base import Factory
10-
from pygithub3.resources.base import GITHUB_DATE_FORMAT
117

128

139
class Service(object):
@@ -44,15 +40,6 @@ def __init__(self, **config):
4440
self._client = Client(**config)
4541
self.request_builder = Factory()
4642

47-
def _normalize_date(self, key, _dict):
48-
""" If ``key`` comes as ``datetime``, it'll normalize it """
49-
try:
50-
key = str(key)
51-
date = datetime.strftime(_dict.get(key), GITHUB_DATE_FORMAT)
52-
_dict.update({key: date})
53-
except:
54-
pass
55-
5643
@property
5744
def remaining_requests(self):
5845
return Client.remaining_requests

pygithub3/services/issues/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def list(self, filter='assigned', state='open', labels='', sort='created',
3535
"""
3636
params = dict(filter=filter, state=state, labels=labels, sort=sort,
3737
direction=direction)
38-
self._normalize_date('since', params)
3938
request = self.request_builder('issues.list')
4039
return self._get_result(request, **params)
4140

@@ -60,7 +59,6 @@ def list_by_repo(self, user=None, repo=None, milestone='*', state='open',
6059
"""
6160
params = dict(milestone=milestone, state=state, assignee=assignee,
6261
mentioned=mentioned, labels=labels, sort=sort, direction=direction)
63-
self._normalize_date('since', params)
6462
request = self.make_request('issues.list_by_repo', user=user,
6563
repo=repo)
6664
return self._get_result(request, **params)

pygithub3/services/issues/milestones.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def create(self, data, user=None, repo=None):
5454
.. note::
5555
Remember :ref:`config precedence`
5656
"""
57-
self._normalize_date('due_on', data)
5857
request = self.make_request('issues.milestones.create', user=user,
5958
repo=repo, body=data)
6059
return self._post(request)
@@ -73,7 +72,6 @@ def update(self, number, data, user=None, repo=None):
7372
.. note::
7473
Remember :ref:`config precedence`
7574
"""
76-
self._normalize_date('due_on', data)
7775
request = self.make_request('issues.milestones.update', user=user,
7876
repo=repo, number=number, body=data)
7977
return self._patch(request)

pygithub3/tests/core/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from mock import patch
66

7-
from pygithub3.tests.utils.core import TestCase
87
from pygithub3.core.client import Client
98
from pygithub3.exceptions import NotFound, BadRequest, UnprocessableEntity
109
from pygithub3.tests.utils.base import mock_response
10+
from pygithub3.tests.utils.core import TestCase
1111

1212

1313
class TestClient(TestCase):

pygithub3/tests/core/test_json.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from datetime import datetime
4+
5+
from pygithub3.tests.utils.core import TestCase
6+
from pygithub3.core import json
7+
8+
class TestJson(TestCase):
9+
"""
10+
Test dates parse and normalize
11+
"""
12+
13+
dict_ = {
14+
'date_ok': datetime(2008, 1, 14, 4, 33, 35),
15+
'date_fake': 'some_fake',
16+
'list': [1, 2, 3],
17+
'nested_dict': {'with_date': datetime(2008, 1, 14, 4, 33, 35)},
18+
}
19+
json_ = '{"date_fake": "some_fake", "list": [1, 2, 3], "date_ok": "2008-01-14T04:33:35Z", "nested_dict": {"with_date": "2008-01-14T04:33:35Z"}}'
20+
21+
def test_encoder(self):
22+
to_json = json.dumps(self.dict_)
23+
self.assertEquals(to_json, self.json_)
24+
25+
def test_decoder(self):
26+
to_dict = json.loads(self.json_)
27+
self.assertEquals(self.dict_, to_dict)

pygithub3/tests/core/test_result.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33

44
from mock import Mock
55

6-
from pygithub3.tests.utils.core import TestCase
76
from pygithub3.core.client import Client
87
from pygithub3.core.result import smart, normal, base
9-
from pygithub3.tests.utils.core import (mock_paginate_github_in_GET, request,
10-
mock_no_paginate_github_in_GET,
11-
MockPaginate)
8+
from pygithub3.tests.utils.core import (TestCase, mock_paginate_github_in_GET,
9+
request, mock_no_paginate_github_in_GET, MockPaginate)
1210

1311

1412
class ResultInitMixin(object):

pygithub3/tests/requests/test_core.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
#!/usr/bin/env python
21
# -*- encoding: utf-8 -*-
32

4-
from mock import Mock
5-
6-
from pygithub3.tests.utils.core import TestCase
7-
from pygithub3.requests.base import Factory, Body, json, Request
83
from pygithub3.exceptions import (UriInvalid, RequestDoesNotExist,
9-
ValidationError, InvalidBodySchema)
10-
from pygithub3.tests.utils.base import mock_json, DummyRequest
11-
from pygithub3.tests.utils.requests import (
12-
RequestWithArgs, RequestCleanedUri, RequestBodyInvalidSchema,
13-
RequestCleanedBody)
14-
15-
json.dumps = Mock(side_effect=mock_json)
16-
json.loads = Mock(side_effect=mock_json)
4+
ValidationError, InvalidBodySchema)
5+
from pygithub3.requests.base import Factory, Body, Request
6+
from pygithub3.tests.utils.base import DummyRequest, dummy_json
7+
from pygithub3.tests.utils.core import TestCase
8+
from pygithub3.tests.utils.requests import (RequestWithArgs, RequestCleanedUri,
9+
RequestBodyInvalidSchema, RequestCleanedBody)
1710

1811

1912
class TestFactory(TestCase):
@@ -70,6 +63,7 @@ def test_without_body_and_without_schema(self):
7063
self.assertIsNone(request.get_body())
7164

7265

66+
@dummy_json
7367
class TestRequestBodyWithSchema(TestCase):
7468

7569
def setUp(self):

pygithub3/tests/resources/test_core.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
#!/usr/bin/env python
21
# -*- encoding: utf-8 -*-
32

43
from datetime import datetime
54

6-
from pygithub3.tests.utils.core import TestCase
5+
from pygithub3.core import json
76
from pygithub3.resources.base import Raw
7+
from pygithub3.tests.utils.core import TestCase
88
from pygithub3.tests.utils.resources import Nested, Simple, HasSimple
99

1010
simple_resource = dict(type='simple')
1111
has_simple = dict(type='has_simple', simple=simple_resource)
12-
github_return = dict(
12+
github_dict = dict(
1313
id=1,
1414
name='name_test',
1515
date='2008-01-14T04:33:35Z',
1616
simple=simple_resource,
1717
list_collection=[has_simple] * 2,
1818
items_collections=dict(arg1=has_simple, arg2=has_simple)
1919
)
20-
github_return_nested = github_return.copy()
21-
github_return.update({'self_nested': github_return_nested})
22-
github_return.update({'self_nested_list': [github_return_nested] * 2})
23-
github_return.update({'self_nested_dict': dict(arg1=github_return_nested)})
20+
github_dict_nested = github_dict.copy()
21+
github_dict.update({'self_nested': github_dict_nested})
22+
github_dict.update({'self_nested_list': [github_dict_nested] * 2})
23+
github_dict.update({'self_nested_dict': dict(arg1=github_dict_nested)})
24+
github_return = json.dumps(github_dict)
2425

2526

2627
class TestResourceMapping(TestCase):
@@ -61,8 +62,7 @@ def test_SELF_nested_in_collections(self):
6162

6263

6364
class TestRawResource(TestCase):
64-
""" Litle obvious :P """
6565

6666
def test_return_original_copy(self):
6767
self.r = Raw.loads(github_return)
68-
self.assertEqual(id(self.r), id(github_return))
68+
self.assertEqual(self.r['id'], github_dict['id'])

pygithub3/tests/services/test_core.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ def test_GET_result(self, request_method):
5555
self.assertFalse(request_method.called)
5656
self.assertIsInstance(result, base.Result)
5757

58-
def test_normalize_ok(self, request_method):
59-
data = {'test': datetime(2012, 12, 12, 3, 3, 3)}
60-
self.s._normalize_date('test', data)
61-
self.assertEqual(data['test'], '2012-12-12T03:03:03Z')
62-
63-
def test_normalize_fail(self, request_method):
64-
data = {'test': 'fail'}
65-
self.s._normalize_date('test', data)
66-
self.assertEqual(data['test'], 'fail')
67-
6858

6959
@patch.object(requests.sessions.Session, 'request')
7060
class TestMimeType(TestCase):

pygithub3/tests/services/test_gists.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
#!/usr/bin/env python
21
# -*- encoding: utf-8 -*-
32

43
import requests
5-
from mock import patch, Mock
4+
from mock import patch
65

7-
from pygithub3.tests.utils.core import TestCase
8-
from pygithub3.resources.base import json
96
from pygithub3.services.gists import Gist, Comments
10-
from pygithub3.tests.utils.base import (mock_response, mock_response_result,
11-
mock_json)
7+
from pygithub3.tests.utils.base import (dummy_json, mock_response,
8+
mock_response_result)
9+
from pygithub3.tests.utils.core import TestCase
1210
from pygithub3.tests.utils.services import _
1311

14-
json.dumps = Mock(side_effect=mock_json)
15-
json.loads = Mock(side_effect=mock_json)
16-
1712

13+
@dummy_json
1814
@patch.object(requests.sessions.Session, 'request')
1915
class TestGistService(TestCase):
2016

@@ -93,6 +89,7 @@ def test_DELETE(self, request_method):
9389
('delete', _('gists/1')))
9490

9591

92+
@dummy_json
9693
@patch.object(requests.sessions.Session, 'request')
9794
class TestCommentService(TestCase):
9895

0 commit comments

Comments
 (0)