def test_eq_operator__considers_path_template_only(self): route1 = routes.CDNRoute("/foo/bar", {"hello", "world"}, sizable=False) route2 = routes.CDNRoute("/foo/bar", {"i", "said", "meow"}, sizable=True) route3 = routes.CDNRoute("/foo/bar", {"i", "said", "meow"}, sizable=False) route4 = routes.CDNRoute("/foo/bar/baz", {"i", "said", "meow"}, sizable=True) assert route1 == route2 assert route1 == route3 assert route1 != route4 assert route2 == route3 assert route2 != route4 assert route3 != route4
def test_hash_operator_considers_path_template_only(self): route1 = routes.CDNRoute("/foo/bar", {"hello", "world"}, sizable=False) route2 = routes.CDNRoute("/foo/bar", {"i", "said", "meow"}, sizable=True) route3 = routes.CDNRoute("/foo/bar", {"i", "said", "meow"}, sizable=False) route4 = routes.CDNRoute("/foo/bar/baz", {"i", "said", "meow"}, sizable=True) assert hash(route1) == hash(route2) assert hash(route1) == hash(route3) assert hash(route1) != hash(route4) assert hash(route2) == hash(route3) assert hash(route2) != hash(route4) assert hash(route3) != hash(route4)
def test_passing_size_on_sizable_does_not_raise_TypeError(self): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "gif"}, sizable=True) route.compile("http://example.com", file_format="png", hash="boooob", size=128)
def test_requesting_non_gif_on_animated_hash_does_not_raise_TypeError( self, format): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "webp", "gif"}, sizable=False) route.compile("http://example.com", file_format=format, hash="a_boooob")
def test_requesting_gif_on_non_animated_hash_raises_TypeError(self): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "gif"}, sizable=False) with pytest.raises(TypeError): route.compile("http://example.com", file_format="gif", hash="boooob")
def test_passing_no_size_does_not_add_query_string(self): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "gif"}, sizable=True) compiled_url = route.compile("http://example.com", file_format="png", hash="boooob") assert "?size=" not in compiled_url, f"compiled_url={compiled_url}"
def test_compile_uses_lowercase_file_format_always(self, input_file_format, expected_file_format): route = routes.CDNRoute("/foo/bar", {"png", "jpg"}, sizable=False) compiled_url = route.compile("http://example.com", file_format=input_file_format) assert compiled_url.endswith( f".{expected_file_format}"), f"compiled_url={compiled_url}"
def test_passing_negative_sizes_to_sizable_raises_ValueError(self, size): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "png"}, sizable=True) with pytest.raises(ValueError, match="size must be positive"): route.compile("http://example.com", file_format="png", hash="boooob", size=size)
def test_passing_size_on_non_sizable_raises_TypeError(self): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "gif"}, sizable=False) with pytest.raises(TypeError): route.compile("http://example.com", file_format="png", hash="boooob", size=128)
def test_passing_valid_sizes_to_sizable_does_not_raise_ValueError( self, size): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "gif"}, sizable=True) route.compile("http://example.com", file_format="png", hash="boooob", size=size)
def test_passing_size_adds_query_string(self): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "gif"}, sizable=True) compiled_url = route.compile("http://example.com", file_format="png", hash="boooob", size=128) assert compiled_url.endswith( ".png?size=128"), f"compiled_url={compiled_url}"
def test_compile_generates_expected_url(self, base_url, template, format, size_kwds, foo, bar, expected_url): route = routes.CDNRoute(template, {"png", "gif", "jpg", "webp"}, sizable=True) actual_url = route.compile(base_url=base_url, file_format=format, foo=foo, bar=bar, **size_kwds) assert actual_url == expected_url
def test_passing_invalid_magnitude_sizes_to_sizable_raises_ValueError( self, size): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "png"}, sizable=True) with pytest.raises( ValueError, match= "size must be an integer power of 2 between 16 and 4096 inclusive" ): route.compile("http://example.com", file_format="png", hash="boooob", size=size)
def test_formats_converted_to_frozenset(self): route = routes.CDNRoute("/foo/bar", {"i", "really", "like", "cats"}) assert isinstance(route.valid_formats, frozenset) assert route.valid_formats == {"i", "really", "like", "cats"}
def test_formats_converted_to_lower(self): route = routes.CDNRoute("/foo/bar", {"FOO", "BaR", "bAz", "bork"}) assert route.valid_formats == {"foo", "bar", "baz", "bork"}
def test_any_formats_results_in_no_error(self): routes.CDNRoute("/foo/bar", {"do", "ray", "me"})
def test_zero_formats_results_in_error(self): with pytest.raises( ValueError, match="/foo/bar must have at least one valid format set"): routes.CDNRoute("/foo/bar", set())
def test_disallowed_file_format_raises_TypeError(self): route = routes.CDNRoute("/foo/bar", {"png", "jpg"}, sizable=False) with pytest.raises(TypeError): route.compile("http://example.com", file_format="gif")
def test_allowed_file_format_does_not_raise_TypeError(self): route = routes.CDNRoute("/foo/bar", {"png", "jpg"}, sizable=False) route.compile("http://example.com", file_format="png")
def test_requesting_gif_without_passing_hash_does_not_raise_TypeError( self): route = routes.CDNRoute("/foo/bar", {"png", "jpg", "gif"}, sizable=False) route.compile("http://example.com", file_format="gif")