|
1 | 1 | import gc
|
| 2 | +import typing as t |
2 | 3 | import uuid
|
3 | 4 |
|
4 | 5 | import pytest
|
@@ -754,48 +755,108 @@ def test_anyconverter():
|
754 | 755 |
|
755 | 756 |
|
756 | 757 | @pytest.mark.parametrize(
|
757 |
| - ("value", "expect"), |
| 758 | + ("endpoint", "value", "expect"), |
758 | 759 | [
|
759 |
| - (None, ""), |
760 |
| - ([None], ""), |
761 |
| - ([None, None], ""), |
762 |
| - ("", "?extra="), |
763 |
| - ([""], "?extra="), |
764 |
| - (1.0, "?extra=1.0"), |
765 |
| - ([1, 2], "?extra=1&extra=2"), |
766 |
| - ([1, None, 2], "?extra=1&extra=2"), |
767 |
| - ([1, "", 2], "?extra=1&extra=&extra=2"), |
| 760 | + ("int", 1, "/1"), |
| 761 | + ("int", None, r.BuildError), |
| 762 | + ("int", [1], TypeError), |
| 763 | + ("list", [1], "/1"), |
| 764 | + ("list", [1, None, 2], "/1.None.2"), |
| 765 | + ("list", 1, TypeError), |
768 | 766 | ],
|
769 | 767 | )
|
770 |
| -def test_build_append_unknown(value, expect): |
771 |
| - map = r.Map([r.Rule("/foo/<name>", endpoint="foo")]) |
772 |
| - adapter = map.bind("example.org", "/", subdomain="test") |
773 |
| - assert ( |
774 |
| - adapter.build("foo", {"name": "bar", "extra": value}) |
775 |
| - == f"http://example.org/foo/bar{expect}" |
| 768 | +def test_build_values_dict(endpoint, value, expect): |
| 769 | + class ListConverter(r.BaseConverter): |
| 770 | + def to_url(self, value: t.Any) -> str: |
| 771 | + return super().to_url(".".join(map(str, value))) |
| 772 | + |
| 773 | + url_map = r.Map( |
| 774 | + [r.Rule("/<int:v>", endpoint="int"), r.Rule("/<list:v>", endpoint="list")], |
| 775 | + converters={"list": ListConverter}, |
776 | 776 | )
|
777 |
| - assert ( |
778 |
| - adapter.build("foo", {"name": "bar", "extra": value}, append_unknown=False) |
779 |
| - == "http://example.org/foo/bar" |
| 777 | + adapter = url_map.bind("localhost") |
| 778 | + |
| 779 | + if isinstance(expect, str): |
| 780 | + assert adapter.build(endpoint, {"v": value}) == expect |
| 781 | + else: |
| 782 | + with pytest.raises(expect): |
| 783 | + adapter.build(endpoint, {"v": value}) |
| 784 | + |
| 785 | + |
| 786 | +@pytest.mark.parametrize( |
| 787 | + ("endpoint", "value", "expect"), |
| 788 | + [ |
| 789 | + ("int", 1, "/1"), |
| 790 | + ("int", [1], "/1"), |
| 791 | + ("int", [], r.BuildError), |
| 792 | + ("int", None, TypeError), |
| 793 | + ("int", [None], TypeError), |
| 794 | + ("list", 1, TypeError), |
| 795 | + ("list", [1], TypeError), |
| 796 | + ("list", [[1]], "/1"), |
| 797 | + ("list", [1, None, 2], "/1.None.2"), |
| 798 | + ], |
| 799 | +) |
| 800 | +def test_build_values_multidict(endpoint, value, expect): |
| 801 | + class ListConverter(r.BaseConverter): |
| 802 | + def to_url(self, value: t.Any) -> str: |
| 803 | + return super().to_url(".".join(map(str, value))) |
| 804 | + |
| 805 | + url_map = r.Map( |
| 806 | + [r.Rule("/<int:v>", endpoint="int"), r.Rule("/<list:v>", endpoint="list")], |
| 807 | + converters={"list": ListConverter}, |
780 | 808 | )
|
| 809 | + adapter = url_map.bind("localhost") |
781 | 810 |
|
| 811 | + if isinstance(expect, str): |
| 812 | + assert adapter.build(endpoint, MultiDict({"v": value})) == expect |
| 813 | + else: |
| 814 | + with pytest.raises(expect): |
| 815 | + adapter.build(endpoint, MultiDict({"v": value})) |
782 | 816 |
|
783 |
| -def test_build_append_multiple(): |
784 |
| - map = r.Map([r.Rule("/bar/<float:foo>", endpoint="endp")]) |
785 |
| - adapter = map.bind("example.org", "/", subdomain="subd") |
786 |
| - params = {"foo": 0.815, "x": [1.0, 3.0], "y": 2.0} |
787 |
| - a, b = adapter.build("endp", params).split("?") |
788 |
| - assert a == "http://example.org/bar/0.815" |
789 |
| - assert set(b.split("&")) == set("y=2.0&x=1.0&x=3.0".split("&")) |
| 817 | + |
| 818 | +@pytest.mark.parametrize( |
| 819 | + ("value", "expect"), |
| 820 | + [ |
| 821 | + (None, ""), |
| 822 | + ([None], ""), |
| 823 | + ([None, None], ""), |
| 824 | + ("", "?v="), |
| 825 | + ([""], "?v="), |
| 826 | + (0, "?v=0"), |
| 827 | + (1.0, "?v=1.0"), |
| 828 | + ([1, 2], "?v=1&v=2"), |
| 829 | + ([1, None, 2], "?v=1&v=2"), |
| 830 | + ([1, "", 2], "?v=1&v=&v=2"), |
| 831 | + ], |
| 832 | +) |
| 833 | +def test_build_append_unknown_dict(value, expect): |
| 834 | + map = r.Map([r.Rule("/", endpoint="a")]) |
| 835 | + adapter = map.bind("localhost") |
| 836 | + assert adapter.build("a", {"v": value}) == f"/{expect}" |
| 837 | + assert adapter.build("a", {"v": value}, append_unknown=False) == "/" |
790 | 838 |
|
791 | 839 |
|
792 |
| -def test_build_append_multidict(): |
793 |
| - map = r.Map([r.Rule("/bar/<float:foo>", endpoint="endp")]) |
794 |
| - adapter = map.bind("example.org", "/", subdomain="subd") |
795 |
| - params = MultiDict((("foo", 0.815), ("x", 1.0), ("x", 3.0), ("y", 2.0))) |
796 |
| - a, b = adapter.build("endp", params).split("?") |
797 |
| - assert a == "http://example.org/bar/0.815" |
798 |
| - assert set(b.split("&")) == set("y=2.0&x=1.0&x=3.0".split("&")) |
| 840 | +@pytest.mark.parametrize( |
| 841 | + ("value", "expect"), |
| 842 | + [ |
| 843 | + (None, ""), |
| 844 | + ([None], ""), |
| 845 | + ([None, None], ""), |
| 846 | + ("", "?v="), |
| 847 | + ([""], "?v="), |
| 848 | + (0, "?v=0"), |
| 849 | + (1.0, "?v=1.0"), |
| 850 | + ([1, 2], "?v=1&v=2"), |
| 851 | + ([1, None, 2], "?v=1&v=2"), |
| 852 | + ([1, "", 2], "?v=1&v=&v=2"), |
| 853 | + ], |
| 854 | +) |
| 855 | +def test_build_append_unknown_multidict(value, expect): |
| 856 | + map = r.Map([r.Rule("/", endpoint="a")]) |
| 857 | + adapter = map.bind("localhost") |
| 858 | + assert adapter.build("a", MultiDict({"v": value})) == f"/{expect}" |
| 859 | + assert adapter.build("a", MultiDict({"v": value}), append_unknown=False) == "/" |
799 | 860 |
|
800 | 861 |
|
801 | 862 | def test_build_drop_none():
|
|
0 commit comments