comparison tests/test_schema.py @ 19:c3a0fe8d4587

Consistent casing of schema items: all lowercase with dash as separator
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 08 Jul 2023 10:05:48 +0200
parents 2a9e7c4b717e
children db3491e1b590
comparison
equal deleted inserted replaced
18:94daf3381338 19:c3a0fe8d4587
1401 1401
1402 def test_d5(self): 1402 def test_d5(self):
1403 x = list(data_schema.validate( 1403 x = list(data_schema.validate(
1404 {"key": "value"}, 1404 {"key": "value"},
1405 {"type": "dict", 1405 {"type": "dict",
1406 "additionalKeys": False})) 1406 "additional-keys": False}))
1407 self.assertEqual(1, len(x)) 1407 self.assertEqual(1, len(x))
1408 self.assertEqual(ERRORS.E10004, x[0].code) 1408 self.assertEqual(ERRORS.E10004, x[0].code)
1409 self.assertEqual("key", x[0].hint) 1409 self.assertEqual("key", x[0].hint)
1410 1410
1411 def test_d5_2(self): 1411 def test_d5_2(self):
1412 x = list(data_schema.validate( 1412 x = list(data_schema.validate(
1413 {"key": "value"}, 1413 {"key": "value"},
1414 {"type": "dict", 1414 {"type": "dict",
1415 "additionalKeys": False}, 1415 "additional-keys": False},
1416 skip_keys=["key"])) 1416 skip_keys=["key"]))
1417 self.assertEqual(0, len(x)) 1417 self.assertEqual(0, len(x))
1418 1418
1419 def test_d5_3(self): 1419 def test_d5_3(self):
1420 x = list(data_schema.validate( 1420 x = list(data_schema.validate(
1421 {"key": "value", 1421 {"key": "value",
1422 "key2": "value"}, 1422 "key2": "value"},
1423 {"type": "dict", 1423 {"type": "dict",
1424 "additionalKeys": False}, 1424 "additional-keys": False},
1425 skip_keys=[re.compile(r"\Akey\d*\Z")])) 1425 skip_keys=[re.compile(r"\Akey\d*\Z")]))
1426 self.assertEqual(0, len(x)) 1426 self.assertEqual(0, len(x))
1427 1427
1428 def test_d5_4(self): 1428 def test_d5_4(self):
1429 x = list(data_schema.validate( 1429 x = list(data_schema.validate(
1430 {"key": "value", 1430 {"key": "value",
1431 "key2": "value"}, 1431 "key2": "value"},
1432 {"type": "dict", 1432 {"type": "dict",
1433 "additionalKeys": False}, 1433 "additional-keys": False},
1434 skip_keys=[re.compile(r"\A__.+"), re.compile(r"\Akey\d+\Z")])) 1434 skip_keys=[re.compile(r"\A__.+"), re.compile(r"\Akey\d+\Z")]))
1435 self.assertEqual(1, len(x)) 1435 self.assertEqual(1, len(x))
1436 self.assertEqual(ERRORS.E10004, x[0].code) 1436 self.assertEqual(ERRORS.E10004, x[0].code)
1437 self.assertEqual("key", x[0].hint) 1437 self.assertEqual("key", x[0].hint)
1438 1438
1439 def test_d6(self): 1439 def test_d6(self):
1440 x = list(data_schema.validate( 1440 x = list(data_schema.validate(
1441 {"key": "value"}, 1441 {"key": "value"},
1442 {"type": "dict", 1442 {"type": "dict",
1443 "additionalKeys": True})) 1443 "additional-keys": True}))
1444 self.assertEqual(0, len(x)) 1444 self.assertEqual(0, len(x))
1445 1445
1446 def test_d7(self): 1446 def test_d7(self):
1447 x = list(data_schema.validate( 1447 x = list(data_schema.validate(
1448 {"key": "value"}, 1448 {"key": "value"},
1449 {"type": "dict", 1449 {"type": "dict",
1450 "additionalKeys": { 1450 "additional-keys": {
1451 "type": "string"}})) 1451 "type": "string"}}))
1452 self.assertEqual(0, len(x)) 1452 self.assertEqual(0, len(x))
1453 1453
1454 def test_d8(self): 1454 def test_d8(self):
1455 x = list(data_schema.validate( 1455 x = list(data_schema.validate(
1456 {"key": 1234}, 1456 {"key": 1234},
1457 {"type": "dict", 1457 {"type": "dict",
1458 "additionalKeys": { 1458 "additional-keys": {
1459 "type": "string"}})) 1459 "type": "string"}}))
1460 self.assertEqual(1, len(x)) 1460 self.assertEqual(1, len(x))
1461 self.assertEqual(ERRORS.E10002, x[0].code) 1461 self.assertEqual(ERRORS.E10002, x[0].code)
1462 self.assertEqual(1234, x[0].hint) 1462 self.assertEqual(1234, x[0].hint)
1463 1463
1464 def test_d8_2(self): 1464 def test_d8_2(self):
1465 x = list(data_schema.validate( 1465 x = list(data_schema.validate(
1466 {"key": 1234}, 1466 {"key": 1234},
1467 {"type": "dict", 1467 {"type": "dict",
1468 "additionalKeys": { 1468 "additional-keys": {
1469 "type": "string"}}, 1469 "type": "string"}},
1470 skip_keys=["key"])) 1470 skip_keys=["key"]))
1471 self.assertEqual(0, len(x)) 1471 self.assertEqual(0, len(x))
1472 1472
1473 def test_d9_non_string_keys(self): 1473 def test_d9_non_string_keys(self):
1474 pr = list(data_schema.validate( 1474 pr = list(data_schema.validate(
1475 {0: "value"}, 1475 {0: "value"},
1476 {"type": "dict", 1476 {"type": "dict",
1477 "additionalKeys": True})) 1477 "additional-keys": True}))
1478 self.assertEqual(1, len(pr)) 1478 self.assertEqual(1, len(pr))
1479 self.assertEqual(ERRORS.E10003, pr[0].code) 1479 self.assertEqual(ERRORS.E10003, pr[0].code)
1480 1480
1481 def test_d10_int_dict_keys(self): 1481 def test_d10_int_dict_keys(self):
1482 pr = list(data_schema.validate( 1482 pr = list(data_schema.validate(
1483 {1: "value", 2: "value2"}, 1483 {1: "value", 2: "value2"},
1484 {"type": "dict", 1484 {"type": "dict",
1485 "keys": { 1485 "keys": {
1486 1: {"type": "string"}}, 1486 1: {"type": "string"}},
1487 "additionalKeys": True, 1487 "additional-keys": True,
1488 "keyNames": {"type": "int"}})) 1488 "key-names": {"type": "int"}}))
1489 self.assertEqual(0, len(pr)) 1489 self.assertEqual(0, len(pr))
1490 1490
1491 def test_error_message(self): 1491 def test_error_message(self):
1492 self.assertEqual("dict expected", 1492 self.assertEqual("dict expected",
1493 data_schema.problem_message(ERRORS.E10000)) 1493 data_schema.problem_message(ERRORS.E10000))
1525 1525
1526 def test_str_minlen(self): 1526 def test_str_minlen(self):
1527 pr = list(data_schema.validate( 1527 pr = list(data_schema.validate(
1528 "", 1528 "",
1529 {"type": "string", 1529 {"type": "string",
1530 "minLength": 0})) 1530 "min-length": 0}))
1531 self.assertEqual(0, len(pr)) 1531 self.assertEqual(0, len(pr))
1532 1532
1533 pr = list(data_schema.validate( 1533 pr = list(data_schema.validate(
1534 "", 1534 "",
1535 {"type": "string", 1535 {"type": "string",
1536 "minLength": 1})) 1536 "min-length": 1}))
1537 self.assertEqual(1, len(pr)) 1537 self.assertEqual(1, len(pr))
1538 self.assertEqual(ERRORS.E10006, pr[0].code) 1538 self.assertEqual(ERRORS.E10006, pr[0].code)
1539 1539
1540 pr = list(data_schema.validate( 1540 pr = list(data_schema.validate(
1541 "x", 1541 "x",
1542 {"type": "string", 1542 {"type": "string",
1543 "minLength": 1})) 1543 "min-length": 1}))
1544 self.assertEqual(0, len(pr)) 1544 self.assertEqual(0, len(pr))
1545 1545
1546 def test_str_maxlen(self): 1546 def test_str_maxlen(self):
1547 pr = list(data_schema.validate( 1547 pr = list(data_schema.validate(
1548 "", 1548 "",
1549 {"type": "string", 1549 {"type": "string",
1550 "maxLength": 0})) 1550 "max-length": 0}))
1551 self.assertEqual(0, len(pr)) 1551 self.assertEqual(0, len(pr))
1552 1552
1553 pr = list(data_schema.validate( 1553 pr = list(data_schema.validate(
1554 "x", 1554 "x",
1555 {"type": "string", 1555 {"type": "string",
1556 "maxLength": 0})) 1556 "max-length": 0}))
1557 self.assertEqual(1, len(pr)) 1557 self.assertEqual(1, len(pr))
1558 self.assertEqual(ERRORS.E10007, pr[0].code) 1558 self.assertEqual(ERRORS.E10007, pr[0].code)
1559 1559
1560 pr = list(data_schema.validate( 1560 pr = list(data_schema.validate(
1561 "x", 1561 "x",
1562 {"type": "string", 1562 {"type": "string",
1563 "maxLength": 1})) 1563 "max-length": 1}))
1564 self.assertEqual(0, len(pr)) 1564 self.assertEqual(0, len(pr))
1565 1565
1566 pr = list(data_schema.validate( 1566 pr = list(data_schema.validate(
1567 b"x", 1567 b"x",
1568 {"type": "string", 1568 {"type": "string",
1569 "maxLength": 1})) 1569 "max-length": 1}))
1570 self.assertEqual(1, len(pr)) 1570 self.assertEqual(1, len(pr))
1571 self.assertEqual(ERRORS.E10002, pr[0].code) 1571 self.assertEqual(ERRORS.E10002, pr[0].code)
1572 1572
1573 @staticmethod 1573 @staticmethod
1574 def _pattern_check_function(obj, schema, context=None): 1574 def _pattern_check_function(obj, schema, context=None):
1667 1667
1668 def test_binary_length(self): 1668 def test_binary_length(self):
1669 pr = list(data_schema.validate( 1669 pr = list(data_schema.validate(
1670 b"", 1670 b"",
1671 {"type": "binary", 1671 {"type": "binary",
1672 "minLength": 1})) 1672 "min-length": 1}))
1673 self.assertEqual(1, len(pr)) 1673 self.assertEqual(1, len(pr))
1674 self.assertEqual(ERRORS.E10036, pr[0].code) 1674 self.assertEqual(ERRORS.E10036, pr[0].code)
1675 1675
1676 pr = list(data_schema.validate( 1676 pr = list(data_schema.validate(
1677 b"1", 1677 b"1",
1678 {"type": "binary", 1678 {"type": "binary",
1679 "maxLength": 0})) 1679 "max-length": 0}))
1680 self.assertEqual(1, len(pr)) 1680 self.assertEqual(1, len(pr))
1681 self.assertEqual(ERRORS.E10037, pr[0].code) 1681 self.assertEqual(ERRORS.E10037, pr[0].code)
1682 1682
1683 def test_deny(self): 1683 def test_deny(self):
1684 pr = list(data_schema.validate("abc", {"type": "deny"})) 1684 pr = list(data_schema.validate("abc", {"type": "deny"}))
1794 ["a", None, {"key": "value"}], 1794 ["a", None, {"key": "value"}],
1795 {"type": "tuple", 1795 {"type": "tuple",
1796 "items": [ 1796 "items": [
1797 {"type": "string"}, 1797 {"type": "string"},
1798 {"type": None}], 1798 {"type": None}],
1799 "additionalItems": True})) 1799 "additional-items": True}))
1800 self.assertEqual(0, len(pr)) 1800 self.assertEqual(0, len(pr))
1801 1801
1802 def test_t6(self): 1802 def test_t6(self):
1803 pr = list(data_schema.validate( 1803 pr = list(data_schema.validate(
1804 ["a", None, {"key": "value"}, {"key": "value"}], 1804 ["a", None, {"key": "value"}, {"key": "value"}],
1805 {"type": "tuple", 1805 {"type": "tuple",
1806 "items": [ 1806 "items": [
1807 {"type": "string"}, 1807 {"type": "string"},
1808 {"type": None}], 1808 {"type": None}],
1809 "additionalItems": { 1809 "additional-items": {
1810 "type": "dict", 1810 "type": "dict",
1811 "keys": {"key": {"type": "string"}} 1811 "keys": {"key": {"type": "string"}}
1812 }})) 1812 }}))
1813 self.assertEqual(0, len(pr)) 1813 self.assertEqual(0, len(pr))
1814 1814
1815 def test_t7(self): 1815 def test_t7(self):
1816 # do not check anything that exceeds maxLength 1816 # do not check anything that exceeds max-length
1817 pr = list(data_schema.validate( 1817 pr = list(data_schema.validate(
1818 ["a", None, {"key": "value"}, {"key": "value"}, {"key2": "value"}], 1818 ["a", None, {"key": "value"}, {"key": "value"}, {"key2": "value"}],
1819 {"type": "tuple", 1819 {"type": "tuple",
1820 "maxLength": 4, 1820 "max-length": 4,
1821 "items": [ 1821 "items": [
1822 {"type": "string"}, 1822 {"type": "string"},
1823 {"type": None}], 1823 {"type": None}],
1824 "additionalItems": { 1824 "additional-items": {
1825 "type": "dict", 1825 "type": "dict",
1826 "keys": {"key": {"type": "string"}} 1826 "keys": {"key": {"type": "string"}}
1827 }})) 1827 }}))
1828 self.assertEqual(1, len(pr)) 1828 self.assertEqual(1, len(pr))
1829 self.assertEqual(ERRORS.E10016, pr[0].code) 1829 self.assertEqual(ERRORS.E10016, pr[0].code)
1830 1830
1831 def test_t8(self): 1831 def test_t8(self):
1832 # do not check anything that exceeds maxLength 1832 # do not check anything that exceeds max-length
1833 pr = list(data_schema.validate( 1833 pr = list(data_schema.validate(
1834 ["a", None, {"key": "value"}, {"key": "value"}, {"key2": "value"}], 1834 ["a", None, {"key": "value"}, {"key": "value"}, {"key2": "value"}],
1835 {"type": "tuple", 1835 {"type": "tuple",
1836 "minLength": 6, 1836 "min-length": 6,
1837 "maxLength": 4, 1837 "max-length": 4,
1838 "items": [ 1838 "items": [
1839 {"type": "string"}, 1839 {"type": "string"},
1840 {"type": None}], 1840 {"type": None}],
1841 "additionalItems": { 1841 "additional-items": {
1842 "type": "dict", 1842 "type": "dict",
1843 "keys": {"key": {"type": "string"}} 1843 "keys": {"key": {"type": "string"}}
1844 }})) 1844 }}))
1845 self.assertEqual(2, len(pr)) 1845 self.assertEqual(2, len(pr))
1846 self.assertEqual(ERRORS.E10015, pr[0].code) 1846 self.assertEqual(ERRORS.E10015, pr[0].code)
1847 self.assertEqual(ERRORS.E10016, pr[1].code) 1847 self.assertEqual(ERRORS.E10016, pr[1].code)
1848 1848
1849 def test_set1(self): 1849 def test_set1(self):
1850 # do not check anything that exceeds maxLength 1850 # do not check anything that exceeds max-length
1851 pr = list(data_schema.validate( 1851 pr = list(data_schema.validate(
1852 set(["a", None, "b"]), 1852 set(["a", None, "b"]),
1853 {"type": "set", 1853 {"type": "set",
1854 "minLength": 3, 1854 "min-length": 3,
1855 "items": {"any-of": [ 1855 "items": {"any-of": [
1856 {"type": "string"}, 1856 {"type": "string"},
1857 {"type": None}]}} 1857 {"type": None}]}}
1858 )) 1858 ))
1859 self.assertEqual(0, len(pr)) 1859 self.assertEqual(0, len(pr))
1860 1860
1861 def test_set1_not_nullable(self): 1861 def test_set1_not_nullable(self):
1862 # do not check anything that exceeds maxLength 1862 # do not check anything that exceeds max-length
1863 pr = list(data_schema.validate( 1863 pr = list(data_schema.validate(
1864 None, 1864 None,
1865 {"type": "set"})) 1865 {"type": "set"}))
1866 self.assertEqual(1, len(pr)) 1866 self.assertEqual(1, len(pr))
1867 self.assertEqual(ERRORS.E10038, pr[0].code) 1867 self.assertEqual(ERRORS.E10038, pr[0].code)
1868 1868
1869 def test_set1_nullable(self): 1869 def test_set1_nullable(self):
1870 # do not check anything that exceeds maxLength 1870 # do not check anything that exceeds max-length
1871 pr = list(data_schema.validate( 1871 pr = list(data_schema.validate(
1872 None, 1872 None,
1873 {"type": "set", "nullable": True})) 1873 {"type": "set", "nullable": True}))
1874 self.assertEqual(0, len(pr)) 1874 self.assertEqual(0, len(pr))
1875 1875
1876 def test_set2(self): 1876 def test_set2(self):
1877 # do not check anything that exceeds maxLength 1877 # do not check anything that exceeds max-length
1878 pr = list(data_schema.validate( 1878 pr = list(data_schema.validate(
1879 set(["a", None, "b"]), 1879 set(["a", None, "b"]),
1880 {"type": "set", 1880 {"type": "set",
1881 "minLength": 4, 1881 "min-length": 4,
1882 "items": {"any-of": [ 1882 "items": {"any-of": [
1883 {"type": "string"}, 1883 {"type": "string"},
1884 {"type": None}]}} 1884 {"type": None}]}}
1885 )) 1885 ))
1886 self.assertEqual(1, len(pr)) 1886 self.assertEqual(1, len(pr))
1887 self.assertEqual(ERRORS.E10039, pr[0].code) 1887 self.assertEqual(ERRORS.E10039, pr[0].code)
1888 1888
1889 def test_set3(self): 1889 def test_set3(self):
1890 # do not check anything that exceeds maxLength 1890 # do not check anything that exceeds max-length
1891 pr = list(data_schema.validate( 1891 pr = list(data_schema.validate(
1892 set(["a", None, "b"]), 1892 set(["a", None, "b"]),
1893 {"type": "set", 1893 {"type": "set",
1894 "maxLength": 2, 1894 "max-length": 2,
1895 "items": {"any-of": [ 1895 "items": {"any-of": [
1896 {"type": "string"}, 1896 {"type": "string"},
1897 {"type": None}]}} 1897 {"type": None}]}}
1898 )) 1898 ))
1899 self.assertEqual(1, len(pr)) 1899 self.assertEqual(1, len(pr))
2165 self.assertEqual(ERRORS.E10023, pr[0].code) 2165 self.assertEqual(ERRORS.E10023, pr[0].code)
2166 2166
2167 pr = list(data_schema.validate( 2167 pr = list(data_schema.validate(
2168 2, 2168 2,
2169 {"type": "int", 2169 {"type": "int",
2170 "minValue": 3, 2170 "min-value": 3,
2171 "maxValue": 1})) 2171 "max-value": 1}))
2172 2172
2173 self.assertEqual(2, len(pr)) 2173 self.assertEqual(2, len(pr))
2174 self.assertEqual(ERRORS.E10021, pr[0].code) 2174 self.assertEqual(ERRORS.E10021, pr[0].code)
2175 self.assertEqual(ERRORS.E10022, pr[1].code) 2175 self.assertEqual(ERRORS.E10022, pr[1].code)
2176 2176
2183 self.assertEqual(ERRORS.E10023, pr[0].code) 2183 self.assertEqual(ERRORS.E10023, pr[0].code)
2184 2184
2185 pr = list(data_schema.validate( 2185 pr = list(data_schema.validate(
2186 2.0, 2186 2.0,
2187 {"type": "real", 2187 {"type": "real",
2188 "minValue": 2.1, 2188 "min-value": 2.1,
2189 "maxValue": 1.9})) 2189 "max-value": 1.9}))
2190 2190
2191 self.assertEqual(2, len(pr)) 2191 self.assertEqual(2, len(pr))
2192 self.assertEqual(ERRORS.E10024, pr[0].code) 2192 self.assertEqual(ERRORS.E10024, pr[0].code)
2193 self.assertEqual(ERRORS.E10025, pr[1].code) 2193 self.assertEqual(ERRORS.E10025, pr[1].code)
2194 2194
2199 self.assertEqual(0, len(pr)) 2199 self.assertEqual(0, len(pr))
2200 2200
2201 pr = list(data_schema.validate( 2201 pr = list(data_schema.validate(
2202 2.0, 2202 2.0,
2203 {"type": "number", 2203 {"type": "number",
2204 "minValue": 3, 2204 "min-value": 3,
2205 "maxValue": 1.3})) 2205 "max-value": 1.3}))
2206 2206
2207 self.assertEqual(2, len(pr)) 2207 self.assertEqual(2, len(pr))
2208 self.assertEqual(ERRORS.E10031, pr[0].code) 2208 self.assertEqual(ERRORS.E10031, pr[0].code)
2209 self.assertEqual(ERRORS.E10032, pr[1].code) 2209 self.assertEqual(ERRORS.E10032, pr[1].code)
2210 2210