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.2.3-59-g8ed1b 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.2.3-59-g8ed1b