def test_convert_types(self): route = itty3.Route("GET", self.complex_uri, self.mock_complex_view) # Sanity check. self.assertEqual( route._type_conversions, { "app_id": "uuid", "title": "slug", "major_version": "int", "release": "str", }, ) matches = { "app_id": "5fdd79e5-c417-42d7-8235-e7b6c6e10c06", "title": "content_dinos", "major_version": "2", "release": "test", } matches = route.convert_types(matches) self.assertEqual( matches, { "app_id": "5fdd79e5-c417-42d7-8235-e7b6c6e10c06", "title": "content_dinos", "major_version": 2, "release": "test", }, )
def setUp(self): self.mock_index_view = mock.Mock() self.mock_simple_view = mock.Mock() self.mock_complex_view = mock.Mock() self.mock_static_view = mock.Mock() self.route_1 = itty3.Route("GET", "/", self.mock_index_view) self.route_2 = itty3.Route("GET", "/greet/", self.mock_simple_view) self.route_3 = itty3.Route("GET", "/greet/<str:name>/<int:variant>/", self.mock_complex_view) self.route_4 = itty3.Route("POST", "/greet/<str:name>/<int:variant>/", self.mock_complex_view) self.route_5 = itty3.Route("GET", "/static/<any:asset_path>", self.mock_static_view) self.complex_uri = ("/app/<uuid:app_id>/<slug:title>/version/" "<int:major_version>/<str:release>/")
def test_extract_kwargs_no_variables(self): uri = "/app/" route = itty3.Route("GET", "/app/<uuid:app_id>/", self.mock_complex_view) # Sanity check. self.assertEqual(route._type_conversions, {"app_id": "uuid"}) self.assertEqual(route.extract_kwargs(uri), {})
def test_convert_types_float(self): route = itty3.Route("GET", "/charge/<float:money>/", self.mock_complex_view) matches = { "money": "22.95", } matches = route.convert_types(matches) self.assertEqual( matches, {"money": 22.95}, )
def test_convert_types_extra_matches(self): route = itty3.Route("GET", "/charge/<float:money>/", self.mock_complex_view) matches = { "money": "22.95", # This shouldn't really happen in practice, but... "hello": "world", } matches = route.convert_types(matches) self.assertEqual( matches, { "money": 22.95, "hello": "world" }, )
def test_extract_kwargs(self): uri = "/app/5fdd79e5-c417-42d7-8235-e7b6c6e10c06/content_dinos/version/2/test/" route = itty3.Route("GET", self.complex_uri, self.mock_complex_view) # Sanity check. self.assertEqual( route._type_conversions, { "app_id": "uuid", "title": "slug", "major_version": "int", "release": "str", }, ) self.assertEqual( route.extract_kwargs(uri), { "app_id": "5fdd79e5-c417-42d7-8235-e7b6c6e10c06", "title": "content_dinos", "major_version": 2, "release": "test", }, )