async def test_json_int(self): expected = 56 raw = json_mocked_request(56) request = RestRequest(raw) observed = await request.content() self.assertEqual(expected, observed)
async def test_with_type_classname(self): expected = 3 raw = json_mocked_request("3") request = RestRequest(raw) observed = await request.content(type_=classname(int)) self.assertEqual(expected, observed)
async def test_json_dict(self): expected = {"doors": 3, "color": "blue", "owner": None} raw = json_mocked_request({"doors": 3, "color": "blue", "owner": None}) request = RestRequest(raw) observed = await request.content() self.assertEqual(expected, observed)
async def test_json_list(self): expected = [{"doors": 3, "color": "blue", "owner": None}, {"doors": 5, "color": "red", "owner": None}] raw = json_mocked_request( [{"doors": 3, "color": "blue", "owner": None}, {"doors": 5, "color": "red", "owner": None}] ) request = RestRequest(raw) observed = await request.content() self.assertEqual(expected, observed)
async def test_with_model_type(self): expected = 3 raw = json_mocked_request("3") request = RestRequest(raw) with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) observed = await request.content(model_type=classname(int)) self.assertEqual(expected, observed)
async def test_with_type(self): # noinspection PyPep8Naming Car = ModelType.build("Car", {"doors": int, "color": str, "owner": type(None)}) expected = [Car(3, "blue", None), Car(5, "red", None)] raw = json_mocked_request([{"doors": "3", "color": "blue"}, {"doors": "5", "color": "red"}]) request = RestRequest(raw) observed = await request.content(type_=list[Car]) self.assertEqual(expected, observed)
async def test_get_callback_with_user(self): user = uuid4() async def _fn(request) -> None: self.assertEqual(user, request.user) self.assertEqual(user, REQUEST_USER_CONTEXT_VAR.get()) mock = AsyncMock(side_effect=_fn) handler = self.handler.get_callback(mock) await handler(json_mocked_request({"foo": "bar"}, user=user)) self.assertEqual(1, mock.call_count)
async def test_get_callback_raises_exception(self): handler = self.handler.get_callback(_Cls._fn_raises_exception) with self.assertRaises(HTTPInternalServerError): await handler(json_mocked_request({"foo": "bar"}))
async def test_get_callback_raises_response(self): handler = self.handler.get_callback(_Cls._fn_raises_response) with self.assertRaises(HTTPBadRequest): await handler(json_mocked_request({"foo": "bar"}))
async def test_get_callback(self): handler = self.handler.get_callback(_Cls._fn) response = await handler(json_mocked_request({"foo": "bar"})) self.assertIsInstance(response, web.Response) self.assertEqual('{"foo": "bar"}', response.text) self.assertEqual("application/json", response.content_type)
def test_has_content_true(self): raw = json_mocked_request(123) request = RestRequest(raw) self.assertEqual(True, request.has_content)