|
1 | 1 | import gc
|
| 2 | +import typing as t |
2 | 3 | import uuid
|
3 | 4 |
|
4 | 5 | import pytest
|
@@ -753,31 +754,87 @@ def test_anyconverter():
|
753 | 754 | assert a.match("/a.2") == ("yes_dot", {"a": "a.2"})
|
754 | 755 |
|
755 | 756 |
|
| 757 | +@pytest.mark.parametrize( |
| 758 | + ("endpoint", "value", "expect"), |
| 759 | + [ |
| 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), |
| 766 | + ], |
| 767 | +) |
| 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 | + ) |
| 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}, |
| 808 | + ) |
| 809 | + adapter = url_map.bind("localhost") |
| 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})) |
| 816 | + |
| 817 | + |
756 | 818 | @pytest.mark.parametrize(
|
757 | 819 | ("value", "expect"),
|
758 | 820 | [
|
759 | 821 | (None, ""),
|
760 | 822 | ([None], ""),
|
761 | 823 | ([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"), |
| 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"), |
768 | 831 | ],
|
769 | 832 | )
|
770 | 833 | 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}" |
776 |
| - ) |
777 |
| - assert ( |
778 |
| - adapter.build("foo", {"name": "bar", "extra": value}, append_unknown=False) |
779 |
| - == "http://example.org/foo/bar" |
780 |
| - ) |
| 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) == "/" |
781 | 838 |
|
782 | 839 |
|
783 | 840 | def test_build_append_multiple():
|
|
0 commit comments