1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
|
import codecs
import logging
import re
import socket
from unittest import mock
import pytest
from mopidy.config import types
from mopidy.internal import log
@pytest.mark.parametrize(
"value, expected",
[
# bytes are coded from UTF-8 and string-escaped:
(b"abc", "abc"),
("æøå".encode(), "æøå"),
(b"a\nb", "a\nb"),
(b"a\\nb", "a\nb"),
# unicode strings are string-escaped:
("abc", "abc"),
("æøå", "æøå"),
("a\nb", "a\nb"),
("a\\nb", "a\nb"),
],
)
def test_decode(value, expected):
assert types.decode(value) == expected
@pytest.mark.parametrize(
"value, expected",
[
# unicode strings are string-escaped and encoded as UTF-8:
("abc", "abc"),
("æøå", "æøå"),
("a\nb", "a\\nb"),
# bytes are string-escaped:
(b"abc", "abc"),
(b"a\nb", "a\\nb"),
],
)
def test_encode(value, expected):
assert types.encode(value) == expected
def test_encode_decode_invalid_utf8():
data = b"\xc3\x00" # invalid utf-8
result = types.encode(types.decode(data))
assert isinstance(result, str)
assert result == data.decode(errors="surrogateescape")
class TestConfigValue:
def test_deserialize_decodes_bytes(self):
cv = types.ConfigValue()
result = cv.deserialize(b"abc")
assert isinstance(result, str)
def test_serialize_conversion_to_string(self):
cv = types.ConfigValue()
result = cv.serialize(object())
assert isinstance(result, str)
def test_serialize_none(self):
cv = types.ConfigValue()
result = cv.serialize(None)
assert isinstance(result, str)
assert result == ""
def test_serialize_supports_display(self):
cv = types.ConfigValue()
result = cv.serialize(object(), display=True)
assert isinstance(result, str)
class TestDeprecated:
def test_deserialize_returns_deprecated_value(self):
cv = types.Deprecated()
result = cv.deserialize(b"foobar")
assert isinstance(result, types.DeprecatedValue)
def test_serialize_returns_deprecated_value(self):
cv = types.Deprecated()
result = cv.serialize("foobar")
assert isinstance(result, types.DeprecatedValue)
class TestString:
def test_deserialize_conversion_success(self):
cv = types.String()
result = cv.deserialize(b" foo ")
assert result == "foo"
assert isinstance(result, str)
assert not isinstance(result, types._TransformedValue)
def test_deserialize_decodes_utf8(self):
cv = types.String()
result = cv.deserialize("æøå".encode())
assert result == "æøå"
def test_deserialize_does_not_double_encode_unicode(self):
cv = types.String()
result = cv.deserialize("æøå")
assert result == "æøå"
def test_deserialize_handles_escapes(self):
cv = types.String(optional=True)
result = cv.deserialize(b"a\\t\\nb")
assert result == "a\t\nb"
def test_deserialize_enforces_choices(self):
cv = types.String(choices=["foo", "bar", "baz"])
assert cv.deserialize(b"foo") == "foo"
with pytest.raises(ValueError):
cv.deserialize(b"foobar")
def test_deserialize_enforces_required(self):
cv = types.String()
with pytest.raises(ValueError):
cv.deserialize(b"")
def test_deserialize_respects_optional(self):
cv = types.String(optional=True)
assert cv.deserialize(b"") is None
assert cv.deserialize(b" ") is None
def test_deserialize_invalid_encoding(self):
cv = types.String()
incorrectly_encoded_bytes = "æøå".encode("iso-8859-1")
assert cv.deserialize(incorrectly_encoded_bytes) == "\udce6\udcf8\udce5"
def test_serialize_returns_text(self):
cv = types.String()
result = cv.serialize("æøå")
assert isinstance(result, str)
assert result == "æøå"
def test_serialize_decodes_bytes(self):
cv = types.String()
bytes_string = "æøå".encode()
result = cv.serialize(bytes_string)
assert isinstance(result, str)
assert result == bytes_string.decode()
def test_serialize_handles_escapes(self):
cv = types.String()
result = cv.serialize("a\n\tb")
assert isinstance(result, str)
assert result == r"a\n\tb"
def test_serialize_none(self):
cv = types.String()
result = cv.serialize(None)
assert isinstance(result, str)
assert result == ""
def test_deserialize_enforces_choices_optional(self):
cv = types.String(optional=True, choices=["foo", "bar", "baz"])
assert cv.deserialize(b"") is None
with pytest.raises(ValueError):
cv.deserialize(b"foobar")
@pytest.mark.parametrize(
("original", "transformed"),
(
("abc", "abc"),
("ABC", "abc"),
("aBc", "abc"),
("123", "123"),
("abc123def456", "abc123def456"),
("ABC123def456GHI789", "abc123def456ghi789"),
),
)
def test_deserialize_utilises_transformer(
self, original: str, transformed: str
):
cv = types.String(transformer=lambda value: value.lower())
result = cv.deserialize(original)
assert isinstance(result, str)
assert isinstance(result, types._TransformedValue)
assert result == transformed
assert result.original == original
@pytest.mark.parametrize(
("original", "transformed"),
(
("abc", "abc"),
("ABC", "abc"),
("aBc", "abc"),
("123", "123"),
("abc123def456", "abc123def456"),
("ABC123def456GHI789", "abc123def456ghi789"),
),
)
def test_serialize_transformed_value(self, original: str, transformed: str):
cv = types.String()
transformed_value = types._TransformedValue(original, transformed)
result = cv.serialize(transformed_value)
assert isinstance(result, str)
assert not isinstance(result, types._TransformedValue)
assert result == original
class TestSecret:
def test_deserialize_decodes_utf8(self):
cv = types.Secret()
result = cv.deserialize("æøå".encode())
assert isinstance(result, str)
assert not isinstance(result, types._TransformedValue)
assert result == "æøå"
def test_deserialize_enforces_required(self):
cv = types.Secret()
with pytest.raises(ValueError):
cv.deserialize(b"")
def test_deserialize_respects_optional(self):
cv = types.Secret(optional=True)
assert cv.deserialize(b"") is None
assert cv.deserialize(b" ") is None
def test_deserialize_utilises_transformer(self):
cv = types.Secret(
transformer=lambda value: codecs.decode(value, encoding="rot13")
)
result = cv.deserialize("zbcvql")
assert isinstance(result, str)
assert isinstance(result, types._TransformedValue)
assert result == "mopidy"
assert result.original == "zbcvql"
def test_serialize_none(self):
cv = types.Secret()
result = cv.serialize(None)
assert isinstance(result, str)
assert result == ""
def test_serialize_transformed_value(self):
cv = types.Secret()
transformed_value = types._TransformedValue("zbcvql", "mopidy")
result = cv.serialize(transformed_value)
assert isinstance(result, str)
assert not isinstance(result, types._TransformedValue)
assert result == "zbcvql"
def test_serialize_for_display_masks_value(self):
cv = types.Secret()
result = cv.serialize("s3cret", display=True)
assert isinstance(result, str)
assert result == "********"
def test_serialize_none_for_display(self):
cv = types.Secret()
result = cv.serialize(None, display=True)
assert isinstance(result, str)
assert result == ""
def test_serialize_transformed_value_for_display_masks_value(self):
cv = types.Secret()
transformed_value = types._TransformedValue("zbcvql", "mopidy")
result = cv.serialize(transformed_value, display=True)
assert isinstance(result, str)
assert not isinstance(result, types._TransformedValue)
assert result == "********"
class TestInteger:
def test_deserialize_conversion_success(self):
cv = types.Integer()
assert cv.deserialize("123") == 123
assert cv.deserialize("0") == 0
assert cv.deserialize("-10") == -10
def test_deserialize_conversion_failure(self):
cv = types.Integer()
with pytest.raises(ValueError):
cv.deserialize("asd")
with pytest.raises(ValueError):
cv.deserialize("3.14")
def test_deserialize_enforces_required(self):
cv = types.Integer()
with pytest.raises(ValueError):
cv.deserialize("")
def test_deserialize_respects_optional(self):
cv = types.Integer(optional=True)
assert cv.deserialize("") is None
def test_deserialize_enforces_choices(self):
cv = types.Integer(choices=[1, 2, 3])
assert cv.deserialize("3") == 3
with pytest.raises(ValueError):
cv.deserialize("5")
def test_deserialize_enforces_minimum(self):
cv = types.Integer(minimum=10)
assert cv.deserialize("15") == 15
with pytest.raises(ValueError):
cv.deserialize("5")
def test_deserialize_enforces_maximum(self):
cv = types.Integer(maximum=10)
assert cv.deserialize("5") == 5
with pytest.raises(ValueError):
cv.deserialize("15")
class TestFloat:
def test_deserialize_conversion_success(self):
cv = types.Float()
assert cv.deserialize("123") == 123.0
assert cv.deserialize("0") == 0.0
assert cv.deserialize("-10") == -10.0
assert cv.deserialize("3.14") == 3.14
assert cv.deserialize("123.45") == 123.45
assert cv.deserialize("-456.78") == -456.78
def test_deserialize_conversion_failure(self):
cv = types.Float()
errmsg = re.escape("could not convert string to float")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("asd")
def test_deserialize_enforces_required(self):
cv = types.Float()
errmsg = "must be set"
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("")
def test_deserialize_respects_optional(self):
cv = types.Float(optional=True)
assert cv.deserialize("") is None
def test_deserialize_enforces_minimum(self):
cv = types.Float(minimum=10)
assert cv.deserialize("10.1") == 10.1
errmsg = re.escape("must be larger than")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("9.9")
def test_deserialize_enforces_maximum(self):
cv = types.Float(maximum=10)
assert cv.deserialize("9.9") == 9.9
errmsg = re.escape("must be smaller than")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("10.1")
class TestBoolean:
def test_deserialize_conversion_success(self):
cv = types.Boolean()
for true in ("1", "yes", "true", "on"):
assert cv.deserialize(true) is True
assert cv.deserialize(true.upper()) is True
assert cv.deserialize(true.capitalize()) is True
for false in ("0", "no", "false", "off"):
assert cv.deserialize(false) is False
assert cv.deserialize(false.upper()) is False
assert cv.deserialize(false.capitalize()) is False
def test_deserialize_conversion_failure(self):
cv = types.Boolean()
with pytest.raises(ValueError):
cv.deserialize("nope")
with pytest.raises(ValueError):
cv.deserialize("sure")
def test_deserialize_enforces_required(self):
cv = types.Boolean()
with pytest.raises(ValueError):
cv.deserialize("")
def test_deserialize_respects_optional(self):
cv = types.Boolean(optional=True)
assert cv.deserialize("") is None
def test_serialize_true(self):
cv = types.Boolean()
result = cv.serialize(True)
assert isinstance(result, str)
assert result == "true"
def test_serialize_false(self):
cv = types.Boolean()
result = cv.serialize(False)
assert isinstance(result, str)
assert result == "false"
def test_serialize_none_as_false(self):
# TODO We should consider making `None` an invalid value, but we have
# existing code that assumes it to work like False.
cv = types.Boolean()
result = cv.serialize(None)
assert isinstance(result, str)
assert result == "false"
def test_serialize_invalid_values(self):
cv = types.Boolean()
with pytest.raises(ValueError):
cv.serialize([])
with pytest.raises(ValueError):
cv.serialize("1")
class TestPair:
def test_deserialize_conversion_success(self):
cv = types.Pair()
result = cv.deserialize("foo|bar")
assert result == ("foo", "bar")
result = cv.deserialize(" foo|bar")
assert result == ("foo", "bar")
result = cv.deserialize("foo|bar ")
assert result == ("foo", "bar")
result = cv.deserialize(" fo o | bar ")
assert result == ("fo o", "bar")
result = cv.deserialize("foo|bar|baz")
assert result == ("foo", "bar|baz")
def test_deserialize_decodes_utf8(self):
cv = types.Pair()
result = cv.deserialize("æ|å".encode())
assert result == ("æ", "å")
result = cv.deserialize("æ | ø\n".encode())
assert result == ("æ", "ø")
result = cv.deserialize("æ ø| å".encode())
assert result == ("æ ø", "å")
result = cv.deserialize(" æ | øå \n".encode())
assert result == ("æ", "øå")
def test_deserialize_enforces_required(self):
cv = types.Pair()
errmsg = re.escape("must be set")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("")
def test_deserialize_respects_optional(self):
cv = types.Pair(optional=True)
assert cv.deserialize("") is None
assert cv.deserialize(" ") is None
def test_deserialize_enforces_required_separator(self):
cv = types.Pair()
errmsg = (
"^"
+ re.escape("Config value must include '|' separator: abc")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc")
def test_deserialize_respects_optional_separator(self):
cv = types.Pair(optional_pair=True)
result = cv.deserialize("abc")
assert result == ("abc", "abc")
result = cv.deserialize("abc|def")
assert result == ("abc", "def")
@pytest.mark.parametrize(
"sep", ("!", "@", "#", "$", "%", "^", "&", "*", "/", "\\")
)
def test_deserialize_respects_custom_separator(self, sep: str):
cv = types.Pair(separator=sep)
result = cv.deserialize(f"abc{sep}def")
assert result == ("abc", "def")
result = cv.deserialize(f"abc|def{sep}ghi|jkl")
assert result == ("abc|def", "ghi|jkl")
result = cv.deserialize(f"abc{sep}def{sep}ghi")
assert result == ("abc", f"def{sep}ghi")
result = cv.deserialize(f"ab|cd{sep}ef|gh{sep}ij|kl")
assert result == ("ab|cd", f"ef|gh{sep}ij|kl")
result = cv.deserialize(f"|abcd|{sep}efgh|")
assert result == ("|abcd|", "efgh|")
errmsg = (
"^"
+ re.escape(f"Config value must include {sep!r} separator: abc|def")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|def")
@pytest.mark.parametrize(
"sep", ("!", "@", "#", "$", "%", "^", "&", "*", "/", "\\")
)
def test_deserialize_respects_optional_custom_separator(self, sep: str):
cv = types.Pair(optional_pair=True, separator=sep)
result = cv.deserialize(f"abc{sep}def")
assert result == ("abc", "def")
result = cv.deserialize("abcdef")
assert result == ("abcdef", "abcdef")
result = cv.deserialize("abc|def")
assert result == ("abc|def", "abc|def")
result = cv.deserialize(f"|abc{sep}def|")
assert result == ("|abc", "def|")
@pytest.mark.parametrize("optional", (True, False))
@pytest.mark.parametrize("optional_pair", (True, False))
def test_deserialize_enforces_required_pair_values(
self, optional: bool, optional_pair: bool
):
cv = types.Pair(optional=optional, optional_pair=optional_pair)
errmsg = re.escape("must be set")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("|def")
@pytest.mark.parametrize("optional", (True, False))
@pytest.mark.parametrize("optional_pair", (True, False))
@pytest.mark.parametrize(
"sep", ("!", "@", "#", "$", "%", "^", "&", "*", "/", "\\")
)
def test_deserialize_enforces_required_pair_values_with_custom_separator(
self, optional: bool, optional_pair: bool, sep: str
):
cv = types.Pair(
optional=optional, optional_pair=optional_pair, separator=sep
)
errmsg = re.escape("must be set")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize(f"abc{sep}")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize(f"{sep}def")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize(f"abc|def{sep}")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize(f"{sep}ghi|jkl")
def test_deserialize_with_custom_subtypes(self):
cv = types.Pair(subtypes=(types.String(), types.Integer()))
result = cv.deserialize("abc|10")
assert result == ("abc", 10)
cv = types.Pair(subtypes=(types.Float(), types.Boolean()))
result = cv.deserialize("3.14|true")
assert result == (3.14, True)
cv = types.Pair(subtypes=(types.Path(), types.String()))
result = cv.deserialize("/dev/null | empty")
assert result == ("/dev/null", "empty")
with mock.patch("socket.getaddrinfo") as getaddrinfo_mock:
cv = types.Pair(subtypes=(types.Hostname(), types.Port()))
result = cv.deserialize("localhost|6680")
assert result == ("localhost", 6680)
getaddrinfo_mock.assert_called_once_with("localhost", None)
def test_deserialize_with_custom_subtypes_enforces_required(self):
cv = types.Pair(subtypes=(types.Integer(), types.Integer()))
errmsg = re.escape("must be set")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("")
def test_deserialize_with_custom_subtypes_respects_optional(self):
cv = types.Pair(optional=True, subtypes=(types.Float(), types.Float()))
assert cv.deserialize("") is None
def test_deserialize_with_custom_subtypes_enforces_required_separator(self):
errmsg = "^" + re.escape("Config value must include '|' separator: ")
cv = types.Pair(subtypes=(types.String(), types.Secret()))
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc")
cv = types.Pair(subtypes=(types.String(), types.Integer()))
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("123")
def test_deserialize_with_custom_subtypes_respects_optional_separator(self):
cv = types.Pair(
optional_pair=True, subtypes=(types.Integer(), types.Integer())
)
result = cv.deserialize("42")
assert result == (42, 42)
cv = types.Pair(
optional_pair=True, subtypes=(types.Path(), types.String())
)
result = cv.deserialize("/dev/null")
assert result == ("/dev/null", "/dev/null")
cv = types.Pair(
optional_pair=True, subtypes=(types.Port(), types.Port())
)
result = cv.deserialize("443")
assert result == (443, 443)
def test_deserialize_with_custom_subtypes_optional_separator_mixed_types(
self,
):
cv = types.Pair(
optional_pair=True, subtypes=(types.String(), types.Integer())
)
errmsg = re.escape("invalid literal for int() with base 10")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc")
def test_deserialize_with_optional_custom_subtypes(self):
cv = types.Pair(subtypes=(types.String(), types.String(optional=True)))
result = cv.deserialize("abc|")
assert result == ("abc", None)
cv = types.Pair(subtypes=(types.String(optional=True), types.String()))
result = cv.deserialize("|def")
assert result == (None, "def")
cv = types.Pair(
subtypes=(types.String(optional=True), types.String(optional=True))
)
result = cv.deserialize("|")
assert result == (None, None)
def test_serialize(self):
cv = types.Pair()
result = cv.serialize(("abc", "def"))
assert result == "abc|def"
result = cv.serialize(("abc", None))
assert result == "abc|"
result = cv.serialize((None, "abc"))
assert result == "|abc"
cv = types.Pair(subtypes=(types.String(), types.Integer()))
result = cv.serialize(("abc", 42))
assert result == "abc|42"
result = cv.serialize(("abc", None))
assert result == "abc|"
result = cv.serialize((None, 42))
assert result == "|42"
cv = types.Pair(subtypes=(types.String(), types.Path()))
result = cv.serialize(("null", "/dev/null"))
assert result == "null|/dev/null"
result = cv.serialize(("tmp", types._ExpandedPath("/tmp", "/tmp")))
assert result == "tmp|/tmp"
result = cv.serialize(("null", None))
assert result == "null|"
result = cv.serialize(
(None, types._ExpandedPath("/dev/null", "/dev/null"))
)
assert result == "|/dev/null"
@pytest.mark.parametrize(
"sep", ("!", "@", "#", "$", "%", "^", "&", "*", "/", "\\")
)
def test_serialize_with_custom_separator(self, sep: str):
cv = types.Pair(separator=sep)
result = cv.serialize(("abc", "def"))
assert result == f"abc{sep}def"
result = cv.serialize(("abc", None))
assert result == f"abc{sep}"
result = cv.serialize((None, "abc"))
assert result == f"{sep}abc"
cv = types.Pair(
separator=sep, subtypes=(types.String(), types.Integer())
)
result = cv.serialize(("abc", 42))
assert result == f"abc{sep}42"
result = cv.serialize(("abc", None))
assert result == f"abc{sep}"
result = cv.serialize((None, 42))
assert result == f"{sep}42"
cv = types.Pair(separator=sep, subtypes=(types.String(), types.Path()))
result = cv.serialize(("null", "/dev/null"))
assert result == f"null{sep}/dev/null"
result = cv.serialize(("tmp", types._ExpandedPath("/tmp", "/tmp")))
assert result == f"tmp{sep}/tmp"
result = cv.serialize(("null", None))
assert result == f"null{sep}"
result = cv.serialize(
(None, types._ExpandedPath("/dev/null", "/dev/null"))
)
assert result == f"{sep}/dev/null"
def test_serialize_returns_single_value_with_optional_pair(self):
cv = types.Pair(optional_pair=True)
result = cv.serialize(("abc", "abc"))
assert result == "abc"
result = cv.serialize(("abc", "def"))
assert result == "abc|def"
result = cv.serialize(("abc", "abc"), display=True)
assert result == "abc|abc"
result = cv.serialize(("abc", "def"), display=True)
assert result == "abc|def"
def test_deserialize_nested_pair_success(self):
cv = types.Pair(subtypes=(types.Integer(), types.Pair()))
result = cv.deserialize("50|def|ghi")
assert result == (50, ("def", "ghi"))
cv = types.Pair(
separator="#",
subtypes=(
types.String(),
types.Pair(subtypes=(types.Integer(), types.Integer())),
),
)
result = cv.deserialize("xyz#4|-5")
assert result == ("xyz", (4, -5))
cv = types.Pair(
subtypes=(
types.Pair(
separator="*", subtypes=(types.Float(), types.Float())
),
types.String(),
),
)
result = cv.deserialize("42*2.5|abc")
assert result == ((42, 2.5), "abc")
cv = types.Pair(
subtypes=(
types.Pair(separator="#"),
types.Pair(),
),
)
result = cv.deserialize("abc#def|ghi|jkl")
assert result == (("abc", "def"), ("ghi", "jkl"))
def test_deserialize_nested_pair_fail(self):
cv = types.Pair(
subtypes=(
types.Pair(),
types.String(),
),
)
errmsg = (
"^"
+ re.escape("Config value must include '|' separator: abc")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|def|ghi")
cv = types.Pair(
optional_pair=True,
subtypes=(
types.Pair(),
types.String(),
),
)
errmsg = (
"^"
+ re.escape("Config value must include '|' separator: abc")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|def|ghi")
cv = types.Pair(
optional_pair=True,
subtypes=(
types.String(),
types.Pair(),
),
)
errmsg = (
"^"
+ re.escape("Config value must include '|' separator: def")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|def")
cv = types.Pair(
subtypes=(
types.Pair(),
types.Pair(separator="#"),
),
)
errmsg = (
"^"
+ re.escape("Config value must include '|' separator: abc")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|def|ghi#jkl")
def test_deserialize_nested_pair_optional(self):
cv = types.Pair(
subtypes=(
types.Pair(optional=True),
types.Pair(),
)
)
result = cv.deserialize("|abc|def")
assert result == (None, ("abc", "def"))
cv = types.Pair(
subtypes=(
types.Pair(),
types.Pair(optional=True),
),
)
errmsg = (
"^"
+ re.escape("Config value must include '|' separator: abc")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|def|")
cv = types.Pair(
subtypes=(
types.Pair(separator="#"),
types.Pair(optional=True),
),
)
result = cv.deserialize("abc#def|")
assert result == (("abc", "def"), None)
cv = types.Pair(
separator="#",
subtypes=(
types.Pair(),
types.Pair(optional=True),
),
)
result = cv.deserialize("mno|xyz#")
assert result == (("mno", "xyz"), None)
cv = types.Pair(
subtypes=(
types.Pair(optional=True),
types.Pair(optional=True),
),
)
result = cv.deserialize("|")
assert result == (None, None)
def test_deserialize_nested_pair_optional_pair(self):
cv = types.Pair(
subtypes=(
types.Pair(optional_pair=True),
types.String(),
),
)
result = cv.deserialize("abc|def|ghi")
assert result == (("abc", "abc"), "def|ghi")
cv = types.Pair(
subtypes=(
types.String(),
types.Pair(optional_pair=True),
)
)
result = cv.deserialize("abc|def")
assert result == ("abc", ("def", "def"))
cv = types.Pair(
subtypes=(
types.Pair(optional_pair=True),
types.Pair(),
),
)
result = cv.deserialize("abc|def|ghi")
assert result == (("abc", "abc"), ("def", "ghi"))
cv = types.Pair(
subtypes=(
types.Pair(),
types.Pair(optional_pair=True),
),
)
errmsg = (
"^"
+ re.escape("Config value must include '|' separator: abc")
+ "$"
)
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("abc|def|ghi")
cv = types.Pair(
subtypes=(
types.Pair(separator="#"),
types.Pair(optional_pair=True),
),
)
result = cv.deserialize("abc#def|ghi")
assert result == (("abc", "def"), ("ghi", "ghi"))
cv = types.Pair(
subtypes=(
types.Pair(optional_pair=True),
types.Pair(optional_pair=True),
),
)
result = cv.deserialize("abc|def")
assert result == (("abc", "abc"), ("def", "def"))
errmsg = re.escape("must be set")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("|")
cv = types.Pair(
optional_pair=True,
subtypes=(
types.Pair(optional_pair=True),
types.Pair(optional_pair=True),
),
)
result = cv.deserialize("abc")
assert result == (("abc", "abc"), ("abc", "abc"))
def test_serialize_nested_pair(self):
cv = types.Pair(subtypes=(types.String(), types.Pair()))
result = cv.serialize(("abc", ("def", "ghi")))
assert result == "abc|def|ghi"
cv = types.Pair(
subtypes=(
types.Pair(separator="#"),
types.String(),
),
)
result = cv.serialize((("abc", "def"), "ghi"))
assert result == "abc#def|ghi"
cv = types.Pair(
separator="#",
subtypes=(types.Pair(), types.Pair()),
)
result = cv.serialize((("abc", "def"), ("ghi", "jkl")))
assert result == "abc|def#ghi|jkl"
cv = types.Pair(
optional_pair=True,
subtypes=(
types.Pair(optional_pair=True),
types.Pair(optional_pair=True),
),
)
result = cv.serialize((("abc", "abc"), ("abc", "abc")))
assert result == "abc"
result = cv.serialize((("abc", "abc"), ("abc", "abc")), display=True)
assert result == "abc|abc|abc|abc"
class TestList:
# TODO: add test_deserialize_handles_escapes
def test_deserialize_conversion_success(self):
cv = types.List()
result = cv.deserialize(b"foo, bar ,baz ")
assert result == ("foo", "bar", "baz")
result = cv.deserialize(b" foo,bar\nbar\nbaz")
assert result == ("foo,bar", "bar", "baz")
def test_deserialize_conversion_success_unique(self):
cv = types.List(unique=True)
result = cv.deserialize("foo, bar, baz ")
assert result == {"foo", "bar", "baz"}
result = cv.deserialize("foo,bar,foo,baz,foo")
assert result == {"foo", "bar", "baz"}
result = cv.deserialize(" foo,bar\nbar\nbaz")
assert result == {"foo,bar", "bar", "baz"}
def test_deserialize_creates_tuples(self):
cv = types.List(optional=True)
assert isinstance(cv.deserialize(b"foo,bar,baz"), tuple)
assert isinstance(cv.deserialize(b""), tuple)
def test_deserialize_creates_frozensets(self):
cv = types.List(optional=True, unique=True)
assert isinstance(cv.deserialize("foo,bar,baz"), frozenset)
assert isinstance(cv.deserialize(""), frozenset)
def test_deserialize_decodes_utf8(self):
cv = types.List()
result = cv.deserialize("æ, ø, å".encode())
assert result == ("æ", "ø", "å")
result = cv.deserialize("æ\nø\nå".encode())
assert result == ("æ", "ø", "å")
def test_deserialize_does_not_double_encode_unicode(self):
cv = types.List()
result = cv.deserialize("æ, ø, å")
assert result == ("æ", "ø", "å")
result = cv.deserialize("æ\nø\nå")
assert result == ("æ", "ø", "å")
def test_deserialize_enforces_required(self):
cv = types.List()
with pytest.raises(ValueError):
cv.deserialize(b"")
def test_deserialize_respects_optional(self):
cv = types.List(optional=True)
assert cv.deserialize(b"") == ()
@pytest.mark.parametrize(
"value",
(
"foo,,bar,,baz",
"foo, ,bar, , baz",
"foo, ,bar, , baz, ",
"foo\n\nbar\n\nbaz\n",
"foo \n bar \n \n baz",
),
)
def test_deserialize_ignores_blanks(self, value):
cv = types.List()
result = cv.deserialize(value)
assert result == ("foo", "bar", "baz")
def test_serialize_tuples(self):
cv = types.List()
result = cv.serialize(("foo", "bar", "baz"))
assert isinstance(result, str)
assert result == "\n foo\n bar\n baz"
def test_serialize_sets(self):
cv = types.List(unique=True)
result = cv.serialize({"foo", "bar", "baz"})
assert isinstance(result, str)
assert "\n foo" in result
assert "\n bar" in result
assert "\n baz" in result
def test_serialize_none(self):
cv = types.List()
result = cv.serialize(None)
assert isinstance(result, str)
assert result == ""
@pytest.mark.parametrize(
"value",
(
("foo", "", "bar", "baz"),
("foo", "bar", "", "", "baz"),
),
)
def test_serialize_ignores_blanks(self, value):
cv = types.List()
result = cv.serialize(value)
assert result == "\n foo\n bar\n baz"
def test_serialize_ignores_blanks_sets(self):
cv = types.List(unique=True)
result = cv.serialize({"foo", "", "bar", "baz"})
assert "\n foo" in result
assert "\n bar" in result
assert "\n baz" in result
assert "\n \n" not in result
assert not result.endswith("\n ")
def test_deserialize_with_custom_subtype(self):
cv = types.List(subtype=types.Integer())
expected = (1, 2, 3)
assert cv.deserialize("1, 2, 3") == expected
assert cv.deserialize("1\n2\n3") == expected
assert cv.deserialize("\n 1\n 2\n 3") == expected
cv = types.List(subtype=types.Pair())
expected = (("a", "x"), ("b", "y"), ("c", "z"))
assert cv.deserialize("a|x,b|y,c|z") == expected
assert cv.deserialize("a|x\nb|y\nc|z") == expected
assert cv.deserialize("\n a|x\n b|y\n c|z") == expected
cv = types.List(
subtype=types.Pair(subtypes=(types.Integer(), types.Integer())),
)
expected = ((7, 1), (8, 2), (9, 3))
assert cv.deserialize("7|1,8|2,9|3") == expected
assert cv.deserialize("7|1\n8|2\n9|3") == expected
assert cv.deserialize("\n 7|1\n 8|2\n 9|3") == expected
with mock.patch("socket.getaddrinfo") as getaddrinfo_mock:
cv = types.List(
subtype=types.Pair(subtypes=(types.Hostname(), types.Port())),
)
expected = (("localhost", 8080), ("example.com", 443))
assert cv.deserialize("localhost|8080,example.com|443") == expected
assert cv.deserialize("localhost|8080\nexample.com|443") == expected
call_localhost = mock.call("localhost", None)
call_examplecom = mock.call("example.com", None)
assert getaddrinfo_mock.mock_calls == [
call_localhost,
call_examplecom,
call_localhost,
call_examplecom,
]
def test_deserialize_with_custom_subtype_enforces_required(self):
cv = types.List(subtype=types.Float())
errmsg = re.escape("must be set")
with pytest.raises(ValueError, match=errmsg):
cv.deserialize("")
def test_deserialize_with_custom_subtype_respects_optional(self):
cv = types.List(optional=True, subtype=types.Float())
assert cv.deserialize("") == ()
def test_serialize_with_custom_subtype(self):
cv = types.List(subtype=types.Integer())
result = cv.serialize((1, 2, 3))
assert result == "\n 1\n 2\n 3"
cv = types.List(subtype=types.Pair())
result = cv.serialize((("a", "x"), ("b", "y"), ("c", "z")))
assert result == "\n a|x\n b|y\n c|z"
cv = types.List(
subtype=types.Pair(
separator="#",
subtypes=(types.Integer(), types.Integer()),
),
)
result = cv.serialize(((7, 1), (8, 2), (9, 3)))
assert result == "\n 7#1\n 8#2\n 9#3"
class TestLogColor:
def test_deserialize(self):
cv = types.LogColor()
assert cv.deserialize(b"RED") == "red"
assert cv.deserialize("RED") == "red"
assert cv.deserialize(b"red") == "red"
assert cv.deserialize("red") == "red"
def test_deserialize_enforces_choices(self):
cv = types.LogColor()
with pytest.raises(ValueError):
cv.deserialize("golden")
def test_serialize(self):
cv = types.LogColor()
assert cv.serialize("red") == "red"
assert cv.serialize("blue") == "blue"
def test_serialize_ignores_unknown_color(self):
cv = types.LogColor()
assert cv.serialize("golden") == ""
class TestLogLevel:
levels = {
"critical": logging.CRITICAL,
"error": logging.ERROR,
"warning": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
"trace": log.TRACE_LOG_LEVEL,
"all": logging.NOTSET,
}
def test_deserialize_conversion_success(self):
cv = types.LogLevel()
for name, level in self.levels.items():
assert cv.deserialize(name) == level
assert cv.deserialize(name.upper()) == level
assert cv.deserialize(name.capitalize()) == level
def test_deserialize_conversion_failure(self):
cv = types.LogLevel()
with pytest.raises(ValueError):
cv.deserialize("red alert")
with pytest.raises(ValueError):
cv.deserialize(" ")
def test_serialize(self):
cv = types.LogLevel()
for name, level in self.levels.items():
assert cv.serialize(level) == name
def test_serialize_ignores_unknown_level(self):
cv = types.LogLevel()
assert cv.serialize(1337) == ""
class TestHostname:
@mock.patch("socket.getaddrinfo")
def test_deserialize_conversion_success(self, getaddrinfo_mock):
cv = types.Hostname()
cv.deserialize("example.com")
getaddrinfo_mock.assert_called_once_with("example.com", None)
@mock.patch("socket.getaddrinfo")
def test_deserialize_conversion_failure(self, getaddrinfo_mock):
cv = types.Hostname()
getaddrinfo_mock.side_effect = socket.error
with pytest.raises(ValueError):
cv.deserialize("example.com")
@mock.patch("socket.getaddrinfo")
def test_deserialize_enforces_required(self, getaddrinfo_mock):
cv = types.Hostname()
with pytest.raises(ValueError):
cv.deserialize("")
with pytest.raises(ValueError):
cv.deserialize(" ")
assert getaddrinfo_mock.call_count == 0
@mock.patch("socket.getaddrinfo")
def test_deserialize_respects_optional(self, getaddrinfo_mock):
cv = types.Hostname(optional=True)
assert cv.deserialize("") is None
assert cv.deserialize(" ") is None
assert getaddrinfo_mock.call_count == 0
@mock.patch("mopidy.internal.path.expand_path")
def test_deserialize_with_unix_socket(self, expand_path_mock):
cv = types.Hostname()
assert cv.deserialize("unix:/tmp/mopidy.socket") is not None
expand_path_mock.assert_called_once_with("/tmp/mopidy.socket")
class TestPort:
def test_valid_ports(self):
cv = types.Port()
assert cv.deserialize("0") == 0
assert cv.deserialize("1") == 1
assert cv.deserialize("80") == 80
assert cv.deserialize("6600") == 6600
assert cv.deserialize("65535") == 65535
def test_invalid_ports(self):
cv = types.Port()
with pytest.raises(ValueError):
cv.deserialize("65536")
with pytest.raises(ValueError):
cv.deserialize("100000")
with pytest.raises(ValueError):
cv.deserialize("-1")
with pytest.raises(ValueError):
cv.deserialize("")
class TestExpandedPath:
def test_is_str(self):
assert isinstance(types._ExpandedPath(b"/tmp", b"foo"), str)
def test_stores_both_expanded_and_original_path(self):
original = "~"
expanded = "expanded_path"
result = types._ExpandedPath(original, expanded)
assert result == expanded
assert result.original == original
class TestPath:
def test_deserialize_conversion_success(self):
cv = types.Path()
result = cv.deserialize(b"/foo")
assert result == "/foo"
assert isinstance(result, types._ExpandedPath)
assert isinstance(result, str)
def test_deserialize_enforces_required(self):
cv = types.Path()
with pytest.raises(ValueError):
cv.deserialize(b"")
def test_deserialize_respects_optional(self):
cv = types.Path(optional=True)
assert cv.deserialize(b"") is None
assert cv.deserialize(b" ") is None
def test_serialize_uses_original(self):
cv = types.Path()
path = types._ExpandedPath("original_path", "expanded_path")
assert cv.serialize(path) == "original_path"
def test_serialize_plain_string(self):
cv = types.Path()
assert cv.serialize(b"path") == "path"
def test_serialize_supports_unicode_string(self):
cv = types.Path()
assert cv.serialize("æøå") == "æøå"
def test_serialize_none(self):
cv = types.Path()
assert cv.serialize(None) == ""
|