|
8 | 8 | import pytest |
9 | 9 |
|
10 | 10 | from voluptuous import ( |
11 | | - ALLOW_EXTRA, PREVENT_EXTRA, All, AllInvalid, Any, Clamp, Coerce, Contains, |
| 11 | + ALLOW_EXTRA, PREVENT_EXTRA, REMOVE_EXTRA, All, AllInvalid, Any, Clamp, Coerce, Contains, |
12 | 12 | ContainsInvalid, Date, Datetime, Email, EmailInvalid, Equal, ExactSequence, |
13 | 13 | Exclusive, Extra, FqdnUrl, In, Inclusive, InInvalid, Invalid, IsDir, IsFile, Length, |
14 | 14 | Literal, LiteralInvalid, Marker, Match, MatchInvalid, Maybe, MultipleInvalid, NotIn, |
@@ -1704,22 +1704,95 @@ def as_int(a): |
1704 | 1704 | assert str(ctx.value.errors[1]) == "expecting a number @ data['four']" |
1705 | 1705 |
|
1706 | 1706 |
|
1707 | | -def test_key3(): |
| 1707 | +def test_any_with_extra_allow(): |
1708 | 1708 | schema = Schema( |
1709 | 1709 | { |
1710 | 1710 | Any("name", "area"): str, |
1711 | 1711 | "domain": str, |
1712 | 1712 | }, |
1713 | 1713 | extra=ALLOW_EXTRA, |
1714 | 1714 | ) |
1715 | | - schema( |
| 1715 | + |
| 1716 | + result = schema( |
| 1717 | + { |
| 1718 | + "name": "one", |
| 1719 | + "domain": "two", |
| 1720 | + "additional_key": "extra", |
| 1721 | + } |
| 1722 | + ) |
| 1723 | + |
| 1724 | + assert result == { |
| 1725 | + "name": "one", |
| 1726 | + "domain": "two", |
| 1727 | + "additional_key": "extra", |
| 1728 | + } |
| 1729 | + |
| 1730 | + |
| 1731 | +def test_any_with_extra_remove(): |
| 1732 | + schema = Schema( |
| 1733 | + { |
| 1734 | + Any("name", "area"): str, |
| 1735 | + "domain": str, |
| 1736 | + }, |
| 1737 | + extra=REMOVE_EXTRA, |
| 1738 | + ) |
| 1739 | + |
| 1740 | + result = schema( |
1716 | 1741 | { |
1717 | 1742 | "name": "one", |
1718 | 1743 | "domain": "two", |
1719 | 1744 | "additional_key": "extra", |
1720 | 1745 | } |
1721 | 1746 | ) |
1722 | 1747 |
|
| 1748 | + assert result == { |
| 1749 | + "name": "one", |
| 1750 | + "domain": "two", |
| 1751 | + } |
| 1752 | + |
| 1753 | + |
| 1754 | +def test_any_with_extra_prevent(): |
| 1755 | + schema = Schema( |
| 1756 | + { |
| 1757 | + Any("name", "area"): str, |
| 1758 | + "domain": str, |
| 1759 | + }, |
| 1760 | + extra=PREVENT_EXTRA, |
| 1761 | + ) |
| 1762 | + |
| 1763 | + with pytest.raises(MultipleInvalid) as ctx: |
| 1764 | + schema( |
| 1765 | + { |
| 1766 | + "name": "one", |
| 1767 | + "domain": "two", |
| 1768 | + "additional_key": "extra", |
| 1769 | + } |
| 1770 | + ) |
| 1771 | + |
| 1772 | + assert len(ctx.value.errors) == 1 |
| 1773 | + assert str(ctx.value.errors[0]) == "not a valid value @ data['additional_key']" |
| 1774 | + |
| 1775 | + |
| 1776 | +def test_any_with_extra_none(): |
| 1777 | + schema = Schema( |
| 1778 | + { |
| 1779 | + Any("name", "area"): str, |
| 1780 | + "domain": str, |
| 1781 | + }, |
| 1782 | + ) |
| 1783 | + |
| 1784 | + with pytest.raises(MultipleInvalid) as ctx: |
| 1785 | + schema( |
| 1786 | + { |
| 1787 | + "name": "one", |
| 1788 | + "domain": "two", |
| 1789 | + "additional_key": "extra", |
| 1790 | + } |
| 1791 | + ) |
| 1792 | + |
| 1793 | + assert len(ctx.value.errors) == 1 |
| 1794 | + assert str(ctx.value.errors[0]) == "not a valid value @ data['additional_key']" |
| 1795 | + |
1723 | 1796 |
|
1724 | 1797 | def test_coerce_enum(): |
1725 | 1798 | """Test Coerce Enum""" |
|
0 commit comments