From 2ca15bb8e847735f566ee0cb896f071b3b5ca056 Mon Sep 17 00:00:00 2001 From: Nat Williams Date: Tue, 17 Apr 2012 14:39:12 -0500 Subject: let request objects specify custom body validations --- pygithub3/tests/requests/test_core.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'pygithub3/tests/requests') diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index cd162b3..9fcec10 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -2,12 +2,14 @@ # -*- 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, DoesNotExists, ValidationError, InvalidBodySchema) -from pygithub3.tests.utils.base import mock_json, DummyRequest +from pygithub3.tests.utils.base import (mock_json, DummyRequest, + DummyRequestValidation) from pygithub3.tests.utils.requests import ( RequestWithArgs, RequestCleanedUri, RequestBodyInvalidSchema, RequestCleanedBody) @@ -74,7 +76,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', '...') @@ -100,3 +102,14 @@ 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() -- cgit v1.3-14-g43fede From 17649c939901573af53beeeeb9d7be08102d1503 Mon Sep 17 00:00:00 2001 From: Nat Williams Date: Tue, 17 Apr 2012 14:42:57 -0500 Subject: more specific exception for missing Request classes --- pygithub3/exceptions.py | 2 +- pygithub3/requests/base.py | 12 ++++++------ pygithub3/tests/requests/test_core.py | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'pygithub3/tests/requests') diff --git a/pygithub3/exceptions.py b/pygithub3/exceptions.py index a467256..20cf058 100644 --- a/pygithub3/exceptions.py +++ b/pygithub3/exceptions.py @@ -8,7 +8,7 @@ class InvalidBodySchema(Exception): pass -class DoesNotExists(Exception): +class RequestDoesNotExist(Exception): """ Raised when `Request` factory can't find the subclass """ pass diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 582a73f..d3e18a3 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -7,8 +7,8 @@ try: except ImportError: import json -from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError, - InvalidBodySchema) +from pygithub3.exceptions import (RequestDoesNotExist, UriInvalid, + ValidationError, InvalidBodySchema) from pygithub3.resources.base import Raw from pygithub3.core.utils import import_module @@ -126,11 +126,11 @@ class Factory(object): % (ABS_IMPORT_PREFIX, module_chunk)) request = getattr(module, request_chunk) except ImportError: - raise DoesNotExists("'%s' module does not exists" - % module_chunk) + raise RequestDoesNotExist("'%s' module does not exist" + % module_chunk) except AttributeError: - raise DoesNotExists( - "'%s' request doesn't exists into '%s' module" + raise RequestDoesNotExist( + "'%s' request does not exist in '%s' module" % (request_chunk, module_chunk)) return func(self, request, **kwargs) return wrapper diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index 9fcec10..4632056 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -6,8 +6,8 @@ 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, DoesNotExists, ValidationError, - InvalidBodySchema) +from pygithub3.exceptions import (UriInvalid, RequestDoesNotExist, + ValidationError, InvalidBodySchema) from pygithub3.tests.utils.base import (mock_json, DummyRequest, DummyRequestValidation) from pygithub3.tests.utils.requests import ( @@ -29,8 +29,8 @@ class TestFactory(TestCase): self.assertRaises(UriInvalid, self.f, '.invalid') def test_BUILDER_with_fake_action(self): - self.assertRaises(DoesNotExists, self.f, 'users.fake') - self.assertRaises(DoesNotExists, self.f, 'fake.users') + self.assertRaises(RequestDoesNotExist, self.f, 'users.fake') + self.assertRaises(RequestDoesNotExist, self.f, 'fake.users') def test_BUILDER_builds_users(self): """ Users.get as real test because it wouldn't be useful mock -- cgit v1.3-14-g43fede From cd7e1b88c9dfa5eaa126ece0a5529e224ae297d4 Mon Sep 17 00:00:00 2001 From: David Medina Date: Sat, 12 May 2012 17:30:05 +0200 Subject: Deleted 'validate_body' behaviour 'clean_body' had been developed to that functionality --- pygithub3/requests/base.py | 52 ++++++++++++++++------------------- pygithub3/tests/requests/test_core.py | 17 ++---------- pygithub3/tests/utils/base.py | 13 +-------- 3 files changed, 26 insertions(+), 56 deletions(-) (limited to 'pygithub3/tests/requests') 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') -- cgit v1.3-14-g43fede