diff options
author | 2012-05-12 17:30:05 +0200 | |
---|---|---|
committer | 2012-05-12 17:45:07 +0200 | |
commit | cd7e1b88c9dfa5eaa126ece0a5529e224ae297d4 (patch) | |
tree | 57dcc80fea6827bf2349d958f8d66e7c421775b0 /pygithub3 | |
parent | Unify json imports (diff) | |
download | python-github3-cd7e1b88c9dfa5eaa126ece0a5529e224ae297d4.tar.xz python-github3-cd7e1b88c9dfa5eaa126ece0a5529e224ae297d4.zip |
Deleted 'validate_body' behaviour
'clean_body' had been developed to that functionality
Diffstat (limited to 'pygithub3')
-rw-r--r-- | pygithub3/requests/base.py | 52 | ||||
-rw-r--r-- | pygithub3/tests/requests/test_core.py | 17 | ||||
-rw-r--r-- | pygithub3/tests/utils/base.py | 13 |
3 files changed, 26 insertions, 56 deletions
diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 13472b0..f49a7a9 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -12,12 +12,12 @@ ABS_IMPORT_PREFIX = 'pygithub3.requests' class Body(object): + """ Input's request handler """ - def __init__(self, content, valid_body, validate_body=None): + def __init__(self, content, schema, required): self.content = content - self.schema = valid_body['schema'] - self.required = valid_body['required'] - self.validate_body = validate_body or (lambda x: None) + self.schema = schema + self.required = required def dumps(self): if not self.schema: @@ -25,12 +25,12 @@ class Body(object): return json.dumps(self.parse()) def parse(self): + """ Parse body with schema-required rules """ if not hasattr(self.content, 'items'): raise ValidationError("'%s' needs a content dictionary" % self.__class__.__name__) parsed = dict([(key, self.content[key]) for key in self.schema if key in self.content]) - self.validate_body(parsed) for attr_required in self.required: if attr_required not in parsed: raise ValidationError("'%s' attribute is required" % @@ -42,22 +42,34 @@ class Body(object): class Request(object): - """ """ uri = '' resource = Raw body_schema = {} def __init__(self, **kwargs): - """ """ - self.body = kwargs.pop('body', None) + self.body = kwargs.pop('body', {}) self.args = kwargs self.clean() + def __getattr__(self, name): + return self.args.get(name) + + def __str__(self): + return self.populate_uri() + + def populate_uri(self): + try: + populated_uri = self.uri.format(**self.args) + except KeyError: + raise ValidationError( + "'%s' request wasn't be able to populate the uri '%s' with " + "'%s' args" % (self.__class__.__name__, self.uri, self.args)) + return str(populated_uri).strip('/') + def clean(self): self.uri = self.clean_uri() or self.uri - self.body = Body(self.clean_body(), self.clean_valid_body(), - self.validate_body) + self.body = Body(self.clean_body(), **self._clean_valid_body()) def clean_body(self): return self.body @@ -65,7 +77,7 @@ class Request(object): def clean_uri(self): return None - def clean_valid_body(self): + def _clean_valid_body(self): schema = set(self.body_schema.get('schema', ())) required = set(self.body_schema.get('required', ())) if not required.issubset(schema): @@ -75,27 +87,9 @@ class Request(object): self.__class__.__name__, required, schema)) return dict(schema=schema, required=required) - def __getattr__(self, name): - return self.args.get(name) - - def __str__(self): - return self.populate_uri() - - def populate_uri(self): - try: - populated_uri = self.uri.format(**self.args) - except KeyError: - raise ValidationError( - "'%s' request wasn't be able to populate the uri '%s' with " - "'%s' args" % (self.__class__.__name__, self.uri, self.args)) - return str(populated_uri).strip('/') - def get_body(self): return self.body.dumps() - def validate_body(self, *args): - pass - class Factory(object): """ Request builder """ diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index 4632056..110f00e 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -2,14 +2,12 @@ # -*- encoding: utf-8 -*- from mock import Mock -from nose.tools import raises from pygithub3.tests.utils.core import TestCase from pygithub3.requests.base import Factory, Body, json, Request from pygithub3.exceptions import (UriInvalid, RequestDoesNotExist, ValidationError, InvalidBodySchema) -from pygithub3.tests.utils.base import (mock_json, DummyRequest, - DummyRequestValidation) +from pygithub3.tests.utils.base import mock_json, DummyRequest from pygithub3.tests.utils.requests import ( RequestWithArgs, RequestCleanedUri, RequestBodyInvalidSchema, RequestCleanedBody) @@ -76,7 +74,7 @@ class TestRequestBodyWithSchema(TestCase): def setUp(self): valid_body = dict(schema=('arg1', 'arg2'), required=('arg1', )) - self.b = Body({}, valid_body) + self.b = Body({}, **valid_body) def test_with_body_empty_and_schema_permissive(self): self.b.schema = ('arg1', 'arg2', '...') @@ -102,14 +100,3 @@ class TestRequestBodyWithSchema(TestCase): def test_only_valid_keys(self): self.b.content = dict(arg1='arg1', arg2='arg2', fake='test') self.assertEqual(self.b.dumps(), dict(arg1='arg1', arg2='arg2')) - - -class TestBodyValidation(TestCase): - @raises(ValidationError) - def test_with_error(self): - req = DummyRequestValidation( - body={'foo': 'bar', 'error': 'yes'}, - ) - req.body_schema = {'schema': ('foo',), - 'required': ('foo',)} - req.get_body() diff --git a/pygithub3/tests/utils/base.py b/pygithub3/tests/utils/base.py index b3c3b76..f90b5d3 100644 --- a/pygithub3/tests/utils/base.py +++ b/pygithub3/tests/utils/base.py @@ -4,7 +4,7 @@ from mock import Mock from pygithub3.resources.base import Resource -from pygithub3.requests.base import Request, ValidationError +from pygithub3.requests.base import Request def mock_json(content): @@ -37,14 +37,3 @@ DummyResource.loads = Mock(side_effect=loads_mock) class DummyRequest(Request): uri = 'dummyrequest' resource = DummyResource - - -class DummyRequestValidation(DummyRequest): - body_schema = { - 'schema': ('foo', 'error'), - 'required': ('foo',) - } - - def validate_body(self, body): - if body.get('error') == 'yes': - raise ValidationError('yes') |