55from mock import Mock
66
77from pygithub3 .requests import Factory , Body , json , Request
8- from pygithub3 .exceptions import UriInvalid , DoesNotExists , ValidationError
8+ from pygithub3 .exceptions import (UriInvalid , DoesNotExists , ValidationError ,
9+ InvalidBodySchema )
10+ from pygithub3 .tests .utils .base import mock_json
911from pygithub3 .tests .utils .requests import (
10- RequestWithArgs , RequestCleanedUri , RequestBodyWithSchema , mock_json ,
11- DummyRequest , RequestCleanedBody )
12+ RequestWithArgs , RequestCleanedUri , RequestBodyInvalidSchema , DummyRequest ,
13+ RequestCleanedBody )
1214
1315json .dumps = Mock (side_effect = mock_json )
1416json .loads = Mock (side_effect = mock_json )
@@ -35,7 +37,7 @@ def test_BUILDER_builds_users(self):
3537 self .assertIsInstance (request , Request )
3638
3739
38- class TestRequestUri (TestCase ):
40+ class TestRequest (TestCase ):
3941
4042 def test_SIMPLE_with_correct_args (self ):
4143 request = RequestWithArgs (arg1 = 'arg1' , arg2 = 'arg2' )
@@ -51,47 +53,45 @@ def test_with_cleaned_uri(self):
5153 request = RequestCleanedUri (notmatters = 'test' )
5254 self .assertEqual (str (request ), 'URI' )
5355
56+ def test_with_cleaned_body (self ):
57+ self .assertRaises (ValidationError , RequestCleanedBody )
5458
55- class TestRequestBody (TestCase ):
56-
57- def test_with_schema_with_valid (self ):
58- request = RequestBodyWithSchema (body = dict (
59- arg1 = 'only' , fake = 't' , fake1 = 't' ))
60- self .assertEqual (request .get_body (), dict (arg1 = 'only' ))
61-
62- def test_with_schema_with_invalid (self ):
63- request = RequestBodyWithSchema (body = 'invalid_data' )
64- self .assertRaises (ValidationError , request .get_body )
65-
66- def test_with_schema_without_body (self ):
67- request = RequestBodyWithSchema ()
68- self .assertIsNone (request .get_body ())
59+ def test_with_invalid_schema (self ):
60+ self .assertRaises (InvalidBodySchema , RequestBodyInvalidSchema )
6961
70- def test_without_schema (self ):
62+ def test_body_without_schema (self ):
7163 request = DummyRequest (body = dict (arg1 = 'test' ))
7264 self .assertEqual (request .get_body (), dict (arg1 = 'test' ))
65+ self .assertEqual (request .body .schema , set (()))
66+ self .assertEqual (request .body .required , set (()))
7367
74- def test_without_schema_without_body (self ):
68+ def test_without_body_and_without_schema (self ):
7569 request = DummyRequest ()
7670 self .assertIsNone (request .get_body ())
7771
78- def test_with_clean_body (self ):
79- self .assertRaises (ValidationError , RequestCleanedBody )
8072
73+ class TestRequestBody (TestCase ):
74+
75+ def setUp (self ):
76+ valid_body = dict (schema = ('arg1' , 'arg2' ), required = ('arg1' , ))
77+ self .b = Body ({}, ** valid_body )
78+
79+ def test_with_required (self ):
80+ self .b .content = dict (arg1 = 'arg1' )
81+ self .assertEqual (self .b .dumps (), dict (arg1 = 'arg1' ))
82+
83+ def test_without_required (self ):
84+ self .b .content = dict (arg2 = 'arg2' )
85+ self .assertRaises (ValidationError , self .b .dumps )
86+
87+ def test_with_invalid (self ):
88+ self .b .content = 'invalid'
89+ self .assertRaises (ValidationError , self .b .dumps )
8190
82- class TestBodyParsers (TestCase ):
91+ def test_without_body (self ):
92+ self .b .content = None
93+ self .assertIsNone (self .b .dumps ())
8394
8495 def test_only_valid_keys (self ):
85- body = Body (
86- dict (arg1 = 'arg1' , arg2 = 'arg2' , arg3 = 'arg3' , arg4 = 'arg4' ),
87- ('arg1' , 'arg3' , 'arg4' ))
88- self .assertEqual (body .parse (), dict (arg1 = 'arg1' , arg3 = 'arg3' ,
89- arg4 = 'arg4' ))
90-
91- def test_none (self ):
92- body = Body ({}, ('arg1' , 'arg2' ))
93- self .assertEqual (body .parse (), {})
94-
95- def test_invalid_content (self ):
96- body = Body ('invalid' , ('arg1' ,))
97- self .assertRaises (ValidationError , body .parse )
96+ self .b .content = dict (arg1 = 'arg1' , arg2 = 'arg2' , fake = 'test' )
97+ self .assertEqual (self .b .dumps (), dict (arg1 = 'arg1' , arg2 = 'arg2' ))
0 commit comments