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 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# See the accompanying LICENSE file.
# This testing code deliberately does nasty stuff so mypy isn't helpful
# mypy: ignore-errors
import array
import collections
import collections.abc
import contextlib
import dataclasses
import gc
import getpass
import glob
import inspect
import io
import itertools
import json
import logging
import math
import mmap
import os
import pickle
import platform
import queue
import random
import re
import shlex
import shutil
import struct
import sys
import tempfile
import textwrap
import threading
import time
import traceback
import typing
import warnings
def ShouldFault(name, pending_exception):
# You can't use print because calls involving print can be nested
# and will then have a fatal exception. Directly writing
# to stderr is ok like this:
#
# os.write(2, f"SF { name } { pending_exception }\n".encode("utf8"))
return False
sys.apsw_should_fault = ShouldFault
import apsw
if not all(hasattr(apsw, name) for name in ("initialize", "format_sql_value")):
msg = """
The compiled portion of APSW is not present where the tests are being run.
It needs to be compiled and in-place first.
""".strip()
sys.exit(msg)
import apsw.bestpractice
import apsw.ext
import apsw.shell
def print_version_info():
print(" Python ", sys.executable, sys.version_info, " ".join(platform.architecture()))
print("Testing with APSW file ", apsw.__file__)
print(" APSW version ", apsw.apsw_version())
print(" SQLite lib version ", apsw.sqlite_lib_version())
print("SQLite headers version ", apsw.SQLITE_VERSION_NUMBER)
print(" Using amalgamation ", apsw.using_amalgamation, flush=True)
# sigh
iswindows = sys.platform in ('win32', )
# prefix for test files (eg if you want it on tmpfs)
TESTFILEPREFIX = os.environ.get("APSWTESTPREFIX", "")
# size of C int in bits
sizeof_c_int = len(struct.pack('i', 0)) * 8
def read_whole_file(name, mode, encoding=None):
if "t" in mode and not encoding:
encoding = "utf8"
if encoding:
f = open(name, mode, encoding=encoding)
else:
f = open(name, mode)
try:
return f.read()
finally:
f.close()
# If two is present then one is encoding
def write_whole_file(name, mode, data, *, encoding=None):
if "t" in mode and not encoding:
encoding = "utf8"
if encoding:
f = open(name, mode, encoding=encoding)
else:
f = open(name, mode)
try:
f.write(data)
finally:
f.close()
# unittest stuff from here on
import unittest
try:
import ctypes
import _ctypes
except:
ctypes = None
_ctypes = None
# yay
is64bit = ctypes and ctypes.sizeof(ctypes.c_size_t) >= 8
# Make curnext switch between the iterator and fetchone alternately
_nextcounter = 0
def curnext(cursor, *args):
global _nextcounter
_nextcounter += 1
if _nextcounter % 2:
return next(cursor, *args)
res = cursor.fetchone()
if res is None:
if args:
return args[0]
return None
return res
# got here via apsw_write_unraisable in C code
def ehook(etype, evalue, etraceback):
sys.stderr.write("Unraisable exception " + str(etype) + ":" + str(evalue) + "\n")
traceback.print_tb(etraceback)
if sys.excepthook == sys.__excepthook__:
sys.excepthook = ehook
# helper functions
def randomintegers(howmany):
for i in range(howmany):
yield (random.randint(0, 9999999999), )
def randomstring(length):
l = list("abcdefghijklmnopqrstuvwxyz0123456789")
while len(l) < length:
l.extend(l)
l = l[:length]
random.shuffle(l)
return "".join(l)
# An instance of this class is used to get the -1 return value to the
# C api PyObject_IsTrue
class BadIsTrue(int):
def __bool__(self):
1 / 0
# helper class - runs code in a separate thread
class ThreadRunner(threading.Thread):
def __init__(self, callable, *args, **kwargs):
threading.Thread.__init__(self)
self.daemon - True
self.callable = callable
self.args = args
self.kwargs = kwargs
self.q = queue.Queue()
self.started = False
def start(self):
if not self.started:
self.started = True
threading.Thread.start(self)
def go(self):
self.start()
t, res = self.q.get()
if t: # result
return res
else: # exception
raise res[1].with_traceback(res[2])
def run(self):
try:
self.q.put((True, self.callable(*self.args, **self.kwargs)))
except:
self.q.put((False, sys.exc_info()))
# Windows doesn't allow files that are open to be deleted. Even after
# we close them, tagalongs such as virus scanners, tortoisesvn etc can
# keep them open. But the good news is that we can rename a file that
# is in use. This background thread does the background deletions of the
# renamed files
def bgdel():
q = bgdelq
while True:
name = q.get()
while os.path.exists(name):
try:
if os.path.isfile(name):
os.remove(name)
else:
shutil.rmtree(name)
except:
pass
if os.path.exists(name):
time.sleep(0.1)
bgdelq = queue.Queue()
bgdelthread = threading.Thread(target=bgdel)
bgdelthread.daemon = True
bgdelthread.start()
def deletefile(name):
try:
os.remove(name)
except:
pass
l = list("abcdefghijklmn")
random.shuffle(l)
newname = name + "-n-" + "".join(l)
count = 0
while os.path.exists(name):
count += 1
try:
os.rename(name, newname)
except:
if count > 30: # 3 seconds we have been at this!
# So give up and give it a stupid name. The sooner
# this so called operating system withers into obscurity
# the better
n = list("abcdefghijklmnopqrstuvwxyz")
random.shuffle(n)
n = "".join(n)
try:
os.rename(name, "windowssucks-" + n + ".deletememanually")
except:
pass
break
# Make windows happy
time.sleep(0.1)
gc.collect()
if os.path.exists(newname):
bgdelq.put(newname)
# Give bg thread a chance to run
time.sleep(0.1)
openflags = apsw.SQLITE_OPEN_READWRITE | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_URI
# main test class/code
class APSW(unittest.TestCase):
maxDiff = 16384
connection_nargs={ # number of args for function. those not listed take zero
'create_aggregate_function': 2,
'create_collation': 2,
'create_scalar_function': 3,
'collation_needed': 1,
'set_authorizer': 1,
'set_busy_handler': 1,
'set_busy_timeout': 1,
'set_commit_hook': 1,
'set_profile': 1,
'set_rollback_hook': 1,
'set_update_hook': 1,
'set_progress_handler': 2,
'enableloadextension': 1,
'create_module': 2,
'file_control': 3,
'set_exec_trace': 1,
'set_row_trace': 1,
'__enter__': 0,
'__exit__': 3,
'backup': 3,
'wal_autocheckpoint': 1,
'set_wal_hook': 1,
'readonly': 1,
'db_filename': 1,
'set_last_insert_rowid': 1,
'serialize': 1,
'deserialize': 2,
'autovacuum_pages': 1,
}
cursor_nargs = {
'execute': 1,
'executemany': 2,
'set_exec_trace': 1,
'set_row_trace': 1,
}
blob_nargs = {'write': 1, 'read': 1, 'read_into': 1, 'reopen': 1, 'seek': 2}
def deltempfiles(self):
for name in ("testdb", "testdb2", "testdb3", "testfile", "testfile2", "testdb2x", "test-shell-1",
"test-shell-1.py", "test-shell-in", "test-shell-out", "test-shell-err"):
for i in "-shm", "-wal", "-journal", "":
if os.path.exists(TESTFILEPREFIX + name + i):
deletefile(TESTFILEPREFIX + name + i)
def setUp(self):
apsw.config(apsw.SQLITE_CONFIG_LOG, None)
apsw.connection_hooks = []
# clean out database and journals from last runs
for c in apsw.connections():
c.close()
gc.collect()
self.deltempfiles()
self.db = apsw.Connection(TESTFILEPREFIX + "testdb", flags=openflags)
self.warnings_filters = warnings.filters
# zipvfs causes some test failures - issue #394
for name in (
'zipvfs',
'cksmvfs',
'zipvfsonly',
'multiplex',
):
if name in apsw.vfs_names():
apsw.unregister_vfs(name)
def tearDown(self):
apsw.config(apsw.SQLITE_CONFIG_LOG, None)
if self.db is not None:
self.db.close(True)
del self.db
for c in apsw.connections():
c.close()
gc.collect()
self.deltempfiles()
warnings.filters = self.warnings_filters
getattr(warnings, "_filters_mutated", lambda: True)()
def suppressWarning(self, name):
if hasattr(__builtins__, name):
warnings.simplefilter("ignore", getattr(__builtins__, name))
def assertRaisesRoot(self, exctype, *args, **kwargs):
# With chained exceptions verifies the first exception raised matches type
# assertRaises checks the last raised type not the root cause
if not args and not kwargs: # context manager
class mgr:
def __enter__(self):
return self
def __exit__(innerself, etype, e, etb):
if not etype or not e:
self.fail("Expected an exception in assertRaisesRoot context manager")
while e.__context__:
e = e.__context__
self.assertIsInstance(e, exctype)
return True
return mgr()
try:
return args[0](*args[1:], **kwargs)
except BaseException as e:
while e.__context__:
e = e.__context__
self.assertIsInstance(e, exctype)
def assertTableExists(self, tablename):
self.assertTrue(self.db.table_exists(None, tablename))
def assertTableNotExists(self, tablename):
self.assertFalse(self.db.table_exists(None, tablename))
def assertTablesEqual(self, dbl, left, dbr, right):
# Ensure tables have the same contents. Rowids can be
# different and select gives unordered results so this is
# quite challenging
l = dbl.cursor()
r = dbr.cursor()
# check same number of rows
lcount = l.execute("select count(*) from [" + left + "]").get
rcount = r.execute("select count(*) from [" + right + "]").get
self.assertEqual(lcount, rcount)
# check same number and names and order for columns
lnames = [row[1] for row in l.execute("pragma table_info([" + left + "])")]
rnames = [row[1] for row in r.execute("pragma table_info([" + left + "])")]
self.assertEqual(lnames, rnames)
# read in contents, sort and compare
lcontents = l.execute("select * from [" + left + "]").fetchall()
rcontents = r.execute("select * from [" + right + "]").fetchall()
lcontents.sort(key=lambda x: repr(x))
rcontents.sort(key=lambda x: repr(x))
self.assertEqual(lcontents, rcontents)
def assertRaisesUnraisable(self, exc, func, *args, **kwargs):
return self.baseAssertRaisesUnraisable(True, exc, func, args, kwargs)
def assertMayRaiseUnraisable(self, exc, func, *args, **kwargs):
"""Like assertRaisesUnraisable but no exception may be raised.
If one is raised, it must have the expected type.
"""
return self.baseAssertRaisesUnraisable(False, exc, func, args, kwargs)
def baseAssertRaisesUnraisable(self, must_raise, exc, func, args, kwargs):
orig = sys.excepthook, getattr(sys, "unraisablehook", None)
try:
called = []
def ehook(*args):
if len(args) == 1:
t = args[0].exc_type
v = args[0].exc_value
tb = args[0].exc_traceback
else:
t, v, tb = args
called.append((t, v, tb))
sys.excepthook = sys.unraisablehook = ehook
try:
try:
return func(*args, **kwargs)
except:
# This ensures frames have their local variables
# cleared before we put the original excepthook
# back. Clearing the variables results in some
# more SQLite operations which also can raise
# unraisables. traceback.clear_frames was
# introduced in Python 3.4 and unittest was
# updated to call it in assertRaises. See issue
# 164
if hasattr(traceback, "clear_frames"):
traceback.clear_frames(sys.exc_info()[2])
raise
finally:
if must_raise and len(called) < 1:
self.fail("Call %s(*%s, **%s) did not do any unraisable" % (func, args, kwargs))
if len(called):
self.assertIsInstance(called[0][1], exc) # check it was the correct type
finally:
sys.excepthook, sys.unraisablehook = orig
def testSanity(self):
"Check all parts compiled and are present"
# check some error codes etc are present - picked first middle and last from lists in code
apsw.SQLError
apsw.MisuseError
apsw.NotADBError
apsw.ThreadingViolationError
apsw.BindingsError
apsw.ExecTraceAbort
apsw.SQLITE_FCNTL_SIZE_HINT
apsw.mapping_file_control["SQLITE_FCNTL_SIZE_HINT"] == apsw.SQLITE_FCNTL_SIZE_HINT
apsw.URIFilename
apsw.SQLITE_INDEX_CONSTRAINT_NE # ticket 289
self.assertTrue(len(apsw.sqlite3_sourceid()) > 10)
def testModuleExposed(self):
"Check what is exposed and usage"
for name in "Connection", "Cursor", "Blob", "Backup", "zeroblob", "VFS", "VFSFile", "URIFilename":
self.assertTrue(hasattr(apsw, name), "expected name apsw." + name)
for name in "Blob", "Backup":
self.assertRaisesRegex(TypeError, "cannot create .* instances", getattr(apsw, name))
def testConnection(self):
"Test connection opening"
# bad keyword arg
self.assertRaises(TypeError, apsw.Connection, ":memory:", user="nobody")
# wrong types
self.assertRaises(TypeError, apsw.Connection, 3)
# bad file (cwd)
self.assertRaises(apsw.CantOpenError, apsw.Connection, ".")
# bad open flags can't be tested as sqlite accepts them all - ticket #3037
# self.assertRaises(apsw.CantOpenError, apsw.Connection, "<non-existent-file>", flags=65535)
# bad vfs
self.assertRaises(TypeError, apsw.Connection, "foo", vfs=3, flags=-1)
self.assertRaises(apsw.SQLError, apsw.Connection, "foo", vfs="jhjkds")
self.assertIsInstance(self.db.system_errno, int)
def testConnectionFileControl(self):
"Verify sqlite3_file_control"
# Note that testVFS deals with success cases and the actual vfs backend
self.assertRaises(TypeError, self.db.file_control, 1, 2)
self.assertRaises(TypeError, self.db.file_control, "main", 1001, "foo")
self.assertRaises(OverflowError, self.db.file_control, "main", 1001, 45236748972389749283)
self.assertEqual(self.db.file_control("main", 1001, 25), False)
self.assertRaises(apsw.SQLError, self.db.read, "kljlkjlkj", 0, 0, 10)
self.assertRaises(TypeError, self.db.read, "kljlkjlkj", 0, deliberate="error")
self.assertRaises(ValueError, self.db.read, "kljlkjlkj", 0, -1, -1)
self.assertRaises(ValueError, self.db.read, "kljlkjlkj", 0, 0, -1)
self.assertRaises(ValueError, self.db.read, "main", 77, 0, 1)
self.assertRaises(ValueError, self.db.read, "main", 0, -1, 1)
# Connection.read fails on memory databases
db2 = apsw.Connection("")
db2.pragma("user_version", 2)
self.assertRaises(apsw.SQLError, db2.read, "main", 0, 0, 16384)
db2.close()
self.assertEqual((False, b"\0" * 10), self.db.read("main", 0, 50 * 1024 * 1024, 10))
# db and journal are empty unless we do something
self.db.execute("create table foo(x); begin immediate; insert into foo values(3); insert into foo values(4)")
self.assertEqual((True, b'SQLite format 3'), self.db.read("main", 0, 0, 15))
self.assertTrue(any(b != 0 for b in self.db.read("main", 1, 0, 15)[1]))
def testConnectionVfsname(self):
"Verify vfsname function"
self.assertRaises(TypeError, self.db.vfsname, 1, 2)
self.assertRaises(TypeError, self.db.vfsname, b"main")
self.assertRaises(TypeError, self.db.vfsname)
# our db should be the default - also handle multipleciphers shim
self.assertTrue(
self.db.vfsname("main") == apsw.vfs_names()[0]
or self.db.vfsname("main").startswith(apsw.vfs_names()[0] + "/")
)
# temp always gives none
self.assertEqual(None, self.db.vfsname("temp"))
# as does nonsense
self.assertEqual(None, self.db.vfsname("nonsense"))
self.db.pragma("user_version", 3)
self.db.execute(f"attach '{self.db.filename}' as nonsense")
self.assertEqual(self.db.vfsname("main"), self.db.vfsname("nonsense"))
class CustomVFS(apsw.VFS):
def __init__(self):
super().__init__("custom", "")
def xOpen(self, name, flags):
return CorrectHorseBatteryStaple(name, flags)
class CorrectHorseBatteryStaple(apsw.VFSFile):
def __init__(self, name, flags):
super().__init__("", name, flags)
xx = CustomVFS()
con2 = apsw.Connection(self.db.filename, vfs="custom")
self.assertIn("CorrectHorseBatteryStaple", con2.vfsname("main"))
class CustomVFS2(apsw.VFS):
def __init__(self):
super().__init__("custom2", "")
xx2 = CustomVFS2()
con3 = apsw.Connection(self.db.filename, vfs="custom2")
self.assertTrue(con3.vfsname("main").endswith("/" + self.db.vfsname("main")))
def testConnectionConfig(self):
"Test Connection.config function"
self.assertRaises(TypeError, self.db.config)
self.assertRaises(TypeError, self.db.config, "three")
x = 0x7fffffff
self.assertRaises(OverflowError, self.db.config, x * x * x * x * x)
self.assertRaises(ValueError, self.db.config, 82397)
self.assertRaises(TypeError, self.db.config, apsw.SQLITE_DBCONFIG_ENABLE_FKEY, "banana")
for i in apsw.SQLITE_DBCONFIG_ENABLE_FKEY, apsw.SQLITE_DBCONFIG_ENABLE_TRIGGER, apsw.SQLITE_DBCONFIG_ENABLE_QPSG:
self.assertEqual(1, self.db.config(i, 1))
self.assertEqual(1, self.db.config(i, -1))
self.assertEqual(0, self.db.config(i, 0))
self.assertEqual(0, self.db.config(apsw.SQLITE_DBCONFIG_REVERSE_SCANORDER, -1))
self.db.pragma("reverse_unordered_selects", 1)
self.assertEqual(1, self.db.config(apsw.SQLITE_DBCONFIG_REVERSE_SCANORDER, -1))
self.db.config(apsw.SQLITE_DBCONFIG_REVERSE_SCANORDER, 0)
self.assertEqual(0, self.db.pragma("reverse_unordered_selects"))
def testConnectionMetadata(self):
"Test uses of sqlite3_table_column_metadata"
self.db.create_collation("BreadFruit", lambda x, y: 1)
self.db.execute("""
create table table1(x);
create table temp.table2(x);
create table table3(
one INTEGER PRIMARY KEY AUTOINCREMENT,
two Bananas COLLATE BreadFruit,
three [ ] NOT NULL);
""")
self.assertRaises(TypeError, self.db.table_exists, 3, 4)
self.assertRaises(TypeError, self.db.column_metadata, 1, 2, 3)
self.assertTrue(self.db.table_exists(None, "table1"))
self.assertTrue(self.db.table_exists("main", "table1"))
self.assertTrue(self.db.table_exists(None, "table2"))
self.assertTrue(self.db.table_exists("temp", "table2"))
self.assertFalse(self.db.table_exists("main", "table2"))
for colname, expected in (
("one", ('INTEGER', 'BINARY', False, True, True)),
("two", ('Bananas', 'BreadFruit', False, False, False)),
("three", (' ', 'BINARY', True, False, False)),
):
self.assertEqual(self.db.column_metadata(None, "table3", colname), expected)
self.assertRaises(apsw.SQLError, self.db.column_metadata, "not a db", "not a table", "not a column")
def testConnectionNames(self):
"Test Connection.db_names"
self.assertRaises(TypeError, self.db.db_names, 3)
expected = ["main", "temp"]
self.assertEqual(expected, self.db.db_names())
for t in "", APSW.wikipedia_text:
self.db.cursor().execute(f"attach '{ self.db.db_filename('main') }' as '{ t }'")
expected.append(t)
self.assertEqual(expected, self.db.db_names())
while True:
t = f"{ expected[-1] }-{ len(expected) }"
try:
self.db.cursor().execute(f"attach '{ self.db.db_filename('main') }' as '{ t }'")
except apsw.SQLError:
# SQLError: too many attached databases - max ....
break
expected.append(t)
self.assertEqual(expected, self.db.db_names())
while len(expected) > 2:
i = random.randint(2, len(expected) - 1)
self.db.cursor().execute(f"detach '{ expected[i] }'")
del expected[i]
self.assertEqual(expected, self.db.db_names())
def testBackwardsCompatibility(self):
"Verifies changed names etc are still accessible through the old ones"
self.assertIs(apsw.main, apsw.shell.main)
self.assertIs(apsw.Shell, apsw.shell.Shell)
def testModuleStringFunctions(self):
"Tests various string comparison/matching functions"
self.assertRaises(TypeError, apsw.strlike, 1, 2, 3)
self.assertRaises(TypeError, apsw.strlike, None, "3", 3)
self.assertEqual(0, apsw.strlike("%c", "abc"))
self.assertNotEqual(0, apsw.strlike("q%c", "abc"))
self.assertRaises(TypeError, apsw.strglob, 1, 2)
self.assertRaises(TypeError, apsw.strglob, None, None)
self.assertEqual(0, apsw.strglob("*.*", "abc.txt"))
self.assertNotEqual(0, apsw.strglob("b*.*", "abc.txt"))
self.assertRaises(TypeError, apsw.stricmp, None, "s")
self.assertRaises(TypeError, apsw.strnicmp, "s", "s", "s")
self.assertEqual(0, apsw.stricmp("ABC", "abc"))
self.assertNotEqual(0, apsw.stricmp("ABC", "abcd"))
self.assertEqual(0, apsw.strnicmp("ABC", "abc", 77))
self.assertEqual(0, apsw.strnicmp("ABC", "abcd", 3))
self.assertNotEqual(0, apsw.strnicmp("ABC", "abcd", 4))
def testCursorFactory(self):
"Test Connection.cursor_factory"
seqbindings = ((3, ), ) * 3
self.assertEqual(self.db.cursor_factory, apsw.Cursor)
for not_callable in (None, apsw, 3):
try:
self.db.cursor_factory = not_callable
1 / 0
except TypeError:
pass
def error():
1 / 0
self.db.cursor_factory = error
self.assertRaises(TypeError, self.db.execute, "select 3")
self.assertRaises(TypeError, self.db.executemany, "select 3", seqbindings)
def error(_):
return 3
self.db.cursor_factory = error
self.assertRaises(TypeError, self.db.execute, "select 3")
self.assertRaises(TypeError, self.db.executemany, "select 3")
class error:
def __init__(self, _):
pass
self.db.cursor_factory = error
self.assertRaises(AttributeError, self.db.execute, "select 3")
self.assertRaises(AttributeError, self.db.executemany, "select ?", seqbindings)
class inherits(apsw.Cursor):
pass
self.db.cursor_factory = inherits
self.assertEqual(self.db.execute("select 3").fetchall(), self.db.cursor().execute("select 3").fetchall())
self.assertEqual(
self.db.executemany("select ?", seqbindings).fetchall(),
self.db.cursor().executemany("select ?", seqbindings).fetchall())
# kwargs
self.assertEqual(
self.db.execute(bindings=tuple(), statements="select 3").fetchall(),
self.db.cursor().execute(bindings=None, statements="select 3").fetchall())
self.assertEqual(
self.db.executemany(sequenceofbindings=seqbindings, statements="select ?").fetchall(),
self.db.cursor().executemany(statements="select ?", sequenceofbindings=seqbindings).fetchall())
# check cursor_factory across closes
class big:
# make the class consume some memory
memory = b"12345678" * 4096
db2 = apsw.Connection("")
self.assertEqual(db2.cursor_factory, apsw.Cursor)
db2.cursor_factory = big
self.assertEqual(db2.cursor_factory, big)
db2.close()
# factory becomes None when closing
self.assertIsNone(db2.cursor_factory)
# if this leaks it will show up in memory reports
db2.cursor_factory = big
del big
def testMemoryLeaks(self):
"MemoryLeaks: Run with a memory profiler such as valgrind and debug Python"
# make and toss away a bunch of db objects, cursors, functions etc - if you use memory profiling then
# simple memory leaks will show up
c = self.db.cursor()
c.execute("create table foo(x)")
vals = [[1], [None], [math.pi], ["kjkljkljl"], ["\u1234\u345432432423423kjgjklhdfgkjhsdfjkghdfjskh"],
[b"78696ghgjhgjhkgjkhgjhg\xfe\xdf"]]
c.executemany("insert into foo values(?)", vals)
for i in range(MEMLEAKITERATIONS):
db = apsw.Connection(TESTFILEPREFIX + "testdb")
db.create_aggregate_function("aggfunc", lambda x: x)
db.create_scalar_function("scalarfunc", lambda x: x)
db.set_busy_handler(lambda x: False)
db.set_busy_timeout(1000)
db.set_commit_hook(lambda x=1: 0)
db.set_rollback_hook(lambda x=2: 1)
db.set_update_hook(lambda x=3: 2)
db.set_wal_hook(lambda *args: 0)
db.collation_needed(lambda x: 4)
def rt1(c, r):
db.set_row_trace(rt2)
return r
def rt2(c, r):
c.set_row_trace(rt1)
return r
def et1(c, s, b):
db.set_exec_trace(et2)
return True
def et2(c, s, b):
c.set_exec_trace(et1)
return True
for i in range(120):
c2 = db.cursor()
c2.set_row_trace(rt1)
c2.set_exec_trace(et1)
for row in c2.execute("select * from foo" + " " * i): # spaces on end defeat statement cache
pass
del c2
db.close()
def testBindings(self):
"Check bindings work correctly"
c = self.db.cursor()
c.execute("create table foo(x,y,z)")
vals = (
("(?,?,?)", (1, 2, 3)),
("(?,?,?)", [1, 2, 3]),
("(?,?,?)", range(1, 4)),
("(:a,$b,:c)", {
'a': 1,
'b': 2,
'c': 3
}),
("(1,?,3)", (2, )),
("(1,$a,$c)", {
'a': 2,
'b': 99,
'c': 3
}),
# some unicode fun
("($\N{LATIN SMALL LETTER E WITH CIRCUMFLEX},:\N{LATIN SMALL LETTER A WITH TILDE},$\N{LATIN SMALL LETTER O WITH DIAERESIS})",
(1, 2, 3)),
("($\N{LATIN SMALL LETTER E WITH CIRCUMFLEX},:\N{LATIN SMALL LETTER A WITH TILDE},$\N{LATIN SMALL LETTER O WITH DIAERESIS})",
{
"\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}": 1,
"\N{LATIN SMALL LETTER A WITH TILDE}": 2,
"\N{LATIN SMALL LETTER O WITH DIAERESIS}": 3,
}))
for str, bindings in vals:
c.execute("insert into foo values" + str, bindings)
self.assertEqual(curnext(c.execute("select * from foo")), (1, 2, 3))
c.execute("delete from foo")
# currently missing dict keys come out as null
self.assertRaises(TypeError, apsw.allow_missing_dict_bindings, object)
apsw.allow_missing_dict_bindings(True)
c.execute("insert into foo values(:a,:b,$c)", {'a': 1, 'c': 3}) # 'b' deliberately missing
apsw.allow_missing_dict_bindings(False)
self.assertEqual((1, None, 3), curnext(c.execute("select * from foo")))
c.execute("delete from foo")
# these ones should cause errors
vals = (
(apsw.BindingsError, "(?,?,?)", (1, 2)), # too few
(apsw.BindingsError, "(?,?,?)", (1, 2, 3, 4)), # too many
(apsw.BindingsError, "(?,?,?)", None), # none at all
(apsw.BindingsError, "(?,?,?)", {
'a': 1
}), # ? type, dict bindings (note that the reverse will work since all
# named bindings are also implicitly numbered
(TypeError, "(?,?,?)", 2), # not a dict or sequence
(TypeError, "(:a,:b,:c)", {
'a': 1,
'b': 2,
'c': self
}), # bad type for c
)
for exc, str, bindings in vals:
self.assertRaises(exc, c.execute, "insert into foo values" + str, bindings)
# with multiple statements
c.execute("insert into foo values(?,?,?); insert into foo values(?,?,?)", (99, 100, 101, 102, 103, 104))
self.assertRaises(apsw.BindingsError, c.execute,
"insert into foo values(?,?,?); insert into foo values(?,?,?); insert some more",
(100, 100, 101, 1000, 103)) # too few
self.assertRaises(apsw.BindingsError, c.execute, "insert into foo values(?,?,?); insert into foo values(?,?,?)",
(101, 100, 101, 1000, 103, 104, 105)) # too many
# check the relevant statements did or didn't execute as appropriate
self.assertEqual(curnext(self.db.cursor().execute("select count(*) from foo where x=99"))[0], 1)
self.assertEqual(curnext(self.db.cursor().execute("select count(*) from foo where x=102"))[0], 1)
self.assertEqual(curnext(self.db.cursor().execute("select count(*) from foo where x=100"))[0], 1)
self.assertEqual(curnext(self.db.cursor().execute("select count(*) from foo where x=1000"))[0], 0)
self.assertEqual(curnext(self.db.cursor().execute("select count(*) from foo where x=101"))[0], 1)
self.assertEqual(curnext(self.db.cursor().execute("select count(*) from foo where x=105"))[0], 0)
# check there are some bindings!
self.assertRaises(apsw.BindingsError, c.execute, "create table bar(x,y,z);insert into bar values(?,?,?)")
# across executemany
vals = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
c.executemany("insert into foo values(?,?,?);", vals)
for x, y, z in vals:
self.assertEqual(curnext(c.execute("select * from foo where x=?", (x, ))), (x, y, z))
# with an iterator
def myvals():
for i in range(10):
yield {'a': i, 'b': i * 10, 'c': i * 100}
c.execute("delete from foo")
c.executemany("insert into foo values($a,:b,$c)", myvals())
c.execute("delete from foo")
# errors for executemany
self.assertRaises(TypeError, c.executemany, "statement", 12, 34, 56) # incorrect num params
self.assertRaises(TypeError, c.executemany, "statement", 12) # wrong type
self.assertRaises(apsw.SQLError, c.executemany, "syntax error", [(1, )]) # error in prepare
def myiter():
yield 1 / 0
self.assertRaises(ZeroDivisionError, c.executemany, "statement", myiter()) # immediate error in iterator
def myiter():
yield self
self.assertRaises(TypeError, c.executemany, "statement", myiter()) # immediate bad type
self.assertRaises(TypeError, c.executemany, "select ?", ((self, ), (1))) # bad val
c.executemany("statement", ()) # empty sequence
# error in iterator after a while
def myvals():
for i in range(2):
yield {'a': i, 'b': i * 10, 'c': i * 100}
1 / 0
self.assertRaises(ZeroDivisionError, c.executemany, "insert into foo values($a,:b,$c)", myvals())
self.assertEqual(curnext(c.execute("select count(*) from foo"))[0], 2)
c.execute("delete from foo")
# return bad type from iterator after a while
def myvals():
for i in range(2):
yield {'a': i, 'b': i * 10, 'c': i * 100}
yield self
self.assertRaises(TypeError, c.executemany, "insert into foo values($a,:b,$c)", myvals())
self.assertEqual(curnext(c.execute("select count(*) from foo"))[0], 2)
c.execute("delete from foo")
# some errors in executemany
self.assertRaises(apsw.BindingsError, c.executemany, "insert into foo values(?,?,?)", ((1, 2, 3), (1, 2, 3, 4)))
self.assertRaises(apsw.BindingsError, c.executemany, "insert into foo values(?,?,?)", ((1, 2, 3), (1, 2)))
# incomplete execution across executemany
c.executemany("select * from foo; select ?", ((1, ), (2, ))) # we don't read
self.assertRaises(apsw.IncompleteExecutionError, c.executemany, "begin")
# set type (pysqlite error with this)
c.execute("create table xxset(x,y,z)")
c.execute("insert into xxset values(?,?,?)", set((1, 2, 3)))
c.executemany("insert into xxset values(?,?,?)", (set((4, 5, 6)), ))
result = [(1, 2, 3), (4, 5, 6)]
for i, v in enumerate(c.execute("select * from xxset order by x")):
self.assertEqual(v, result[i])
def testCursor(self):
"Check functionality of the cursor"
c = self.db.cursor()
# shouldn't be able to manually create
self.assertRaises(TypeError, apsw.Cursor)
self.assertRaises(TypeError, apsw.Cursor, 3)
self.assertRaises(TypeError, apsw.Cursor, c)
class consub(apsw.Connection):
pass
con2 = consub("")
assert isinstance(con2, apsw.Connection) and not type(con2) == apsw.Connection
apsw.Cursor(con2)
# give bad params
self.assertRaises(TypeError, c.execute)
self.assertRaises(TypeError, c.execute, "foo", "bar", "bam")
# empty statements
c.execute("")
c.execute(" ;\n\t\r;;")
# unicode
self.assertEqual(3, curnext(c.execute(u"select 3"))[0])
# does it work?
c.execute("create table foo(x,y,z)")
# table should be empty
entry = -1
for entry, values in enumerate(c.execute("select * from foo")):
pass
self.assertEqual(entry, -1, "No rows should have been returned")
# add ten rows
for i in range(10):
c.execute("insert into foo values(1,2,3)")
for entry, values in enumerate(c.execute("select * from foo")):
# check we get back out what we put in
self.assertEqual(values, (1, 2, 3))
self.assertEqual(entry, 9, "There should have been ten rows")
# does get_connection return the right object
self.assertIs(c.get_connection(), self.db)
self.assertIs(c.connection, self.db)
self.assertIs(c.connection, c.get_connection())
# check get_description - note column with space in name and [] syntax to quote it
cols = (
("x a space", "INTEGER"),
("y", "TEXT"),
("z", "foo"),
("a", "char"),
("\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}\N{LATIN SMALL LETTER A WITH TILDE}",
"\N{LATIN SMALL LETTER O WITH DIAERESIS}\N{LATIN SMALL LETTER U WITH CIRCUMFLEX}"),
)
c.execute("drop table foo; create table foo (%s)" % (", ".join(["[%s] %s" % (n, t) for n, t in cols]), ))
c.execute("insert into foo([x a space]) values(1)")
c.execute(
"create temp table two(fred banana); insert into two values(7); create temp view three as select fred as [a space] from two"
)
c.execute("select 3") # see issue #370
has_full = any(o == "ENABLE_COLUMN_METADATA" or o.startswith("ENABLE_COLUMN_METADATA=")
for o in apsw.compile_options) if apsw.using_amalgamation else hasattr(c, "description_full")
for row in c.execute("select * from foo"):
self.assertEqual(cols, c.get_description())
self.assertEqual(has_full, hasattr(c, "description_full"))
self.assertEqual(cols, tuple([d[:2] for d in c.description]))
self.assertEqual((None, None, None, None, None), c.description[0][2:])
self.assertEqual(list(map(len, c.description)), [7] * len(cols))
if has_full:
for row in c.execute("select * from foo join three"):
self.assertEqual(c.description_full,
(('x a space', 'INTEGER', 'main', 'foo', 'x a space'),
('y', 'TEXT', 'main', 'foo', 'y'), ('z', 'foo', 'main', 'foo', 'z'),
('a', 'char', 'main', 'foo', 'a'), ('êã', 'öû', 'main', 'foo', 'êã'),
('a space', 'banana', 'temp', 'two', 'fred')))
# check description caching isn't broken
cols2 = cols[1:4]
for row in c.execute("select y,z,a from foo"):
self.assertEqual(cols2, c.get_description())
self.assertEqual(cols2, tuple([d[:2] for d in c.description]))
self.assertEqual((None, None, None, None, None), c.description[0][2:])
self.assertEqual(list(map(len, c.description)), [7] * len(cols2))
# execution is complete ...
self.assertRaises(apsw.ExecutionCompleteError, c.get_description)
self.assertRaises(apsw.ExecutionCompleteError, lambda: c.description)
if has_full:
self.assertRaises(apsw.ExecutionCompleteError, lambda: c.description_full)
self.assertRaises(StopIteration, lambda xx=0: next(c))
self.assertRaises(StopIteration, lambda xx=0: next(c))
# fetchone is used throughout, check end behaviour
self.assertEqual(None, c.fetchone())
self.assertEqual(None, c.fetchone())
self.assertEqual(None, c.fetchone())
# nulls for get_description
for row in c.execute("pragma user_version"):
self.assertEqual(c.get_description(), (('user_version', None), ))
# incomplete
c.execute("select * from foo; create table bar(x)") # we don't bother reading leaving
self.assertRaises(apsw.IncompleteExecutionError, c.execute, "select * from foo") # execution incomplete
self.assertTableNotExists("bar")
# autocommit
self.assertEqual(True, self.db.get_autocommit())
c.execute("begin immediate")
self.assertEqual(False, self.db.get_autocommit())
# pragma
c.execute("pragma user_version")
c.execute("pragma pure=nonsense")
# error
self.assertRaises(apsw.SQLError, c.execute,
"create table bar(x,y,z); this is a syntax error; create table bam(x,y,z)")
self.assertTableExists("bar")
self.assertTableNotExists("bam")
# fetchall
self.assertEqual(c.fetchall(), [])
self.assertEqual(c.execute("select 3; select 4").fetchall(), [(3, ), (4, )])
# readonly, explain & expanded_sql attributes
res = None
# tracing errors
self.assertRaises(TypeError, setattr, c, "exec_trace", 3)
self.assertRaises(TypeError, setattr, self.db, "exec_trace", 3)
self.assertRaises(TypeError, setattr, c, "row_trace", 3)
self.assertRaises(TypeError, setattr, self.db, "row_trace", 3)
self.assertIsNone(c.row_trace)
xx = lambda *args: 1 / 0
c.row_trace = xx
self.assertIs(c.row_trace, xx)
c.row_trace = None
def tracer(cur, query, bindings):
nonlocal res
res = {
"cursor": cur,
"query": query,
"bindings": bindings,
"readonly": cur.is_readonly,
"explain": cur.is_explain
}
return True
self.assertIsNone(c.exec_trace)
c.set_exec_trace(tracer)
self.assertIs(c.exec_trace, tracer)
c.execute("pragma user_version")
self.assertIs(res["cursor"], c)
self.assertTrue(res["readonly"])
self.assertEqual(res["explain"], 0)
c.execute("explain pragma user_version")
self.assertEqual(res["explain"], 1)
c.execute("explain query plan select 3")
self.assertEqual(res["explain"], 2)
c.execute("pragma user_version=42")
self.assertFalse(res["readonly"])
biggy = "9" * 24 * 1024
ran = False
for row in c.execute("select ?,?", (biggy, biggy)):
ran = True
self.assertEqual(f"select '{ biggy }','{ biggy }'", c.expanded_sql)
existing = self.db.limit(apsw.SQLITE_LIMIT_LENGTH, 25 * 1024)
self.assertRaises(MemoryError, getattr, c, "expanded_sql")
self.db.limit(apsw.SQLITE_LIMIT_LENGTH, existing)
self.assertTrue(ran)
# keyword args
c.execute("pragma user_version=73", bindings=None, can_cache=False, prepare_flags=0).fetchall()
c.executemany(statements="select ?", sequenceofbindings=((1, ), (2, )), can_cache=False,
prepare_flags=0).fetchall()
# non-contiguous buffers
self.assertRaises(BufferError, c.execute, "select ?", (memoryview(b"234567890")[::2], ))
def testIssue373(self):
"issue 373: dict type checking in bindings"
class not_a_dict:
pass
class dict_lookalike(collections.abc.Mapping):
def __getitem__(self, _):
return 99
def __iter__(*args):
raise NotImplementedError
def __len__(*args):
raise NotImplementedError
class errors_be_here:
def __instancecheck__(self, _):
1 / 0
def __subclasscheck__(self, _):
1 / 0
class dict_with_error:
def __getitem__(self, _):
1 / 0
collections.abc.Mapping.register(dict_with_error)
class coerced_to_list:
# this is not registered as dict, and instead PySequence_Fast will
# turn it into a list calling the method for each key
def __getitem__(self, key):
if key < 10:
return key
1 / 0
class dict_subclass(dict):
pass
class list_subclass(list):
pass
self.assertRaises(TypeError, self.db.execute, "select :name", not_a_dict())
self.assertEqual([(99, )], self.db.execute("select :name", dict_lookalike()).fetchall())
# make sure these aren't detected as dict
for thing in (1, ), {1}, [1], list_subclass([1]):
self.assertRaises(TypeError, self.db.execute("select :name", thing))
self.assertRaises(TypeError, self.db.execute, "select :name", errors_be_here())
self.assertRaises(ZeroDivisionError, self.db.execute, "select :name", dict_with_error())
apsw.allow_missing_dict_bindings(True)
self.assertEqual([(None, )], self.db.execute("select :name", {}).fetchall())
self.assertEqual([(None, )], self.db.execute("select :name", dict_subclass()).fetchall())
apsw.allow_missing_dict_bindings(False)
self.assertRaises(ZeroDivisionError, self.db.execute, "select ?", coerced_to_list())
# same tests with executemany
self.assertRaises(TypeError, self.db.executemany, "select :name", (not_a_dict(), ))
self.assertEqual([(99, )], self.db.executemany("select :name", [dict_lookalike()]).fetchall())
# make sure these aren't detected as dict
for thing in (1, ), {1}, [1]:
self.assertRaises(TypeError, self.db.executemany("select :name", [thing]))
self.assertRaises(TypeError, self.db.executemany, "select :name", errors_be_here())
self.assertRaises(ZeroDivisionError, self.db.executemany, "select :name", dict_with_error())
apsw.allow_missing_dict_bindings(True)
self.assertEqual([(None, )], self.db.executemany("select :name", ({}, )).fetchall())
self.assertEqual([(None, )], self.db.executemany("select :name", [dict_subclass()]).fetchall())
apsw.allow_missing_dict_bindings(False)
self.assertRaises(ZeroDivisionError, self.db.executemany, "select ?", (coerced_to_list(), ))
def testIssue376(self):
"Whitespace treated as incomplete execution"
c = self.db.cursor()
for statement in (
"select 3",
"select 3;",
"select 3; ",
"select 3; ;\t\r\n; ",
):
c.execute(statement)
# should not throw incomplete
c.execute("select 4")
self.assertEqual([(3, ), (4, )], c.execute(statement + "; select 4").fetchall())
def testIssue425(self):
"Infinite recursion"
class VFSA(apsw.VFS):
def __init__(self):
super().__init__("vfsa", "")
def xOpen(self, name, flags):
return VFSAFile(name, flags)
class VFSAFile(apsw.VFSFile):
def __init__(self, filename, flags):
super().__init__("vfsb", filename, flags)
class VFSB(apsw.VFS):
def __init__(self):
super().__init__("vfsb", "")
def xOpen(self, name, flags):
return VFSBFile(name, flags)
class VFSBFile(apsw.VFSFile):
def __init__(self, filename, flags):
super().__init__("vfsa", filename, flags)
a = VFSA()
b = VFSB()
sys.setrecursionlimit(200)
print("A message due to RecursionError is possible, and what is being tested", file=sys.stderr)
self.assertRaises(RecursionError, apsw.Connection, "testdb", vfs="vfsa")
sys.setrecursionlimit(1000)
def handler(): # incorrect number of arguments on purpose
pass
try:
apsw.config(apsw.SQLITE_CONFIG_LOG, handler)
self.assertRaisesUnraisable(TypeError, apsw.log, 11, "recursion error forced")
finally:
apsw.config(apsw.SQLITE_CONFIG_LOG, None)
def testTypes(self):
"Check type information is maintained"
# zeroblob in functions
def func(n: int):
return apsw.zeroblob(n)
self.db.create_scalar_function("func", func)
self.assertEqual(self.db.execute("select func(17)").fetchall()[0][0], b"\0" * 17)
c = self.db.cursor()
c.execute("create table foo(row,x)")
vals = test_types_vals
for i, v in enumerate(vals):
c.execute("insert into foo values(?,?)", (i, v))
# add function to test conversion back as well
def snap(*args):
return args[0]
self.db.create_scalar_function("snap", snap)
# now see what we got out
count = 0
for row, v, fv in c.execute("select row,x,snap(x) from foo"):
count += 1
if type(vals[row]) is float:
self.assertAlmostEqual(vals[row], v)
self.assertAlmostEqual(vals[row], fv)
else:
self.assertEqual(vals[row], v)
self.assertEqual(vals[row], fv)
self.assertEqual(count, len(vals))
# check some out of bounds conditions
# integer greater than signed 64 quantity (SQLite only supports up to that)
self.assertRaises(OverflowError, c.execute, "insert into foo values(9999,?)", (922337203685477580799, ))
self.assertRaises(OverflowError, c.execute, "insert into foo values(9999,?)", (-922337203685477580799, ))
# not valid types for SQLite
self.assertRaises(TypeError, c.execute, "insert into foo values(9999,?)", (apsw, )) # a module
self.assertRaises(TypeError, c.execute, "insert into foo values(9999,?)", (type, )) # type
self.assertRaises(TypeError, c.execute, "insert into foo values(9999,?)", (dir, )) # function
# check nothing got inserted
self.assertEqual(0, curnext(c.execute("select count(*) from foo where row=9999"))[0])
def testMissingDictBindings(self):
"How missing bindings are handled"
orig = apsw.allow_missing_dict_bindings(True)
self.assertEqual(self.db.execute("select :foo, $bar", {"foo": 3, "XXX": 4}).fetchall(), [(3, None)])
apsw.allow_missing_dict_bindings(False)
self.assertRaises(KeyError, self.db.execute, "select :foo, $bar", {"foo": 3, "XXX": 4})
apsw.allow_missing_dict_bindings(orig)
def testFormatSQLValue(self):
"Verify text formatting of values"
wt = APSW.wikipedia_text
vals = (
(3, "3"),
(3.1, "3.1"),
(-3, "-3"),
(-3.1, "-3.1"),
(9223372036854775807, "9223372036854775807"),
(-9223372036854775808, "-9223372036854775808"),
(None, "NULL"),
("ABC", "'ABC'"),
("\N{BLACK STAR} \N{WHITE STAR} \N{LIGHTNING} \N{COMET} ",
"'" + "\N{BLACK STAR} \N{WHITE STAR} \N{LIGHTNING} \N{COMET} " + "'"),
("\N{BLACK STAR} \N{WHITE STAR} ' \N{LIGHTNING} \N{COMET} ",
"'" + "\N{BLACK STAR} \N{WHITE STAR} '' \N{LIGHTNING} \N{COMET} " + "'"),
("", "''"),
("'", "''''"),
("'a", "'''a'"),
("a'", "'a'''"),
("''", "''''''"),
("'" * 20000, "'" + "'" * 40000 + "'"),
("\0", "''||X'00'||''"),
("\0\0\0", "''||X'00'||''||X'00'||''||X'00'||''"),
("AB\0C", "'AB'||X'00'||'C'"),
("A'B'\0C", "'A''B'''||X'00'||'C'"),
("\0A'B", "''||X'00'||'A''B'"),
("A'B\0", "'A''B'||X'00'||''"),
(b"ABDE\0C", "X'414244450043'"),
(b"", "X''"),
(wt, "'" + wt + "'"),
(wt[:77] + "'" + wt[77:], "'" + wt[:77] + "''" + wt[77:] + "'"),
# SQLite doesn't understand 'inf', 'nan' or negative zero
# so output what it does
(math.inf, "1e999"),
(-math.inf, "-1e999"),
(math.nan, "NULL"),
(-0.0, "0.0"),
)
for vin, vout in vals:
out = apsw.format_sql_value(vin)
self.assertEqual(out, vout)
for row in self.db.execute("select " + vout):
if vin in (math.nan, -0.0):
continue
self.assertEqual(row[0], vin)
# Errors
self.assertRaises(TypeError, apsw.format_sql_value, apsw)
self.assertRaises(TypeError, apsw.format_sql_value)
def testVTableStuff(self):
"Test new stuff added for Virtual tables"
columns = [f"c{ n }" for n in range(30)]
class Source:
indexinfo_saved = None
bio_callback = lambda *args: True
filter_callback = lambda *args: 0
create_callback = lambda: 0
sn_called = set()
schema = f"create table ignored({ ','.join(columns) })"
def Create(self, *args):
Source.create_callback()
return self.schema, Source.Table()
Connect = Create
def ShadowName(self, suffix):
Source.sn_called.add((id(self), suffix))
class Table:
def BestIndexObject(Self, o):
Source.indexinfo_saved = o
return Source.bio_callback(o)
def BestIndex(self, constraints, orderby):
# non-string for idxstr
return [[0], 1, object()]
def Open(self):
return Source.Cursor()
def Disconnect(self):
pass
Destroy = Disconnect
def UpdateChangeRow(self_table, *args):
self.assertEqual(apsw.mapping_conflict_resolution_modes[self.db.vtab_on_conflict()],
Source.expected_conflict)
def UpdateDeleteRow(self, rowid):
pass
def Savepoint1(self, level):
pass
def Savepoint2(self):
1 / 0
def Release1(self, level):
pass
def Release2(self):
1 / 0
def RollbackTo1(self, level):
pass
def RollbackTo2(self):
1 / 0
class Cursor:
max_row = -1
def Filter(self, *args):
self.rownum = 0
return Source.filter_callback(*args)
def Eof(self):
return self.rownum >= self.max_row
def Close(self):
return
def Rowid(self):
return self.rownum
def Column(self, num):
return self.rownum
def Next(self):
self.rownum += 1
self.db.create_module("foo", Source(), use_bestindex_object=True)
self.db.create_module("foo2", Source(), use_bestindex_object=False)
self.assertRaises(ValueError, self.db.create_module, "foo2", Source(), iVersion=77)
self.db.execute("create virtual table bar using foo()")
self.db.execute("create virtual table bar2 using foo2()")
self.assertRaises(TypeError, self.db.execute, "select * from bar2 where c0>7")
def basic(o):
odict = apsw.ext.index_info_to_dict(o)
self.assertEqual(expected, odict)
exercise(o)
return True
# check out of bounds/setting etc
def exercise(o):
self.assertRaises(OverflowError, setattr, o, "idxNum", 0x7fff_ffff * 0x1_000)
self.assertRaises(TypeError, setattr, o, "orderByConsumed", "not an int")
self.assertRaises(OverflowError, setattr, o, "idxFlags", 0x7fff_ffff * 0x1_000)
self.assertRaises(TypeError, setattr, o, "estimatedRows", "not an int")
self.assertRaises(TypeError, o.get_aConstraint_iColumn, "not an int")
self.assertRaises(TypeError, o.get_aConstraint_op, "not an int")
self.assertRaises(TypeError, o.get_aConstraint_usable, "not an int")
self.assertRaises(TypeError, o.get_aConstraint_collation, "not an int")
self.assertRaises(TypeError, o.get_aConstraint_rhs, "not an int")
self.assertRaises(TypeError, o.get_aOrderBy_iColumn, "not an int")
self.assertRaises(TypeError, o.get_aOrderBy_desc, "not an int")
self.assertRaises(TypeError, o.get_aConstraintUsage_in, "not an int")
self.assertRaises(IndexError, o.set_aConstraintUsage_argvIndex, -1, 0)
self.assertRaises(IndexError, o.set_aConstraintUsage_argvIndex, o.nConstraint, 0)
self.assertRaises(OverflowError, o.set_aConstraintUsage_argvIndex, 2**(1 + sizeof_c_int), 0)
self.assertRaises(IndexError, o.set_aConstraintUsage_omit, -1, 0)
self.assertRaises(IndexError, o.set_aConstraintUsage_omit, o.nConstraint, 0)
self.assertRaises(OverflowError, o.set_aConstraintUsage_omit, 2**(1 + sizeof_c_int), 0)
self.assertRaises(IndexError, o.get_aConstraintUsage_argvIndex, -1)
self.assertRaises(IndexError, o.get_aConstraintUsage_argvIndex, o.nConstraint)
self.assertRaises(OverflowError, o.get_aConstraintUsage_argvIndex, 2**(1 + sizeof_c_int))
self.assertRaises(IndexError, o.get_aConstraintUsage_omit, -1)
self.assertRaises(IndexError, o.get_aConstraintUsage_omit, o.nConstraint)
self.assertRaises(OverflowError, o.get_aConstraintUsage_omit, 2**(1 + sizeof_c_int))
for n in dir(o):
if n.startswith("_") or n.startswith("get_") or n.startswith("set_"):
continue
ro = "(Read-only)" in inspect.getdoc(getattr(apsw.IndexInfo, n))
if ro:
try:
setattr(o, n, getattr(o, n))
except AttributeError as e:
self.assertIn("is not writable", str(e))
continue
# should be able to set to what it already is
v = getattr(o, n)
setattr(o, n, v)
self.assertEqual(v, getattr(o, n))
if v in (True, False):
setattr(o, n, not v)
self.assertEqual(getattr(o, n), not v)
self.assertRaises(TypeError, setattr, o, n, "not a bool")
self.assertRaises(TypeError, setattr, o, n, object())
elif n == "estimatedCost":
for newval in (v + 1, -v / (v + 1), True):
setattr(o, "estimatedCost", newval)
self.assertEqual(o.estimatedCost, newval)
self.assertRaises(TypeError, setattr, o, "estimatedCost", "not a float")
self.assertRaises(TypeError, setattr, o, "estimatedCost", object())
elif n == "idxStr":
self.assertRaises(TypeError, setattr, o, "idxStr", 3)
self.assertRaises(TypeError, setattr, o, "idxStr", b"aabbcc")
self.assertRaises(TypeError, setattr, o, "idxStr", object())
for i in range(50):
if i % 2:
o.idxStr = None
self.assertIsNone(o.idxStr)
t = f"stuff { i }"
o.idxStr = t
self.assertEqual(o.idxStr, t)
elif n == "estimatedRows":
self.assertRaises(TypeError, setattr, o, n, 3.2)
self.assertRaises(TypeError, setattr, o, n, "seven")
self.assertRaises(TypeError, setattr, o, n, [])
for newval in (25, 77, 2 * 1024 * 1024 * 1024):
setattr(o, n, newval)
self.assertEqual(newval, getattr(o, n))
else:
raise Exception(f"untested attribute { n }")
Source.bio_callback = basic
for query, expected in self.index_info_test_patterns:
for row in self.db.execute(query, can_cache=False):
pass
# check we get failures outside of bestindex
saved = Source.indexinfo_saved
for name in dir(saved):
if name.startswith("_"):
continue
if name.startswith("get_") or name.startswith("set_"):
try:
getattr(saved, name)(0)
1 / 0
except ValueError as e:
self.assertIn("IndexInfo is out of scope", str(e))
continue
doc = inspect.getdoc(getattr(apsw.IndexInfo, name))
try:
getattr(saved, name)
1 / 0
except ValueError as e:
self.assertIn("IndexInfo is out of scope", str(e))
if "(Read-only)" in doc:
continue
try:
setattr(saved, name, 7)
1 / 0
except ValueError as e:
self.assertIn("IndexInfo is out of scope", str(e))
# error returns
def bio1(o):
return False
def bio2(o):
raise Exception("bio2 not playing")
def bio3(o):
return "not a bool"
def bio4(o):
return None
for f in bio1, bio2, bio3, bio4:
Source.bio_callback = f
try:
self.db.execute(self.index_info_test_patterns[0][0], can_cache=False)
except Exception as e:
if f is bio1:
self.assertIn("no query solution", str(e))
elif f is bio2:
self.assertIn("bio2 not playing", str(e))
elif f in (bio3, bio4):
self.assertIsInstance(e, TypeError)
else:
1 / 0
# how we get usable == False
def bioUnusable(o):
# we can get usable == False by having an IN which comes across as CONSTRAINT_EQ
# and using it. then the next call will have it as unusable
for i in range(o.nConstraint):
if o.get_aConstraint_op(i) == apsw.SQLITE_INDEX_CONSTRAINT_EQ \
and o.get_aConstraint_usable(i):
o.set_aConstraintUsage_argvIndex(i, 1)
return True
else:
self.assertTrue(any(o.get_aConstraint_usable(i) == False for i in range(o.nConstraint)))
return True
Source.bio_callback = bioUnusable
for _ in self.db.execute("select * from bar where c1 in (1,2) and c2>6"):
pass
# check it actually works
def bio(o):
# we work on the second query in index_info_test_patterns
self.assertEqual(apsw.ext.index_info_to_dict(o), self.index_info_test_patterns[1][1])
o.set_aConstraintUsage_argvIndex(0, 1)
o.set_aConstraintUsage_omit(0, True)
self.assertNotEqual(apsw.ext.index_info_to_dict(o), self.index_info_test_patterns[1][1])
o.idxStr = "some text"
return True
def bio_f(*args):
self.assertEqual(args, (0, 'some text', ('foo', )))
Source.bio_callback = bio
Source.filter_callback = bio_f
self.db.execute(self.index_info_test_patterns[1][0])
# More than 63 columns affects colUsed
manycol_names = [f'c{ n }' for n in range(100)]
Source.schema = f"create table ignored({ ','.join(manycol_names) })"
self.db.execute("create virtual table manycol using foo()")
def check(o):
d = apsw.ext.index_info_to_dict(o, column_names=manycol_names)
self.assertIn("c61", d["colUsed_names"])
self.assertNotIn("c62", d["colUsed_names"])
for name in manycol_names[63:]:
self.assertIn(name, d["colUsed_names"])
return True
Source.bio_callback = check
Source.filter_callback = lambda *args: 0
self.db.execute("select c61, c64 from manycol where c72>7")
# in args
def bio(o):
d = apsw.ext.index_info_to_dict(o)
self.assertTrue(d["aConstraintUsage"][0]["in"])
self.assertTrue(d["aConstraintUsage"][1]["in"])
self.assertFalse(d["aConstraintUsage"][2]["in"])
self.assertRaises(TypeError, o.set_aConstraintUsage_in, 0, 'foo')
self.assertRaises(IndexError, o.set_aConstraintUsage_in, 99, True)
self.assertRaises(ValueError, o.set_aConstraintUsage_in, 2, True)
o.set_aConstraintUsage_in(0, True)
o.set_aConstraintUsage_in(1, True)
for i in range(3):
o.set_aConstraintUsage_argvIndex(i, i + 1)
return True
def bio_f(*args):
self.assertEqual(args, (0, None, ({1, 'one', 1.1}, {b'aabbcc', None}, None)))
Source.schema = "create table ignored(c0,c1,c2)"
Source.bio_callback = bio
Source.filter_callback = bio_f
self.db.execute(
"create virtual table in_filter using foo(); select * from in_filter where c0 in (1,1.1,'one') and c1 in (?,?,?) and c2 in (?)",
(None, b'aabbcc', None, None))
# vtab_config
self.assertRaises(ValueError, self.db.vtab_config, apsw.SQLITE_VTAB_CONSTRAINT_SUPPORT)
def check():
self.assertRaises(TypeError, self.db.vtab_config, "three")
self.assertRaises(TypeError, self.db.vtab_config, apsw.SQLITE_VTAB_CONSTRAINT_SUPPORT, "three")
self.assertRaises(TypeError, self.db.vtab_config, apsw.SQLITE_VTAB_CONSTRAINT_SUPPORT,
apsw.SQLITE_VTAB_CONSTRAINT_SUPPORT, apsw.SQLITE_VTAB_CONSTRAINT_SUPPORT)
self.assertRaises(ValueError, self.db.vtab_config, 97657)
self.db.vtab_config(apsw.SQLITE_VTAB_CONSTRAINT_SUPPORT, 0)
self.db.vtab_config(apsw.SQLITE_VTAB_INNOCUOUS)
self.db.vtab_config(apsw.SQLITE_VTAB_DIRECTONLY, 0)
Source.create_callback = check
self.db.execute("create virtual table vtab_config using foo()")
self.assertRaises(ValueError, self.db.vtab_config, apsw.SQLITE_VTAB_CONSTRAINT_SUPPORT)
# vtab_on_conflict
self.db.execute("create virtual table vtab_on_conflict using foo()")
self.assertRaises(ValueError, self.db.vtab_on_conflict)
Source.bio_callback = Source.filter_callback = lambda *args: True
Source.Cursor.max_row = 1
for mode in ("ROLLBACK", "FAIL", "ABORT", "IGNORE", "REPLACE"):
Source.expected_conflict = "SQLITE_" + mode
self.db.execute(f"update or { mode } vtab_on_conflict set c0=7 where rowid=0")
self.assertRaises(ValueError, self.db.vtab_on_conflict)
# savepoints
Source.Cursor.max_row = 20
Source.filter_callback = lambda *args: 0
Source.bio_callback = lambda *args: True
self.db.create_module("sptest", Source(), use_bestindex_object=True, iVersion=3)
self.db.execute("create virtual table sptest using sptest()")
def clear():
while not self.db.get_autocommit():
self.db.execute("rollback")
query = """begin;
delete from sptest where rowid=7;
savepoint t0 ;
delete from sptest where rowid=8;
savepoint t1;
delete from sptest where rowid=9;
savepoint t2;
delete from sptest where rowid=10;
savepoint t3;
delete from sptest where rowid=10;
release t3; rollback to t1;
commit;
"""
# check it works
clear()
self.db.execute(query)
Source.Table.Savepoint = Source.Table.Savepoint2
clear()
self.assertRaises(TypeError, self.db.execute, query)
Source.Table.Savepoint = Source.Table.Savepoint1
clear()
self.db.execute(query)
Source.Table.Release = Source.Table.Release2
clear()
self.assertRaises(TypeError, self.db.execute, query)
Source.Table.Release = Source.Table.Release1
clear()
self.db.execute(query)
Source.Table.RollbackTo = Source.Table.RollbackTo2
clear()
self.assertRaises(TypeError, self.db.execute, query)
Source.Table.RollbackTo = Source.Table.RollbackTo1
clear()
self.db.execute(query)
# shadowname
self.assertEqual(len(Source.sn_called), 0)
def make_shadow(bn):
self.db.execute(f"create table { bn }_foo(x); create table { bn }_bar(x)")
make_shadow("sptest")
self.assertEqual(len(Source.sn_called), 2)
Source.ShadowName = lambda *args: 1 / 0
self.assertRaisesUnraisable(ZeroDivisionError, self.db.execute, "create table sptest_bam(x)")
Source.ShadowName = lambda *args: "foo"
self.assertRaisesUnraisable(TypeError, self.db.execute, "create table sptest_bam2(x)")
Source.ShadowName = lambda *args: 3 + 4j
self.assertRaisesUnraisable(TypeError, self.db.execute, "create table sptest_bam3(x)")
Source.ShadowName = lambda *args: True
self.db.execute("create table sptest_bam4(x)")
def clear_all():
self.db.close()
self.db = apsw.Connection(":memory:")
# run out of shadowname slots
clear_all()
for i in range(2000):
try:
self.db.create_module(f"sptest{ i }", Source(), use_bestindex_object=True, iVersion=3)
except Exception as e:
limit = i
self.assertIn("No xShadowName slots are available", str(e))
break
# do some deliberate thrashing
clear_all()
registered = set()
for i in range(limit * 20):
if len(registered) == limit:
victim = random.choice(list(registered))
self.db.execute(f"drop table v{ victim }")
self.db.create_module(victim, None)
registered.remove(victim)
name = f"sptest{ i }"
self.db.create_module(f"sptest{ i }", Source(), use_bestindex_object=False, iVersion=3)
self.db.execute(f"create virtual table v{ name } using { name }")
make_shadow(f"v{ name }")
registered.add(name)
index_info_test_patterns = (("select * from bar where c7 is null", {
'nConstraint':
1,
'aConstraint': [{
'iColumn': 7,
'op': 71,
'op_str': 'SQLITE_INDEX_CONSTRAINT_ISNULL',
'usable': True,
'collation': 'BINARY',
'rhs': None
}],
'nOrderBy':
0,
'aOrderBy': [],
'aConstraintUsage': [{
'argvIndex': 0,
'in': False,
'omit': False
}],
'idxNum':
0,
'idxStr':
None,
'orderByConsumed':
False,
'estimatedCost':
5e+98,
'estimatedRows':
25,
'idxFlags':
0,
'idxFlags_set':
set(),
'colUsed':
{0, 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},
'distinct':
0
}), (
"select c2, c4, c6 from bar where c3 is not null and c10>'foo' and c12!=c14 group by c8 order by c10 desc",
{
'nConstraint':
2,
'aConstraint': [{
'iColumn': 10,
'op': 4,
'op_str': 'SQLITE_INDEX_CONSTRAINT_GT',
'usable': True,
'collation': 'BINARY',
'rhs': 'foo'
}, {
'iColumn': 3,
'op': 70,
'op_str': 'SQLITE_INDEX_CONSTRAINT_ISNOTNULL',
'usable': True,
'collation': 'BINARY',
'rhs': None
}],
'nOrderBy':
1,
'aOrderBy': [{
'iColumn': 8,
'desc': True
}],
'aConstraintUsage': [{
'argvIndex': 0,
'omit': False,
'in': False,
}, {
'argvIndex': 0,
'omit': False,
'in': False,
}],
'idxNum':
0,
'idxStr':
None,
'orderByConsumed':
False,
'estimatedCost':
5e+98,
'estimatedRows':
25,
'idxFlags':
0,
'idxFlags_set':
set(),
'colUsed': {2, 3, 4, 6, 8, 10, 12, 14},
'distinct':
1
},
))
def testVTableNoChange(self):
"Test virtual table no change values on update"
class Source:
data = [(i, 2, i, 4, i) for i in range(10)]
def Create(self, *args):
return "create table ignored(c0, c1, c2, c3, c4)", Source.Table()
Connect = Create
class Table:
def BestIndex(self, *args):
return None
def Open(self):
return Source.Cursor()
def Disconnect(self):
pass
Destroy = Disconnect
def UpdateChangeRow(tself, rowid, newrowid, fields):
d = Source.data
expected = (apsw.no_change, d[rowid][2] + 1, apsw.no_change, 4, apsw.no_change)
self.assertEqual(fields, expected)
class Cursor:
def Filter(self, *args):
self.pos = 0
def Eof(self):
return self.pos >= len(Source.data)
def Next(self):
self.pos += 1
def Column(self, n):
return Source.data[self.pos][n]
def ColumnNoChange(self, n):
if n % 2:
return self.Column(n)
return apsw.no_change
def Close(self):
pass
def Rowid(self):
return self.pos
self.db.create_module("testing", Source(), eponymous=True, use_no_change=True)
self.db.execute("update testing set c1=c2+1")
def testWAL(self):
"Test WAL functions"
# note that it is harmless calling wal functions on a db not in wal mode
self.assertRaises(TypeError, self.db.wal_autocheckpoint)
self.assertRaises(TypeError, self.db.wal_autocheckpoint, "a strinbg")
self.db.wal_autocheckpoint(8912)
self.assertRaises(TypeError, self.db.wal_checkpoint, -1)
self.db.wal_checkpoint()
self.db.wal_checkpoint("main")
v = self.db.wal_checkpoint(mode=apsw.SQLITE_CHECKPOINT_PASSIVE)
self.assertTrue(isinstance(v, tuple) and len(v) == 2 and isinstance(v[0], int) and isinstance(v[1], int))
self.assertRaises(apsw.MisuseError, self.db.wal_checkpoint, mode=876786)
self.assertRaises(TypeError, self.db.set_wal_hook)
self.assertRaises(TypeError, self.db.set_wal_hook, 12)
self.db.set_wal_hook(None)
# check we can set wal mode
self.assertEqual("wal", self.db.cursor().execute("pragma journal_mode=wal").fetchall()[0][0])
# errors in wal callback
def zerodiv(*args):
1 / 0
self.db.set_wal_hook(zerodiv)
self.assertRaises(ZeroDivisionError, self.db.cursor().execute, "create table one(x)")
# the error happens after the wal commit so the table should exist
self.assertTableExists("one")
def badreturn(*args):
return "three"
self.db.set_wal_hook(badreturn)
self.assertRaises(TypeError, self.db.cursor().execute, "create table two(x)")
self.assertTableExists("two")
expectdbname = ""
def walhook(conn, dbname, pages):
self.assertTrue(conn is self.db)
self.assertTrue(pages > 0)
self.assertEqual(dbname, expectdbname)
return apsw.SQLITE_OK
expectdbname = "main"
self.db.set_wal_hook(walhook)
self.db.cursor().execute("create table three(x)")
self.db.cursor().execute("attach '%stestdb2?psow=0' as fred" % ("file:" + TESTFILEPREFIX, ))
self.assertEqual("wal", self.db.cursor().execute("pragma fred.journal_mode=wal").fetchall()[0][0])
expectdbname = "fred"
self.db.cursor().execute("create table fred.three(x)")
def testAuthorizer(self):
"Verify the authorizer works"
retval = apsw.SQLITE_DENY
def authorizer(operation, paramone, paramtwo, databasename, triggerorview):
# we fail creates of tables starting with "private"
if operation == apsw.SQLITE_CREATE_TABLE and paramone.startswith("private"):
return retval
return apsw.SQLITE_OK
c = self.db.cursor()
# this should succeed
c.execute("create table privateone(x)")
# this should fail
self.assertRaises(TypeError, self.db.set_authorizer, 12) # must be callable
self.assertRaises(TypeError, setattr, self.db, "authorizer", 12)
self.db.set_authorizer(authorizer)
self.assertIs(self.db.authorizer, authorizer)
for val in apsw.SQLITE_DENY, apsw.SQLITE_DENY, 0x800276889000212112:
retval = val
if val < 100:
self.assertRaises(apsw.AuthError, c.execute, "create table privatetwo(x)")
else:
self.assertRaises(OverflowError, c.execute, "create table privatetwo(x)")
# this should succeed
self.db.set_authorizer(None)
self.assertIsNone(self.db.authorizer)
c.execute("create table privatethree(x)")
self.assertTableExists("privateone")
self.assertTableNotExists("privatetwo")
self.assertTableExists("privatethree")
# error in callback
def authorizer(operation, *args):
if operation == apsw.SQLITE_CREATE_TABLE:
1 / 0
return apsw.SQLITE_OK
self.db.authorizer = authorizer
self.assertRaises(ZeroDivisionError, c.execute, "create table shouldfail(x)")
self.assertTableNotExists("shouldfail")
# bad return type in callback
def authorizer(operation, *args):
return "a silly string"
self.db.set_authorizer(authorizer)
self.assertRaises(TypeError, c.execute, "create table shouldfail(x); select 3+5")
self.db.authorizer = None # otherwise next line will fail!
self.assertTableNotExists("shouldfail")
# back to normal
self.db.authorizer = None
c.execute("create table shouldsucceed(x)")
self.assertTableExists("shouldsucceed")
def testExecTracing(self):
"Verify tracing of executed statements and bindings"
self.db.set_exec_trace(None)
self.assertIsNone(self.db.exec_trace)
self.db.exec_trace = None
self.assertIsNone(self.db.exec_trace)
c = self.db.cursor()
cmds = [] # this is maniulated in tracefunc
def tracefunc(cursor, cmd, bindings):
cmds.append((cmd, bindings))
return True
c.execute("create table one(x,y,z)")
self.assertEqual(len(cmds), 0)
self.assertRaises(TypeError, c.set_exec_trace, 12) # must be callable
self.assertRaises(TypeError, self.db.set_exec_trace, 12) # must be callable
c.set_exec_trace(tracefunc)
self.assertIs(c.exec_trace, tracefunc)
statements = [
("insert into one values(?,?,?)", (1, 2, 3)),
("insert into one values(:a,$b,$c)", {
'a': 1,
'b': "string",
'c': None
}),
]
for cmd, values in statements:
c.execute(cmd, values)
self.assertEqual(cmds, statements)
self.assertTrue(c.get_exec_trace() is tracefunc)
c.exec_trace = None
self.assertTrue(c.get_exec_trace() is None)
c.execute("create table bar(x,y,z)")
# cmds should be unchanged
self.assertEqual(cmds, statements)
# tracefunc can abort execution
count = curnext(c.execute("select count(*) from one"))[0]
def tracefunc(cursor, cmd, bindings):
return False # abort
c.set_exec_trace(tracefunc)
self.assertRaises(apsw.ExecTraceAbort, c.execute, "insert into one values(1,2,3)")
# table should not have been modified
c.set_exec_trace(None)
self.assertEqual(count, curnext(c.execute("select count(*) from one"))[0])
# error in tracefunc
def tracefunc(cursor, cmd, bindings):
1 / 0
c.set_exec_trace(tracefunc)
self.assertRaises(ZeroDivisionError, c.execute, "insert into one values(1,2,3)")
c.set_exec_trace(None)
self.assertEqual(count, curnext(c.execute("select count(*) from one"))[0])
# test across executemany and multiple statements
counter = [0]
def tracefunc(cursor, cmd, bindings):
counter[0] = counter[0] + 1
return True
c.set_exec_trace(tracefunc)
c.execute(
"create table two(x);insert into two values(1); insert into two values(2); insert into two values(?); insert into two values(?)",
(3, 4))
self.assertEqual(counter[0], 5)
counter[0] = 0
c.executemany("insert into two values(?); insert into two values(?)", [[n, n + 1] for n in range(5)])
self.assertEqual(counter[0], 10)
# error in func but only after a while
c.execute("delete from two")
counter[0] = 0
def tracefunc(cursor, cmd, bindings):
counter[0] = counter[0] + 1
if counter[0] > 3:
1 / 0
return True
c.set_exec_trace(tracefunc)
self.assertRaises(
ZeroDivisionError, c.execute,
"insert into two values(1); insert into two values(2); insert into two values(?); insert into two values(?)",
(3, 4))
self.assertEqual(counter[0], 4)
c.set_exec_trace(None)
# check the first statements got executed
self.assertEqual(3, curnext(c.execute("select max(x) from two"))[0])
# executemany
def tracefunc(cursor, cmd, bindings):
1 / 0
c.set_exec_trace(tracefunc)
self.assertRaises(ZeroDivisionError, c.executemany, "select ?", [(1, )])
c.set_exec_trace(None)
# tracefunc with wrong number of arguments
def tracefunc(a, b, c, d, e, f):
1 / 0
c.set_exec_trace(tracefunc)
self.assertRaises(TypeError, c.execute, "select max(x) from two")
def tracefunc(*args):
return BadIsTrue()
c.set_exec_trace(tracefunc)
self.assertRaises(ZeroDivisionError, c.execute, "select max(x) from two")
# connection based tracing
self.assertEqual(self.db.get_exec_trace(), None)
traced = [False, False]
def contrace(*args):
traced[0] = True
return True
def curtrace(*args):
traced[1] = True
return True
c.set_exec_trace(curtrace)
c.execute("select 3")
self.assertEqual(traced, [False, True])
traced = [False, False]
self.db.set_exec_trace(contrace)
c.execute("select 3")
self.assertEqual(traced, [False, True])
traced = [False, False]
c.set_exec_trace(None)
c.execute("select 3")
self.assertEqual(traced, [True, False])
traced = [False, False]
self.db.cursor().execute("select 3")
self.assertEqual(traced, [True, False])
self.assertEqual(self.db.get_exec_trace(), contrace)
self.assertEqual(c.get_exec_trace(), None)
self.assertEqual(self.db.cursor().get_exec_trace(), None)
c.set_exec_trace(curtrace)
self.assertEqual(c.get_exec_trace(), curtrace)
def testRowTracing(self):
"Verify row tracing"
self.db.set_row_trace(None)
c = self.db.cursor()
c.execute("create table foo(x,y,z)")
vals = (1, 2, 3)
c.execute("insert into foo values(?,?,?)", vals)
def tracefunc(cursor, row):
return tuple([7 for i in row])
# should get original row back
self.assertEqual(curnext(c.execute("select * from foo")), vals)
self.assertRaises(TypeError, c.set_row_trace, 12) # must be callable
c.set_row_trace(tracefunc)
self.assertTrue(c.get_row_trace() is tracefunc)
# all values replaced with 7
self.assertEqual(curnext(c.execute("select * from foo")), tuple([7] * len(vals)))
def tracefunc(cursor, row):
return (7, )
# a single 7
c.set_row_trace(tracefunc)
self.assertEqual(curnext(c.execute("select * from foo")), (7, ))
# no alteration again
c.set_row_trace(None)
self.assertEqual(curnext(c.execute("select * from foo")), vals)
# error in function
def tracefunc(*result):
1 / 0
c.set_row_trace(tracefunc)
try:
for row in c.execute("select * from foo"):
self.fail("Should have had exception")
break
except ZeroDivisionError:
pass
c.set_row_trace(None)
self.assertEqual(curnext(c.execute("select * from foo")), vals)
# returning null
c.execute("create table bar(x)")
c.executemany("insert into bar values(?)", [[x] for x in range(10)])
counter = [0]
def tracefunc(cursor, args):
counter[0] = counter[0] + 1
if counter[0] % 2:
return None
return args
c.set_row_trace(tracefunc)
countertoo = 0
for row in c.execute("select * from bar"):
countertoo += 1
c.set_row_trace(None)
self.assertEqual(countertoo, 5) # half the rows should be skipped
# connection based
self.assertRaises(TypeError, self.db.set_row_trace, 12)
self.assertEqual(self.db.get_row_trace(), None)
traced = [False, False]
def contrace(cursor, row):
traced[0] = True
return row
def curtrace(cursor, row):
traced[1] = True
return row
for row in c.execute("select 3,3"):
pass
self.assertEqual(traced, [False, False])
traced = [False, False]
self.db.set_row_trace(contrace)
for row in self.db.cursor().execute("select 3,3"):
pass
self.assertEqual(traced, [True, False])
traced = [False, False]
c.set_row_trace(curtrace)
for row in c.execute("select 3,3"):
pass
self.assertEqual(traced, [False, True])
traced = [False, False]
c.set_row_trace(None)
for row in c.execute("select 3"):
pass
self.assertEqual(traced, [True, False])
self.assertEqual(self.db.get_row_trace(), contrace)
def testScalarFunctions(self):
"Verify scalar functions"
c = self.db.cursor()
def ilove7(*args):
return 7
self.assertRaises(TypeError, self.db.create_scalar_function, "twelve", 12) # must be callable
self.assertRaises(TypeError, self.db.create_scalar_function, "twelve", 12, 27, 28) # too many params
try:
self.db.create_scalar_function("twelve", ilove7, 900) # too many args
except (apsw.SQLError, apsw.MisuseError):
# misuse can be returned for too many args
pass
# some unicode fun
self.db.create_scalar_function, "twelve\N{BLACK STAR}", ilove7
try:
# SQLite happily registers the function, but you can't
# call it
self.assertEqual(c.execute("select " + "twelve\N{BLACK STAR}" + "(3)").fetchall(), [[7]])
except apsw.SQLError:
pass
self.db.create_scalar_function("seven", ilove7)
c.execute("create table foo(x,y,z)")
for i in range(10):
c.execute("insert into foo values(?,?,?)", (i, i, i))
for i in range(10):
self.assertEqual((7, ), curnext(c.execute("select seven(x,y,z) from foo where x=?", (i, ))))
# clear func
self.assertRaises(apsw.BusyError, self.db.create_scalar_function, "seven",
None) # active select above so no funcs can be changed
for row in c.execute("select null"):
pass # no active sql now
self.db.create_scalar_function("seven", None)
# function names are limited to 255 characters - SQLerror is the rather unintuitive error return
try:
self.db.create_scalar_function("a" * 300, ilove7)
except (apsw.SQLError, apsw.MisuseError):
pass # see sqlite ticket #3875
# have an error in a function
def badfunc(*args):
return 1 / 0
self.db.create_scalar_function("badscalarfunc", badfunc)
self.assertRaises(ZeroDivisionError, c.execute, "select badscalarfunc(*) from foo")
# return non-allowed types
for v in ({'a': 'dict'}, ['a', 'list'], self):
def badtype(*args):
return v
self.db.create_scalar_function("badtype", badtype)
self.assertRaises(TypeError, c.execute, "select badtype(*) from foo")
# return non-unicode string
def ilove8bit(*args):
return "\x99\xaa\xbb\xcc"
self.db.create_scalar_function("ilove8bit", ilove8bit)
# coverage
def bad(*args):
1 / 0
self.db.create_scalar_function("bad", bad)
self.assertRaises(ZeroDivisionError, c.execute, "select bad(3)+bad(4)")
# turn a blob into a string to fail python utf8 conversion
self.assertRaises(UnicodeDecodeError, c.execute, "select bad(cast (x'fffffcfb9208' as TEXT))")
# register same named function taking different number of arguments
for i in range(-1, 4):
self.db.create_scalar_function("multi", lambda *args: len(args), i)
gc.collect()
for row in c.execute("select multi(), multi(1), multi(1,2), multi(1,2,3), multi(1,2,3,4), multi(1,2,3,4,5)"):
self.assertEqual(row, (0, 1, 2, 3, 4, 5))
# deterministic flag
# check error handling
self.assertRaises(TypeError, self.db.create_scalar_function, "twelve", deterministic="324")
self.assertRaises(TypeError, self.db.create_scalar_function, "twelve", deterministic=324)
# check it has an effect
class Counter: # on calling returns how many times this instance has been called
num_calls = 0
def __call__(self):
self.num_calls += 1
return self.num_calls
self.db.create_scalar_function("deterministic", Counter(), deterministic=True)
self.db.create_scalar_function("nondeterministic", Counter(), deterministic=False)
self.db.create_scalar_function("unspecdeterministic", Counter())
# only deterministic can be used for indices
c.execute("create table td(a,b); create index tda on td(a) where deterministic()")
self.assertEqual(c.execute("select nondeterministic()=nondeterministic()").fetchall()[0][0], 0)
self.assertEqual(c.execute("select unspecdeterministic()=unspecdeterministic()").fetchall()[0][0], 0)
self.assertRaises(apsw.SQLError, c.execute, "create index tdb on td(b) where nondeterministic()")
def testAggregateFunctions(self):
"Verify aggregate functions"
c = self.db
c.execute("create table foo(x,y,z)")
# aggregate function
class longest:
def __init__(self):
self.result = ""
def step(self, context, *args):
for i in args:
if len(str(i)) > len(self.result):
self.result = str(i)
def final(self, context):
return self.result
def factory():
v = longest()
return None, v.step, v.final
factory = staticmethod(factory)
self.assertRaises(TypeError, self.db.create_aggregate_function, True, True, True,
True) # wrong number/type of params
self.assertRaises(TypeError, self.db.create_aggregate_function, "twelve", 12) # must be callable
if "DEBUG" not in apsw.compile_options:
# these cause assertion failures in sqlite
try:
self.db.create_aggregate_function("twelve", longest.factory, 923) # max args is 127
except (apsw.SQLError, apsw.MisuseError):
# misuse is returned for too many args
pass
self.db.create_aggregate_function("twelve", None)
self.assertRaises(TypeError, self.db.create_aggregate_function, "twelve\N{BLACK STAR}", 12) # must be ascii
self.db.create_aggregate_function("longest", longest.factory)
vals = (
("kjfhgk", "gkjlfdhgjkhsdfkjg",
"gklsdfjgkldfjhnbnvc,mnxb,mnxcv,mbncv,mnbm,ncvx,mbncv,mxnbcv,"), # last one is deliberately the longest
("gdfklhj", ":gjkhgfdsgfd", "gjkfhgjkhdfkjh"),
("gdfjkhg", "gkjlfd", ""),
(1, 2, 30),
)
for v in vals:
c.execute("insert into foo values(?,?,?)", v)
v = curnext(c.execute("select longest(x,y,z) from foo"))[0]
self.assertEqual(v, vals[0][2])
# SQLite doesn't allow step functions to return an error, so we have to defer to the final
def badfactory():
def badfunc(*args):
1 / 0
def final(*args):
self.fail("This should not be executed")
return 1
return None, badfunc, final
self.db.create_aggregate_function("badfunc", badfactory)
self.assertRaises(ZeroDivisionError, c.execute, "select badfunc(x) from foo")
# error in final
def badfactory():
def badfunc(*args):
pass
def final(*args):
1 / 0
return None, badfunc, final
self.db.create_aggregate_function("badfunc", badfactory)
self.assertRaises(ZeroDivisionError, c.execute, "select badfunc(x) from foo")
# error in step and final
def badfactory():
def badfunc(*args):
1 / 0
def final(*args):
raise ImportError() # zero div from above is what should be returned
return None, badfunc, final
self.db.create_aggregate_function("badfunc2", badfactory)
self.assertRaises(ZeroDivisionError, c.execute, "select badfunc2(x) from foo")
# bad return from factory
def badfactory():
def badfunc(*args):
pass
def final(*args):
return 0
return {}
self.db.create_aggregate_function("badfunc", badfactory)
self.assertRaises(AttributeError, c.execute, "select badfunc(x) from foo")
# incorrect number of items returned
def badfactory():
def badfunc(*args):
pass
def final(*args):
return 0
return (None, badfunc, final, badfactory)
self.db.create_aggregate_function("badfunc3", badfactory)
self.assertRaises(TypeError, c.execute, "select badfunc3(x) from foo")
# step not callable
def badfactory():
def badfunc(*args):
pass
def final(*args):
return 0
return (None, True, final)
self.db.create_aggregate_function("badfunc4", badfactory)
self.assertRaises(TypeError, c.execute, "select badfunc4(x) from foo")
# final not callable
def badfactory():
def badfunc(*args):
pass
def final(*args):
return 0
return (None, badfunc, True)
self.db.create_aggregate_function("badfunc5", badfactory)
self.assertRaises(TypeError, c.execute, "select badfunc5(x) from foo")
# error in factory method
def badfactory():
1 / 0
self.db.create_aggregate_function("badfunc6", badfactory)
self.assertRaises(ZeroDivisionError, c.execute, "select badfunc6(x) from foo")
# class based aggregate
class summer:
def __init__(self):
self.sum = 0
def step(self, v):
self.sum += v
def final(self):
return self.sum
c.execute("create table xyz(val)")
c.executemany("insert into xyz values(?)", ((i, ) for i in range(20)))
self.db.create_aggregate_function("summer", summer, -1)
self.assertEqual(sum(range(20)), c.execute("select summer(val) from xyz").get)
del summer.final
self.assertRaises(AttributeError, c.execute, "select summer(val) from xyz")
del summer.step
self.assertRaises(AttributeError, c.execute, "select summer(val) from xyz")
summer.step = summer.final = 3
self.db.create_aggregate_function("summer1", summer, -1)
self.assertRaisesRegex(TypeError, "step function must be callable", c.execute, "select summer1(val) from xyz")
summer.step = lambda v: 0
self.db.create_aggregate_function("summer2", summer, -1)
self.assertRaisesRegex(TypeError, "final function must be callable", c.execute, "select summer2(val) from xyz")
def testWindowFunctions(self):
"Verify window functions"
# check the sqlite example works
class windowfunc:
def __init__(self):
self.v = 0
def step(self, arg):
self.v += arg
def inverse(self, arg):
self.v -= arg
def final(self):
return self.v
def value(self):
return self.v
self.assertRaises(TypeError, self.db.create_window_function, 3, 3)
self.db.create_window_function("sumint", None)
self.db.create_window_function("sumint", windowfunc)
self.db.execute("""CREATE TABLE t3(x, y);
INSERT INTO t3 VALUES('a', 4),('b', 5),('c', 3),('d', 8),('e', 1);""")
query = """SELECT x, sumint(y) OVER (ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sum_y
FROM t3 ORDER BY x;"""
expected = [('a', 9), ('b', 12), ('c', 16), ('d', 12), ('e', 9)]
self.assertEqual(self.db.execute(query).fetchall(), expected)
self.db.create_window_function("sumint", None)
try:
self.db.execute(query)
1 / 0 # should not be reached
except apsw.SQLError as e:
self.assertIn("no such function: sumint", str(e))
def factory():
return windowfunc(), windowfunc.step, windowfunc.value, windowfunc.final, windowfunc.inverse
self.db.create_window_function("sumint", factory)
self.assertEqual(self.db.execute(query).fetchall(), expected)
# now all the errors
for factory, exc in (
(lambda: 3, AttributeError),
(lambda x: 3, TypeError),
(lambda: 1 / 0, ZeroDivisionError),
(lambda: [
object,
] + [99] * 3, TypeError),
(lambda: [
object,
] + [99] * 5, TypeError),
):
self.db.create_window_function("sumint", factory)
self.assertRaises(exc, self.db.execute, query)
args = [lambda: 3] * 4
names = "step", "final", "value", "inverse"
for can_cache in (False, True):
for counter, n in enumerate(names):
self.db.create_window_function("sumint", None)
a = args[:]
a[counter] = "a string"
self.db.create_window_function("sumint", lambda: [object] + a)
try:
self.db.execute(query)
1 / 0
except TypeError as e:
self.assertIn(n, str(e))
setattr(windowfunc, n + "orig", getattr(windowfunc, n))
setattr(windowfunc, n, lambda *args: 1 / 0)
self.db.create_window_function("sumint", windowfunc)
try:
self.db.execute(query, can_cache=can_cache).fetchall()
raise Exception("should not be reached")
except ZeroDivisionError:
pass
setattr(windowfunc, n, getattr(windowfunc, n + "orig"))
# cause final to run while the statement is being closed
# https://sqlite.org/forum/forumpost/d72cba6ff7
class windowfunc2:
def step(*args):
return 3
inverse = value = step
def final(*args):
1 / 0
self.db.create_window_function("sumint", windowfunc2)
with self.assertRaises(ZeroDivisionError):
self.db.execute(query, can_cache=False).close()
with self.assertRaises(ZeroDivisionError):
self.db.execute(query, can_cache=True).close()
self.db.close()
def testFastcall(self):
"fastcall argument processing"
# function that takes one argument
self.assertRaisesRegex(TypeError, "Missing required parameter", apsw.sleep)
self.assertRaisesRegex(TypeError, "'fred' is an invalid keyword argument", apsw.sleep, fred=3)
self.assertRaisesRegex(TypeError,
"argument 'milliseconds' given by name and position",
apsw.sleep,
10,
milliseconds=20)
self.assertRaisesRegex(TypeError, "Too many positional arguments", apsw.sleep, 10, 20)
# many args where varargs had to be converted to fastcall
self.assertRaisesRegex(TypeError, "Missing required parameter", apsw.Connection)
apsw.Connection("")
apsw.Connection("", vfs=None)
self.assertRaisesRegex(apsw.SQLError, "no such vfs", apsw.Connection, "", vfs="fred")
c = apsw.Connection(statementcachesize=22, flags=apsw.SQLITE_OPEN_READONLY, vfs=None, filename="")
self.assertEqual(22, c.cache_stats()["size"])
self.assertRaisesRegex(TypeError,
"Missing required parameter",
apsw.Connection,
statementcachesize=22,
flags=apsw.SQLITE_OPEN_READONLY,
vfs=None)
# keyword only args
self.assertRaisesRegex(TypeError, "Too many positional arguments", c.execute, "select 3", None, False)
self.assertEqual(3,
c.execute(explain=0, prepare_flags=1, can_cache=1, bindings=None, statements="select 3").get)
self.assertRaisesRegex(TypeError,
"argument 'statements' given by name and position",
c.execute,
"select 4",
explain=0,
prepare_flags=1,
can_cache=1,
bindings=None,
statements="select 3")
def testCollation(self):
"Verify collations"
# create a whole bunch to check they are freed
for i in range(1024):
self.db.create_collation("x" * i, lambda x, y: i)
for ii in range(1024):
self.db.create_collation("x" * ii, lambda x, y: ii)
c = self.db.cursor()
def strnumcollate(s1, s2):
"return -1 if s1<s2, +1 if s1>s2 else 0. Items are string head and numeric tail"
# split values into two parts - the head and the numeric tail
values = [s1, s2]
for vn, v in enumerate(values):
for i in range(len(v), 0, -1):
if v[i - 1] not in "01234567890":
break
try:
v = v[:i], int(v[i:])
except ValueError:
v = v[:i], None
values[vn] = v
# compare
if values[0] < values[1]:
return -1 # return an int
if values[0] > values[1]:
return 1 # and a long
return 0
self.assertRaises(TypeError, self.db.create_collation, "twelve", strnumcollate, 12) # wrong # params
self.assertRaises(TypeError, self.db.create_collation, "twelve", 12) # must be callable
self.db.create_collation("strnum", strnumcollate)
c.execute("create table foo(x)")
# adding this unicode in front improves coverage
uni = "\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}"
vals = (uni + "file1", uni + "file7", uni + "file9", uni + "file17", uni + "file20")
valsrev = list(vals)
valsrev.reverse() # put them into table in reverse order
valsrev = valsrev[1:] + valsrev[:1] # except one out of order
c.executemany("insert into foo values(?)", [(x, ) for x in valsrev])
for i, row in enumerate(c.execute("select x from foo order by x collate strnum")):
self.assertEqual(vals[i], row[0])
# collation function with an error
def collerror(*args):
return 1 / 0
self.db.create_collation("collerror", collerror)
self.assertRaises(ZeroDivisionError, c.execute, "select x from foo order by x collate collerror")
# collation function that returns bad value
def collerror(*args):
return {}
self.db.create_collation("collbadtype", collerror)
self.assertRaises(TypeError, c.execute, "select x from foo order by x collate collbadtype")
# get error when registering
c.execute("select x from foo order by x collate strnum") # nb we don't read so cursor is still active
self.assertRaises(apsw.BusyError, self.db.create_collation, "strnum", strnumcollate)
# unregister
for row in c:
pass
self.db.create_collation("strnum", None)
# check it really has gone
try:
c.execute("select x from foo order by x collate strnum")
except apsw.SQLError:
pass
# check statement still works
for _ in c.execute("select x from foo"):
pass
# collation needed testing
self.assertRaises(TypeError, self.db.collation_needed, 12)
def cn1():
pass
def cn2(x, y):
1 / 0
def cn3(x, y):
self.assertTrue(x is self.db)
self.assertEqual(y, "strnum")
self.db.create_collation("strnum", strnumcollate)
self.db.collation_needed(cn1)
try:
for _ in c.execute("select x from foo order by x collate strnum"):
pass
except TypeError:
pass
self.db.collation_needed(cn2)
try:
for _ in c.execute("select x from foo order by x collate strnum"):
pass
except ZeroDivisionError:
pass
self.db.collation_needed(cn3)
for _ in c.execute("select x from foo order by x collate strnum"):
pass
self.db.collation_needed(None)
self.db.create_collation("strnum", None)
# check it really has gone
try:
c.execute("select x from foo order by x collate strnum")
except apsw.SQLError:
pass
def testProgressHandler(self):
"Verify progress handler"
c = self.db.cursor()
phcalledcount = [0]
def ph():
phcalledcount[0] = phcalledcount[0] + 1
return 0
# make 400 rows of random numbers
c.execute("begin ; create table foo(x)")
c.executemany("insert into foo values(?)", randomintegers(400))
c.execute("commit")
self.assertRaises(TypeError, self.db.set_progress_handler, 12) # must be callable
self.assertRaises(TypeError, self.db.set_progress_handler, ph, "foo") # second param is steps
self.db.set_progress_handler(ph, -17) # SQLite doesn't complain about negative numbers
self.db.set_progress_handler(ph, 20)
curnext(c.execute("select max(x) from foo"))
self.assertNotEqual(phcalledcount[0], 0)
saved = phcalledcount[0]
# put an error in the progress handler
def ph():
return 1 / 0
self.db.set_progress_handler(ph, 1)
self.assertRaises(ZeroDivisionError, c.execute, "update foo set x=-10")
self.db.set_progress_handler(None) # clear ph so next line runs
# none should have taken
self.assertEqual(0, curnext(c.execute("select count(*) from foo where x=-10"))[0])
# and previous ph should not have been called
self.assertEqual(saved, phcalledcount[0])
def ph():
return BadIsTrue()
self.db.set_progress_handler(ph, 1)
self.assertRaises(ZeroDivisionError, c.execute, "update foo set x=-10")
def testChanges(self):
"Verify reporting of changes"
c = self.db.cursor()
c.execute("create table foo (x);begin")
for i in range(100):
c.execute("insert into foo values(?)", (i + 1000, ))
c.execute("commit")
c.execute("update foo set x=0 where x>=1000")
self.assertEqual(100, self.db.changes())
c.execute("begin")
for i in range(100):
c.execute("insert into foo values(?)", (i + 1000, ))
# for coverage
self.db.cache_flush()
getattr(self.db, "release_" "memory")() # avoids old name testing false positive
c.execute("commit")
self.assertEqual(300, self.db.total_changes())
if hasattr(apsw, "faultdict"):
# check 64 bit conversion works
apsw.faultdict["ConnectionChanges64"] = True
self.assertEqual(1000000000 * 7 * 3, self.db.changes())
def testLastInsertRowId(self):
"Check last insert row id"
c = self.db.cursor()
c.execute("create table foo (x integer primary key)")
for i in range(10):
c.execute("insert into foo values(?)", (i, ))
self.assertEqual(i, self.db.last_insert_rowid())
# get a 64 bit value
v = 2**40
c.execute("insert into foo values(?)", (v, ))
self.assertEqual(v, self.db.last_insert_rowid())
# try setting it
self.assertRaises(
TypeError,
self.db.set_last_insert_rowid,
)
self.assertRaises(TypeError, self.db.set_last_insert_rowid, "3")
self.assertRaises(TypeError, self.db.set_last_insert_rowid, "3", 3)
self.assertRaises(OverflowError, self.db.set_last_insert_rowid, 2**40 * 2**40)
for v in -20, 0, 20, 2**32 - 1, -2**32 - 1, 2**60, -2**60:
c.execute("insert into foo values(?)", (v - 3, ))
self.assertNotEqual(v, self.db.last_insert_rowid())
self.db.set_last_insert_rowid(v)
self.assertEqual(v, self.db.last_insert_rowid())
def testComplete(self):
"Completeness of SQL statement checking"
# the actual underlying routine just checks that there is a semi-colon
# at the end, not inside any quotes etc
self.assertEqual(False, apsw.complete("select * from"))
self.assertEqual(False, apsw.complete("select * from \";\""))
self.assertEqual(False, apsw.complete("select * from \";"))
self.assertEqual(True, apsw.complete("select * from foo; select *;"))
self.assertEqual(False, apsw.complete("select * from foo where x=1"))
self.assertEqual(True, apsw.complete("select * from foo;"))
self.assertEqual(True, apsw.complete("select '\u9494\ua7a7';"))
self.assertRaises(TypeError, apsw.complete, 12) # wrong type
self.assertRaises(TypeError, apsw.complete) # not enough args
self.assertRaises(TypeError, apsw.complete, "foo", "bar") # too many args
def testBusyHandling(self):
"Verify busy handling"
c = self.db.cursor()
c.execute("create table foo(x); begin")
c.executemany("insert into foo values(?)", randomintegers(400))
c.execute("commit")
# verify it is blocked
db2 = apsw.Connection(TESTFILEPREFIX + "testdb")
c2 = db2.cursor()
c2.execute("begin exclusive")
try:
self.assertRaises(apsw.BusyError, c.execute, "begin immediate ; select * from foo")
finally:
del c2
db2.close()
del db2
# close and reopen databases - sqlite will return Busy immediately to a connection
# it previously returned busy to
del c
self.db.close()
del self.db
self.db = apsw.Connection(TESTFILEPREFIX + "testdb")
db2 = apsw.Connection(TESTFILEPREFIX + "testdb")
c = self.db.cursor()
c2 = db2.cursor()
# Put in busy handler
bhcalled = [0]
def bh(*args):
bhcalled[0] = bhcalled[0] + 1
if bhcalled[0] == 4:
return False
return True
self.assertRaises(TypeError, db2.set_busy_handler, 12) # must be callable
self.assertRaises(TypeError, db2.set_busy_timeout, "12") # must be int
db2.set_busy_timeout(
-77) # SQLite doesn't complain about negative numbers, but if it ever does this will catch it
self.assertRaises(TypeError, db2.set_busy_timeout, 77, 88) # too many args
self.db.set_busy_handler(bh)
c2.execute("begin exclusive")
try:
for row in c.execute("begin immediate ; select * from foo"):
self.fail("Transaction wasn't exclusive")
except apsw.BusyError:
pass
self.assertEqual(bhcalled[0], 4)
# Close and reopen again
del c
del c2
db2.close()
self.db.close()
del db2
del self.db
self.db = apsw.Connection(TESTFILEPREFIX + "testdb")
db2 = apsw.Connection(TESTFILEPREFIX + "testdb")
c = self.db.cursor()
c2 = db2.cursor()
# Put in busy timeout
TIMEOUT = 3 # seconds, must be integer as sqlite can round down to nearest second anyway
c2.execute("begin exclusive")
self.assertRaises(TypeError, self.db.set_busy_handler, "foo")
self.db.set_busy_timeout(int(TIMEOUT * 1000))
b4 = time.time()
try:
c.execute("begin immediate ; select * from foo")
except apsw.BusyError:
pass
after = time.time()
took = after - b4
# this sometimes fails in virtualized environments due to time
# going backwards or not going forwards consistently.
if took + 1 < TIMEOUT:
print(f"Timeout was { TIMEOUT } seconds but only { took } seconds elapsed!")
self.assertTrue(took >= TIMEOUT)
# check clearing of handler
c2.execute("rollback")
self.db.set_busy_handler(None)
b4 = time.time()
c2.execute("begin exclusive")
try:
c.execute("begin immediate ; select * from foo")
except apsw.BusyError:
pass
after = time.time()
self.assertTrue(after - b4 < TIMEOUT)
# Close and reopen again
del c
del c2
db2.close()
self.db.close()
del db2
del self.db
self.db = apsw.Connection(TESTFILEPREFIX + "testdb")
db2 = apsw.Connection(TESTFILEPREFIX + "testdb")
c = self.db.cursor()
c2 = db2.cursor()
# error in busyhandler
def bh(*args):
1 / 0
c2.execute("begin exclusive")
self.db.set_busy_handler(bh)
self.assertRaises(ZeroDivisionError, c.execute, "begin immediate ; select * from foo")
del c
del c2
db2.close()
def bh(*args):
return BadIsTrue()
db2 = apsw.Connection(TESTFILEPREFIX + "testdb")
c = self.db.cursor()
c2 = db2.cursor()
c2.execute("begin exclusive")
self.db.set_busy_handler(bh)
self.assertRaises(ZeroDivisionError, c.execute, "begin immediate ; select * from foo")
del c
del c2
db2.close()
def testBusyHandling2(self):
"Another busy handling test"
# Based on an issue in 3.3.10 and before
con2 = apsw.Connection(TESTFILEPREFIX + "testdb")
cur = self.db.cursor()
cur2 = con2.cursor()
cur.execute("create table test(x,y)")
cur.execute("begin")
cur.execute("insert into test values(123,'abc')")
self.assertRaises(apsw.BusyError, cur2.execute, "insert into test values(456, 'def')")
cur.execute("commit")
self.assertEqual(1, curnext(cur2.execute("select count(*) from test where x=123"))[0])
con2.close()
def testInterruptHandling(self):
"Verify interrupt function"
# this is tested by having a user defined function make the interrupt
self.assertFalse(self.db.is_interrupted)
c = self.db.cursor()
c.execute("create table foo(x);begin")
c.executemany("insert into foo values(?)", randomintegers(400))
c.execute("commit")
def ih(*args):
self.db.interrupt()
self.assertTrue(self.db.is_interrupted)
return 7
self.db.create_scalar_function("seven", ih)
try:
for row in c.execute("select seven(x) from foo"):
pass
except apsw.InterruptError:
pass
self.db.close(True)
def testCommitHook(self):
"Verify commit hooks"
c = self.db.cursor()
c.execute("create table foo(x)")
c.executemany("insert into foo values(?)", randomintegers(10))
chcalled = [0]
def ch():
chcalled[0] = chcalled[0] + 1
if chcalled[0] == 4:
return 1 # abort
return 0 # continue
self.assertRaises(TypeError, self.db.set_commit_hook, 12) # not callable
self.db.set_commit_hook(ch)
self.assertRaises(apsw.ConstraintError, c.executemany, "insert into foo values(?)", randomintegers(10))
self.assertEqual(4, chcalled[0])
self.db.set_commit_hook(None)
def ch():
chcalled[0] = 99
return 1
self.db.set_commit_hook(ch)
self.assertRaises(apsw.ConstraintError, c.executemany, "insert into foo values(?)", randomintegers(10))
# verify it was the second one that was called
self.assertEqual(99, chcalled[0])
# error in commit hook
def ch():
return 1 / 0
self.db.set_commit_hook(ch)
self.assertRaises(ZeroDivisionError, c.execute, "insert into foo values(?)", (1, ))
def ch():
return BadIsTrue()
self.db.set_commit_hook(ch)
self.assertRaises(ZeroDivisionError, c.execute, "insert into foo values(?)", (1, ))
def testRollbackHook(self):
"Verify rollback hooks"
c = self.db.cursor()
c.execute("create table foo(x)")
rhcalled = [0]
def rh():
rhcalled[0] = rhcalled[0] + 1
return 1
self.assertRaises(TypeError, self.db.set_rollback_hook, 12) # must be callable
self.db.set_rollback_hook(rh)
c.execute("begin ; insert into foo values(10); rollback")
self.assertEqual(1, rhcalled[0])
self.db.set_rollback_hook(None)
c.execute("begin ; insert into foo values(10); rollback")
self.assertEqual(1, rhcalled[0])
def rh():
1 / 0
self.db.set_rollback_hook(rh)
# SQLite doesn't allow reporting an error from a rollback hook, so it will be seen
# in the next command (eg the select in this case)
self.assertRaises(ZeroDivisionError, c.execute,
"begin ; insert into foo values(10); rollback; select * from foo")
# check cursor still works
for row in c.execute("select * from foo"):
pass
def testUpdateHook(self):
"Verify update hooks"
c = self.db.cursor()
c.execute("create table foo(x integer primary key, y)")
uhcalled = []
def uh(type, databasename, tablename, rowid):
uhcalled.append((type, databasename, tablename, rowid))
self.assertRaises(TypeError, self.db.set_update_hook, 12) # must be callable
self.db.set_update_hook(uh)
statements = (
("insert into foo values(3,4)", (apsw.SQLITE_INSERT, 3)),
("insert into foo values(30,40)", (apsw.SQLITE_INSERT, 30)),
(
"update foo set y=47 where x=3",
(apsw.SQLITE_UPDATE, 3),
),
(
"delete from foo where y=47",
(apsw.SQLITE_DELETE, 3),
),
)
for sql, res in statements:
c.execute(sql)
results = [(type, "main", "foo", rowid) for sql, (type, rowid) in statements]
self.assertEqual(uhcalled, results)
self.db.set_update_hook(None)
c.execute("insert into foo values(99,99)")
self.assertEqual(len(uhcalled), len(statements)) # length should have remained the same
def uh(*args):
1 / 0
self.db.set_update_hook(uh)
self.assertRaises(ZeroDivisionError, c.execute, "insert into foo values(100,100)")
self.db.set_update_hook(None)
# improve code coverage
c.execute("create table bar(x,y); insert into bar values(1,2); insert into bar values(3,4)")
def uh(*args):
1 / 0
self.db.set_update_hook(uh)
self.assertRaises(ZeroDivisionError, c.execute, "insert into foo select * from bar")
self.db.set_update_hook(None)
# check cursor still works
c.execute("insert into foo values(1000,1000)")
self.assertEqual(1, curnext(c.execute("select count(*) from foo where x=1000"))[0])
def testProfile(self):
"Verify profiling"
# we do the test by looking for the maximum of PROFILESTEPS random
# numbers with an index present and without. The former
# should be way quicker.
c = self.db.cursor()
c.execute("create table foo(x); begin")
c.executemany("insert into foo values(?)", randomintegers(PROFILESTEPS))
profileinfo = []
def profile(statement, timing):
profileinfo.append((statement, timing))
c.execute("commit; create index foo_x on foo(x)")
self.assertRaises(TypeError, self.db.set_profile, 12) # must be callable
self.db.set_profile(profile)
for val1 in c.execute("select max(x) from foo"):
pass # profile is only run when results are exhausted
self.db.set_profile(None)
c.execute("drop index foo_x")
self.db.set_profile(profile)
for val2 in c.execute("select max(x) from foo"):
pass
self.assertEqual(val1, val2)
self.assertTrue(len(profileinfo) >= 2) # see SQLite ticket 2157
self.assertEqual(profileinfo[0][0], profileinfo[-1][0])
self.assertEqual("select max(x) from foo", profileinfo[0][0])
self.assertEqual("select max(x) from foo", profileinfo[-1][0])
# the query using the index should take way less time
self.assertTrue(profileinfo[0][1] <= profileinfo[-1][1])
def profile(*args):
1 / 0
self.db.set_profile(profile)
self.assertRaises(ZeroDivisionError, c.execute, "create table bar(y)")
# coverage
wasrun = [False]
def profile(*args):
wasrun[0] = True
def uh(*args):
1 / 0
self.db.set_profile(profile)
self.db.set_update_hook(uh)
self.assertRaises(ZeroDivisionError, c.execute, "insert into foo values(3)")
self.assertEqual(wasrun[0], False)
self.db.set_profile(None)
self.db.set_update_hook(None)
def testThreading(self):
"Verify threading behaviour"
# We used to require all operations on a connection happen in
# the same thread. Now they can happen in any thread, so we
# ensure that inuse errors are detected by doing a long
# running operation in one thread.
c = self.db.cursor()
c.execute("create table foo(x);begin;")
c.executemany("insert into foo values(?)", randomintegers(10000))
c.execute("commit")
vals = {"stop": False, "raised": False}
def wt():
try:
while not vals["stop"]:
c.execute("select min(max(x-1+x),min(x-1+x)) from foo")
except apsw.ThreadingViolationError:
vals["raised"] = True
vals["stop"] = True
t = ThreadRunner(wt)
t.start()
# ensure thread t has started
time.sleep(0.1)
b4 = time.time()
# try to get a threadingviolation for 30 seconds
try:
try:
while not vals["stop"] and time.time() - b4 < 30:
c.execute("select * from foo")
except apsw.ThreadingViolationError:
vals["stop"] = True
vals["raised"] = True
finally:
vals["stop"] = True
t.go()
self.assertEqual(vals["raised"], True)
def testStringsWithNulls(self):
"Verify that strings with nulls in them are handled correctly"
c = self.db.cursor()
c.execute("create table foo(row,str)")
vals = ("\0a simple string", "a simple string\0with a null", "a string\0with two\0nulls",
"or even a \0\0\0\0\0\0sequence\0\0\0\0of them", "a \u1234 unic\0de \ufe54 string \u0089",
"a \u1234 unicode \ufe54 string \u0089\0and some text",
"\N{BLACK STAR} \N{WHITE STAR} \N{LIGHTNING} \N{COMET}\0more\0than you\0can handle",
"\N{BLACK STAR} \N{WHITE STAR} \N{LIGHTNING} \N{COMET}\0\0\0\0\0sequences\0\0\0of them")
vals = vals + (
"a simple string\0",
"a \u1234 unicode \ufe54 string \u0089\0",
)
for i, v in enumerate(vals):
c.execute("insert into foo values(?,?)", (i, v))
# add function to test conversion back as well
def snap(*args):
return args[0]
self.db.create_scalar_function("snap", snap)
# now see what we got out
count = 0
for row, v, fv in c.execute("select row,str,snap(str) from foo"):
count += 1
self.assertEqual(vals[row], v)
self.assertEqual(vals[row], fv)
self.assertEqual(count, len(vals))
# check execute
for v in vals:
self.assertEqual(v, curnext(c.execute("select ?", (v, )))[0])
# nulls not allowed in main query string, so lets check the other bits (unicode etc)
v2 = v.replace("\0", " zero ")
self.assertEqual(v2, curnext(c.execute("select '%s'" % (v2, )))[0])
# collation
def colcmp(l, r):
self.assertTrue("\0" in l and "\0" in r)
if l < r: return -1
if l > r: return 1
return 0
self.db.create_collation("colcmp", colcmp)
self.db.execute("select str from foo order by str collate colcmp").get
def testSharedCache(self):
"Verify setting of shared cache"
# check parameters - wrong # or type of args
self.assertRaises(TypeError, apsw.enable_shared_cache)
self.assertRaises(TypeError, apsw.enable_shared_cache, "foo")
self.assertRaises(TypeError, apsw.enable_shared_cache, True, None)
# the setting can be changed at almost any time
apsw.enable_shared_cache(True)
apsw.enable_shared_cache(False)
def testSerialize(self):
"Verify serialize/deserialize calls"
# check param types
self.assertRaises(TypeError, self.db.serialize)
self.assertRaises(TypeError, self.db.serialize, "a", "b")
self.assertRaises(TypeError, self.db.serialize, 3)
self.assertRaises(TypeError, self.db.deserialize, 3)
self.assertRaises(TypeError, self.db.deserialize, "main", "main")
# SQLite implementation detail: empty temp db gives back None
self.assertEqual(None, self.db.serialize("temp"))
# SQLite implementation detail: unknown name gives back None instead of error
self.assertEqual(None, self.db.serialize("nosuchdbname"))
# populate with real content
self.db.cursor().execute("create table temp.foo(x); insert into temp.foo values(3), (4), (5)")
# must have content now
self.assertNotEqual(None, self.db.serialize("temp"))
self.assertTableNotExists("main.foo")
self.db.deserialize("main", self.db.serialize("temp"))
# without this renaming, things get confused between identical tables in main and temp
self.db.cursor().execute("alter table main.foo rename to bar")
self.assertTablesEqual(self.db, "bar", self.db, "foo")
# check we can modify deserialized
self.db.cursor().execute("insert into bar values(3)")
self.db.deserialize("main", self.db.serialize("temp"))
self.db.cursor().execute("alter table temp.foo rename to bar")
self.assertTablesEqual(self.db, "foo", self.db, "bar")
# add a megabyte to table
self.db.cursor().execute("insert into foo values(zeroblob(1024024))")
# A check that various extensions (such as fts3, rtree, icu)
# actually work. We don't know if they were supposed to be
# compiled in or not so the assumption is that they aren't.
# However setup.py is being run then it sets environment variables
# saying the extensions *must* be present if they were enabled.
# See https://github.com/rogerbinns/apsw/issues/55 for what
# led to this.
def checkOptionalExtension(self, name, testquery):
try:
present = False
apsw.Connection(":memory:").cursor().execute(testquery)
present = True
except apsw.Error:
pass
if "APSW_TEST_" + name.upper() in os.environ:
self.assertEqual(present, True)
return present
def testFTSExtension(self):
"Check FTS extensions (if present)"
for v in 3, 4, 5:
self.checkFTSExtension(v)
def checkFTSExtension(self, v):
self.db.cursor().execute("drop table if exists foo; drop table if exists test")
if not self.checkOptionalExtension("fts" + str(v), "create virtual table foo using fts%d()" % v):
return
c = self.db.cursor()
data = {
'cake': 'flour, eggs, milk',
'bbq ribs': 'ribs, hot sauce',
'mayo': 'oil, Eggs',
'glue': 'Egg',
'salmon': 'Fish',
'burger': 'Mechanically recovered meat',
'broccoli stew': 'broccoli peppers cheese tomatoes',
'pumpkin stew': 'pumpkin onions garlic celery',
'broccoli pie': 'broccoli cheese onions flour',
'pumpkin pie': 'pumpkin sugar flour butter'
}
c.execute("create virtual table test using fts%d(name, ingredients)" % v)
c.executemany("insert into test values(?,?)", data.items())
def check(pattern, expectednames):
names = [n[0] for n in c.execute("select name from test where ingredients match ?", (pattern, ))]
names.sort()
expectednames = list(expectednames)
expectednames.sort()
self.assertEqual(names, expectednames)
check('onions cheese', ['broccoli pie'])
check('eggs OR oil', ['cake', 'mayo'])
check('"pumpkin onions"', ['pumpkin stew'])
def testRTreeExtension(self):
"Check RTree extension if present"
if not self.checkOptionalExtension("rtree",
"create virtual table foo using rtree(one, two, three, four, five)"):
return
c = self.db.cursor()
data = (
(1, 2, 3, 4),
(5.1, 6, 7.2, 8),
(1, 4, 9, 12),
(77, 77.1, 3, 9),
)
c.execute("create virtual table test using rtree(ii, x1, x2, y1, y2)")
for i, row in enumerate(data):
c.execute("insert into test values(?,?,?,?,?)", (i, row[0], row[1], row[2], row[3]))
def check(pattern, expectedrows):
rows = [n[0] for n in c.execute("select ii from test where " + pattern)]
rows.sort()
expectedrows = list(expectedrows)
expectedrows.sort()
self.assertEqual(rows, expectedrows)
check("x1>2 AND x2<7 AND y1>17.2 AND y2<=8", [])
check("x1>5 AND x2<=6 AND y1>-11 AND y2<=8", [1])
def testGeopolyExtenstion(self):
"Check geopoly extension if present"
if not self.checkOptionalExtension("geopoly", "CREATE VIRTUAL TABLE newtab USING geopoly()"):
return
found = 0
for row in self.db.cursor().execute(
"CREATE VIRTUAL TABLE newtab USING geopoly();"
"INSERT INTO newtab(_shape) VALUES('[[0,0],[1,0],[0.5,1],[0,0]]');"
"SELECT * FROM newtab WHERE geopoly_overlap(_shape, $1);", ("[[0,0],[1,0],[0.5,1],[0,0]]", )):
found += 1
self.assertEqual(found, 1)
def testICUExtension(self):
"Check ICU extension if present"
if not self.checkOptionalExtension("icu", "select lower('I', 'tr_tr')"):
return
c = self.db.cursor()
# we compare SQLite standard vs icu
def check(text, locale, func="lower", equal=False):
q = "select " + func + "(?%s)"
sqlite = c.execute(q % ("", ), (text, )).fetchall()
icu = c.execute(q % (",'" + locale + "'", ), (text, )).fetchall()
if equal:
self.assertEqual(sqlite, icu)
else:
self.assertNotEqual(sqlite, icu)
check("I", "tr_tr")
check("I", "en_us", equal=True)
def testJSON1Extension(self):
# some sanity checks that it is working
l = self.db.cursor().execute("select json_array_length('[1,2,3,4]')").fetchall()[0][0]
self.assertEqual(l, 4)
l = self.db.cursor().execute(
"""select json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f')""").fetchall()[0][0]
self.assertEqual(l, 7)
def testTracebacks(self):
"Verify augmented tracebacks"
def badfunc(*args):
zebra = 3
1 / 0
self.db.create_scalar_function("badfunc", badfunc)
try:
c = self.db.cursor()
c.execute("select badfunc(1,'two',3.14)")
self.fail("Exception should have occurred")
except ZeroDivisionError:
tb = sys.exc_info()[2]
frames = []
while tb:
frames.append(tb.tb_frame)
tb = tb.tb_next
except:
self.fail("Wrong exception type")
frames.reverse()
frame = frames[1] # frame[0] is badfunc above
self.assertTrue(frame.f_code.co_filename.endswith(".c"))
self.assertTrue(frame.f_lineno > 100)
self.assertTrue(frame.f_code.co_name.endswith("-badfunc"))
# check local variables
if platform.python_implementation() != "PyPy":
l = frame.f_locals
self.assertIn("NumberOfArguments", l)
self.assertEqual(l["NumberOfArguments"], 3)
def testLoadExtension(self):
"Check loading of extensions"
# unicode issues
# they need to be enabled first (off by default)
if self.db.config(apsw.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, -1):
# someone wanted extension loading on by default! Turn it back off
self.db.config(apsw.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 0)
self.assertRaises(apsw.ExtensionLoadingError, self.db.loadextension, LOADEXTENSIONFILENAME)
self.assertEqual(self.db.config(apsw.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, -1), 0)
self.db.enableloadextension(False)
self.assertRaises(ZeroDivisionError, self.db.enableloadextension, BadIsTrue())
# should still be disabled
self.assertEqual(self.db.config(apsw.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 0), 0)
self.assertRaises(apsw.ExtensionLoadingError, self.db.loadextension, LOADEXTENSIONFILENAME)
self.assertEqual(self.db.config(apsw.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1), 1)
self.db.loadextension(LOADEXTENSIONFILENAME)
self.assertEqual(self.db.config(apsw.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 0), 0)
self.db.enableloadextension(True)
# make sure it checks args
self.assertRaises(TypeError, self.db.loadextension)
self.assertRaises(TypeError, self.db.loadextension, 12)
self.assertRaises(TypeError, self.db.loadextension, "foo", 12)
self.assertRaises(TypeError, self.db.loadextension, "foo", "bar", 12)
self.db.loadextension(LOADEXTENSIONFILENAME)
c = self.db.cursor()
self.assertEqual(1, curnext(c.execute("select half(2)"))[0])
# second entry point hasn't been called yet
self.assertRaises(apsw.SQLError, c.execute, "select doubleup(2)")
# load using other entry point
self.assertRaises(apsw.ExtensionLoadingError, self.db.loadextension, LOADEXTENSIONFILENAME, "doesntexist")
self.db.loadextension(LOADEXTENSIONFILENAME, "alternate_sqlite3_extension_init")
self.assertEqual(4, curnext(c.execute("select doubleup(2)"))[0])
def testMakeSqliteMsgFromException(self):
"Test C function that converts exception into SQLite error code"
class Source:
def Create1(self, *args):
e = apsw.IOError()
e.extendedresult = apsw.SQLITE_IOERR_ACCESS
raise e
def Create2(self, *args):
e = apsw.IOError()
e.extendedresult = (0x80 << 32) + apsw.SQLITE_IOERR_ACCESS # bigger than 32 bits
raise e
self.db.create_module("foo", Source())
for i in "1", "2":
Source.Create = getattr(Source, "Create" + i)
try:
self.db.cursor().execute("create virtual table vt using foo()")
1 / 0
except:
klass, value, tb = sys.exc_info()
self.assertEqual(klass, apsw.IOError)
self.assertTrue(isinstance(value, apsw.IOError))
self.assertEqual(value.extendedresult & ((0xffff << 16) | 0xffff), apsw.SQLITE_IOERR_ACCESS)
def testVtables(self):
"Test virtual table functionality"
data = ( # row 0 is headers, column 0 is rowid
("rowid", "name", "number", "item", "description"),
(1, "Joe Smith", 1.1, "\u00f6\u1234", "foo"),
(6000000000, "Road Runner", -7.3, "\u00f6\u1235", "foo"),
(77, "Fred", 0, "\u00f6\u1236", "foo"),
)
dataschema = "create table this_should_be_ignored" + str(data[0][1:])
# a query that will get constraints on every column
allconstraints = "select rowid,* from foo where rowid>-1000 and name>='A' and number<=12.4 and item>'A' and description=='foo' order by item"
allconstraintsl = [
(-1, apsw.SQLITE_INDEX_CONSTRAINT_GT), # rowid >
(0, apsw.SQLITE_INDEX_CONSTRAINT_GE), # name >=
(1, apsw.SQLITE_INDEX_CONSTRAINT_LE), # number <=
(2, apsw.SQLITE_INDEX_CONSTRAINT_GT), # item >
(3, apsw.SQLITE_INDEX_CONSTRAINT_EQ), # description ==
]
for i in range(20):
self.db.create_module("x" * i, lambda x: i)
# If shared cache is enabled then vtable creation is supposed to fail
try:
apsw.enable_shared_cache(True)
db = apsw.Connection(TESTFILEPREFIX + "testdb2")
db.create_module("y", lambda x: 2)
finally:
apsw.enable_shared_cache(False)
# The testing uses a different module name each time. SQLite
# doc doesn't define the semantics if a 2nd module is
# registered with the same name as an existing one and I was
# getting coredumps. It looks like issues inside SQLite.
cur = self.db.cursor()
# should fail since module isn't registered
self.assertRaises(apsw.SQLError, cur.execute, "create virtual table vt using testmod(x,y,z)")
# wrong args
self.assertRaises(TypeError, self.db.create_module, 1, 2, 3)
# give a bad object
self.db.create_module("testmod", 12) # next line fails due to lack of Create method
self.assertRaises(AttributeError, cur.execute, "create virtual table xyzzy using testmod(x,y,z)")
class Source:
def __init__(self, *expectargs):
self.expectargs = expectargs
def Create(self, *args): # db, modname, dbname, tablename, args
if self.expectargs != args[1:]:
raise ValueError("Create arguments are not correct. Expected " + str(self.expectargs) +
" but got " + str(args[1:]))
1 / 0
def CreateErrorCode(self, *args):
# This makes sure that sqlite error codes happen. The coverage checker
# is what verifies the code actually works.
raise apsw.BusyError("foo")
def CreateUnicodeException(self, *args):
raise Exception(
"\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}\N{LATIN SMALL LETTER A WITH TILDE}\N{LATIN SMALL LETTER O WITH DIAERESIS}"
)
def CreateBadSchemaType(self, *args):
return 12, None
def CreateBadSchema(self, *args):
return "this isn't remotely valid sql", None
def CreateWrongNumReturns(self, *args):
return "way", "too", "many", "items", 3
def CreateBadSequence(self, *args):
class badseq(object):
def __getitem__(self, which):
if which != 0:
1 / 0
return 12
def __len__(self):
return 2
return badseq()
# check Create does the right thing - we don't include db since it creates a circular reference
self.db.create_module("testmod1", Source("testmod1", "main", "xyzzy", "1", '"one"'))
self.assertRaises(ZeroDivisionError, cur.execute, 'create virtual table xyzzy using testmod1(1,"one")')
# unicode
uni = "\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}\N{LATIN SMALL LETTER A WITH TILDE}\N{LATIN SMALL LETTER O WITH DIAERESIS}"
self.db.create_module("testmod1dash1", Source("testmod1dash1", "main", uni, "1", '"' + uni + '"'))
self.assertRaises(ZeroDivisionError, cur.execute,
'create virtual table %s using testmod1dash1(1,"%s")' % (uni, uni))
Source.Create = Source.CreateErrorCode
self.assertRaises(apsw.BusyError, cur.execute, 'create virtual table xyzzz using testmod1(2, "two")')
Source.Create = Source.CreateUnicodeException
self.assertRaises(Exception, cur.execute, 'create virtual table xyzzz using testmod1(2, "two")')
Source.Create = Source.CreateBadSchemaType
self.assertRaises(TypeError, cur.execute, 'create virtual table xyzzz using testmod1(2, "two")')
Source.Create = Source.CreateBadSchema
self.assertRaises(apsw.SQLError, cur.execute, 'create virtual table xyzzz2 using testmod1(2, "two")')
Source.Create = Source.CreateWrongNumReturns
self.assertRaises(TypeError, cur.execute, 'create virtual table xyzzz2 using testmod1(2, "two")')
Source.Create = Source.CreateBadSequence
self.assertRaises(ZeroDivisionError, cur.execute, 'create virtual table xyzzz2 using testmod1(2, "two")')
# a good version of Source
class Source:
def Create(self, *args):
return dataschema, VTable(list(data))
Connect = Create
class VTable:
# A set of results from bestindex which should all generate TypeError.
# Coverage checking will ensure all the code is appropriately tickled
badbestindex = (
12,
(12, ),
((), ),
(((), ), ),
((((), ), ), ),
(((((), ), ), ), ),
((None, None, None, None, "bad"), ),
((0, None, (0, ), None, None), ),
((("bad", True), None, None, None, None), ),
(((0, True), "bad", None, None, None), ),
(None, "bad"),
[4, (3, True), [2, False], 1, [0]],
)
numbadbextindex = len(badbestindex)
def __init__(self, data):
self.data = data
self.bestindex3val = 0
def BestIndex1(self, wrong, number, of, arguments):
1 / 0
def BestIndex2(self, *args):
1 / 0
def BestIndex3(self, constraints, orderbys):
retval = self.badbestindex[self.bestindex3val]
self.bestindex3val += 1
if self.bestindex3val >= self.numbadbextindex:
self.bestindex3val = 0
return retval
def BestIndex4(self, constraints, orderbys):
return (None, 12, "\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}", "anything", "bad")
def BestIndex4_1(self, constraints, orderbys):
return (None, 12, "\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}", True, "bad")
def BestIndex5(self, constraints, orderbys):
# unicode error
return (None, None, "\xde\xad\xbe\xef")
def BestIndex6(self, constraints, orderbys):
return ((0, 1, (2, BadIsTrue()), 3, 4), )
def BestIndex7(self, constraints, orderbys):
return (None, 77, "foo", BadIsTrue(), 99)
_bestindexreturn = 99
def BestIndex99(self, constraints, orderbys):
cl = list(constraints)
cl.sort()
assert allconstraintsl == cl
assert orderbys == ((2, False), )
retval = ([4, (3, True), [2, False], 1, (0, False)], 997, "\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}",
False, 99)[:self._bestindexreturn]
return retval
def BestIndexGood(self, constraints, orderbys):
return None
def BestIndexGood2(self, constraints, orderbys):
return [] # empty list is same as None
def Open(self):
return Cursor(self)
def Open1(self, wrong, number, of, arguments):
1 / 0
def Open2(self):
1 / 0
def Open3(self):
return None
def Open99(self):
return Cursor(self)
UpdateInsertRow1 = None
def UpdateInsertRow2(self, too, many, args):
1 / 0
def UpdateInsertRow3(self, rowid, fields):
1 / 0
def UpdateInsertRow4(self, rowid, fields):
assert rowid is None
return None
def UpdateInsertRow5(self, rowid, fields):
assert rowid is None
return "this is not a number"
def UpdateInsertRow6(self, rowid, fields):
assert rowid is None
return -922337203685477580799 # too big
def UpdateInsertRow7(self, rowid, fields):
assert rowid is None
return 9223372036854775807 # ok
def UpdateInsertRow8(self, rowid, fields):
assert rowid is not None
assert rowid == -12
return "this should be ignored since rowid was supplied"
def UpdateChangeRow1(self, too, many, args, methinks):
1 / 0
def UpdateChangeRow2(self, rowid, newrowid, fields):
1 / 0
def UpdateChangeRow3(self, rowid, newrowid, fields):
assert newrowid == rowid
def UpdateChangeRow4(self, rowid, newrowid, fields):
assert newrowid == rowid + 20
def UpdateDeleteRow1(self, too, many, args):
1 / 0
def UpdateDeleteRow2(self, rowid):
1 / 0
def UpdateDeleteRow3(self, rowid):
assert rowid == 77
def Disconnect1(self, too, many, args):
1 / 0
def Disconnect2(self):
1 / 0
def Disconnect3(self):
pass
def Destroy1(self, too, many, args):
1 / 0
def Destroy2(self):
1 / 0
def Destroy3(self):
pass
def Begin1(self, too, many, args):
1 / 0
def Begin2(self):
1 / 0
def Begin3(self):
pass
def Sync(self):
pass
def Commit(self):
pass
def Rollback(self):
pass
def Rename1(self, too, many, args):
1 / 0
def Rename2(self, x):
1 / 0
def Rename3(self, x):
return ["thisshouldbeignored" * 25, [1]]
def FindFunction1(self, too, many, args):
1 / 0
def FindFunction2(self, name, nargs):
1 / 0
def FindFunction3(self, name, nargs):
return "this isn't a function"
def FindFunction4(self, name, nargs):
if nargs == 2:
return lambda x, y: x + y
return None
def FindFunction5(self, name, nargs):
return (1, 2, 3, 4)
def FindFunction6(self, name, nargs):
return (1, 2)
def FindFunction7(self, name, nargs):
return (1, lambda x: x)
def Integrity1(Self, schema, table, is_quick):
self.assertEqual(schema, "main")
self.assertEqual(table, "foo")
self.assertEqual(is_quick, 0)
return "a message"
def Integrity2(Self, schema, table, is_quick):
self.assertEqual(schema, "main")
self.assertEqual(table, "foo")
self.assertEqual(is_quick, 1)
return 3
def Integrity99(self, schema, table, is_quick):
return None
class Cursor:
_bestindexreturn = 99
def __init__(self, table):
self.table = table
def Filter1(self, toofewargs):
1 / 0
def Filter2(self, *args):
1 / 0
def Filter99(self, idxnum, idxstr, constraintargs):
self.pos = 1 # row 0 is headers
if self._bestindexreturn == 0:
assert idxnum == 0
assert idxstr == None
assert constraintargs == ()
return
if self._bestindexreturn == 1:
assert idxnum == 0
assert idxstr == None
assert constraintargs == ('foo', 'A', 12.4, 'A', -1000)
return
if self._bestindexreturn == 2:
assert idxnum == 997
assert idxstr == None
assert constraintargs == ('foo', 'A', 12.4, 'A', -1000)
return
# 3 or more
assert idxnum == 997
assert idxstr == "\N{LATIN SMALL LETTER E WITH CIRCUMFLEX}"
assert constraintargs == ('foo', 'A', 12.4, 'A', -1000)
def Filter(self, *args):
self.Filter99(*args)
1 / 0
def FilterGood(self, *args):
self.pos = 1 # row 0 is headers
def Eof1(self, toomany, args):
1 / 0
def Eof2(self):
1 / 0
def Eof3(self):
return BadIsTrue()
def Eof99(self):
return not (self.pos < len(self.table.data))
def Rowid1(self, too, many, args):
1 / 0
def Rowid2(self):
1 / 0
def Rowid3(self):
return "cdrom"
def Rowid99(self):
return self.table.data[self.pos][0]
def Column1(self):
1 / 0
def Column2(self, too, many, args):
1 / 0
def Column3(self, col):
1 / 0
def Column4(self, col):
return self # bad type
def Column99(self, col):
return self.table.data[self.pos][col + 1] # col 0 is row id
def Close1(self, too, many, args):
1 / 0
def Close2(self):
1 / 0
def Close99(self):
del self.table # deliberately break ourselves
def Next1(self, too, many, args):
1 / 0
def Next2(self):
1 / 0
def Next99(self):
self.pos += 1
# use our more complete version
self.db.create_module("testmod2", Source(), iVersion=4)
cur.execute("create virtual table foo using testmod2(2,two)")
# are missing/mangled methods detected correctly?
self.assertRaises(AttributeError, cur.execute, "select rowid,* from foo order by number")
VTable.BestIndex = VTable.BestIndex1
self.assertRaises(TypeError, cur.execute, "select rowid,* from foo order by number")
VTable.BestIndex = VTable.BestIndex2
self.assertRaises(ZeroDivisionError, cur.execute, "select rowid,* from foo order by number")
# check bestindex results
VTable.BestIndex = VTable.BestIndex3
for i in range(VTable.numbadbextindex):
self.assertRaises(TypeError, cur.execute, allconstraints)
VTable.BestIndex = VTable.BestIndex4
self.assertRaises(TypeError, cur.execute, allconstraints)
VTable.BestIndex = VTable.BestIndex4_1
self.assertRaises(ValueError, cur.execute, allconstraints)
VTable.BestIndex = VTable.BestIndex6
self.assertRaises(ZeroDivisionError, cur.execute, allconstraints)
VTable.BestIndex = VTable.BestIndex7
self.assertRaises(ZeroDivisionError, cur.execute, allconstraints)
# check varying number of return args from bestindex
VTable.BestIndex = VTable.BestIndex99
for i in range(6):
VTable._bestindexreturn = i
Cursor._bestindexreturn = i
self.assertRaisesRoot(ZeroDivisionError, cur.execute, " " + allconstraints + " " * i)
# error cases ok, return real values and move on to cursor methods
del VTable.Open
del Cursor.Filter
self.assertRaises(AttributeError, cur.execute, allconstraints) # missing open
VTable.Open = VTable.Open1
self.assertRaises(TypeError, cur.execute, allconstraints)
VTable.Open = VTable.Open2
self.assertRaises(ZeroDivisionError, cur.execute, allconstraints)
VTable.Open = VTable.Open3
self.assertRaises(AttributeError, cur.execute, allconstraints)
VTable.Open = VTable.Open99
self.assertRaises(AttributeError, cur.execute, allconstraints)
# put in filter
Cursor.Filter = Cursor.Filter1
self.assertRaisesRoot(TypeError, cur.execute, allconstraints)
Cursor.Filter = Cursor.Filter2
self.assertRaisesRoot(ZeroDivisionError, cur.execute, allconstraints)
Cursor.Filter = Cursor.Filter99
self.assertRaisesRoot(AttributeError, cur.execute, allconstraints)
Cursor.Eof = Cursor.Eof1
self.assertRaisesRoot(TypeError, cur.execute, allconstraints)
Cursor.Eof = Cursor.Eof2
self.assertRaisesRoot(ZeroDivisionError, cur.execute, allconstraints)
Cursor.Eof = Cursor.Eof3
self.assertRaisesRoot(ZeroDivisionError, cur.execute, allconstraints)
Cursor.Eof = Cursor.Eof99
self.assertRaisesRoot(AttributeError, cur.execute, allconstraints)
# now onto to rowid
Cursor.Rowid = Cursor.Rowid1
self.assertRaisesRoot(TypeError, cur.execute, allconstraints)
Cursor.Rowid = Cursor.Rowid2
self.assertRaisesRoot(ZeroDivisionError, cur.execute, allconstraints)
Cursor.Rowid = Cursor.Rowid3
self.assertRaisesRoot(ValueError, cur.execute, allconstraints)
Cursor.Rowid = Cursor.Rowid99
self.assertRaisesRoot(AttributeError, cur.execute, allconstraints)
# column
Cursor.Column = Cursor.Column1
self.assertRaisesRoot(TypeError, cur.execute, allconstraints)
Cursor.Column = Cursor.Column2
self.assertRaisesRoot(TypeError, cur.execute, allconstraints)
Cursor.Column = Cursor.Column3
self.assertRaisesRoot(ZeroDivisionError, cur.execute, allconstraints)
Cursor.Column = Cursor.Column4
self.assertRaisesRoot(TypeError, cur.execute, allconstraints)
Cursor.Column = Cursor.Column99
try:
for row in cur.execute(allconstraints):
pass
except AttributeError:
pass
# next
Cursor.Next = Cursor.Next1
with self.assertRaisesRoot(TypeError):
for row in cur.execute(allconstraints):
pass
Cursor.Next = Cursor.Next2
with self.assertRaisesRoot(ZeroDivisionError):
for row in cur.execute(allconstraints):
pass
Cursor.Next = Cursor.Next99
with self.assertRaisesRoot(AttributeError):
for row in cur.execute(allconstraints):
pass
# close
Cursor.Close = Cursor.Close1
try:
for row in cur.execute(allconstraints):
pass
except TypeError:
pass
Cursor.Close = Cursor.Close2
try:
for row in cur.execute(allconstraints):
pass
except ZeroDivisionError:
pass
Cursor.Close = Cursor.Close99
# update (insert)
sql = "insert into foo (name, description) values('gunk', 'foo')"
self.assertRaises(AttributeError, cur.execute, sql)
VTable.UpdateInsertRow = VTable.UpdateInsertRow1
self.assertRaises(TypeError, cur.execute, sql)
VTable.UpdateInsertRow = VTable.UpdateInsertRow2
self.assertRaises(TypeError, cur.execute, sql)
VTable.UpdateInsertRow = VTable.UpdateInsertRow3
self.assertRaises(ZeroDivisionError, cur.execute, sql)
VTable.UpdateInsertRow = VTable.UpdateInsertRow4
self.assertRaises(TypeError, cur.execute, sql)
VTable.UpdateInsertRow = VTable.UpdateInsertRow5
self.assertRaises(TypeError, cur.execute, sql)
VTable.UpdateInsertRow = VTable.UpdateInsertRow6
self.assertRaises(OverflowError, cur.execute, sql)
VTable.UpdateInsertRow = VTable.UpdateInsertRow7
cur.execute(sql)
self.assertEqual(self.db.last_insert_rowid(), 9223372036854775807)
VTable.UpdateInsertRow = VTable.UpdateInsertRow8
cur.execute("insert into foo (rowid,name, description) values(-12,'gunk', 'foo')")
# update (change)
VTable.BestIndex = VTable.BestIndexGood
Cursor.Filter = Cursor.FilterGood
sql = "update foo set description=='bar' where description=='foo'"
self.assertRaises(AttributeError, cur.execute, sql)
VTable.UpdateChangeRow = VTable.UpdateChangeRow1
self.assertRaises(TypeError, cur.execute, sql)
VTable.UpdateChangeRow = VTable.UpdateChangeRow2
self.assertRaises(ZeroDivisionError, cur.execute, sql)
VTable.UpdateChangeRow = VTable.UpdateChangeRow3
cur.execute(sql)
VTable.UpdateChangeRow = VTable.UpdateChangeRow4
cur.execute("update foo set rowid=rowid+20 where 1")
# update (delete)
VTable.BestIndex = VTable.BestIndexGood2 # improves code coverage
sql = "delete from foo where name=='Fred'"
self.assertRaises(AttributeError, cur.execute, sql)
VTable.UpdateDeleteRow = VTable.UpdateDeleteRow1
self.assertRaises(TypeError, cur.execute, sql)
VTable.UpdateDeleteRow = VTable.UpdateDeleteRow2
self.assertRaises(ZeroDivisionError, cur.execute, sql)
VTable.UpdateDeleteRow = VTable.UpdateDeleteRow3
cur.execute(sql)
# rename
sql = "alter table foo rename to bar"
VTable.Rename = VTable.Rename1
self.assertRaises(TypeError, cur.execute, sql)
VTable.Rename = VTable.Rename2
self.assertRaises(ZeroDivisionError, cur.execute, sql)
VTable.Rename = VTable.Rename3
# this is to catch memory leaks
cur.execute(sql)
del VTable.Rename # method is optional
cur.execute("alter table bar rename to foo") # put things back
# findfunction
# mess with overload function first
self.assertRaises(TypeError, self.db.overload_function, 1, 1)
self.assertRaises(apsw.MisuseError, self.db.overload_function, "a" * 1024, 1)
self.db.overload_function("xyz", 2)
self.assertRaises(apsw.SQLError, cur.execute, "select xyz(item,description) from foo", can_cache=False)
VTable.FindFunction = VTable.FindFunction1
self.assertRaises(apsw.SQLError,
self.assertRaisesUnraisable,
TypeError,
cur.execute,
"select xyz(item,description) from foo ",
can_cache=False)
VTable.FindFunction = VTable.FindFunction2
self.assertRaises(apsw.SQLError,
self.assertRaisesUnraisable,
ZeroDivisionError,
cur.execute,
"select xyz(item,description) from foo ",
can_cache=False)
VTable.FindFunction = VTable.FindFunction3
def foo():
for row in cur.execute("select xyz(item,description) from foo ", can_cache=False):
pass
self.assertRaises(apsw.SQLError, self.assertRaisesUnraisable, TypeError, foo)
# this should work
VTable.FindFunction = VTable.FindFunction4
for row in cur.execute("select xyz(item,description) from foo ", can_cache=False):
pass
for ff, e in ((VTable.FindFunction5, TypeError), (VTable.FindFunction6, TypeError), (VTable.FindFunction7,
ValueError)):
VTable.FindFunction = ff
self.assertRaises(apsw.SQLError,
self.assertRaisesUnraisable,
e,
cur.execute,
"select xyz(item,description) from foo",
can_cache=False)
# integrity check
self.assertEqual("ok", self.db.pragma("integrity_check", "foo"))
VTable.Integrity = VTable.Integrity1
self.assertEqual("a message", self.db.pragma("integrity_check", "foo"))
VTable.Integrity = VTable.Integrity2
self.assertRaises(TypeError, self.db.pragma, "quick_check", "foo")
VTable.Integrity = VTable.Integrity99
self.assertEqual("ok", self.db.pragma("integrity_check", "foo"))
# transaction control
# Begin, Sync, Commit and rollback all use the same underlying code
sql = "delete from foo where name=='Fred'"
VTable.Begin = VTable.Begin1
self.assertRaises(TypeError, cur.execute, sql)
VTable.Begin = VTable.Begin2
self.assertRaises(ZeroDivisionError, cur.execute, sql)
VTable.Begin = VTable.Begin3
cur.execute(sql)
# disconnect - sqlite ignores any errors
db = apsw.Connection(TESTFILEPREFIX + "testdb")
db.create_module("testmod2", Source())
cur2 = db.cursor()
for _ in cur2.execute("select * from foo"):
pass
VTable.Disconnect = VTable.Disconnect1
self.assertRaisesUnraisable(TypeError, db.close) # nb close succeeds!
self.assertRaises(apsw.CursorClosedError, cur2.execute, "select * from foo")
del db
db = apsw.Connection(TESTFILEPREFIX + "testdb")
db.create_module("testmod2", Source())
cur2 = db.cursor()
for _ in cur2.execute("select * from foo"):
pass
VTable.Disconnect = VTable.Disconnect2
self.assertRaisesUnraisable(ZeroDivisionError, db.close) # nb close succeeds!
self.assertRaises(apsw.CursorClosedError, cur2.execute, "select * from foo")
del db
db = apsw.Connection(TESTFILEPREFIX + "testdb")
db.create_module("testmod2", Source())
cur2 = db.cursor()
for _ in cur2.execute("select * from foo"):
pass
VTable.Disconnect = VTable.Disconnect3
db.close()
del db
# destroy
VTable.Destroy = VTable.Destroy1
self.assertRaises(apsw.SQLError, self.assertRaisesUnraisable, TypeError, cur.execute, "drop table foo")
VTable.Destroy = VTable.Destroy2
self.assertRaises(apsw.SQLError, self.assertRaisesUnraisable, ZeroDivisionError, cur.execute, "drop table foo")
VTable.Destroy = VTable.Destroy3
cur.execute("drop table foo")
self.db.close()
def testVTableUnusable(self):
"Tests vtable bestindex with unusable constraints and other error handling"
class Source:
def Create(self, db, modulename, dbname, tablename, *args):
return "create table ignored(a,b,c)", Table()
Connect = Create
class Table:
last = None
def BestIndex(tself, constraints, orderbys):
if tself.last is None:
self.assertEqual(len(constraints), 3)
tself.last = constraints
else:
# the in clause should have become unusable
self.assertEqual(len(constraints), 2)
res = []
estimated = 9999999999999999
for c, op in constraints:
if op == apsw.SQLITE_INDEX_CONSTRAINT_EQ: # actually IN
res.append(0)
else:
res.append(None)
if all(r is None for r in res):
res[-1] = 0
estimated = 7 # make it prefer this
return [res, 0, "fred", False, estimated]
bifg_seqnum = 0
def BestIndex_fail_getitem(self, *args):
res = Table.bifg_sequence[Table.bifg_seqnum]
Table.bifg_seqnum += 1
return res
def Open(self):
return Cursor()
def Disconnect(self):
pass
Destroy = Disconnect
def bad(n, *args):
class badgi(list):
def __getitem__(self, n):
if n == self.failnum:
1 / 0
return super().__getitem__(n)
res = badgi(*args)
res.failnum = n
return res
all_none = (None, None, None, None, None)
Table.bifg_sequence = (
# outer list
bad(0, all_none),
bad(1, all_none),
bad(2, all_none),
bad(3, all_none),
bad(4, all_none),
# constraints
[bad(0, [None])],
[[bad(0, (1, 2))]],
[[bad(1, (1, 2))]],
)
class Cursor:
def Filter(cself, idxnum, idxstr, args):
# if we correctly handled unusable then args should contain column c
# bound value
self.assertEqual(args, (100, ))
def FilterIgnore(*args):
pass
def Eof(self):
return True
def Rowid(self):
return 10
def Column(self, col):
return 10
def Next(self):
pass
def Close(self):
pass
self.db.create_module("bestindex", Source())
self.db.execute(
"create virtual table foo using bestindex(); select * from foo where a in (66,77) and b>? and c>?",
(99, 100))
Table.BestIndex = Table.BestIndex_fail_getitem
Cursor.Filter = Cursor.FilterIgnore
for i in range(len(Table.bifg_sequence)):
self.assertRaises(ZeroDivisionError, self.db.execute, "select * from foo where a > 7")
def testClosingChecks(self):
"Check closed connection/blob/cursor is correctly detected"
cur = self.db.cursor()
rowid = curnext(
cur.execute("create table foo(x blob); insert into foo values(zeroblob(98765)); select rowid from foo"))[0]
blob = self.db.blob_open("main", "foo", "x", rowid, True)
blob.close()
nargs = self.blob_nargs
for func in [x for x in dir(blob) if not x.startswith("__") and not x in ("close", )]:
args = ("one", "two", "three")[:nargs.get(func, 0)]
try:
getattr(blob, func)(*args)
self.fail(f"blob method/attribute { func } didn't notice that the connection is closed")
except ValueError: # we issue ValueError to be consistent with file objects
pass
self.db.close()
nargs = self.connection_nargs
tested = 0
for func in [x for x in dir(self.db) if x in nargs or (not x.startswith("__") and not x in ("close", ))]:
tested += 1
args = ("one", "two", "three")[:nargs.get(func, 0)]
try:
# attributes come back as None after a close
func = getattr(self.db, func)
if func:
func(*args)
self.fail(f"connection method/attribute { func } didn't notice that the connection is closed")
except apsw.ConnectionClosedError:
pass
self.assertTrue(tested > len(nargs))
# do the same thing, but for cursor
nargs = self.cursor_nargs
tested = 0
for func in [x for x in dir(cur) if not x.startswith("__") and not x in ("close", )]:
tested += 1
args = ("one", "two", "three")[:nargs.get(func, 0)]
try:
getattr(cur, func)(*args)
self.fail(f"cursor method/attribute { func } didn't notice that the connection is closed")
except apsw.CursorClosedError:
pass
self.assertTrue(tested >= len(nargs))
def testClosing(self):
"Verify behaviour of close() functions"
cur = self.db.cursor()
cur.execute("select 3;select 4")
self.assertRaises(apsw.IncompleteExecutionError, cur.close)
# now force it
self.assertRaises(TypeError, cur.close, sys)
self.assertRaises(TypeError, cur.close, 1, 2, 3)
cur.close(True)
l = [self.db.cursor() for i in range(1234)]
cur = self.db.cursor()
cur.execute("select 3; select 4; select 5")
l2 = [self.db.cursor() for i in range(1234)]
self.assertRaises(apsw.IncompleteExecutionError, self.db.close)
self.assertRaises(TypeError, self.db.close, sys)
self.assertRaises(TypeError, self.db.close, 1, 2, 3)
self.db.close(True) # force it
self.db.close() # should be fine now
# coverage - close cursor after closing db
db = apsw.Connection(":memory:")
cur = db.cursor()
db.close()
cur.close()
def testLargeObjects(self):
"Verify handling of large strings/blobs (>2GB) [requires 64 bit platform]"
assert is64bit
# For binary/blobs I use an anonymous area slightly larger than 2GB chunk of memory, but don't touch any of it
f = mmap.mmap(-1, 2 * 1024 * 1024 * 1024 + 25000)
c = self.db.cursor()
c.execute("create table foo(theblob)")
self.assertRaises(apsw.TooBigError, c.execute, "insert into foo values(?)", (f, ))
c.execute("insert into foo values(?)", ("jkghjk" * 1024, ))
b = self.db.blob_open("main", "foo", "theblob", self.db.last_insert_rowid(), True)
b.read(1)
self.assertRaises(ValueError, b.write, f)
def func():
return f
self.db.create_scalar_function("toobig", func)
self.assertRaises(apsw.TooBigError, c.execute, "select toobig()")
f.close()
def testErrorCodes(self):
"Verify setting of result codes on error/exception"
fname = TESTFILEPREFIX + "gunk-errcode-test"
write_whole_file(fname, "wb", b"A" * 8192)
db = None
try:
# The exception could be thrown on either of these lines
# depending on several factors
db = apsw.Connection(fname)
db.cursor().execute("select * from sqlite_schema")
1 / 0 # should not be reachable
except:
klass, e, tb = sys.exc_info()
self.assertTrue(isinstance(e, apsw.NotADBError))
self.assertEqual(e.result, apsw.SQLITE_NOTADB)
self.assertEqual(e.extendedresult & 0xff, apsw.SQLITE_NOTADB)
if db is not None:
db.close(True)
try:
deletefile(fname)
except:
pass
try:
self.db.execute("select a from XYZ banana 4")
except apsw.SQLError as e:
self.assertEqual(25, e.error_offset)
def testLimits(self):
"Verify setting and getting limits"
self.assertRaises(TypeError, self.db.limit, "apollo", 11)
c = self.db.cursor()
c.execute("create table foo(x)")
c.execute("insert into foo values(?)", ("x" * 1024, ))
old = self.db.limit(apsw.SQLITE_LIMIT_LENGTH)
self.db.limit(apsw.SQLITE_LIMIT_LENGTH, 1023)
self.assertRaises(apsw.TooBigError, c.execute, "insert into foo values(?)", ("y" * 1024, ))
self.assertEqual(1023, self.db.limit(apsw.SQLITE_LIMIT_LENGTH, 0))
self.assertRaises(apsw.TooBigError, c.execute, "insert into foo values(?)", ("x" * 1024, ))
def testConnectionHooks(self):
"Verify connection hooks"
del apsw.connection_hooks
try:
db = apsw.Connection(":memory:")
except AttributeError:
pass
apsw.connection_hooks = sys # bad type
try:
db = apsw.Connection(":memory:")
except TypeError:
pass
apsw.connection_hooks = ("a", "tuple", "of", "non-callables")
try:
db = apsw.Connection(":memory:")
except TypeError:
pass
apsw.connection_hooks = (dir, lambda x: 1 / 0)
try:
db = apsw.Connection(":memory:")
except ZeroDivisionError:
pass
def delit(db):
del db
apsw.connection_hooks = [delit for _ in range(9000)]
db = apsw.Connection(":memory:")
db.close()
apsw.connection_hooks = [lambda x: x]
db = apsw.Connection(":memory:")
db.close()
def testCompileOptions(self):
"Verify getting compile options"
# We don't know what the right answers are, so just check
# there are more than zero entries.
v = apsw.compile_options
self.assertEqual(type(v), tuple)
self.assertTrue(len(v) > 1)
def testKeywords(self):
"Verify keywords"
k = apsw.keywords
self.assertTrue("INSERT" in k)
def testIssue4(self):
"Issue 4: Error messages and SQLite ticket 3063"
connection = apsw.Connection(":memory:")
cursor = connection.cursor()
cursor.execute("CREATE TABLE A_TABLE (ID ABC PRIMARY KEY NOT NULL)")
try:
cursor.execute("INSERT INTO A_TABLE VALUES (NULL)")
except:
klass, e, tb = sys.exc_info()
assert "A_TABLE.ID" in str(e)
try:
cursor.execute("INSERT INTO A_TABLE VALUES (?)", (None, ))
except:
klass, e, tb = sys.exc_info()
assert "A_TABLE.ID" in str(e)
def testIssue15(self):
"Issue 15: Release GIL during calls to prepare"
self.db.cursor().execute("create table foo(x)")
self.db.cursor().execute("begin exclusive")
db2 = apsw.Connection(TESTFILEPREFIX + "testdb")
db2.set_busy_timeout(30000)
t = ThreadRunner(db2.cursor().execute, "select * from foo")
t.start()
time.sleep(1)
self.db.cursor().execute("commit")
t.go()
def testIssue19(self):
"Issue 19: Incomplete cursor execution"
c = self.db.cursor()
c.execute("create table numbers(x)")
for i in range(10):
c.execute("insert into numbers values(?)", (i, ))
c.execute("select * from numbers")
curnext(c)
curnext(c)
curnext(c)
self.db.cursor().execute("delete from numbers where x=5")
curnext(c)
curnext(c)
def testIssue24(self):
"Issue 24: Ints and Longs"
c = self.db.cursor()
for row in c.execute("select 3"):
pass
self.assertEqual(int, type(row[0]))
for row in c.execute("select -2147483647-1"):
pass
self.assertEqual(int, type(row[0]))
for row in c.execute("select 2147483647"):
pass
self.assertEqual(int, type(row[0]))
# Depending on the platform, sizeof(long), 64 bitness etc we
# may remain as python type int or type long. Check we are
# getting the right numbers no matter what. This duplicates
# testTypes but you can never be too careful.
for v in "2147483646", "2147483647", "2147483648", "2147483649", \
"21474836460", "21474836470", "21474836480", "21474836490", \
"147483646", "147483647", "147483648", "147483649":
for neg in ("-", ""):
val = c.execute("select " + neg + v).fetchall()[0][0]
val = repr(val)
if val.endswith("L"):
val = val[:-1]
self.assertEqual(val, neg + v)
def testIssue31(self):
"Issue 31: GIL & SQLite mutexes with heavy threading, threadsafe errors from SQLite"
randomnumbers = [random.randint(0, 10000) for _ in range(10000)]
cursor = self.db.cursor()
cursor.execute("create table foo(x)")
cursor.execute("begin")
for num in randomnumbers:
cursor.execute("insert into foo values(?)", (num, ))
cursor.execute("end")
self.db.create_scalar_function("timesten", lambda x: x * 10)
def dostuff(n):
# spend n seconds doing stuff to the database
c = self.db.cursor()
b4 = time.time()
while time.time() - b4 < n:
i = random.choice(randomnumbers)
if i % 5 == 0:
sql = "select timesten(x) from foo where x=%d order by x" % (i, )
c.execute(sql)
elif i % 5 == 1:
sql = "select timesten(x) from foo where x=? order by x"
called = 0
for row in self.db.cursor().execute(sql, (i, )):
called += 1
self.assertEqual(row[0], 10 * i)
# same value could be present multiple times
self.assertTrue(called >= 1)
elif i % 5 == 2:
try:
self.db.cursor().execute("deliberate syntax error")
except apsw.SQLError:
assert ("deliberate" in str(sys.exc_info()[1]))
elif i % 5 == 3:
try:
self.db.cursor().execute("bogus syntax error")
except apsw.SQLError:
assert ("bogus" in str(sys.exc_info()[1]))
else:
sql = "select timesten(x) from foo where x=? order by x"
self.db.cursor().execute(sql, (i, ))
runtime = float(os.getenv("APSW_HEAVY_DURATION")) if os.getenv("APSW_HEAVY_DURATION") else 15
threads = [ThreadRunner(dostuff, runtime) for _ in range(20)]
for t in threads:
t.start()
for t in threads:
# if there were any errors then exceptions would be raised here
t.go()
def testIssue50(self):
"Issue 50: Check Blob.read return value on eof"
# first get what the system returns on eof
f = open(os.devnull, "rb")
try:
# deliberately hit eof
f.read()
# now try to read some more
feof = f.read(10)
finally:
f.close()
cur = self.db.cursor()
# make a blob to play with
rowid = curnext(
cur.execute("create table foo(x blob); insert into foo values(zeroblob(98765)); select rowid from foo"))[0]
blobro = self.db.blob_open("main", "foo", "x", rowid, False)
try:
blobro.read(98765)
beof = blobro.read(10)
self.assertEqual(type(beof), type(feof))
self.assertEqual(beof, feof)
finally:
blobro.close()
def testIssue98(self, runfrom106=None):
"Issue 98: An error in context manager commit should do a rollback"
self.db.cursor().execute("create table foo(x); insert into foo values(3); insert into foo values(4)")
# We need the reader to block a writer, which requires non-WAL mode
self.db.cursor().execute("pragma journal_mode=delete")
db2 = apsw.Connection(TESTFILEPREFIX + "testdb")
if runfrom106:
db2.set_exec_trace(runfrom106)
db2.cursor().execute("pragma journal_mode=delete")
# deliberately don't read from cursor on connection 1 which will prevent a commit
x = self.db.cursor().execute("select * from foo")
db2.__enter__()
db2.cursor().execute("insert into foo values(5)") # transaction is buffered in memory by SQLite
try:
db2.__exit__(None, None, None)
except apsw.BusyError:
pass
# Ensure transaction was rolled back
x.fetchall()
for row in db2.cursor().execute("select * from foo where x=5"):
self.fail("Transaction was not rolled back")
db2.close()
if runfrom106: return
# Verify that error in tracer results in rollback
self.db.__enter__()
def h(*args):
1 / 0
self.db.cursor().execute("insert into foo values(6)")
self.db.set_exec_trace(h)
try:
self.db.__exit__(None, None, None)
except ZeroDivisionError:
self.db.set_exec_trace(None)
pass
for row in self.db.cursor().execute("select * from foo where x=6"):
self.fail("Transaction was not rolled back")
def testIssue103(self):
"Issue 103: Error handling when sqlite3_declare_vtab fails"
class Source:
def Create(self, *args):
return "create table x(delete)", None
self.db.create_module("issue103", Source())
try:
self.db.cursor().execute("create virtual table foo using issue103()")
1 / 0 # should not be reached
except apsw.SQLError:
assert "near \"delete\": syntax error" in str(sys.exc_info()[1])
def testIssue106(self):
"Issue 106: Profiling and tracing"
traces = []
def tracer(cur, sql, bindings):
sql = sql.lower().split()[0]
if sql in ("savepoint", "release", "rollback"):
traces.append(sql)
return True
self.testIssue98(tracer)
self.assertTrue(len(traces) >= 3)
self.assertTrue("savepoint" in traces)
self.assertTrue("release" in traces)
self.assertTrue("rollback" in traces)
def testIssue142(self):
"Issue 142: bytes from system during dump"
orig_strftime = time.strftime
orig_getuser = getpass.getuser
fh = []
try:
time.strftime = lambda arg: b"gjkTIMEJUNKhgjhg\xfe\xdf"
getpass.getuser = lambda: b"\x81\x82\x83gjkhgUSERJUNKjhg\xfe\xdf"
fh = [open(TESTFILEPREFIX + "test-shell-" + t, "w+", encoding="utf8") for t in ("in", "out", "err")]
kwargs = {"stdin": fh[0], "stdout": fh[1], "stderr": fh[2]}
rows = (["correct"], ["horse"], ["battery"], ["staple"])
self.db.cursor().execute("create table foo(x)")
self.db.cursor().executemany("insert into foo values(?)", rows)
shell = apsw.shell.Shell(db=self.db, **kwargs)
shell.command_dump([])
fh[1].seek(0)
out = fh[1].read()
for row in rows:
self.assertTrue(row[0] in out)
self.assertTrue("TIMEJUNK" in out)
self.assertTrue("USERJUNK" in out)
finally:
for f in fh:
f.close()
time.strftime = orig_strftime
getpass.getuser = orig_getuser
def testIssue186(self):
"Issue 186: desription cache between statements"
cur = self.db.cursor()
for i, row in enumerate(cur.execute("select 1; select 1,2; select 1,2,3; select 1,2,3,4;")):
# this catches if the order of getting them makes a difference
if i % 2:
self.assertEqual(len(cur.description), len(cur.get_description()))
else:
self.assertEqual(len(cur.get_description()), len(cur.description))
self.assertEqual(len(cur.description), i + 1)
# check executemany too
for i, row in enumerate(
cur.executemany("select ?; select ?,?; select ?,?,?; select ?,?,?,?;", [
(1, 1, 2, 1, 2, 3, 1, 2, 3, 4),
(1, 1, 2, 1, 2, 3, 1, 2, 3, 4),
])):
i %= 4
self.assertEqual(len(cur.get_description()), i + 1)
# and the tracers
def tracer(cursor, *args):
self.assertEqual(len(cursor.get_description()), expect)
return True
expect = 1
cur.set_exec_trace(tracer)
cur.set_row_trace(tracer)
for i, row in enumerate(cur.execute("select 1; select 1,2; select 1,2,3; select 1,2,3,4;")):
expect += 1
expect = 1
for i, row in enumerate(
cur.executemany("select ?; select ?,?; select ?,?,?; select ?,?,?,?;", [
(1, 1, 2, 1, 2, 3, 1, 2, 3, 4),
(1, 1, 2, 1, 2, 3, 1, 2, 3, 4),
])):
expect += 1
if expect > 4: expect = 1
def testTicket2158(self):
"Check we are not affected by SQLite ticket #2158"
def dummy(x, y):
if x < y: return -1
if x > y: return 1
return 0
self.db.create_collation("dummy", dummy)
cur = self.db.cursor()
cur.execute("create table foo(x)")
cur.executemany("insert into foo values(?)", randomintegers(20))
for row in cur.execute("select * from foo order by x collate dummy"):
pass
self.db.create_collation("dummy", None)
self.assertRaises(apsw.SQLError, cur.execute, "select * from foo order by x collate dummy")
def testIssue199(self):
"Backup API should accept Connection subclasses"
# https://github.com/rogerbinns/apsw/issues/199
class subclass(apsw.Connection):
pass
dbsub = subclass("")
dbsub.cursor().execute("create table a(b);insert into a values(3);")
b = self.db.backup("main", dbsub, "main")
try:
while not b.done:
b.step(100)
finally:
b.finish()
def testIssue311(self):
"Indirect descendents of VFS should support WAL (in addition to direct subclasses)"
class vfswrapped(apsw.VFS):
def __init__(self):
self.myname = "testIssue311"
self.base = ""
apsw.VFS.__init__(self, self.myname, self.base)
def xOpen(self, name, flags):
return vfsfilewrap(self.base, name, flags)
class vfsfilewrap(apsw.VFSFile):
def __init__(self, parent, name, flags):
apsw.VFSFile.__init__(self, parent, name, flags)
# we make testdb be wal and then try to work with it
self.db.cursor().execute(
"pragma journal_mode=wal; create table test(x,y); insert into test values(3,4)").fetchall()
wrap = vfswrapped()
con = apsw.Connection(TESTFILEPREFIX + "testdb", vfs=wrap.myname)
for row in con.cursor().execute("select x+y from test"):
self.assertEqual(row[0], 7)
break
else:
self.fail("No rows seen")
def testIssue314(self):
"Reference cycles between instance, Connection and instance.method"
cleared = []
class SelfReferencer:
def __del__(self):
cleared.append(id(self))
def __init__(self):
self.db = apsw.Connection("")
self.db.set_busy_handler(self.refme)
self.cur = self.db.cursor()
self.cur.set_row_trace(self.refme)
def refme(self):
pass
for i in range(1000):
SelfReferencer()
gc.collect()
self.assertEqual(1000, len(cleared))
def testIssue394(self):
"Default vfs manipulation"
starting = apsw.vfs_names()
if len(starting) > 1:
apsw.set_default_vfs(starting[1])
self.assertEqual(apsw.vfs_names()[0], starting[1])
self.assertNotEqual(starting, apsw.vfs_names())
apsw.set_default_vfs(starting[0])
# we assume it is safe to remove the last listed vfs for testing
apsw.unregister_vfs(starting[-1])
self.assertEqual(apsw.vfs_names(), starting[:-1])
self.assertRaises(TypeError, apsw.set_default_vfs)
self.assertRaises(TypeError, apsw.set_default_vfs, 3)
self.assertRaises(TypeError, apsw.set_default_vfs, "3", 3)
self.assertRaises(ValueError, apsw.set_default_vfs, "4324324234")
self.assertRaises(TypeError, apsw.unregister_vfs)
self.assertRaises(TypeError, apsw.unregister_vfs, 3)
self.assertRaises(TypeError, apsw.unregister_vfs, "3", 3)
self.assertRaises(ValueError, apsw.unregister_vfs, "4342345324")
def testIssue431(self):
"All applicable constants for config/status calls"
self.assertEqual(0, self.db.config(apsw.SQLITE_DBCONFIG_TRIGGER_EQP, -1))
self.db.execute("create table foo(x)")
self.assertTrue(self.db.table_exists("main", "foo"))
self.db.config(apsw.SQLITE_DBCONFIG_RESET_DATABASE, 1)
self.db.execute("VACUUM")
self.db.config(apsw.SQLITE_DBCONFIG_RESET_DATABASE, 0)
self.assertFalse(self.db.table_exists("main", "foo"))
def testIssue433(self):
"Comments and exec tracing"
p = None
def et(cursor, sql, bindings):
nonlocal p
p = (cursor, sql, bindings)
return False
cur = self.db.cursor()
cur.exec_trace = et
for query in ("-- foo", "select 3;;;; "):
with contextlib.suppress(apsw.ExecTraceAbort):
cur.execute(query)
self.assertEqual(p[1], query)
self.assertEqual(p[0].has_vdbe, "select" in query)
def testIssue475(self):
"Unused cursor attributes"
# The cause of this issue was a new cursor attribute access, so we check
# all objects to catch anything else
objects = [
apsw, apsw.Connection, apsw.Cursor, apsw.Blob, apsw.Error, apsw.VFS, apsw.VFSFile, self.db,
self.db.cursor()
]
self.db.execute("create table x(y); insert into x values(x'abcdef1012');select * from x")
objects.append(self.db.blob_open("main", "x", "y", self.db.last_insert_rowid(), 0))
objects.append(apsw.VFS("aname", ""))
objects.append(
apsw.VFSFile("", self.db.db_filename("main"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0]))
objects.append(apsw.VFSFcntlPragma)
for o in objects:
for n in dir(o):
try:
getattr(o, n)
except apsw.Error:
pass
def testIssue488(self):
"__init__ called multiple times"
objects = [
self.db,
self.db.cursor(),
apsw.zeroblob(3),
apsw.VFS("aname", ""),
apsw.VFSFile(
"",
self.db.db_filename("main"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0],
)
]
for o in objects:
self.assertRaisesRegex(RuntimeError, "__init__ has already been called, and cannot be called again",
o.__init__, "issue 488")
def testIssue506(self):
"VFS file with None/NULL filename"
vfs_saw_none = False
vfsfile_saw_none = False
class TVFS(apsw.VFS):
vfsname = "test"
def __init__(self):
super().__init__(self.vfsname, "")
def xOpen(self, name, flags):
nonlocal vfs_saw_none
if name is None:
vfs_saw_none = True
return TVFSFile(name, flags)
class TVFSFile(apsw.VFSFile):
def __init__(self, filename, flags):
nonlocal vfsfile_saw_none
if filename is None:
vfsfile_saw_none = True
super().__init__("", filename, flags)
tvfs = TVFS()
con = apsw.Connection(self.db.filename, vfs=tvfs.vfsname)
# put enough stuff in temp table that it spills to disk
con.pragma("cache_size", 10)
temp_store = con.pragma("temp_store")
try:
con.pragma("temp_store", 1)
con.execute("create temp table testissue506(x UNIQUE,y UNIQUE, PRIMARY KEY(x,y))")
con.executemany("insert into testissue506 values(?,?)", (("a"*i, "b"*i) for i in range(1000)))
finally:
con.pragma("temp_store", temp_store)
# verify we saw the Nones
self.assertTrue(vfs_saw_none)
self.assertTrue(vfsfile_saw_none)
def testIssue526(self):
"Error in VFS xRandomness"
class RandomVFS(apsw.VFS):
def __init__(self):
apsw.VFS.__init__(self, "issue526", "", 1)
vfs = RandomVFS()
self.db.pragma("journal_mode", "WAL")
try:
with self.db:
RandomVFS.xRandomness = lambda *args: 1/0
# this resets SQLite randomness so xRandonness of default VFS will be called again
apsw.randomness(0)
self.db.pragma("user_version", 1)
raise Exception("Should not reach here")
except ZeroDivisionError:
pass
def testCursorGet(self):
"Cursor.get"
for query, expected in (("select 3,4", (3, 4)), ("select 3; select 4", [3, 4]), ("select 3,4; select 4,5", [
(3, 4), (4, 5)
]), ("select 3,4; select 5", [(3, 4), 5]), ("select 3", 3)):
self.assertEqual(self.db.execute(query).get, expected)
q = "select ?"
b = [(i, ) for i in range(10)]
self.assertEqual(self.db.executemany(q, b).get, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(self.db.execute("/* comment */").get, None)
# second query is deliberate syntax error
cur = self.db.execute("select 3 ; select 3 +")
self.assertRaises(apsw.SQLError, getattr, cur, "get")
# a query where all results are exhausted
cur = self.db.cursor()
cur.execute("select 3").fetchall()
self.assertEqual(cur.get, None)
def testConnectionPragma(self):
"Connection.pragma"
self.assertRaises(TypeError, self.db.pragma)
self.assertRaises(TypeError, self.db.pragma, 3)
self.assertRaises(TypeError, self.db.pragma, "three", "four", "five")
self.assertRaises(TypeError, self.db.pragma, "three", 4 + 3j)
self.assertEqual(self.db.pragma("journal_mode"), "delete")
self.assertEqual(self.db.pragma("journal_mode", "wal"), "wal")
self.assertEqual(self.db.pragma("user_version"), 0)
self.db.pragma("user_version", 7)
self.assertEqual(self.db.pragma("user_version"), 7)
self.assertRaises(apsw.SQLError, self.db.pragma, "user_version", "abc\0def")
# check not cached - #525
self.db.pragma("should_not_cache", "should_not_cache")
self.db.pragma("should_not_cache")
for entry in self.db.cache_stats(include_entries=True)["entries"]:
self.assertNotIn("should_not_cache", entry["query"])
# schema - #524
self.db.pragma("user_version", 7, schema="temp")
self.assertEqual(7, self.db.execute("pragma temp.user_version").get)
self.assertRaisesRegex(apsw.SQLError, ".*unknown database.*", self.db.pragma, "quack", schema='"')
self.db.execute(f"""attach '{self.db.filename}' as '"' """)
self.db.pragma("quack", schema='"')
# check no syntax error because quoting is correct
self.db.pragma(r"""qu\'"`ack""", schema='"')
def testSleep(self):
"apsw.sleep"
apsw.sleep(1)
apsw.sleep(-1)
self.assertRaises(OverflowError, apsw.sleep, 2_500_000_000)
self.assertRaises(TypeError, apsw.sleep, "2_500_000_000")
def testPysqliteRecursiveIssue(self):
"Check an issue that affected pysqlite"
# https://code.google.com/p/pysqlite/source/detail?r=260ee266d6686e0f87b0547c36b68a911e6c6cdb
cur = self.db.cursor()
cur.execute("create table a(x); create table b(y);")
def foo():
yield (1, )
cur.execute("insert into a values(?)", (1, ))
yield (2, )
self.assertRaises(apsw.ThreadingViolationError, cur.executemany, "insert into b values(?)", foo())
def testWriteUnraisable(self):
"Verify writeunraisable replacement function"
def unraise():
# We cause an unraisable error to happen by writing to a
# blob open for reading. The close method called in the
# destructor will then also give the error
db = apsw.Connection(":memory:")
rowid = curnext(db.cursor().execute(
"create table foo(x); insert into foo values(x'aabbccdd'); select rowid from foo"))[0]
blob = db.blob_open("main", "foo", "x", rowid, False)
try:
blob.write(b"badd")
except apsw.ReadOnlyError:
pass
del db
del blob
gc.collect()
# Normal excepthook
self.assertRaisesUnraisable(apsw.ReadOnlyError, unraise)
# excepthook with error to check PyErr_Display is called
xx = sys.excepthook
yy = sys.stderr
sys.stderr = open(TESTFILEPREFIX + "errout.txt", "wt", encoding="utf8")
def ehook(blah):
1 / 0
sys.excepthook = ehook
unraise()
sys.stderr.close()
v = open(TESTFILEPREFIX + "errout.txt", "rt", encoding="utf8").read()
deletefile(TESTFILEPREFIX + "errout.txt")
self.assertTrue(len(v))
sys.excepthook = xx
sys.stderr = yy
def testStatementCache(self, scsize=17):
"Verify statement cache integrity"
self.db.close()
self.db = apsw.Connection(TESTFILEPREFIX + "testdb", statementcachesize=scsize)
self.assertRaises(TypeError, self.db.cache_stats, include_entries="orange")
actual = self.db.cache_stats()["size"]
self.assertTrue(actual == scsize or (scsize > 0 and actual > 0))
cur = self.db.cursor()
cur.execute("create table foo(x,y)")
cur.execute("create index foo_x on foo(x)")
cur.execute("insert into foo values(1,2)")
cur.execute("drop index foo_x")
cur.execute("insert into foo values(1,2)") # cache hit, but needs reprepare
cur.execute("drop table foo; create table foo(x)")
try:
cur.execute("insert into foo values(1,2)") # cache hit, but invalid sql
except apsw.SQLError:
pass
cur.executemany("insert into foo values(?)", [[1], [2]])
# overflow the statement cache
l = [self.db.cursor().execute("select x from foo" + " " * i) for i in range(actual + 200)]
del l
gc.collect()
# coverage
l = []
for i in range(actual * 2 + 10):
l.append(self.db.cursor().execute("select x from foo" + " " * i))
for row in self.db.cursor().execute("select * from foo"):
pass
# other wrangling
l = [self.db.cursor().execute("select x from foo") for i in range(actual + 200)]
for i in range(actual * 2 + 200):
for row in self.db.cursor().execute("select * from foo" + " " * i):
pass
del l
gc.collect()
db2 = apsw.Connection(TESTFILEPREFIX + "testdb", statementcachesize=scsize)
self.assertEqual(db2.cache_stats()["size"], actual)
cur2 = db2.cursor()
cur2.execute("create table bar(x,y)")
for _ in cur.execute("select * from foo"):
pass
db2.close()
# Get some coverage - overflow cache and recycling
l = [self.db.cursor().execute("select 3" + " " * i) for i in range(100 + 256 + 17)]
while l:
l.pop().fetchall()
# embedded nulls
got = []
try:
for row in cur.execute("select 3;select 4\0;select 5"):
got.append(row[0])
except ValueError:
self.assertEqual(got, [3])
# these compile to null vdbe
for _ in range(5): # ensure statement cache is used
for query in (
"",
"-- foo",
";",
"\n",
):
for row in cur.execute(query):
self.fail("Query is empty")
# check with stats
s = self.db.cache_stats()
self.assertEqual(s["size"], min(scsize, 512)) # 512 is current max
s2 = self.db.cache_stats(True)
s2.pop("entries")
self.assertEqual(s, s2)
self.assertEqual(self.db.execute("select 3" + " " * s["max_cacheable_bytes"] + "+1").fetchall(), [(4, )])
self.assertEqual(s["too_big"] + 1, self.db.cache_stats().pop("too_big"))
s = self.db.cache_stats()
self.db.execute("select 997", can_cache=False).fetchall()
self.assertEqual(s["no_cache"] + 1, self.db.cache_stats().pop("no_cache"))
self.db.execute("select 997", can_cache=True).fetchall()
self.assertEqual(s["misses"] + 2, self.db.cache_stats().pop("misses"))
self.db.execute("select 997", can_cache=True).fetchall()
self.assertEqual(s["misses"] + 2 + (1 if not scsize else 0), self.db.cache_stats().pop("misses"))
# prepare_flags
class VTModule:
def Create(self, *args):
return ("create table dontcare(x int)", VTTable())
Connect = Create
class VTTable:
def Open(self):
return VTCursor()
def BestIndex(self, *args):
return None
class VTCursor:
rows = [[99], [100]]
def __init__(self):
self.pos = 0
def Filter(self, *args):
self.pos = 0
def Eof(self):
return self.pos >= len(self.rows)
def Column(self, num):
if num < 0:
return self.pos + 1_000_000
return self.rows[self.pos][num]
def Next(self):
self.pos += 1
def Close(self):
pass
vt = VTModule()
self.db.create_module("fortestingonly", vt)
# no_vtab doesn't block creating a vtab
self.db.execute("create VIRTUAL table fred USING fortestingonly()", prepare_flags=apsw.SQLITE_PREPARE_NO_VTAB)
# make sure query using vtab is identical so cache would be hit
query = "select * from fred"
self.assertEqual(self.db.execute(query).fetchall(), [(99, ), (100, )])
# this should fail (sqlite pretends the vtabs don't exist rather than giving specific error)
self.assertRaises(apsw.SQLError,
self.db.execute,
"select * from fred",
prepare_flags=apsw.SQLITE_PREPARE_NO_VTAB)
def testStatementCacheZeroSize(self):
"Rerun statement cache tests with a zero sized/disabled cache"
self.db = apsw.Connection(TESTFILEPREFIX + "testdb", statementcachesize=-1)
self.testStatementCache(0)
def testStatementCacheLargeSize(self):
"Rerun statement cache tests with a large cache"
self.db = apsw.Connection(TESTFILEPREFIX + "testdb", statementcachesize=17000)
self.testStatementCache(170000)
def testStmtExplain(self):
"Verify sqlite3_stmt_explain operation"
# sanity check
for row in self.db.execute("explain select 3", explain=0):
self.assertEqual(row, (3, ))
for row in self.db.execute("explain query plan select 3", explain=0):
self.assertEqual(row, (3, ))
for row in self.db.execute("explain select 3", explain=1):
self.assertNotEqual(row, (3, ))
is_explain = -99
def ehook(cur, sql, bindings):
nonlocal is_explain
is_explain = cur.is_explain
return True
self.db.exec_trace = ehook
cur = self.db.cursor()
cur.exec_trace = ehook
queries = {"select 3": 0, "explain select 4": 1, "explain query plan select 5": 2}
for target in self.db, cur:
for explain in (-1, 0, 1, 2):
for meth in "execute", "executemany":
bindings = None if meth == "execute" else (tuple(), )
for q, v in queries.items():
is_explain = -99
for row in getattr(target, meth)(q, bindings, explain=explain):
pass
if explain < 0:
self.assertEqual(is_explain, v)
else:
self.assertEqual(is_explain, explain)
self.assertRaises(apsw.SQLError, self.db.execute, "select 6", explain=7)
# the text also includes characters that can't be represented in 16 bits (BMP)
wikipedia_text = """Wikipedia\nThe Free Encyclopedia\nEnglish\n6 383 000+ articles\n日本語\n1 292 000+ 記事\nРусский\n1 756 000+ статей\nDeutsch\n2 617 000+ Artikel\nEspañol\n1 717 000+ artículos\nFrançais\n2 362 000+ articles\nItaliano\n1 718 000+ voci\n中文\n1 231 000+ 條目\nPolski\n1 490 000+ haseł\nPortuguês\n1 074 000+ artigos\nSearch Wikipedia\nEN\nEnglish\n\n Read Wikipedia in your language\n1 000 000+ articles\nPolski\nالعربية\nDeutsch\nEnglish\nEspañol\nFrançais\nItaliano\nمصرى\nNederlands\n日本語\nPortuguês\nРусский\nSinugboanong Binisaya\nSvenska\nУкраїнська\nTiếng Việt\nWinaray\n中文\n100 000+ articles\nAfrikaans\nSlovenčina\nAsturianu\nAzərbaycanca\nБългарски\nBân-lâm-gú / Hō-ló-oē\nবাংলা\nБеларуская\nCatalà\nČeština\nCymraeg\nDansk\nEesti\nΕλληνικά\nEsperanto\nEuskara\nفارسی\nGalego\n한국어\nՀայերեն\nहिन्दी\nHrvatski\nBahasa Indonesia\nעברית\nქართული\nLatina\nLatviešu\nLietuvių\nMagyar\nМакедонски\nBahasa Melayu\nBahaso Minangkabau\nNorskbokmålnynorsk\nНохчийн\nOʻzbekcha / Ўзбекча\nҚазақша / Qazaqşa / قازاقشا\nRomână\nSimple English\nSlovenščina\nСрпски / Srpski\nSrpskohrvatski / Српскохрватски\nSuomi\nதமிழ்\nТатарча / Tatarça\nภาษาไทย\nТоҷикӣ\nتۆرکجه\nTürkçe\nاردو\nVolapük\n粵語\nမြန်မာဘာသာ\n10 000+ articles\nBahsa Acèh\nAlemannisch\nአማርኛ\nAragonés\nBasa Banyumasan\nБашҡортса\nБеларуская (Тарашкевіца)\nBikol Central\nবিষ্ণুপ্রিয়া মণিপুরী\nBoarisch\nBosanski\nBrezhoneg\nЧӑвашла\nDiné Bizaad\nEmigliàn–Rumagnòl\nFøroyskt\nFrysk\nGaeilge\nGàidhlig\nગુજરાતી\nHausa\nHornjoserbsce\nIdo\nIlokano\nInterlingua\nИрон æвзаг\nÍslenska\nJawa\nಕನ್ನಡ\nKreyòl Ayisyen\nKurdî / كوردی\nکوردیی ناوەندی\nКыргызча\nКырык Мары\nLëtzebuergesch\nLimburgs\nLombard\nLìgure\nमैथिली\nMalagasy\nമലയാളം\n文言\nमराठी\nმარგალური\nمازِرونی\nMìng-dĕ̤ng-ngṳ̄ / 閩東語\nМонгол\nनेपाल भाषा\nनेपाली\nNnapulitano\nNordfriisk\nOccitan\nМарий\nଓଡି଼ଆ\nਪੰਜਾਬੀ (ਗੁਰਮੁਖੀ)\nپنجابی (شاہ مکھی)\nپښتو\nPiemontèis\nPlattdüütsch\nQırımtatarca\nRuna Simi\nसंस्कृतम्\nСаха Тыла\nScots\nShqip\nSicilianu\nසිංහල\nسنڌي\nŚlůnski\nBasa Sunda\nKiswahili\nTagalog\nతెలుగు\nᨅᨔ ᨕᨙᨁᨗ / Basa Ugi\nVèneto\nWalon\n吳語\nייִדיש\nYorùbá\nZazaki\nŽemaitėška\nisiZulu\n1 000+ articles\nАдыгэбзэ\nÆnglisc\nAkan\nаԥсшәа\nԱրեւմտահայերէն\nArmãneashce\nArpitan\nܐܬܘܪܝܐ\nAvañe’ẽ\nАвар\nAymar\nBasa Bali\nBahasa Banjar\nभोजपुरी\nBislama\nབོད་ཡིག\nБуряад\nChavacano de Zamboanga\nCorsu\nVahcuengh / 話僮\nDavvisámegiella\nDeitsch\nދިވެހިބަސް\nDolnoserbski\nЭрзянь\nEstremeñu\nFiji Hindi\nFurlan\nGaelg\nGagauz\nGĩkũyũ\nگیلکی\n贛語\nHak-kâ-ngî / 客家語\nХальмг\nʻŌlelo Hawaiʻi\nIgbo\nInterlingue\nKabɩyɛ\nKapampangan\nKaszëbsczi\nKernewek\nភាសាខ្មែរ\nKinyarwanda\nКоми\nKongo\nकोंकणी / Konknni\nKriyòl Gwiyannen\nພາສາລາວ\nDzhudezmo / לאדינו\nЛакку\nLatgaļu\nЛезги\nLingála\nlojban\nLuganda\nMalti\nReo Mā’ohi\nMāori\nMirandés\nМокшень\nߒߞߏ\nNa Vosa Vaka-Viti\nNāhuatlahtōlli\nDorerin Naoero\nNedersaksisch\nNouormand / Normaund\nNovial\nAfaan Oromoo\nঅসমীযা়\nपालि\nPangasinán\nPapiamentu\nПерем Коми\nPfälzisch\nPicard\nКъарачай–Малкъар\nQaraqalpaqsha\nRipoarisch\nRumantsch\nРусиньскый Язык\nGagana Sāmoa\nSardu\nSeeltersk\nSesotho sa Leboa\nChiShona\nSoomaaliga\nSranantongo\nTaqbaylit\nTarandíne\nTetun\nTok Pisin\nfaka Tonga\nTürkmençe\nТыва дыл\nУдмурт\nئۇيغۇرچه\nVepsän\nVõro\nWest-Vlams\nWolof\nisiXhosa\nZeêuws\n100+ articles\nBamanankan\nChamoru\nChichewa\nEʋegbe\nFulfulde\n𐌲𐌿𐍄𐌹𐍃𐌺\nᐃᓄᒃᑎᑐᑦ / Inuktitut\nIñupiak\nKalaallisut\nكٲشُر\nLi Niha\nNēhiyawēwin / ᓀᐦᐃᔭᐍᐏᐣ\nNorfuk / Pitkern\nΠοντιακά\nརྫོང་ཁ\nRomani\nKirundi\nSängö\nSesotho\nSetswana\nСловѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ\nSiSwati\nThuɔŋjäŋ\nᏣᎳᎩ\nTsėhesenėstsestotse\nTshivenḓa\nXitsonga\nchiTumbuka\nTwi\nትግርኛ\nဘာသာ မန်\n"""
assert (any(ord(c) > 65536 for c in wikipedia_text))
def testWikipedia(self):
"Use front page of wikipedia to check unicode handling"
self.db.close()
text = APSW.wikipedia_text
for encoding in "UTF-16", "UTF-16le", "UTF-16be", "UTF-8":
if os.path.exists(TESTFILEPREFIX + "testdb"):
deletefile(TESTFILEPREFIX + "testdb")
db = apsw.Connection(TESTFILEPREFIX + "testdb")
c = db.cursor()
c.execute("pragma encoding=\"%s\"" % (encoding, ))
for row in c.execute("pragma encoding"):
# we use startswith as UTF-16 will be returned with le/be suffix
self.assertTrue(row[0].startswith(encoding))
c.execute("create table foo(x); insert into foo values(?)", (text, ))
for row in c.execute("select * from foo"):
self.assertEqual(row[0], text)
db.close()
# calls that need protection
calls={
'sqlite3api': { # items of interest - sqlite3 calls
'match': re.compile(r"(sqlite3_[A-Za-z0-9_]+)\s*\("),
# what must also be on same or preceding line
'needs': re.compile("PYSQLITE(_|_BLOB_|_CON_|_CUR_|_SC_|_VOID_|_BACKUP_)CALL"),
# except if match.group(1) matches this - these don't
# acquire db mutex so no need to wrap (determined by
# examining sqlite3.c). If they acquire non-database
# mutexes then that is ok.
# In the case of sqlite3_result_*|declare_vtab, the mutex
# is already held by enclosing sqlite3_step and the
# methods will only be called from that same thread so it
# isn't a problem.
'skipcalls': re.compile("^sqlite3_(blob_bytes|column_count|bind_parameter_count|data_count|vfs_.+|changes64|total_changes64"
"|bind_parameter_name"
"|get_" "autocommit|last_insert_rowid|complete|interrupt|limit|malloc64|free|threadsafe|value_.+"
"|libversion|enable_" "shared_cache|initialize|shutdown|config|memory_.+|soft_heap_limit64|hard_heap_limit64"
"|randomness|db_readonly|db_filename|release_" "memory|status64|result_.+|user_data|mprintf|aggregate_context"
"|declare_vtab|backup_remaining|backup_pagecount|mutex_enter|mutex_leave|sourceid|uri_.+"
"|column_name|column_decltype|column_database_name|column_table_name|column_origin_name"
"|stmt_isexplain|stmt_readonly|filename_journal|filename_wal|stmt_status|sql|log|vtab_collation"
"|vtab_rhs_value|vtab_distinct|vtab_config|vtab_on_conflict|vtab_in_first|vtab_in_next|vtab_in"
"|vtab_nochange|is_interrupted|extended_errcode)$"),
# error message
'desc': "sqlite3_ calls must wrap with PYSQLITE_CALL",
},
'inuse': {
'match': re.compile(r"(convert_column_to_pyobject|statementcache_prepare|statementcache_finalize|statementcache_next)\s*\("),
'needs': re.compile("INUSE_CALL"),
'desc': "call needs INUSE wrapper",
"skipfiles": re.compile(r".*[/\\]statementcache.c$"),
},
}
def sourceCheckMutexCall(self, filename, name, lines):
# we check that various calls are wrapped with various macros
for i, line in enumerate(lines):
if "PYSQLITE_CALL" in line and "Py" in line:
self.fail("%s: %s() line %d - Py call while GIL released - %s" % (filename, name, i, line.strip()))
for k, v in self.calls.items():
if v.get('skipfiles', None) and v['skipfiles'].match(filename):
continue
mo = v['match'].search(line)
if mo:
func = mo.group(1)
if v.get('skipcalls', None) and v['skipcalls'].match(func):
continue
if not v["needs"].search(line) and not v["needs"].search(lines[i - 1]):
self.fail("%s: %s() line %d call to %s(): %s - %s\n" %
(filename, name, i, func, v['desc'], line.strip()))
def sourceCheckFunction(self, filename, name, lines):
# existing exception in callbacks
if any("PyGILState_Ensure" in line for line in lines):
if not any("MakeExistingException" in line for line in lines):
self.fail(
f"file { filename } function { name } calls PyGILState_Ensure but does not have MakeExistingException"
)
# not further checked
if name.split("_")[0] in ("ZeroBlobBind", "APSWVFS", "APSWVFSFile", "APSWBuffer", "FunctionCBInfo"):
return
checks = {
"APSWCursor": {
"skip": ("dealloc", "init", "dobinding", "dobindings", "do_exec_trace", "do_row_trace", "step", "close",
"close_internal", "tp_traverse", "tp_str"),
"req": {
"use": "CHECK_USE",
"closed": "CHECK_CURSOR_CLOSED",
},
"order": ("use", "closed")
},
"Connection": {
"skip": ("internal_cleanup", "dealloc", "init", "close", "interrupt", "close_internal",
"remove_dependent", "readonly", "getmainfilename", "db_filename", "traverse", "clear",
"tp_traverse", "get_cursor_factory", "set_cursor_factory", "tp_str"),
"req": {
"use": "CHECK_USE",
"closed": "CHECK_CLOSED",
},
"order": ("use", "closed")
},
"APSWBlob": {
"skip": ("dealloc", "init", "close", "close_internal", "tp_str"),
"req": {
"use": "CHECK_USE",
"closed": "CHECK_BLOB_CLOSED"
},
"order": ("use", "closed")
},
"APSWBackup": {
"skip": ("dealloc", "init", "close_internal", "get_remaining", "get_page_count", "tp_str"),
"req": {
"use": "CHECK_USE",
"closed": "CHECK_BACKUP_CLOSED"
},
"order": ("use", "closed")
},
"apswvfs": {
"req": {
"preamble": "VFSPREAMBLE",
"tb": "AddTraceBackHere",
"postamble": "VFSPOSTAMBLE"
},
"order": ("preamble", "tb", "postamble")
},
"apswvfspy": {
"req": {
"check": "CHECKVFSPY",
"notimpl": "VFSNOTIMPLEMENTED(%(base)s,"
},
"order": ("check", "notimpl"),
},
"apswvfspy_unregister": {
"req": {
"check": "CHECKVFSPY",
},
},
"apswvfsfile": {
"req": {
"preamble": "FILEPREAMBLE",
"postamble": "FILEPOSTAMBLE",
},
"order": ("preamble", "postamble")
},
"apswvfsfilepy": {
"skip": ("xClose", ),
"req": {
"check": "CHECKVFSFILEPY",
"notimpl": "VFSFILENOTIMPLEMENTED(%(base)s,"
},
"order": ("check", "notimpl"),
},
"SqliteIndexInfo": {
"req": {
"check": "CHECK_INDEX",
},
},
"apswfcntl": {
"req": {}
},
"apswurifilename": {
"req": {
"check": "CHECK_SCOPE"
}
}
}
prefix, base = name.split("_", 1)
if name in checks:
checker = checks[name]
elif prefix in checks:
checker = checks[prefix]
else:
self.fail(filename + ": " + prefix + " not in checks (" + name + ")")
if base in checker.get("skip", ()):
return
format = {"base": base, "prefix": prefix}
found = {}
for k in checker["req"]:
found[k] = None
# check the lines
for i, line in enumerate(lines):
for k, v in checker["req"].items():
v = v % format
if v in line and found[k] is None:
found[k] = i
# check they are present
for k, v in checker["req"].items():
if found[k] is None:
v = v % format
self.fail(filename + ": " + k + " " + v + " missing in " + name)
# check order
order = checker.get("order", ())
for i in range(len(order) - 2):
b4 = order[i]
after = order[i + 1]
if found[b4] > found[after]:
self.fail(filename + ": " + checker["req"][b4] % format + " should be before " +
checker["req"][after] % format + " in " + name)
return
should_use_compat = ("PyObject_CheckReadBuffer", "PyObject_AsReadBuffer")
def testSourceChecks(self):
"Check various source code issues"
# We expect a coding style where the functions are named
# Object_method, are at the start of the line and have a first
# parameter named self.
for filename in glob.glob("src/*.c"):
if filename.endswith("testextension.c") or filename.endswith("constants.c"):
continue
# check not using C++ style comments
code = read_whole_file(filename, "rt").replace("http://", "http:__").replace("https://", "https:__")
if "//" in code:
self.fail("// style comment in " + filename)
if filename.replace("\\", "/") != "src/pyutil.c":
for n in self.should_use_compat:
if n in code:
self.fail("Should be using compat function for %s in file %s" % (n, filename))
# not allowed PyObject_New because we can't faultinject it
if re.search(r"\bPyObject_New\b", code):
self.fail(f"In { filename } you must use _PyObject_New (leading underscore)")
# check check funcs
funcpat1 = re.compile(r"^(\w+_\w+)\s*\(\s*\w+\s*\*\s*self")
funcpat2 = re.compile(r"^(\w+)\s*\(")
name1 = None
name2 = None
lines = []
infunc = 0
for line in read_whole_file(filename, "rt").split("\n"):
if line.startswith("}") and infunc:
if infunc == 1:
self.sourceCheckMutexCall(filename, name1, lines)
self.sourceCheckFunction(filename, name1, lines)
elif infunc == 2:
self.sourceCheckMutexCall(filename, name2, lines)
else:
assert False
infunc = 0
lines = []
name1 = None
name2 = None
continue
if name1 and line.startswith("{"):
infunc = 1
continue
if name2 and line.startswith("{"):
infunc = 2
continue
if infunc:
lines.append(line)
continue
m = funcpat1.match(line)
if m:
name1 = m.group(1)
continue
m = funcpat2.match(line)
if m:
name2 = m.group(1)
continue
def testConfig(self):
"Verify sqlite3_config wrapper"
# we need to ensure there are no outstanding sqlite objects
self.db = None
gc.collect()
self.assertRaises(apsw.MisuseError, apsw.config, apsw.SQLITE_CONFIG_MEMSTATUS, True)
self.assertEqual(0, len(apsw.connections()))
apsw.shutdown()
try:
self.assertRaises(TypeError, apsw.config)
self.assertRaises(TypeError, apsw.config, "chicken")
apsw.config(apsw.SQLITE_CONFIG_SINGLETHREAD)
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_SINGLETHREAD, 2)
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_MEMSTATUS)
apsw.config(apsw.SQLITE_CONFIG_MEMSTATUS, True)
apsw.config(apsw.SQLITE_CONFIG_MEMSTATUS, False)
self.assertRaises(TypeError, apsw.config, 89748937)
x = 0x7fffffff
self.assertRaises(OverflowError, apsw.config, x * x * x * x)
self.assertTrue(apsw.config(apsw.SQLITE_CONFIG_PCACHE_HDRSZ) >= 0)
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_PCACHE_HDRSZ, "extra")
apsw.config(apsw.SQLITE_CONFIG_PMASZ, -1)
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_MMAP_SIZE, "3")
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_MMAP_SIZE, 3, "3")
self.assertRaises(OverflowError, apsw.config, apsw.SQLITE_CONFIG_MMAP_SIZE, 2**65, 2**65)
apsw.config(apsw.SQLITE_CONFIG_MMAP_SIZE, 2**63 - 1, 2**63 - 1)
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_MEMDB_MAXSIZE, "3")
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_MEMDB_MAXSIZE, 3, "3")
self.assertRaises(OverflowError, apsw.config, apsw.SQLITE_CONFIG_MEMDB_MAXSIZE, 2**65)
apsw.config(apsw.SQLITE_CONFIG_MEMDB_MAXSIZE, 2**63 - 1)
finally:
# put back to normal
apsw.config(apsw.SQLITE_CONFIG_SERIALIZED)
apsw.config(apsw.SQLITE_CONFIG_MEMSTATUS, True)
apsw.initialize()
def testFaultInjectionTested(self):
"Make sure all fault injection is tested"
faults = set()
for filename in glob.glob("src/*.c"):
with open(filename, "rt", encoding="utf8") as f:
for line in f:
if "APSW_FAULT_INJECT" in line and "#define" not in line:
mo = re.match(r".*APSW_FAULT_INJECT\s*\(\s*(?P<name>\w+)\s*,.*", line)
assert mo, f"Failed to match line { line }"
name = mo.group("name")
assert name not in faults, f"fault inject name { name } found multiple times"
faults.add(name)
testcode = read_whole_file(__file__, "rt", "utf8")
for name in sorted(faults):
self.assertTrue(re.search(f"\\b{ name }\\b", testcode), f"Couldn't find test for fault '{ name }'")
def testMemory(self):
"Verify memory tracking functions"
self.assertNotEqual(apsw.memory_used(), 0)
self.assertTrue(apsw.memory_high_water() >= apsw.memory_used())
self.assertRaises(TypeError, apsw.memory_high_water, "eleven")
apsw.memory_high_water(True)
self.assertEqual(apsw.memory_high_water(), apsw.memory_used())
self.assertRaises(TypeError, apsw.soft_heap_limit, 1, 2)
self.assertRaises(TypeError, apsw.hard_heap_limit, 1, 2)
apsw.soft_heap_limit(0)
self.assertRaises(TypeError, apsw.release_memory, 1, 2)
res = apsw.release_memory(0x7fffffff)
self.assertTrue(type(res) in (int, ))
apsw.soft_heap_limit(0x1234567890abc)
self.assertEqual(0x1234567890abc, apsw.soft_heap_limit(0x1234567890abe))
apsw.hard_heap_limit(0x1234567890abd)
self.assertEqual(0x1234567890abd, apsw.hard_heap_limit(0x1234567890abe))
def testRandomness(self):
"Verify randomness routine"
self.assertRaises(TypeError, apsw.randomness, "three")
self.assertRaises(OverflowError, apsw.randomness, 0xffffffffee)
self.assertRaises(ValueError, apsw.randomness, -2)
self.assertEqual(0, len(apsw.randomness(0)))
self.assertEqual(1, len(apsw.randomness(1)))
self.assertEqual(16383, len(apsw.randomness(16383)))
self.assertNotEqual(apsw.randomness(77), apsw.randomness(77))
def testSqlite3Pointer(self):
"Verify getting underlying sqlite3 pointer"
self.assertRaises(TypeError, self.db.sqlite3_pointer, 7)
self.assertTrue(type(self.db.sqlite3_pointer()) in (int, ))
self.assertEqual(self.db.sqlite3_pointer(), self.db.sqlite3_pointer())
self.assertNotEqual(self.db.sqlite3_pointer(), apsw.Connection(":memory:").sqlite3_pointer())
def testPickle(self):
"Verify data etc can be pickled"
PicklingError = pickle.PicklingError
# work out what protocol versions we can use
versions = []
for num in range(-1, 20):
try:
pickle.dumps(3, num)
versions.append(num)
except ValueError:
pass
# some objects to try pickling
vals = test_types_vals
cursor = self.db.cursor()
cursor.execute("create table if not exists t(i,x)")
def canpickle(val):
return True
cursor.execute("BEGIN")
cursor.executemany("insert into t values(?,?)", [(i, v) for i, v in enumerate(vals) if canpickle(v)])
cursor.execute("COMMIT")
for ver in versions:
for row in cursor.execute("select * from t"):
self.assertEqual(row, pickle.loads(pickle.dumps(row, ver)))
rownum, val = row
if type(vals[rownum]) is float:
self.assertAlmostEqual(vals[rownum], val)
else:
self.assertEqual(vals[rownum], val)
# can't pickle cursors
try:
pickle.dumps(cursor, ver)
except TypeError:
pass
except PicklingError:
pass
# some versions can pickle the db, but give a zeroed db back
db = None
try:
db = pickle.loads(pickle.dumps(self.db, ver))
except TypeError:
pass
if db is not None:
self.assertRaises(apsw.ConnectionClosedError, db.db_filename, "main")
self.assertRaises(apsw.ConnectionClosedError, db.cursor)
self.assertRaises(apsw.ConnectionClosedError, db.get_autocommit)
self.assertRaises(apsw.ConnectionClosedError, db.in_transaction)
def testDropModules(self):
"Verify dropping virtual table modules"
# simplest implementation possible
class Source:
def Create(self, db, modulename, dbname, tablename, *args):
return "create table placeholder(x)", Source.Table()
Connect = Create
class Table:
def BestIndex(*args):
return
def BestIndexObject(*args):
return True
def Open(*args):
return Source.Cursor()
class Cursor:
def Eof(*args):
return True
def Filter(*args):
pass
Close = Filter
counter = 0
def check_module(name: str, shouldfail: bool) -> None:
nonlocal counter
counter += 1
errs = []
# eponymous only will have no_module even when they exist
no_module = False
no_table = False
try:
self.db.execute(f"create virtual table ex{ counter } using { name }()")
except apsw.SQLError as e:
no_module = f"no such module: { name }" in str(e)
try:
self.db.execute(f"select * from { name }()")
except apsw.SQLError as e:
no_table = f"no such table: { name }" in str(e)
if shouldfail:
self.assertTrue(no_module and no_table)
else:
self.assertFalse(no_module and no_table)
self.db.create_module("abc", Source())
check_module("abc", False)
self.db.create_module("abc", None) # should drop the table
check_module("abc", True)
# we register a whole bunch, and then unregister subsets
args = []
for use_bestindex_object in (False, True):
for iVersion in (1, 2, 3):
for eponymous in (False, True):
for eponymous_only in (False, True):
for read_only in (False, True):
args.append({
"use_bestindex_object": use_bestindex_object,
"iVersion": iVersion,
"eponymous": eponymous,
"eponymous_only": eponymous_only,
"read_only": read_only
})
names = list("".join(x) for x in itertools.permutations("abcd"))
for n in names:
try:
self.db.create_module(n, Source(), **random.choice(args))
except Exception as e:
self.assertIn("No xShadowName slots are available.", str(e))
continue
check_module(n, False)
random.shuffle(names)
for i in range(len(names), -1, -1):
if not names:
break
keep = random.sample(names, i)
self.db.drop_modules(keep)
for n in names:
check_module(n, n not in keep)
check_module("madeup", True)
names = keep
self.assertRaises(TypeError, self.db.drop_modules)
self.assertRaises(TypeError, self.db.drop_modules, ["one", 2, "three"])
def testStatus(self):
"Verify status function"
self.assertRaises(TypeError, apsw.status, "zebra")
self.assertRaises(apsw.MisuseError, apsw.status, 2323)
for i in apsw.mapping_status:
if type(i) != type(""): continue
res = apsw.status(getattr(apsw, i))
self.assertEqual(len(res), 2)
self.assertEqual(type(res), tuple)
self.assertTrue(res[0] <= res[1])
def testDBStatus(self):
"Verify db status function"
self.assertRaises(TypeError, self.db.status, "zebra")
self.assertRaises(apsw.SQLError, self.db.status, 2323)
for i in apsw.mapping_db_status:
if type(i) != type(""): continue
res = self.db.status(getattr(apsw, i))
self.assertEqual(len(res), 2)
self.assertEqual(type(res), tuple)
self.assertTrue(res[1] == 0 or res[0] <= res[1])
def testTxnState(self):
"Verify db.txn_state"
n = "\u1234\u3454324"
self.assertRaises(TypeError, self.db.txn_state, 3)
self.assertEqual(apsw.mapping_txn_state["SQLITE_TXN_NONE"], self.db.txn_state())
self.db.cursor().execute("BEGIN EXCLUSIVE")
self.assertEqual(apsw.mapping_txn_state["SQLITE_TXN_WRITE"], self.db.txn_state())
self.db.cursor().execute("END")
self.assertEqual(apsw.mapping_txn_state["SQLITE_TXN_NONE"], self.db.txn_state())
self.assertRaises(ValueError, self.db.txn_state, n)
self.assertEqual(apsw.mapping_txn_state["SQLITE_TXN_NONE"], self.db.txn_state("main"))
def testZeroBlob(self):
"Verify handling of zero blobs"
self.assertRaises(TypeError, apsw.zeroblob)
self.assertRaises(TypeError, apsw.zeroblob, "foo")
self.assertRaises(TypeError, apsw.zeroblob, -7)
self.assertRaises(apsw.TooBigError, self.db.execute, "select ?", (apsw.zeroblob(4000000000), ))
cur = self.db.cursor()
cur.execute("create table foo(x)")
cur.execute("insert into foo values(?)", (apsw.zeroblob(27), ))
v = curnext(cur.execute("select * from foo"))[0]
self.assertEqual(v, b"\x00" * 27)
# Make sure inheritance works
class multi:
def __init__(self, *args):
self.foo = 3
class derived(apsw.zeroblob):
def __init__(self, num):
#multi.__init__(self)
apsw.zeroblob.__init__(self, num)
cur.execute("delete from foo; insert into foo values(?)", (derived(28), ))
v = curnext(cur.execute("select * from foo"))[0]
self.assertEqual(v, b"\x00" * 28)
self.assertEqual(apsw.zeroblob(91210).length(), 91210)
def testBlobIO(self):
"Verify Blob input/output"
cur = self.db.cursor()
rowid = curnext(
cur.execute("create table foo(x blob); insert into foo values(zeroblob(98765)); select rowid from foo"))[0]
self.assertRaises(TypeError, self.db.blob_open, 1)
self.assertRaises(TypeError, self.db.blob_open, "main", "foo\xf3")
self.assertRaises(TypeError, self.db.blob_open, "main", "foo", "x", complex(-1, -1), True)
self.assertRaises(TypeError, self.db.blob_open, "main", "foo", "x", rowid, True, False)
self.assertRaises(apsw.SQLError, self.db.blob_open, "main", "foo", "x", rowid + 27, False)
self.assertRaises(apsw.SQLError, self.db.blob_open, "foo", "foo", "x", rowid, False)
self.assertRaises(apsw.SQLError, self.db.blob_open, "main", "x", "x", rowid, False)
self.assertRaises(apsw.SQLError, self.db.blob_open, "main", "foo", "y", rowid, False)
blobro = self.db.blob_open("main", "foo", "x", rowid, False)
# sidebar: check they can't be manually created
self.assertRaises(TypeError, type(blobro))
# check vals
self.assertEqual(blobro.length(), 98765)
self.assertEqual(blobro.length(), 98765)
self.assertEqual(blobro.read(0), b"")
zero = b"\x00"
step = 5 # must be exact multiple of size
assert (blobro.length() % step == 0)
for i in range(0, 98765, step):
x = blobro.read(step)
self.assertEqual(zero * step, x)
x = blobro.read(10)
self.assertEqual(x, b"")
blobro.seek(0, 1)
self.assertEqual(blobro.tell(), 98765)
blobro.seek(0)
self.assertEqual(blobro.tell(), 0)
self.assertEqual(len(blobro.read(11119999)), 98765)
blobro.seek(2222)
self.assertEqual(blobro.tell(), 2222)
blobro.seek(0, 0)
self.assertEqual(blobro.tell(), 0)
self.assertEqual(blobro.read(), b"\x00" * 98765)
blobro.seek(-3, 2)
self.assertEqual(blobro.read(), b"\x00" * 3)
# check types
self.assertRaises(TypeError, blobro.read, "foo")
self.assertRaises(TypeError, blobro.tell, "foo")
self.assertRaises(TypeError, blobro.seek)
self.assertRaises(TypeError, blobro.seek, "foo", 1)
self.assertRaises(TypeError, blobro.seek, 0, 1, 2)
self.assertRaises(ValueError, blobro.seek, 0, -3)
self.assertRaises(ValueError, blobro.seek, 0, 3)
# can't seek before beginning or after end of file
self.assertRaises(ValueError, blobro.seek, -1, 0)
self.assertRaises(ValueError, blobro.seek, 25, 1)
self.assertRaises(ValueError, blobro.seek, 25, 2)
self.assertRaises(ValueError, blobro.seek, 100000, 0)
self.assertRaises(ValueError, blobro.seek, -100000, 1)
self.assertRaises(ValueError, blobro.seek, -100000, 2)
# close testing
blobro.seek(0, 0)
self.assertRaises(apsw.ReadOnlyError, blobro.write, b"kermit was here")
# you get the error on the close too, and blob is always closed - sqlite ticket #2815
self.assertRaises(apsw.ReadOnlyError, blobro.close)
# check can't work on closed blob
self.assertRaises(ValueError, blobro.read)
self.assertRaises(ValueError, blobro.read_into, b"ab")
self.assertRaises(ValueError, blobro.seek, 0, 0)
self.assertRaises(ValueError, blobro.tell)
self.assertRaises(ValueError, blobro.write, "abc")
# read_into tests
rowidri = self.db.cursor().execute(
"insert into foo values(x'112233445566778899aabbccddeeff'); select last_insert_rowid()").fetchall()[0][0]
blobro = self.db.blob_open("main", "foo", "x", rowidri, False)
self.assertRaises(TypeError, blobro.read_into)
self.assertRaises(TypeError, blobro.read_into, 3)
buffers = []
buffers.append(array.array("b", b"\0\0\0\0"))
buffers.append(bytearray(b"\0\0\0\0"))
# bytearray returns ints rather than chars so a fixup
def _fixup(c):
if type(c) == int:
return bytes([c])
return c
for buf in buffers:
self.assertRaises(TypeError, blobro.read_into)
self.assertRaises(TypeError, blobro.read_into, buf, buf)
self.assertRaises(TypeError, blobro.read_into, buf, 1, buf)
self.assertRaises(TypeError, blobro.read_into, buf, 1, 1, buf)
blobro.seek(0)
blobro.read_into(buf, 1, 1)
self.assertEqual(_fixup(buf[0]), b"\x00")
self.assertEqual(_fixup(buf[1]), b"\x11")
self.assertEqual(_fixup(buf[2]), b"\x00")
self.assertEqual(_fixup(buf[3]), b"\x00")
self.assertEqual(len(buf), 4)
blobro.seek(3)
blobro.read_into(buf)
def check_unchanged():
self.assertEqual(_fixup(buf[0]), b"\x44")
self.assertEqual(_fixup(buf[1]), b"\x55")
self.assertEqual(_fixup(buf[2]), b"\x66")
self.assertEqual(_fixup(buf[3]), b"\x77")
self.assertEqual(len(buf), 4)
check_unchanged()
blobro.seek(14)
# too much requested
self.assertRaises(ValueError, blobro.read_into, buf, 1)
check_unchanged()
# bounds errors
self.assertRaises(ValueError, blobro.read_into, buf, 1, -1)
self.assertRaises(ValueError, blobro.read_into, buf, 1, 7)
self.assertRaises(ValueError, blobro.read_into, buf, -1, 2)
self.assertRaises(ValueError, blobro.read_into, buf, 10000, 2)
self.assertRaises(OverflowError, blobro.read_into, buf, 1, 45236748972389749283)
check_unchanged()
# get a read error
blobro.seek(0)
self.db.cursor().execute("update foo set x=x'112233445566' where rowid=?", (rowidri, ))
self.assertRaises(apsw.AbortError, blobro.read_into, buf)
# should fail with buffer being a string
self.assertRaises(TypeError, blobro.read_into, "abcd", 1, 1)
self.assertRaises(TypeError, blobro.read_into, "abcd", 1, 1)
# write tests
blobrw = self.db.blob_open("main", "foo", "x", rowid, True)
self.assertEqual(blobrw.length(), 98765)
blobrw.write(b"abcd")
blobrw.seek(0, 0)
self.assertEqual(blobrw.read(4), b"abcd")
blobrw.write(b"efg")
blobrw.seek(0, 0)
self.assertEqual(blobrw.read(7), b"abcdefg")
blobrw.seek(50, 0)
blobrw.write(b"hijkl")
blobrw.seek(-98765, 2)
self.assertEqual(blobrw.read(55), b"abcdefg" + b"\x00" * 43 + b"hijkl")
self.assertRaises(TypeError, blobrw.write, 12)
self.assertRaises(TypeError, blobrw.write)
self.assertRaises(TypeError, blobrw.write, "foo")
# try to go beyond end
self.assertRaises(ValueError, blobrw.write, b" " * 100000)
self.assertRaises(TypeError, blobrw.close, "elephant")
# coverage
blobro = self.db.blob_open("main", "foo", "x", rowid, False)
self.assertRaises(apsw.ReadOnlyError, blobro.write, b"abcd")
blobro.close(True)
self.db.cursor().execute("insert into foo(_rowid_, x) values(99, 1)")
blobro = self.db.blob_open("main", "foo", "x", rowid, False)
self.assertRaises(TypeError, blobro.reopen)
self.assertRaises(TypeError, blobro.reopen, "banana")
self.assertRaises(OverflowError, blobro.reopen, 45236748972389749283)
first = blobro.read(2)
# check position is reset
blobro.reopen(rowid)
self.assertEqual(blobro.tell(), 0)
self.assertEqual(first, blobro.read(2))
# invalid reopen
self.assertRaises(apsw.SQLError, blobro.reopen, 0x1ffffffff)
blobro.close()
def testBlobReadError(self):
"Ensure blob read errors are handled well"
cur = self.db.cursor()
cur.execute("create table ioerror (x, blob)")
cur.execute("insert into ioerror (rowid,x,blob) values (2,3,x'deadbeef')")
blob = self.db.blob_open("main", "ioerror", "blob", 2, False)
blob.read(1)
# Do a write which cause blob to become invalid
cur.execute("update ioerror set blob='fsdfdsfasd' where x=3")
try:
blob.read(1)
1 / 0
except:
klass, value = sys.exc_info()[:2]
self.assertTrue(klass is apsw.AbortError)
def testAutovacuumPages(self):
self.assertRaises(TypeError, self.db.autovacuum_pages)
self.assertRaises(TypeError, self.db.autovacuum_pages, 3)
for stmt in ("pragma page_size=512", "pragma auto_vacuum=FULL", "create table foo(x)", "begin"):
self.db.cursor().execute(stmt)
self.db.cursor().executemany("insert into foo values(zeroblob(1023))", [tuple() for _ in range(500)])
self.db.cursor().execute("commit")
rowids = [row[0] for row in self.db.cursor().execute("select ROWID from foo")]
last_free = [0]
def avpcb(schema, nPages, nFreePages, nBytesPerPage):
self.assertEqual(schema, "main")
self.assertTrue(nFreePages < nPages)
self.assertTrue(nFreePages >= 2)
self.assertEqual(nBytesPerPage, 512)
# we always return 1, so second call must have more free pages than first
if last_free[0]:
self.assertTrue(nFreePages > last_free[0])
else:
last_free[0] = nFreePages
return 1
def noparams():
pass
def badreturn(*args):
return "seven"
self.db.cursor().execute("delete from foo where rowid=?", (rowids.pop(), ))
self.db.autovacuum_pages(noparams)
self.assertRaises(TypeError, self.db.cursor().execute, "delete from foo where rowid=?", (rowids.pop(), ))
self.db.autovacuum_pages(None)
self.db.cursor().execute("delete from foo where rowid=?", (rowids.pop(), ))
self.db.autovacuum_pages(avpcb)
self.db.cursor().execute("delete from foo where rowid=?", (rowids.pop(), ))
self.db.autovacuum_pages(badreturn)
self.assertRaises(TypeError, self.db.cursor().execute, "delete from foo where rowid=?", (rowids.pop(), ))
self.db.autovacuum_pages(None)
self.db.cursor().execute("delete from foo where rowid=?", (rowids.pop(), ))
def testTraceV2(self):
"Connection.trace_v2"
self.assertRaises(TypeError, self.db.trace_v2, "abc", "abc")
self.assertRaises(TypeError, self.db.trace_v2, 1, "abc")
self.assertRaises(ValueError, self.db.trace_v2, 1, None)
self.assertRaises(ValueError, self.db.trace_v2, 0, lambda x: x)
self.assertRaises(ValueError, self.db.trace_v2, 0xffffff, lambda x: x)
results = []
def tracecb(data):
results.append(data)
query = "select 3"
self.db.trace_v2(apsw.SQLITE_TRACE_STMT, tracecb)
for _ in self.db.execute(query):
pass
self.assertEqual(1, len(results))
x = results.pop()
self.assertEqual(apsw.SQLITE_TRACE_STMT, x["code"])
self.assertIs(self.db, x["connection"])
self.assertEqual(query, x["sql"])
self.db.trace_v2(apsw.SQLITE_TRACE_ROW, tracecb)
for _ in self.db.execute(query):
pass
self.assertEqual(1, len(results))
x = results.pop()
self.assertEqual(apsw.SQLITE_TRACE_ROW, x["code"])
self.assertIs(self.db, x["connection"])
self.assertEqual(query, x["sql"])
self.db.trace_v2(apsw.SQLITE_TRACE_PROFILE, tracecb)
for _ in self.db.execute(query):
time.sleep(0.1)
self.assertEqual(1, len(results))
x = results.pop()
self.assertEqual(apsw.SQLITE_TRACE_PROFILE, x["code"])
self.assertIs(self.db, x["connection"])
self.assertEqual(query, x["sql"])
self.assertGreater(x["nanoseconds"], 0)
self.assertIn("stmt_status", x)
for n in apsw.mapping_statement_status:
if isinstance(n, str):
self.assertGreaterEqual(x["stmt_status"][n], 0)
db2 = apsw.Connection("")
db2.trace_v2(apsw.SQLITE_TRACE_CLOSE, tracecb)
for _ in db2.execute(query):
pass
db2.close()
self.assertEqual(1, len(results))
x = results.pop()
self.assertEqual(apsw.SQLITE_TRACE_CLOSE, x["code"])
self.assertIs(db2, x["connection"])
def tracehook(x):
1 / 0
self.db.trace_v2(apsw.SQLITE_TRACE_STMT, tracehook)
self.assertRaisesUnraisable(ZeroDivisionError, self.db.execute, query)
self.assertEqual(0, len(results))
def testURIFilenames(self):
assertRaises = self.assertRaises
assertEqual = self.assertEqual
class TVFS(apsw.VFS):
def __init__(self):
apsw.VFS.__init__(self, "uritest", "")
def xOpen(self, name, flags):
assert isinstance(name, apsw.URIFilename)
# The various errors
assertRaises(TypeError, name.uri_parameter)
assertRaises(TypeError, name.uri_parameter, 2)
assertRaises(TypeError, name.uri_int)
assertRaises(TypeError, name.uri_int, 7)
assertRaises(TypeError, name.uri_int, 7, 7)
assertRaises(TypeError, name.uri_int, 7, 7, 7)
assertRaises(TypeError, name.uri_int, "seven", "seven")
assertRaises(TypeError, name.uri_boolean, "seven")
assertRaises(TypeError, name.uri_boolean, "seven", "seven")
assertRaises(TypeError, name.uri_boolean, "seven", None)
# Check values
assert name.filename().endswith("testdb2")
assertEqual(name.uri_parameter("notexist"), None)
assertEqual(name.uri_parameter("foo"), "1&2=3")
assertEqual(name.uri_int("foo", -7), -7)
assertEqual(name.uri_int("bar", -7), 43242342)
# https://sqlite.org/src/info/5f41597f7c
# assertEqual(name.uri_boolean("foo", False), False)
assertEqual(name.uri_boolean("bam", False), True)
assertEqual(name.uri_boolean("baz", True), False)
assertRaises(AttributeError, setattr, name, "parameters", 3)
assertEqual(name.parameters, ('foo', 'bar', 'bam', 'baz'))
1 / 0
testvfs = TVFS()
self.assertRaises(ZeroDivisionError,
apsw.Connection,
"file:testdb2?foo=1%262%3D3&bar=43242342&bam=true&baz=fal%73%65",
flags=apsw.SQLITE_OPEN_READWRITE | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_URI,
vfs="uritest")
def testVFSWithWAL(self):
"Verify VFS using WAL"
apsw.connection_hooks.append(
lambda c: c.cursor().execute("pragma journal_mode=WAL; PRAGMA wal_autocheckpoint=1").fetchall())
try:
self.testVFS()
finally:
apsw.connection_hooks.pop()
def testVFS(self):
"Verify VFS functionality"
global testtimeout
testdb = vfstestdb
# some coverage related stuff
self.assertRaises(TypeError, apsw.VFSFile, "", 3, [0, 0])
self.assertRaises(TypeError, apsw.VFS, "ignored", base=7)
self.assertRaises(TypeError, apsw.VFS, 7)
self.assertRaises(TypeError, apsw.VFS, "ignored", maxpathname="seven")
self.assertRaises(TypeError, apsw.VFS, "ignored", iVersion="seven")
self.assertRaises(ValueError, apsw.VFS, "ignored", iVersion=-2)
self.assertRaises(TypeError, apsw.VFS, "ignored", exclude=2)
self.assertNotIn("ignored", apsw.vfs_names())
# Check basic functionality and inheritance - make an obfuscated provider
# obfusvfs code
def encryptme(data):
# An "encryption" scheme in honour of MAPI and SQL server passwords
if not data: return data
return bytes([x ^ 0xa5 for x in data])
class ObfuscatedVFSFile(apsw.VFSFile):
def __init__(self, inheritfromvfsname, filename, flags):
apsw.VFSFile.__init__(self, inheritfromvfsname, filename, flags)
def xRead(self, amount, offset):
return encryptme(super().xRead(amount, offset))
def xWrite(self, data, offset):
super().xWrite(encryptme(data), offset)
class ObfuscatedVFS(apsw.VFS):
def __init__(self, vfsname="obfu", basevfs=""):
self.vfsname = vfsname
self.basevfs = basevfs
apsw.VFS.__init__(self, self.vfsname, self.basevfs, exclude=None)
def xOpen(self, name, flags):
return ObfuscatedVFSFile(self.basevfs, name, flags)
vfs = ObfuscatedVFS()
query = "create table foo(x,y); insert into foo values(1,2); insert into foo values(3,4)"
self.db.cursor().execute(query)
db2 = apsw.Connection(TESTFILEPREFIX + "testdb2", vfs=vfs.vfsname)
db2.cursor().execute(query)
db2.close()
self.db.cursor().execute("pragma journal_mode=delete").fetchall()
self.db.close() # flush
# check the two databases are the same (modulo the XOR)
orig = read_whole_file(TESTFILEPREFIX + "testdb", "rb")
obfu = read_whole_file(TESTFILEPREFIX + "testdb2", "rb")
self.assertEqual(len(orig), len(obfu))
self.assertNotEqual(orig, obfu)
# we ignore wal/non-wal differences
def compare(one, two):
self.assertEqual(one[0:18], two[:18])
self.assertEqual(one[96:], two[96:])
compare(orig, encryptme(obfu))
# versioning and excludes
meth_names = {}
for ver in (1, 2, 3):
name = f"foo{ ver }"
v = apsw.VFS(name, iVersion=ver)
registered = [vfs for vfs in apsw.vfs_details() if vfs["zName"] == name][0]
assert registered["iVersion"] == ver
for k in registered:
if k.startswith("x") and k not in meth_names:
meth_names[k] = ver
for ver in (1, 2, 3):
name = f"bar{ ver }"
exclude = set(random.sample(list(meth_names.keys()), 3))
v = apsw.VFS(name, iVersion=ver, exclude=exclude)
registered = [vfs for vfs in apsw.vfs_details() if vfs["zName"] == name][0]
assert registered["iVersion"] == ver
for name in registered:
if not name.startswith("x"):
continue
assert meth_names[name] <= ver
if name in exclude:
assert registered[name] == 0
else:
assert registered[name] != 0
# helper routines
self.assertRaises(TypeError, apsw.exception_for, "three")
self.assertRaises(ValueError, apsw.exception_for, 8764324)
self.assertRaises(OverflowError, apsw.exception_for, 0xffffffffffffffff10)
# test raw file object
f = ObfuscatedVFSFile("", os.path.abspath(TESTFILEPREFIX + "testdb"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_READONLY, 0])
del f # check closes
f = ObfuscatedVFSFile("", os.path.abspath(TESTFILEPREFIX + "testdb"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_READONLY, 0])
data = f.xRead(len(obfu), 0) # will encrypt it
compare(obfu, data)
f.xClose()
f.xClose()
f2 = apsw.VFSFile("", os.path.abspath(TESTFILEPREFIX + "testdb"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_READONLY, 0])
del f2
f2 = apsw.VFSFile("", os.path.abspath(TESTFILEPREFIX + "testdb2"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_READONLY, 0])
data = f2.xRead(len(obfu), 0)
self.assertEqual(obfu, data)
f2.xClose()
f2.xClose()
# cleanup so it doesn't interfere with following code using the same file
del f
del f2
db2.close()
del db2
vfs.unregister()
gc.collect()
### Detailed vfs testing
self.db = None
gc.collect()
defvfs = apsw.vfs_names()[0] # we want to inherit from this one
def testrand():
gc.collect()
apsw.randomness(0)
vfs = RandomVFS()
db = apsw.Connection(TESTFILEPREFIX + "testdb")
curnext(db.cursor().execute("select randomblob(10)"))
class RandomVFSUpper(apsw.VFS):
def __init__(self):
apsw.VFS.__init__(self, "randomupper", defvfs)
def xRandomness1(self, n):
return b"\xaa\xbb"
class RandomVFS(apsw.VFS):
def __init__(self):
apsw.VFS.__init__(self, "random", "randomupper", makedefault=True)
def xRandomness1(self, bad, number, of, arguments):
1 / 0
def xRandomness2(self, n):
1 / 0
def xRandomness3(self, n):
return b"abcd"
def xRandomness4(self, n):
return "abcd"
def xRandomness5(self, n):
return b"a" * (2 * n)
def xRandomness6(self, n):
return None
def xRandomness7(self, n):
return 3
def xRandomness99(self, n):
return super().xRandomness(n + 2049)
vfsupper = RandomVFSUpper()
vfs = RandomVFS()
self.assertRaises(TypeError, vfs.xRandomness, "jksdhfsd")
self.assertRaises(TypeError, vfs.xRandomness, 3, 3)
self.assertRaises(ValueError, vfs.xRandomness, -88)
RandomVFS.xRandomness = RandomVFS.xRandomness1
self.assertRaises(TypeError, testrand)
RandomVFS.xRandomness = RandomVFS.xRandomness2
self.assertRaises(ZeroDivisionError, testrand)
RandomVFS.xRandomness = RandomVFS.xRandomness3
testrand() # shouldn't have problems
RandomVFS.xRandomness = RandomVFS.xRandomness4
self.assertRaises(TypeError, testrand)
RandomVFS.xRandomness = RandomVFS.xRandomness5
testrand() # shouldn't have problems
RandomVFS.xRandomness = RandomVFS.xRandomness6
testrand() # shouldn't have problems
RandomVFS.xRandomness = RandomVFS.xRandomness7
self.assertRaises(TypeError, testrand)
RandomVFS.xRandomness = RandomVFS.xRandomness99
testrand() # shouldn't have problems
vfsupper.xRandomness = vfsupper.xRandomness1
testrand() # coverage
vfsupper.unregister()
vfs.unregister()
class ErrorVFS(apsw.VFS):
# A vfs that returns errors for all methods
def __init__(self):
apsw.VFS.__init__(self, "errorvfs", "")
def errorme(self, *args):
raise apsw.exception_for(apsw.SQLITE_IOERR)
class TestVFS(apsw.VFS):
def init1(self):
super().__init__("apswtest")
def init99(self, name="apswtest", base="", **kwargs):
super().__init__(name, base, **kwargs)
def xDelete1(self, name, syncdir):
super().xDelete(".", False)
def xDelete2(self, bad, number, of, args):
1 / 0
def xDelete3(self, name, syncdir):
1 / 0
def xDelete4(self, name, syncdir):
super().xDelete("bad", "arguments")
def xDelete99(self, name, syncdir):
assert isinstance(name, str)
assert isinstance(syncdir, bool)
return super().xDelete(name, syncdir)
def xAccess1(self, bad, number, of, args):
1 / 0
def xAccess2(self, name, flags):
1 / 0
def xAccess3(self, name, flags):
return super().xAccess("bad", "arguments")
def xAccess4(self, name, flags):
return (3, )
def xAccess99(self, name, flags):
assert (type(name) == type(""))
assert (type(flags) == type(1))
return super().xAccess(name, flags)
def xFullPathname1(self, bad, number, of, args):
1 / 0
def xFullPathname2(self, name):
1 / 0
def xFullPathname3(self, name):
return super().xFullPathname("bad", "args")
def xFullPathname4(self, name):
# parameter is larger than default buffer sizes used by sqlite
return super().xFullPathname(name * 10000)
def xFullPathname5(self, name):
# result is larger than default buffer sizes used by sqlite
return "a" * 10000
def xFullPathname6(self, name):
return 12 # bad return type
def xFullPathname99(self, name):
assert (type(name) == type(""))
return super().xFullPathname(name)
def xOpen1(self, bad, number, of, arguments):
1 / 0
def xOpen2(self, name, flags):
super().xOpen(name, 3)
1 / 0
def xOpen3(self, name, flags):
v = super().xOpen(name, flags)
flags.append(v)
return v
def xOpen4(self, name, flags):
return None
def xOpen99(self, name, flags):
assert (isinstance(name, apsw.URIFilename) or name is None or type(name) == type(""))
assert (type(flags) == type([]))
assert (len(flags) == 2)
assert (type(flags[0]) in (int, ))
assert (type(flags[1]) in (int, ))
return super().xOpen(name, flags)
def xOpen100(self, name, flags):
return TestFile(name, flags)
def xDlOpen1(self, bad, number, of, arguments):
1 / 0
def xDlOpen2(self, name):
1 / 0
def xDlOpen3(self, name):
return -1
def xDlOpen4(self, name):
return "fred"
def xDlOpen5(self, name):
return super().xDlOpen(3)
# python 3 only test
def xDlOpen6(self, name):
return super().xDlOpen(b"abcd") # bad string type
def xDlOpen7(self, name):
return 0xffffffffffffffff10
def xDlOpen99(self, name):
assert (type(name) == type(""))
res = super().xDlOpen(name)
if ctypes:
try:
cres = ctypes.cdll.LoadLibrary(name)._handle
except:
cres = 0
assert (res == cres)
return res
def xDlSym1(self, bad, number, of, arguments):
1 / 0
def xDlSym2(self, handle, name):
1 / 0
def xDlSym3(self, handle, name):
return "fred"
def xDlSym4(self, handle, name):
super().xDlSym(3, 3)
def xDlSym5(self, handle, name):
return super().xDlSym(handle, b"abcd")
def xDlSym6(self, handle, name):
return 0xffffffffffffffff10
def xDlSym99(self, handle, name):
assert (type(handle) in (int, ))
assert (type(name) == type(""))
res = super().xDlSym(handle, name)
# pypy doesn't have dlsym
if not iswindows and hasattr(_ctypes, "dlsym"):
assert (_ctypes.dlsym(handle, name) == res)
# windows has funky issues I don't want to deal with here
return res
def xDlClose1(self, bad, number, of, arguments):
1 / 0
def xDlClose2(self, handle):
1 / 0
def xDlClose3(self, handle):
return super().xDlClose("three")
def xDlClose99(self, handle):
assert (type(handle) in (int, ))
super().xDlClose(handle)
def xDlError1(self, bad, number, of, arguments):
1 / 0
def xDlError2(self):
1 / 0
def xDlError3(self):
return super().xDlError("three")
def xDlError4(self):
return 3
def xDlError5(self):
return b"abcd"
def xDlError6(self):
return None
def xDlError99(self):
return super().xDlError()
def xSleep1(self, bad, number, of, arguments):
1 / 0
def xSleep2(self, microseconds):
1 / 0
def xSleep3(self, microseconds):
return super().xSleep("three")
def xSleep4(self, microseconds):
return "three"
def xSleep5(self, microseconds):
return 0xffffffff0
def xSleep6(self, microseconds):
return 0xffffffffeeeeeeee0
def xSleep99(self, microseconds):
assert (type(microseconds) in (int, ))
return super().xSleep(microseconds)
def xCurrentTime1(self, bad, args):
1 / 0
def xCurrentTime2(self):
1 / 0
def xCurrentTime3(self):
return super().xCurrentTime("three")
def xCurrentTime4(self):
return "three"
def xCurrentTime5(self):
return math.exp(math.pi) * 26000
def xCurrentTimeCorrect(self):
# actual correct implementation https://stackoverflow.com/questions/466321/convert-unix-timestamp-to-julian
return time.time() / 86400.0 + 2440587.5
def xCurrentTime99(self):
return super().xCurrentTime()
def xCurrentTimeInt641(self, bad, args):
1 / 0
def xCurrentTimeInt642(self):
1 / 0
def xCurrentTimeInt643(self):
return super().xCurrentTimeInt64("three")
def xCurrentTimeInt644(self):
return "three"
def xCurrentTimeInt645(self):
return 2**66
def xCurrentTimeCorrect(self):
# actual correct implementation https://stackoverflow.com/questions/466321/convert-unix-timestamp-to-julian
return time.time() / 86400.0 + 2440587.5
def xCurrentTime99(self):
return super().xCurrentTime()
def xCurrentTimeInt6499(self):
return super().xCurrentTimeInt64()
def xGetLastError1(self, bad, args):
1 / 0
def xGetLastError2(self):
1 / 0
def xGetLastError3(self):
return super().xGetLastError("three")
def xGetLastError4(self):
return 3
def xGetLastError5(self):
return -17, "a" * 1500
def xGetLastError6(self):
return -0x7fffffff - 200, None
def xGetLastError7(self):
class te(tuple):
def __getitem__(self, n):
if n == 0:
return 23
1 / 0
return te((1, 2))
def xGetLastError8(self):
return 0, None
def xGetLastError9(self):
return 0, "Some sort of message"
def xGetLastError10(self):
return "banana", "Some sort of message"
def xGetLastError99(self):
return super().xGetLastError()
def xNextSystemCall1(self, bad, args):
1 / 0
def xNextSystemCall2(self, name):
return 3
def xNextSystemCall3(self, name):
return "foo\xf3"
def xNextSystemCall4(self, name):
1 / 0
def xNextSystemCall99(self, name):
return super().xNextSystemCall(name)
def xGetSystemCall1(self, bad, args):
1 / 0
def xGetSystemCall2(self, name):
1 / 0
def xGetSystemCall3(self, name):
return "fred"
def xGetSystemCall4(self, name):
return 3.7
def xGetSystemCall99(self, name):
return super().xGetSystemCall(name)
def xSetSystemCall1(self, bad, args, args3):
1 / 0
def xSetSystemCall2(self, name, ptr):
1 / 0
def xSetSystemCall3(self, name, ptr):
raise apsw.NotFoundError()
def xSetSystemCall99(self, name, ptr):
return super().xSetSystemCall(name, ptr)
class TestFile(apsw.VFSFile):
def init1(self, name, flags):
super().__init__("bogus", "arguments")
def init2(self, name, flags):
super().__init__("bogus", 3, 4)
def init3(self, name, flags):
super().__init__("bogus", "4", 4)
def init4(self, name, flags):
super().__init__("bogus", "4", [4, 4, 4, 4])
def init5(self, name, flags):
super().__init__("", name, [0xffffffffeeeeeeee0, 0xffffffffeeeeeeee0])
def init6(self, name, flags):
super().__init__("", name, [0xffffffffa, 0]) # 64 bit int vs long overflow
def init7(self, name, flags):
super().__init__("", name, (6, 7))
def init8(self, name, flags):
super().__init__("bogus", name, flags)
def init9(self, name, flags):
super().__init__("", name, (6, "six"))
def init99(self, name, flags):
super().__init__("", name, flags)
def xRead1(self, bad, number, of, arguments):
1 / 0
def xRead2(self, amount, offset):
1 / 0
def xRead3(self, amount, offset):
return 3
def xRead4(self, amount, offset):
return "a" * amount
def xRead5(self, amount, offset):
return super().xRead(amount - 1, offset)
def xRead99(self, amount, offset):
return super().xRead(amount, offset)
def xWrite1(self, bad, number, of, arguments):
1 / 0
def xWrite2(self, buffy, offset):
1 / 0
def xWrite99(self, buffy, offset):
return super().xWrite(buffy, offset)
def xUnlock1(self, bad, number, of, arguments):
1 / 0
def xUnlock2(self, level):
1 / 0
def xUnlock99(self, level):
return super().xUnlock(level)
def xLock1(self, bad, number, of, arguments):
1 / 0
def xLock2(self, level):
1 / 0
def xLock99(self, level):
return super().xLock(level)
def xTruncate1(self, bad, number, of, arguments):
1 / 0
def xTruncate2(self, size):
1 / 0
def xTruncate99(self, size):
return super().xTruncate(size)
def xSync1(self, bad, number, of, arguments):
1 / 0
def xSync2(self, flags):
1 / 0
def xSync99(self, flags):
return super().xSync(flags)
def xSectorSize1(self, bad, number, of, args):
1 / 0
def xSectorSize2(self):
1 / 0
def xSectorSize3(self):
return "three"
def xSectorSize4(self):
return 0xffffffffeeeeeeee0
def xSectorSize99(self):
return super().xSectorSize()
def xDeviceCharacteristics1(self, bad, number, of, args):
1 / 0
def xDeviceCharacteristics2(self):
1 / 0
def xDeviceCharacteristics3(self):
return "three"
def xDeviceCharacteristics4(self):
return 0xffffffffeeeeeeee0
def xDeviceCharacteristics99(self):
return super().xDeviceCharacteristics()
def xFileSize1(self, bad, number, of, args):
1 / 0
def xFileSize2(self):
1 / 0
def xFileSize3(self):
return "three"
def xFileSize4(self):
return 0xffffffffeeeeeeee0
def xFileSize99(self):
res = super().xFileSize()
if res < 100000:
return int(res)
return res
def xCheckReservedLock1(self, bad, number, of, args):
1 / 0
def xCheckReservedLock2(self):
1 / 0
def xCheckReservedLock3(self):
return "three"
def xCheckReservedLock4(self):
return 0xffffffffeeeeeeee0
def xCheckReservedLock99(self):
return super().xCheckReservedLock()
def xFileControl1(self, bad, number, of, args):
1 / 0
def xFileControl2(self, op, ptr):
1 / 0
def xFileControl3(self, op, ptr):
return "banana"
def xFileControl99(self, op, ptr):
if op == 1027:
assert (ptr == 1027)
elif op == 1028:
if ctypes:
assert (True is ctypes.py_object.from_address(ptr).value)
else:
return super().xFileControl(op, ptr)
return True
TestVFS.xCurrentTime = TestVFS.xCurrentTimeCorrect
# check initialization
self.assertRaises(TypeError, apsw.VFS, "3", 3)
self.assertRaises(ValueError, apsw.VFS, "never", "klgfkljdfsljgklfjdsglkdfs")
self.assertTrue("never" not in apsw.vfs_names())
TestVFS.__init__ = TestVFS.init1
vfs = TestVFS()
self.assertRaises(apsw.VFSNotImplementedError, testdb)
del vfs
gc.collect()
TestVFS.__init__ = TestVFS.init99
vfs = TestVFS()
vfs_notime64_base = TestVFS(name="apswtest_notime64_base", exclude={"xCurrentTimeInt64"})
vfs_notime64_base.xCurrentTime = lambda *args: 3.1415
vfs_notime64 = TestVFS(name="apswtest_notime64", base="apswtest_notime64_base", exclude={"xCurrentTimeInt64"})
self.assertIn("apswtest", apsw.vfs_names())
self.assertIn("apswtest_notime64", apsw.vfs_names())
# Should work without any overridden methods
testdb()
## xDelete
self.assertRaises(TypeError, vfs.xDelete, "bogus", "arguments")
TestVFS.xDelete = TestVFS.xDelete1
self.assertRaises(apsw.IOError, testdb)
TestVFS.xDelete = TestVFS.xDelete2
self.assertRaises(TypeError, testdb)
TestVFS.xDelete = TestVFS.xDelete3
self.assertRaises(ZeroDivisionError, testdb)
TestVFS.xDelete = TestVFS.xDelete4
self.assertRaises(TypeError, testdb)
TestVFS.xDelete = TestVFS.xDelete99
testdb()
## xAccess
self.assertRaises(TypeError, vfs.xAccess, "bogus", "arguments")
TestVFS.xAccess = TestVFS.xAccess1
self.assertRaises(TypeError, testdb)
TestVFS.xAccess = TestVFS.xAccess2
self.assertRaises(ZeroDivisionError, testdb)
TestVFS.xAccess = TestVFS.xAccess3
self.assertRaises(TypeError, testdb)
TestVFS.xAccess = TestVFS.xAccess4
self.assertRaises(TypeError, testdb)
TestVFS.xAccess = TestVFS.xAccess99
if iswindows:
self.assertRaises(apsw.IOError, vfs.xAccess, "<bad<filename:", apsw.SQLITE_ACCESS_READWRITE)
else:
self.assertEqual(False, vfs.xAccess("<bad<filename:", apsw.SQLITE_ACCESS_READWRITE))
self.assertEqual(True, vfs.xAccess(".", apsw.SQLITE_ACCESS_EXISTS))
# unix vfs doesn't ever return error so we have to indirect through one of ours
errvfs = ErrorVFS()
errvfs.xAccess = errvfs.errorme
vfs2 = TestVFS("apswtest2", "errorvfs")
self.assertRaises(apsw.IOError, testdb, vfsname="apswtest2")
del vfs2
del errvfs
gc.collect()
## xFullPathname
self.assertRaises(TypeError, vfs.xFullPathname, "bogus", "arguments")
self.assertRaises(TypeError, vfs.xFullPathname, 3)
TestVFS.xFullPathname = TestVFS.xFullPathname1
self.assertRaises(TypeError, testdb)
TestVFS.xFullPathname = TestVFS.xFullPathname2
self.assertRaises(ZeroDivisionError, testdb)
TestVFS.xFullPathname = TestVFS.xFullPathname3
self.assertRaises(TypeError, testdb)
TestVFS.xFullPathname = TestVFS.xFullPathname4
if not iswindows:
# ::TODO:: check this on windows
# SQLite doesn't give an error even though the vfs is silently truncating
# the full pathname. See SQLite ticket 3373
self.assertRaises(apsw.CantOpenError, testdb) # we get cantopen on the truncated fullname
TestVFS.xFullPathname = TestVFS.xFullPathname5
self.assertRaises(apsw.TooBigError, testdb)
TestVFS.xFullPathname = TestVFS.xFullPathname6
self.assertRaises(TypeError, testdb)
TestVFS.xFullPathname = TestVFS.xFullPathname99
testdb()
## xOpen
self.assertRaises(TypeError, vfs.xOpen, 3)
self.assertRaises(TypeError, vfs.xOpen, 3, 3)
self.assertRaises(TypeError, vfs.xOpen, None, (1, 2))
self.assertRaises(TypeError, vfs.xOpen, None, [1, 2, 3])
self.assertRaises(TypeError, vfs.xOpen, None, ["1", 2])
self.assertRaises(TypeError, vfs.xOpen, None, [1, "2"])
self.assertRaises(OverflowError, vfs.xOpen, None, [0xffffffffeeeeeeee0, 2])
self.assertRaises(OverflowError, vfs.xOpen, None, [0xffffffff0, 2])
self.assertRaises(OverflowError, vfs.xOpen, None, [1, 0xffffffff0])
self.assertRaises(
apsw.CantOpenError, testdb,
filename="notadir/notexist/nochance") # can't open due to intermediate directories not existing
TestVFS.xOpen = TestVFS.xOpen1
self.assertRaises(TypeError, testdb)
TestVFS.xOpen = TestVFS.xOpen2
self.assertRaises(TypeError, testdb)
TestVFS.xOpen = TestVFS.xOpen3
self.assertRaises(TypeError, testdb)
TestVFS.xOpen = TestVFS.xOpen4
self.assertRaises(AttributeError, testdb)
TestVFS.xOpen = TestVFS.xOpen99
testdb()
if hasattr(apsw.Connection(":memory:"), "enableloadextension") and os.path.exists(LOADEXTENSIONFILENAME):
## xDlOpen
self.assertRaises(TypeError, vfs.xDlOpen, 3)
self.assertRaises(TypeError, vfs.xDlOpen, b"\xfb\xfc\xfd\xfe\xff\xff\xff\xff")
TestVFS.xDlOpen = TestVFS.xDlOpen1
self.assertRaises(TypeError, testdb)
TestVFS.xDlOpen = TestVFS.xDlOpen2
self.assertRaises(ZeroDivisionError, testdb)
TestVFS.xDlOpen = TestVFS.xDlOpen3
self.assertRaises(TypeError, testdb)
TestVFS.xDlOpen = TestVFS.xDlOpen4
self.assertRaises(TypeError, testdb)
TestVFS.xDlOpen = TestVFS.xDlOpen5
self.assertRaises(TypeError, testdb)
TestVFS.xDlOpen = TestVFS.xDlOpen6
self.assertRaises(TypeError, testdb)
TestVFS.xDlOpen = TestVFS.xDlOpen7
self.assertRaises(OverflowError, testdb)
TestVFS.xDlOpen = TestVFS.xDlOpen99
testdb()
## xDlSym
self.assertRaises(TypeError, vfs.xDlSym, 3)
self.assertRaises(TypeError, vfs.xDlSym, 3, 3)
self.assertRaises(TypeError, vfs.xDlSym, "three", "three")
TestVFS.xDlSym = TestVFS.xDlSym1
self.assertRaises(TypeError, testdb)
TestVFS.xDlSym = TestVFS.xDlSym2
self.assertRaises(ZeroDivisionError, testdb)
TestVFS.xDlSym = TestVFS.xDlSym3
self.assertRaises(TypeError, testdb)
TestVFS.xDlSym = TestVFS.xDlSym4
self.assertRaises(TypeError, testdb)
TestVFS.xDlSym = TestVFS.xDlSym5
self.assertRaises(TypeError, testdb)
TestVFS.xDlSym = TestVFS.xDlSym6
self.assertRaises(OverflowError, testdb)
TestVFS.xDlSym = TestVFS.xDlSym99
testdb()
## xDlClose
self.assertRaises(TypeError, vfs.xDlClose, "three")
self.assertRaises(OverflowError, vfs.xDlClose, 0xffffffffffffffff10)
TestVFS.xDlClose = TestVFS.xDlClose1
self.assertRaises(TypeError, testdb)
TestVFS.xDlClose = TestVFS.xDlClose2
self.assertRaises(ZeroDivisionError, testdb)
TestVFS.xDlClose = TestVFS.xDlClose3
self.assertRaises(TypeError, testdb)
TestVFS.xDlClose = TestVFS.xDlClose99
testdb()
## xDlError
self.assertRaises(TypeError, vfs.xDlError, "three")
TestVFS.xDlError = TestVFS.xDlError1
self.assertRaises(TypeError, testdb)
TestVFS.xDlError = TestVFS.xDlError2
self.assertRaises(ZeroDivisionError, testdb)
TestVFS.xDlError = TestVFS.xDlError3
self.assertRaises(TypeError, testdb)
TestVFS.xDlError = TestVFS.xDlError4
self.assertRaises(TypeError, testdb)
TestVFS.xDlError = TestVFS.xDlError5
self.assertRaises(TypeError, testdb)
TestVFS.xDlError = TestVFS.xDlError6 # should not error
testdb()
TestVFS.xDlError = TestVFS.xDlError99
testdb()
## xSleep
testtimeout = True
self.assertRaises(TypeError, vfs.xSleep, "three")
self.assertRaises(TypeError, vfs.xSleep, 3, 3)
TestVFS.xSleep = TestVFS.xSleep1
self.assertRaises(TypeError, testdb, mode="delete")
TestVFS.xSleep = TestVFS.xSleep2
self.assertRaises(ZeroDivisionError, testdb, mode="delete")
TestVFS.xSleep = TestVFS.xSleep3
self.assertRaises(TypeError, testdb, mode="delete")
TestVFS.xSleep = TestVFS.xSleep4
self.assertRaises(TypeError, testdb, mode="delete")
TestVFS.xSleep = TestVFS.xSleep5
self.assertRaises(OverflowError, testdb, mode="delete")
TestVFS.xSleep = TestVFS.xSleep6
self.assertRaises(OverflowError, testdb, mode="delete")
TestVFS.xSleep = TestVFS.xSleep99
testdb(mode="delete")
testtimeout = False
## xCurrentTime / xCurrentTimeInt64
self.assertRaises(TypeError, vfs.xCurrentTime, "three")
self.assertRaises(TypeError, vfs.xCurrentTimeInt64, "three")
for vname in "apswtest", "apswtest_notime64":
TestVFS.xCurrentTime = TestVFS.xCurrentTime1
TestVFS.xCurrentTimeInt64 = TestVFS.xCurrentTimeInt641
self.assertRaises(TypeError, testdb, vfsname=vname)
TestVFS.xCurrentTime = TestVFS.xCurrentTime2
TestVFS.xCurrentTimeInt64 = TestVFS.xCurrentTimeInt642
self.assertRaises(ZeroDivisionError, testdb, vfsname=vname)
TestVFS.xCurrentTime = TestVFS.xCurrentTime3
TestVFS.xCurrentTimeInt64 = TestVFS.xCurrentTimeInt643
self.assertRaises(TypeError, testdb, vfsname=vname)
TestVFS.xCurrentTime = TestVFS.xCurrentTime4
TestVFS.xCurrentTimeInt64 = TestVFS.xCurrentTimeInt644
self.assertRaises(TypeError, testdb, vfsname=vname)
TestVFS.xCurrentTime = TestVFS.xCurrentTime5
TestVFS.xCurrentTimeInt64 = TestVFS.xCurrentTimeInt645
self.assertRaisesRoot(OverflowError, testdb, vfsname=vname)
TestVFS.xCurrentTime = TestVFS.xCurrentTime99
TestVFS.xCurrentTimeInt64 = TestVFS.xCurrentTimeInt6499
testdb(vfsname=vname)
TestVFS.xCurrentTime = TestVFS.xCurrentTimeCorrect
TestVFS.xCurrentTimeInt64 = TestVFS.xCurrentTimeInt6499
testdb(vfsname=vname)
## xGetLastError
# We can't directly test because the methods are called as side effects
# of other errors. However coverage shows we are exercising the code.
def provoke_error():
self.assertRaisesRoot(apsw.CantOpenError, testdb, attachdb='.')
for n in range(1, 11):
TestVFS.xGetLastError = getattr(TestVFS, "xGetLastError" + str(n))
provoke_error()
TestVFS.xGetLastError = TestVFS.xGetLastError99
provoke_error()
## System call stuff
if "unix" in apsw.vfs_names():
class VFS2(apsw.VFS):
def __init__(self):
apsw.VFS.__init__(self, "apswtest2", "apswtest")
vfs2 = VFS2()
## xNextSystemCall
self.assertRaises(TypeError, vfs.xNextSystemCall, 0)
items = [None]
while True:
n = vfs.xNextSystemCall(items[-1])
if n is None:
break
items.append(n)
items = items[1:]
self.assertNotEqual(0, len(items))
self.assertTrue("open" in items)
TestVFS.xNextSystemCall = TestVFS.xNextSystemCall1
self.assertRaisesRoot(TypeError, vfs2.xNextSystemCall, "open")
TestVFS.xNextSystemCall = TestVFS.xNextSystemCall2
self.assertRaisesRoot(TypeError, vfs2.xNextSystemCall, "open")
TestVFS.xNextSystemCall = TestVFS.xNextSystemCall4
self.assertEqual(None, self.assertRaisesRoot(ZeroDivisionError, vfs2.xNextSystemCall, "open"))
TestVFS.xNextSystemCall = TestVFS.xNextSystemCall99
vfs2.xNextSystemCall("open")
## xGetSystemCall
self.assertRaises(TypeError, vfs.xGetSystemCall)
self.assertRaises(TypeError, vfs.xGetSystemCall, 3)
self.assertEqual(None, vfs.xGetSystemCall("a name that won't exist"))
self.assertTrue(isinstance(vfs.xGetSystemCall("open"), (int, )))
TestVFS.xGetSystemCall = TestVFS.xGetSystemCall1
self.assertRaises(TypeError, vfs2.xGetSystemCall, "open")
TestVFS.xGetSystemCall = TestVFS.xGetSystemCall2
self.assertRaises(ZeroDivisionError, vfs2.xGetSystemCall, "open")
TestVFS.xGetSystemCall = TestVFS.xGetSystemCall3
self.assertRaises(TypeError, vfs2.xGetSystemCall, "open")
TestVFS.xGetSystemCall = TestVFS.xGetSystemCall4
self.assertRaises(TypeError, vfs2.xGetSystemCall, "open")
TestVFS.xGetSystemCall = TestVFS.xGetSystemCall99
self.assertTrue(vfs2.xGetSystemCall("open") > 0)
## xSetSystemCall
fallback = apsw.VFS("fallback", base="") # undo any damage we do
try:
self.assertRaises(TypeError, vfs.xSetSystemCall)
self.assertRaises(TypeError, vfs.xSetSystemCall, 3, 4)
self.assertRaises((TypeError, ValueError), vfs.xSetSystemCall, "a\0b", 4)
self.assertRaises(TypeError, vfs.xSetSystemCall, "none", 3.7)
realopen = vfs.xGetSystemCall("open")
self.assertEqual(False, vfs.xSetSystemCall("doesn't exist", 0))
self.assertEqual(True, vfs.xSetSystemCall("open", realopen + 1))
self.assertEqual(realopen + 1, vfs.xGetSystemCall("open"))
self.assertEqual(True, vfs.xSetSystemCall("open", realopen))
TestVFS.xSetSystemCall = TestVFS.xSetSystemCall1
self.assertRaises(TypeError, vfs2.xSetSystemCall, "open", realopen)
TestVFS.xSetSystemCall = TestVFS.xSetSystemCall2
self.assertRaises(ZeroDivisionError, vfs2.xSetSystemCall, "open", realopen)
TestVFS.xSetSystemCall = TestVFS.xSetSystemCall3
self.assertEqual(False, vfs2.xSetSystemCall("doesn't exist", 0))
TestVFS.xSetSystemCall = TestVFS.xSetSystemCall99
self.assertEqual(True, vfs2.xSetSystemCall("open", realopen))
finally:
# undocumented - this resets all calls to their defaults
fallback.xSetSystemCall(None, 0)
fallback.unregister()
##
## VFS file testing
##
## init
TestVFS.xOpen = TestVFS.xOpen100
TestFile.__init__ = TestFile.init1
self.assertRaises(TypeError, testdb)
TestFile.__init__ = TestFile.init2
self.assertRaises(TypeError, testdb)
TestFile.__init__ = TestFile.init3
self.assertRaises(TypeError, testdb)
TestFile.__init__ = TestFile.init4
self.assertRaises(TypeError, testdb)
TestFile.__init__ = TestFile.init5
self.assertRaises(OverflowError, testdb)
TestFile.__init__ = TestFile.init6
self.assertRaises(OverflowError, testdb)
TestFile.__init__ = TestFile.init7
self.assertRaises(TypeError, testdb)
TestFile.__init__ = TestFile.init8
self.assertRaises(ValueError, testdb)
TestFile.__init__ = TestFile.init9
self.assertRaises(TypeError, testdb)
TestFile.__init__ = TestFile.init99
testdb() # should work just fine
# cause an open failure
self.assertRaises(apsw.CantOpenError, TestFile, ".",
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0])
## xRead
t = TestFile(os.path.abspath(TESTFILEPREFIX + "testfile"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0])
self.assertRaises(TypeError, t.xRead, "three", "four")
self.assertRaises(OverflowError, t.xRead, 0xffffffffeeeeeeee0, 1)
self.assertRaises(OverflowError, t.xRead, 1, 0xffffffffeeeeeeee0)
TestFile.xRead = TestFile.xRead1
self.assertRaises(TypeError, testdb)
TestFile.xRead = TestFile.xRead2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xRead = TestFile.xRead3
self.assertRaises(TypeError, testdb)
TestFile.xRead = TestFile.xRead4
self.assertRaises(TypeError, testdb)
TestFile.xRead = TestFile.xRead5
self.assertRaises(apsw.IOError, testdb)
TestFile.xRead = TestFile.xRead99
testdb()
## xWrite
self.assertRaises(TypeError, t.xWrite, "three", "four")
self.assertRaises(OverflowError, t.xWrite, b"three", 0xffffffffeeeeeeee0)
self.assertRaises(TypeError, t.xWrite, "foo", 0)
TestFile.xWrite = TestFile.xWrite1
self.assertRaises(TypeError, testdb)
TestFile.xWrite = TestFile.xWrite2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xWrite = TestFile.xWrite99
testdb()
## xUnlock
self.assertRaises(TypeError, t.xUnlock, "three")
self.assertRaises(OverflowError, t.xUnlock, 0xffffffffeeeeeeee0)
# doesn't care about nonsensical levels - assert fails in debug build
# t.xUnlock(-1)
if not apsw.connection_hooks:
TestFile.xUnlock = TestFile.xUnlock1
self.assertRaises(TypeError, testdb)
TestFile.xUnlock = TestFile.xUnlock2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xUnlock = TestFile.xUnlock99
testdb()
## xLock
self.assertRaises(TypeError, t.xLock, "three")
self.assertRaises(OverflowError, t.xLock, 0xffffffffeeeeeeee0)
# doesn't care about nonsensical levels - assert fails in debug build
# t.xLock(0xffffff)
TestFile.xLock = TestFile.xLock1
self.assertRaises(TypeError, testdb)
TestFile.xLock = TestFile.xLock2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xLock = TestFile.xLock99
testdb()
## xTruncate
self.assertRaises(TypeError, t.xTruncate, "three")
self.assertRaises(OverflowError, t.xTruncate, 0xffffffffeeeeeeee0)
if not iswindows:
# windows is happy to truncate to -77 bytes
self.assertRaises(apsw.IOError, t.xTruncate, -77)
TestFile.xTruncate = TestFile.xTruncate1
self.assertRaises(TypeError, testdb)
TestFile.xTruncate = TestFile.xTruncate2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xTruncate = TestFile.xTruncate99
testdb()
## xSync
saved = apsw.connection_hooks
apsw.connection_hooks = []
try:
self.assertRaises(TypeError, t.xSync, "three")
self.assertRaises(OverflowError, t.xSync, 0xffffffffeeeeeeee0)
TestFile.xSync = TestFile.xSync1
self.assertRaises(TypeError, testdb)
TestFile.xSync = TestFile.xSync2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xSync = TestFile.xSync99
testdb()
finally:
apsw.connection_hooks = saved
## xSectorSize
self.assertRaises(TypeError, t.xSectorSize, 3)
TestFile.xSectorSize = TestFile.xSectorSize1
self.assertRaises(TypeError, testdb)
TestFile.xSectorSize = TestFile.xSectorSize2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xSectorSize = TestFile.xSectorSize3
self.assertRaises(TypeError, testdb)
TestFile.xSectorSize = TestFile.xSectorSize4
self.assertRaises(OverflowError, testdb)
TestFile.xSectorSize = TestFile.xSectorSize99
testdb()
## xDeviceCharacteristics
self.assertRaises(TypeError, t.xDeviceCharacteristics, 3)
TestFile.xDeviceCharacteristics = TestFile.xDeviceCharacteristics1
self.assertRaisesUnraisable(TypeError, testdb)
TestFile.xDeviceCharacteristics = TestFile.xDeviceCharacteristics2
self.assertRaisesUnraisable(ZeroDivisionError, testdb)
TestFile.xDeviceCharacteristics = TestFile.xDeviceCharacteristics3
self.assertRaisesUnraisable(TypeError, testdb)
TestFile.xDeviceCharacteristics = TestFile.xDeviceCharacteristics4
self.assertRaisesUnraisable(OverflowError, testdb)
TestFile.xDeviceCharacteristics = TestFile.xDeviceCharacteristics99
testdb()
## xFileSize
self.assertRaises(TypeError, t.xFileSize, 3)
TestFile.xFileSize = TestFile.xFileSize1
self.assertRaises(TypeError, testdb)
TestFile.xFileSize = TestFile.xFileSize2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xFileSize = TestFile.xFileSize3
self.assertRaises(TypeError, testdb)
TestFile.xFileSize = TestFile.xFileSize4
self.assertRaises(OverflowError, testdb)
TestFile.xFileSize = TestFile.xFileSize99
testdb()
## xCheckReservedLock
self.assertRaises(TypeError, t.xCheckReservedLock, 8)
if not iswindows:
# we don't do checkreservedlock test on windows as the
# various files that need to be copied and finagled behind
# the scenes are locked
TestFile.xCheckReservedLock = TestFile.xCheckReservedLock1
self.assertRaises(TypeError, testdb)
TestFile.xCheckReservedLock = TestFile.xCheckReservedLock2
self.assertRaises(ZeroDivisionError, testdb)
TestFile.xCheckReservedLock = TestFile.xCheckReservedLock3
self.assertRaises(TypeError, testdb)
TestFile.xCheckReservedLock = TestFile.xCheckReservedLock4
self.assertRaises(OverflowError, testdb)
TestFile.xCheckReservedLock = TestFile.xCheckReservedLock99
db = testdb()
## xFileControl
self.assertRaises(TypeError, t.xFileControl, "three", "four")
self.assertRaises(OverflowError, t.xFileControl, 10, 0xffffffffeeeeeeee0)
self.assertRaises(TypeError, t.xFileControl, 10, "three")
self.assertEqual(t.xFileControl(2000, 3000), False)
fc1 = testdb(TESTFILEPREFIX + "testdb", closedb=False).file_control
fc2 = testdb(TESTFILEPREFIX + "testdb2", closedb=False).file_control
TestFile.xFileControl = TestFile.xFileControl1
self.assertRaises(TypeError, fc1, "main", 1027, 1027)
TestFile.xFileControl = TestFile.xFileControl2
self.assertRaises(ZeroDivisionError, fc2, "main", 1027, 1027)
TestFile.xFileControl = TestFile.xFileControl3
self.assertRaises(TypeError, fc2, "main", 1027, 1027)
TestFile.xFileControl = TestFile.xFileControl99
del fc1
del fc2
# these should work
testdb(closedb=False).file_control("main", 1027, 1027)
if ctypes:
objwrap = ctypes.py_object(True)
testdb(closedb=False).file_control("main", 1028, ctypes.addressof(objwrap))
# for coverage
class VFSx(apsw.VFS):
def __init__(self):
apsw.VFS.__init__(self, "file_control", "apswtest")
vfs2 = VFSx()
testdb(vfsname="file_control", closedb=False).file_control("main", 1027, 1027)
del vfs2
## xClose
t.xClose()
# make sure there is no problem closing twice
t.xClose()
del t
gc.collect()
t = apsw.VFSFile("", os.path.abspath(TESTFILEPREFIX + "testfile2"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0])
t.xClose()
# check all functions detect closed file
for n in dir(t):
if n not in ('xClose', 'excepthook') and not n.startswith("__"):
self.assertRaises(apsw.VFSFileClosedError, getattr(t, n))
def testWith(self):
"Context manager functionality"
# Does it work?
# the autocommit tests are to make sure we are not in a transaction
self.assertEqual(True, self.db.get_autocommit())
self.assertEqual(False, self.db.in_transaction)
self.assertTableNotExists("foo1")
with self.db as db:
db.cursor().execute('create table foo1(x)')
self.assertTableExists("foo1")
self.assertEqual(True, self.db.get_autocommit())
self.assertEqual(False, self.db.in_transaction)
# with an error
self.assertEqual(True, self.db.get_autocommit())
self.assertEqual(False, self.db.in_transaction)
self.assertTableNotExists("foo2")
try:
with self.db as db:
db.cursor().execute('create table foo2(x)')
1 / 0
except ZeroDivisionError:
pass
self.assertTableNotExists("foo2")
self.assertEqual(True, self.db.get_autocommit())
# nested - simple - success
with self.db as db:
self.assertEqual(False, self.db.get_autocommit())
self.assertEqual(True, self.db.in_transaction)
db.cursor().execute('create table foo2(x)')
with db as db2:
self.assertEqual(False, self.db.get_autocommit())
db.cursor().execute('create table foo3(x)')
with db2 as db3:
self.assertEqual(False, self.db.get_autocommit())
db.cursor().execute('create table foo4(x)')
self.assertEqual(True, self.db.get_autocommit())
self.assertTableExists("foo2")
self.assertTableExists("foo3")
self.assertTableExists("foo4")
# nested - simple - failure
try:
self.db.cursor().execute('begin; create table foo5(x)')
with self.db as db:
self.assertEqual(False, self.db.get_autocommit())
db.cursor().execute('create table foo6(x)')
with db as db2:
self.assertEqual(False, self.db.get_autocommit())
db.cursor().execute('create table foo7(x)')
with db2 as db3:
self.assertEqual(False, self.db.get_autocommit())
db.cursor().execute('create table foo8(x)')
1 / 0
except ZeroDivisionError:
pass
self.assertEqual(False, self.db.get_autocommit())
self.db.cursor().execute("commit")
self.assertEqual(True, self.db.get_autocommit())
self.assertTableExists("foo5")
self.assertTableNotExists("foo6")
self.assertTableNotExists("foo7")
self.assertTableNotExists("foo8")
# improve coverage and various corner cases
self.db.__enter__()
self.assertRaises(TypeError, self.db.__exit__, 1)
for i in range(10):
self.db.__exit__(None, None, None)
# make an exit fail
self.db.__enter__()
self.db.cursor().execute("commit")
# deliberately futz with the outstanding transaction
self.assertRaises(apsw.SQLError, self.db.__exit__, None, None, None)
self.db.__exit__(None, None, None) # extra exit should be harmless
# exectracing
traces = []
def et(con, sql, bindings):
if con == self.db:
traces.append(sql)
return True
self.db.set_exec_trace(et)
try:
with self.db as db:
db.cursor().execute('create table foo2(x)')
except apsw.SQLError: # table already exists so we should get an error
pass
# check we saw the right things in the traces
self.assertTrue(len(traces) == 3)
for s in traces:
self.assertTrue("SAVEPOINT" in s.upper())
def et(*args):
return BadIsTrue()
self.db.set_exec_trace(et)
try:
with self.db as db:
db.cursor().execute('create table etfoo2(x)')
except ZeroDivisionError:
pass
self.assertTableNotExists("etfoo2")
def et(*args):
return False
self.db.set_exec_trace(et)
try:
with self.db as db:
db.cursor().execute('create table etfoo2(x)')
except apsw.ExecTraceAbort:
pass
self.db.set_exec_trace(None)
self.assertTableNotExists("etfoo2")
# test blobs with context manager
self.db.cursor().execute("create table blobby(x); insert into blobby values(x'aabbccddee')")
rowid = self.db.last_insert_rowid()
blob = self.db.blob_open('main', 'blobby', 'x', rowid, 0)
with blob as b:
self.assertEqual(id(blob), id(b))
b.read(1)
# blob gives ValueError if you do operations on closed blob
self.assertRaises(ValueError, blob.read)
self.db.cursor().execute("insert into blobby values(x'aabbccddee')")
rowid = self.db.last_insert_rowid()
blob = self.db.blob_open('main', 'blobby', 'x', rowid, 0)
try:
with blob as b:
self.assertEqual(id(blob), id(b))
1 / 0
b.read(1)
except ZeroDivisionError:
# blob gives ValueError if you do operating on closed blob
self.assertRaises(ValueError, blob.read)
# backup code
if not hasattr(self.db, "backup"): return # experimental
db2 = apsw.Connection(":memory:")
with db2.backup("main", self.db, "main") as b:
while not b.done:
b.step(1)
self.assertEqual(b.done, True)
self.assertDbIdentical(self.db, db2)
def fillWithRandomStuff(self, db, seed=1):
"Fills a database with random content"
db.cursor().execute("create table a(x)")
for i in range(1, 11):
db.cursor().execute("insert into a values(?)",
("aaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" * i * 8192, ))
def assertDbIdentical(self, db1, db2):
"Ensures databases are identical"
c1 = db1.cursor()
c2 = db2.cursor()
self.assertEqual(list(c1.execute("select * from sqlite_schema order by _ROWID_")),
list(c2.execute("select * from sqlite_schema order by _ROWID_")))
for table in db1.cursor().execute("select name from sqlite_schema where type='table'"):
table = table[0]
self.assertEqual(
list(c1.execute("select * from [%s] order by _ROWID_" % (table, ))),
list(c2.execute("select * from [%s] order by _ROWID_" % (table, ))),
)
for table in db2.cursor().execute("select name from sqlite_schema where type='table'"):
table = table[0]
self.assertEqual(
list(c1.execute("select * from [%s] order by _ROWID_" % (table, ))),
list(c2.execute("select * from [%s] order by _ROWID_" % (table, ))),
)
def testBackup(self):
"Verify hot backup functionality"
# bad calls
self.assertRaises(TypeError, self.db.backup, "main", "main", "main", "main")
self.assertRaises(TypeError, self.db.backup, "main", 3, "main")
db2 = apsw.Connection(":memory:")
db2.close()
self.assertRaises(ValueError, self.db.backup, "main", db2, "main")
# can't copy self
self.assertRaises(ValueError, self.db.backup, "main", self.db, "it doesn't care what is here")
# try and get inuse error
if not os.getenv("APSW_VALGRIND"):
# valgrind only runs one thread at a time (kinda like the GIL)
# which makes this race condition almost impossible to cause
# so we skip
dbt = apsw.Connection(":memory:")
vals = {"stop": False, "raised": False}
def wt():
# worker thread spins grabbing and releasing inuse flag
while not vals["stop"]:
try:
dbt.set_busy_timeout(100)
except apsw.ThreadingViolationError:
# this means main thread grabbed inuse first
pass
runtime = float(os.getenv("APSW_HEAVY_DURATION")) if os.getenv("APSW_HEAVY_DURATION") else 30
t = ThreadRunner(wt)
t.start()
b4 = time.time()
# try to get inuse error
try:
try:
while not vals["stop"] and time.time() - b4 < runtime:
self.db.backup("main", dbt, "main").close()
except apsw.ThreadingViolationError:
vals["stop"] = True
vals["raised"] = True
finally:
vals["stop"] = True
# standard usage
db2 = apsw.Connection(":memory:")
self.fillWithRandomStuff(db2)
b = self.db.backup("main", db2, "main")
self.assertRaises(TypeError, b.step, '3')
try:
b.step(1)
self.assertTrue(b.remaining > 0)
self.assertTrue(b.page_count > 0)
while not b.done:
b.step(1)
finally:
b.finish()
self.assertDbIdentical(self.db, db2)
self.db.cursor().execute("drop table a")
# don't clean up
b = self.db.backup("main", db2, "main")
try:
while not b.done:
b.step(1)
finally:
b.finish()
self.assertDbIdentical(self.db, db2)
del b
del db2
fname = self.db.filename
self.db = None
gc.collect()
# check dest db can't be used for anything else
db2 = apsw.Connection(":memory:")
c = db2.cursor()
c.execute("create table x(y); insert into x values(3); select * from x")
self.db = apsw.Connection(":memory:")
self.fillWithRandomStuff(self.db)
self.assertRaises(apsw.ThreadingViolationError, db2.backup, "main", self.db, "main")
c.close()
b = db2.backup("main", self.db, "main")
# double check cursor really is dead
self.assertRaises(apsw.CursorClosedError, c.execute, "select 3")
# with the backup object existing, all operations on db2 should fail
self.assertRaises(apsw.ThreadingViolationError, db2.cursor)
# finish and then trying to step
b.finish()
self.assertRaises(apsw.ConnectionClosedError, b.step)
# make step and finish fail with locked error
self.db = apsw.Connection(fname)
def lockerr():
db2 = apsw.Connection(self.db.filename)
db2.cursor().execute("begin exclusive")
db3 = apsw.Connection(self.db.filename)
b = db3.backup("main", self.db, "main")
# if step gets busy then so does finish, but step has to be called at least once
self.assertRaises(apsw.BusyError, b.step)
return b
b = lockerr()
b.close(True)
del b
b = lockerr()
self.assertRaises(apsw.BusyError, b.close, False)
del b
b = lockerr()
self.assertRaises(apsw.BusyError, b.finish)
b.finish() # should be ok the second time
del b
b = lockerr()
self.assertRaises(TypeError, b.close, "3")
self.assertRaises(apsw.BusyError, b.close, False)
b.close() # should also be ok
del b
def f():
b = lockerr()
del b
gc.collect()
self.assertRaisesUnraisable(apsw.BusyError, f)
# coverage
b = lockerr()
self.assertRaises(TypeError, b.__exit__, 3)
self.assertRaises(apsw.BusyError, b.__exit__, None, None, None)
b.__exit__(None, None, None)
def testLog(self):
"Verifies logging functions"
self.assertRaises(TypeError, apsw.log)
self.assertRaises(TypeError, apsw.log, 1)
self.assertRaises(TypeError, apsw.log, 1, 2)
self.assertRaises(TypeError, apsw.log, 1, 2, 3)
self.assertRaises(TypeError, apsw.log, 1, None)
apsw.log(apsw.SQLITE_MISUSE, "Hello world") # nothing should happen
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_LOG, 2)
self.assertRaises(TypeError, apsw.config, apsw.SQLITE_CONFIG_LOG)
try:
apsw.config(apsw.SQLITE_CONFIG_LOG, None)
apsw.log(apsw.SQLITE_MISUSE, "Hello world")
called = [0]
def handler(code, message, called=called):
called[0] += 1
self.assertEqual(code, apsw.SQLITE_MISUSE)
self.assertEqual(message, "a \u1234 unicode ' \ufe54 string \u0089")
apsw.config(apsw.SQLITE_CONFIG_LOG, handler)
apsw.log(apsw.SQLITE_MISUSE, "a \u1234 unicode ' \ufe54 string \u0089")
self.assertEqual(called[0], 1)
def badhandler(code, message, called=called):
if message and "apsw_write_unraisable" in message:
return
called[0] += 1
self.assertEqual(code, apsw.SQLITE_NOMEM)
self.assertEqual(message, "Xa \u1234 unicode ' \ufe54 string \u0089")
1 / 0
apsw.config(apsw.SQLITE_CONFIG_LOG, badhandler)
self.assertRaisesUnraisable(ZeroDivisionError, apsw.log, apsw.SQLITE_NOMEM,
"Xa \u1234 unicode ' \ufe54 string \u0089")
self.assertEqual(called[0], 2)
finally:
gc.collect()
apsw.config(apsw.SQLITE_CONFIG_LOG, None)
def testReadonly(self):
"Check Connection.readonly()"
self.assertEqual(self.db.readonly("main"), False)
c = apsw.Connection(TESTFILEPREFIX + "testdb", flags=apsw.SQLITE_OPEN_READONLY)
self.assertEqual(c.readonly("main"), True)
self.assertRaises(apsw.SQLError, self.db.readonly, "sdfsd")
class foo:
def __str__(self):
1 / 0
self.assertRaises(TypeError, self.db.readonly, foo())
def testFilename(self):
"Check connections and filenames"
self.assertTrue(self.db.filename.endswith("testdb"))
self.assertTrue(os.sep in self.db.filename)
self.assertEqual(self.db.filename, self.db.db_filename("main"))
self.assertRaises(TypeError, self.db.db_filename, 3)
self.db.cursor().execute("attach '%s' as foo" % (TESTFILEPREFIX + "testdb2", ))
self.assertEqual(self.db.filename + "2", self.db.db_filename("foo"))
self.assertTrue(self.db.filename_wal.startswith(self.db.filename))
self.assertTrue(self.db.filename_journal.startswith(self.db.filename))
xdb = apsw.Connection("")
# these all end up empty string
self.assertTrue(xdb.filename == xdb.filename_wal and xdb.filename_wal == xdb.filename_journal)
def testShell(self, shellclass=None):
"Check Shell functionality"
if shellclass is None:
shellclass = apsw.shell.Shell
fh = [open(TESTFILEPREFIX + "test-shell-" + t, "w+", encoding="utf8") for t in ("in", "out", "err")]
kwargs = {"stdin": fh[0], "stdout": fh[1], "stderr": fh[2]}
def reset():
for i in fh:
i.truncate(0)
i.seek(0)
def isempty(x):
self.assertEqual(get(x), "")
def isnotempty(x):
self.assertNotEqual(len(get(x)), 0)
def cmd(c):
assert fh[0].tell() == 0
fh[0].truncate(0)
fh[0].seek(0)
fh[0].write(c)
fh[0].seek(0)
def get(x):
x.seek(0)
return x.read()
# Make one and ensure help works
shellclass(stdin=fh[0], stdout=fh[1], stderr=fh[2], args=["", ".help"])
self.assertNotIn("Traceback", get(fh[2]))
reset()
# Lets give it some harmless sql arguments and do a sanity check
s = shellclass(args=[TESTFILEPREFIX + "testdb", "create table x(x)", "insert into x values(1)"], **kwargs)
self.assertTrue(s.db.filename.endswith("testdb"))
# do a dump and check our table is there with its values
s.command_dump([])
self.assertTrue("x(x)" in get(fh[1]))
self.assertTrue("(1);" in get(fh[1]))
# empty args
self.assertEqual((None, [], []), s.process_args(None))
# input description
reset()
write_whole_file(TESTFILEPREFIX + "test-shell-1", "wt", "syntax error")
try:
shellclass(args=[TESTFILEPREFIX + "testdb", ".read %stest-shell-1" % (TESTFILEPREFIX, )], **kwargs)
except shellclass.Error:
self.assertTrue("test-shell-1" in get(fh[2]))
isempty(fh[1])
# Check single and double dash behave the same
reset()
try:
shellclass(args=["-init"], **kwargs)
except shellclass.Error:
isempty(fh[1])
self.assertTrue("specify a filename" in get(fh[2]))
reset()
s = shellclass(**kwargs)
try:
s.process_args(["--init"])
except shellclass.Error:
self.assertTrue("specify a filename" in str(sys.exc_info()[1]))
# various command line options
# an invalid one
reset()
try:
shellclass(args=["---tripledash"], **kwargs)
except shellclass.Error:
isempty(fh[1])
self.assertTrue("-tripledash" in get(fh[2]))
self.assertTrue("--tripledash" not in get(fh[2]))
###
### --init
###
reset()
write_whole_file(TESTFILEPREFIX + "test-shell-1", "wt", "syntax error")
try:
shellclass(args=["-init", TESTFILEPREFIX + "test-shell-1"], **kwargs)
except shellclass.Error:
# we want to make sure it read the file
isempty(fh[1])
self.assertTrue("syntax error" in get(fh[2]))
reset()
write_whole_file(TESTFILEPREFIX + "test-shell-1", "wt", "select 3;")
shellclass(args=["-init", TESTFILEPREFIX + "test-shell-1"], **kwargs)
# we want to make sure it read the file
isempty(fh[2])
self.assertTrue("3" in get(fh[1]))
###
### --header
###
reset()
s = shellclass(**kwargs)
s.process_args(["--header"])
self.assertEqual(s.header, True)
s.process_args(["--noheader"])
self.assertEqual(s.header, False)
s.process_args(["--noheader", "-header", "-noheader", "--header"])
self.assertEqual(s.header, True)
s.process_args(["-no-colour", "--nocolor"])
self.assertEqual(s.colour_scheme, "off")
# did they actually turn on?
isempty(fh[1])
isempty(fh[2])
s.process_args([TESTFILEPREFIX + "testdb", ".mode column", "select 3"])
isempty(fh[2])
self.assertTrue("3" in get(fh[1]))
self.assertTrue("----" in get(fh[1]))
###
### --echo, --bail, --interactive
###
reset()
for v in ("echo", "bail", "interactive"):
s = shellclass(**kwargs)
b4 = getattr(s, v)
s.process_args(["--" + v])
# setting should have changed
self.assertNotEqual(b4, getattr(s, v))
isempty(fh[1])
isempty(fh[2])
###
### --batch
###
reset()
s = shellclass(**kwargs)
s.interactive = True
s.process_args(["-batch"])
self.assertEqual(s.interactive, False)
isempty(fh[1])
isempty(fh[2])
###
### --separator, --nullvalue, --encoding
###
for v, val in ("separator", "\n"), ("nullvalue", "abcdef"), ("encoding", "iso8859-1"):
reset()
s = shellclass(args=["--" + v, val], **kwargs)
# We need the eval because shell processes backslashes in
# string. After deliberating that is the right thing to
# do
if v == "encoding":
self.assertEqual((val, None), getattr(s, v))
else:
self.assertEqual(val, getattr(s, v))
isempty(fh[1])
isempty(fh[2])
self.assertRaises(shellclass.Error, shellclass, args=["-" + v, val, "--" + v], **kwargs)
isempty(fh[1])
self.assertTrue(v in get(fh[2]))
###
### --version
###
reset()
self.assertRaises(SystemExit, shellclass, args=["--version"], **kwargs)
# it writes to stdout
isempty(fh[2])
self.assertTrue(apsw.sqlite_lib_version() in get(fh[1]))
###
### --help
###
reset()
self.assertRaises(SystemExit, shellclass, args=["--help"], **kwargs)
# it writes to stderr
isempty(fh[1])
self.assertTrue("-version" in get(fh[2]))
###
### Items that correspond to output mode
###
reset()
shellclass(args=[
"--python", "--column", "--python", ":memory:", "create table x(x)", "insert into x values(x'aa')",
"select * from x;"
],
**kwargs)
isempty(fh[2])
self.assertTrue('b"' in get(fh[1]) or "buffer(" in get(fh[1]))
###
### Is process_unknown_args called as documented?
###
reset()
class s2(shellclass):
def process_unknown_args(self, args):
1 / 0
self.assertRaises(ZeroDivisionError, s2, args=["--unknown"], **kwargs)
isempty(fh[1])
self.assertTrue("division" in get(fh[2])) # py2 says "integer division", py3 says "int division"
class s3(shellclass):
def process_unknown_args(_, args):
self.assertEqual(args[0:2], ["myoption", "myvalue"])
return args[2:]
reset()
self.assertRaises(s3.Error, s3, args=["--python", "--myoption", "myvalue", "--init"], **kwargs)
isempty(fh[1])
self.assertTrue("-init" in get(fh[2]))
###
### .open
####
reset()
s = shellclass(**kwargs)
self.assertTrue(s.db.filename == "")
for n in "testdb", "testdb2", "testdb3":
fn = TESTFILEPREFIX + n
reset()
cmd(".open " + fn)
s.cmdloop()
self.assertTrue(s.db.filename.endswith(fn))
reset()
fn = TESTFILEPREFIX + "testdb"
cmd(".open " + fn)
cmd("create table foo(x); insert into foo values(2);")
s.cmdloop()
for row in s.db.cursor().execute("select * from foo"):
break
else:
self.fail("Table doesn't have any rows")
reset()
cmd(".open --wipe " + fn)
s.cmdloop()
for row in s.db.cursor().execute("select * from sqlite_schema"):
self.fail("--wipe didn't wipe file")
N = "sentinel-chidh-jklhfd"
class vfstest(apsw.VFS):
def __init__(self):
super().__init__(name=N, base="")
ref = vfstest()
reset()
cmd(f".open --vfs { N } { fn }\n.connection\n.close")
s.cmdloop()
isempty(fh[2])
self.assertIn(f"({ N })", get(fh[1]))
del ref
###
### Some test data
###
reset()
s = shellclass(**kwargs)
s.cmdloop()
def testnasty():
reset()
cmd("""create table if not exists nastydata(x,y);
insert into nastydata values(x'', 1e999); -- see issue 482 for zero sized blob
insert into nastydata values(null,'xxx\\u1234\\uabcdyyy\r\n\t\"this is nasty\u0001stuff!');
""")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".bail on\n.header OFF\nselect * from nastydata;")
s.cmdloop()
isempty(fh[2])
isnotempty(fh[1])
###
### Output formats - column
###
reset()
x = 'a' * 20
cmd(".mode column\n.header ON\nselect '" + x + "';")
s.cmdloop()
isempty(fh[2])
# colwidth should be 2 more
sep = '-' * (len(x) + 2) # apostrophes quoting string in column header
out = get(fh[1]).replace("\n", "")
self.assertEqual(len(out.split(sep)), 2)
self.assertEqual(len(out.split(sep)[0]), len(x) + 2) # plus two apostrophes
self.assertEqual(len(out.split(sep)[1]), len(x) + 2) # same
self.assertTrue(" " in out.split(sep)[1]) # space padding
# make sure truncation happens
reset()
cmd(".width 5\nselect '" + x + "';\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue("a" * 6 not in get(fh[1]))
# right justification
reset()
cmd(".header off\n.width -3 -3\nselect 3,3;\n.width 3 3\nselect 3,3;")
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
self.assertTrue(v.startswith(" 3 3"))
v = v.split("\n")
self.assertNotEqual(v[0], v[1])
self.assertEqual(len(v[0]), len(v[1]))
# do not output blob as is
self.assertTrue("\xaa" not in get(fh[1]))
# undo explain
reset()
cmd(".explain OFF\n")
s.cmdloop()
testnasty()
###
### Output formats - csv
###
reset()
# mode change should reset separator
cmd(".separator F\n.mode csv\nselect 3,3;\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue("3,3" in get(fh[1]))
# tab sep
reset()
cmd(".separator '\\t'\nselect 3,3;\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue("3\t3" in get(fh[1]))
# back to comma
reset()
cmd(".mode csv\nselect 3,3;\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue("3,3" in get(fh[1]))
# quoting
reset()
cmd(".header ON\nselect 3 as [\"one\"], 4 as [\t];\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue('"""one""",\t' in get(fh[1]))
# custom sep
reset()
cmd(".separator |\nselect 3 as [\"one\"], 4 as [\t];\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue("3|4\n" in get(fh[1]))
self.assertTrue('"one"|\t\n' in get(fh[1]))
# testnasty() - csv module is pretty much broken
###
### Output formats - html
###
reset()
cmd(".mode html\n.header OFF\nselect 3,4;\n")
s.cmdloop()
isempty(fh[2])
# should be no header
self.assertTrue("<th>" not in get(fh[1]).lower())
# does it actually work?
self.assertTrue("<td>3</td>" in get(fh[1]).lower())
# check quoting works
reset()
cmd(".header ON\nselect 3 as [<>&];\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue("<th><>&</th>" in get(fh[1]).lower())
# do we output rows?
self.assertTrue("<tr>" in get(fh[1]).lower())
self.assertTrue("</tr>" in get(fh[1]).lower())
testnasty()
###
### Output formats - insert
###
reset()
all = "3,3.1,'3.11',null,x'0311'"
cmd(".mode insert\n.header OFF\nselect " + all + ";\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue(all in get(fh[1]).lower())
# empty values
reset()
all = "0,0.0,'',null,x''"
cmd("select " + all + ";\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue(all in get(fh[1]).lower())
# header, separator and nullvalue should make no difference
save = get(fh[1])
reset()
cmd(".header ON\n.separator %\n.nullvalue +\nselect " + all + ";\n")
s.cmdloop()
isempty(fh[2])
self.assertEqual(save, get(fh[1]))
# check the table name
self.assertTrue(get(fh[1]).lower().startswith('insert into "table" values'))
reset()
cmd(".mode insert funkychicken\nselect " + all + ";\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue(get(fh[1]).lower().startswith("insert into funkychicken values"))
testnasty()
###
### Output formats - json
###
reset()
all = "3,2.2,'string',null,x'0311'"
cmd(".mode json\n.header ON\n select " + all + ";")
s.cmdloop()
isempty(fh[2])
out = json.loads(get(fh[1]))
self.assertEqual(out, [{"3": 3, "2.2": 2.2, "'string'": "string", "null": None, "x'0311'": "AxE="}])
# a regular table
reset()
cmd(f"""create table jsontest([int], [float], [string], [null], [blob]);
insert into jsontest values({ all });
insert into jsontest values({ all });
select * from jsontest;""")
s.cmdloop()
isempty(fh[2])
out = json.loads(get(fh[1]))
self.assertEqual(out, [{"int": 3, "float": 2.2, "string": "string", "null": None, "blob": "AxE="}] * 2)
testnasty()
###
### Output formats - jsonl
###
reset()
cmd(".mode jsonl\n.header ON\n select " + all + ";")
s.cmdloop()
isempty(fh[2])
v = get(fh[1]).strip()
out = json.loads(v)
self.assertEqual(out, {"3": 3, "2.2": 2.2, "'string'": "string", "null": None, "x'0311'": "AxE="})
reset()
cmd("select * from jsontest;")
s.cmdloop()
isempty(fh[2])
out = [json.loads(line) for line in get(fh[1]).splitlines()]
self.assertEqual(out, [{"int": 3, "float": 2.2, "string": "string", "null": None, "blob": "AxE="}] * 2)
testnasty()
###
### Output formats - line
###
reset()
cmd(".header OFF\n.nullvalue *\n.mode line\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa' as e;\n")
s.cmdloop()
isempty(fh[2])
out = get(fh[1]).replace(" ", "")
self.assertTrue("a=3\n" in out)
self.assertTrue("b=*\n" in out)
self.assertTrue("c=0.0\n" in out)
self.assertTrue("d=a\n" in out)
self.assertTrue("e=<Binarydata>\n" in out)
self.assertEqual(7, len(out.split("\n"))) # one for each col plus two trailing newlines
# header should make no difference
reset()
cmd(".header ON\n.nullvalue *\n.mode line\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa' as e;\n")
s.cmdloop()
isempty(fh[2])
self.assertEqual(out, get(fh[1]).replace(" ", ""))
# wide column name
reset()
ln = "kjsfhgjksfdjkgfhkjsdlafgjkhsdkjahfkjdsajfhsdja" * 12
cmd("select 3 as %s, 3 as %s1;" % (ln, ln))
s.cmdloop()
isempty(fh[2])
self.assertEqual(get(fh[1]), " %s = 3\n%s1 = 3\n\n" % (ln, ln))
testnasty()
###
### Output formats - list
###
reset()
cmd(".header off\n.mode list\n.nullvalue (\n.separator &\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa' as e;\n"
)
s.cmdloop()
isempty(fh[2])
self.assertEqual(get(fh[1]), '3&(&0.0&a&<Binary data>\n')
reset()
# header on
cmd(".header on\n.mode list\n.nullvalue (\n.separator &\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa' as e;\n"
)
s.cmdloop()
isempty(fh[2])
self.assertTrue(get(fh[1]).startswith("a&b&c&d&e\n"))
testnasty()
###
### Output formats - python
###
reset()
cmd(".header off\n.mode python\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa44bb' as e;\n")
s.cmdloop()
isempty(fh[2])
v = eval(get(fh[1]))
self.assertEqual(len(v), 1) # 1 tuple
self.assertEqual(v, ((3, None, 0.0, 'a', b"\xaa\x44\xbb"), ))
reset()
cmd(".header on\n.mode python\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa44bb' as e;\n")
s.cmdloop()
isempty(fh[2])
v = eval("(" + get(fh[1]) + ")") # need parentheses otherwise indent rules apply
self.assertEqual(len(v), 2) # headers and row
self.assertEqual(v, (
("a", "b", "c", "d", "e"),
(3, None, 0.0, 'a', b"\xaa\x44\xbb"),
))
testnasty()
###
### Output formats - TCL
###
reset()
cmd(".header off\n.mode tcl\n.separator -\n.nullvalue ?\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa44bb' as e;\n"
)
s.cmdloop()
isempty(fh[2])
self.assertEqual(get(fh[1]), '"3"-"?"-"0.0"-"a"-"\\xAAD\\xBB"\n')
reset()
cmd(".header on\nselect 3 as a, null as b, 0.0 as c, 'a' as d, x'aa44bb' as e;\n")
s.cmdloop()
isempty(fh[2])
self.assertTrue('"a"-"b"-"c"-"d"-"e"' in get(fh[1]))
testnasty()
###
### Output formats - box/qbox/table
###
if hasattr(s, "output_box"):
# ensure others error
reset()
cmd(".mode tcl --foo bar")
self.assertRaises(apsw.shell.Shell.Error, s.cmdloop)
for mode in "box", "qbox", "table":
reset()
cmd(f".mode { mode } --no-unicode --word-wrap --width 65")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
testnasty()
# What happens if db cannot be opened?
s.process_args(args=["/"])
reset()
cmd("select * from sqlite_schema;\n.bail on\nselect 3;\n")
self.assertRaises(apsw.CantOpenError, s.cmdloop)
isempty(fh[1])
self.assertTrue("unable to open database file" in get(fh[2]))
# echo testing - multiple statements
s.process_args([":memory:"]) # back to memory db
reset()
cmd(".bail off\n.echo on\nselect 3;\n")
s.cmdloop()
self.assertTrue("select 3;\n" in get(fh[2]))
# multiline
reset()
cmd("select 3;select 4;\n")
s.cmdloop()
self.assertTrue("select 3;\n" in get(fh[2]))
self.assertTrue("select 4;\n" in get(fh[2]))
# multiline with error
reset()
cmd("select 3;select error;select 4;\n")
s.cmdloop()
# worked line should be present
self.assertTrue("select 3;\n" in get(fh[2]))
# as should the error
self.assertTrue("no such column: error" in get(fh[2]))
# is timing info output correctly?
reset()
timersupported = False
try:
cmd(".bail on\n.echo off\n.timer on\n.timer off\n")
s.cmdloop()
timersupported = True
except s.Error:
pass
if timersupported:
reset()
# create something that should take some time to execute
s.db.cursor().execute("create table xyz(x); begin;")
s.db.cursor().executemany("insert into xyz values(?)", randomintegers(4000))
s.db.cursor().execute("end")
reset()
# this takes .6 seconds on my machine so we should
# definitely have non-zero timing information
cmd(".timer ON\nselect max(x),min(x),max(x+x),min(x-x) from xyz union select x+max(x),x-min(x),3,4 from xyz union select x,x,x,x from xyz union select x,x,x,x from xyz;select 3;\n"
)
s.cmdloop()
isnotempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".bail off\n.timer off")
s.cmdloop()
# command handling
reset()
cmd(".nonexist 'unclosed")
s.cmdloop()
isempty(fh[1])
self.assertTrue("no closing quotation" in get(fh[2]).lower())
reset()
cmd(".notexist ")
s.cmdloop()
isempty(fh[1])
self.assertTrue('Unknown command "notexist"' in get(fh[2]))
###
### Commands - backup and restore
###
reset()
cmd(".backup with too many parameters")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".backup ") # too few
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".restore with too many parameters")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".restore ") # too few
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
# bogus filenames
for i in ('/', '"main" /'):
for c in (".backup ", ".restore "):
reset()
cmd(c + i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
def randomtable(cur, dbname=None):
name = list("abcdefghijklmnopqrstuvwxtz")
random.shuffle(name)
name = "".join(name)
fullname = name
if dbname:
fullname = dbname + "." + fullname
cur.execute("begin;create table %s(x)" % (fullname, ))
cur.executemany("insert into %s values(?)" % (fullname, ), randomintegers(400))
cur.execute("end")
return name
# Straight forward backup. The gc.collect() is needed because
# non-gc cursors hanging around will prevent the backup from
# happening.
n = randomtable(s.db.cursor())
contents = s.db.cursor().execute("select * from " + n).fetchall()
reset()
cmd(".backup %stestdb2" % (TESTFILEPREFIX, ))
gc.collect()
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd("drop table " + n + ";")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
self.assertTrue(os.path.isfile("%stestdb2" % (TESTFILEPREFIX, )))
reset()
cmd(".restore %stestdb2" % (TESTFILEPREFIX, ))
gc.collect()
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
newcontents = s.db.cursor().execute("select * from " + n).fetchall()
# no guarantee of result order
contents.sort()
newcontents.sort()
self.assertEqual(contents, newcontents)
# do they pay attention to the dbname
s.db.cursor().execute("attach ':memory:' as memdb")
n = randomtable(s.db.cursor(), "memdb")
contents = s.db.cursor().execute("select * from memdb." + n).fetchall()
reset()
gc.collect()
cmd(".backup memdb %stestdb2" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
s.db.cursor().execute("detach memdb; attach ':memory:' as memdb2")
reset()
gc.collect()
cmd(".restore memdb2 %stestdb2" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
newcontents = s.db.cursor().execute("select * from memdb2." + n).fetchall()
# no guarantee of result order
contents.sort()
newcontents.sort()
self.assertEqual(contents, newcontents)
###
### Commands - bail
###
reset()
cmd(".bail")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".bail on\n.mode list\nselect 3;\nselect error;\nselect 4;\n")
self.assertRaises(apsw.Error, s.cmdloop)
self.assertTrue("3" in get(fh[1]))
self.assertTrue("4" not in get(fh[1]))
reset()
cmd(".bail oFf\n.mode list\nselect 3;\nselect error;\nselect 4;\n")
s.cmdloop()
self.assertTrue("3" in get(fh[1]))
self.assertTrue("4" in get(fh[1]))
###
### Commands - changes
###
reset()
cmd(".changes on\nselect 3;")
s.cmdloop()
isempty(fh[2])
self.assertNotIn("changes:", get(fh[1]))
reset()
cmd("create table testchange(x); insert into testchange values(3);")
s.cmdloop()
isempty(fh[2])
self.assertIn("changes:", get(fh[1]))
reset()
cmd(".changes off\ninsert into testchange values(4);")
s.cmdloop()
isempty(fh[2])
self.assertNotIn("changes:", get(fh[1]))
###
### Commands - cd / connection / close
###
@contextlib.contextmanager
def chdir(path):
before = os.getcwd()
os.chdir(path)
yield
os.chdir(before)
def in_open_dbs(filename):
count = 0
for c in apsw.connections():
try:
if not c.filename:
continue
except apsw.ConnectionClosedError:
continue
if os.path.samefile(filename, c.filename):
count += 1
return count
with tempfile.TemporaryDirectory(prefix="apsw-shell-test-") as tmpd1:
with chdir("."):
reset()
cmd(f".cd { shlex.quote( tmpd1 )}")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
self.assertTrue(os.path.samefile(tmpd1, os.getcwd()))
with chdir(tmpd1):
reset()
V = "sentinel-jhdgfsfjdskh-1"
cmd(f".open { shlex.quote(V) }\ncreate table foo(x);\n.open { shlex.quote(V) }")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
self.assertEqual(2, in_open_dbs(V))
reset()
cmd(f".open --wipe { shlex.quote(V) }")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
self.assertEqual(1, in_open_dbs(V))
reset()
cmd(f".close")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
self.assertEqual(0, in_open_dbs(V))
###
### Commands - databases
###
reset()
cmd(".databases foo")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
# clean things up
s = shellclass(**kwargs)
reset()
cmd(".header oFF\n.databases")
s.cmdloop()
isempty(fh[2])
for i in "main", "name", "file":
self.assertTrue(i in get(fh[1]))
reset()
cmd("attach '%stestdb' as quack;\n.databases" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[2])
for i in "main", "name", "file", "testdb", "quack":
self.assertTrue(i in get(fh[1]))
reset()
cmd("detach quack;")
s.cmdloop()
isempty(fh[2])
for i in "testdb", "quack":
self.assertTrue(i not in get(fh[1]))
###
### Command - dbconfig
###
reset()
cmd(".dbconfig")
s.cmdloop()
isempty(fh[2])
self.assertIn("trigger_eqp:", get(fh[1]))
reset()
self.assertFalse(s.db.config(apsw.SQLITE_DBCONFIG_TRIGGER_EQP, -1))
cmd(".dbconfig trigger_eqp 1")
s.cmdloop()
isempty(fh[2])
self.assertTrue(s.db.config(apsw.SQLITE_DBCONFIG_TRIGGER_EQP, -1))
###
### Command - dbinfo
###
if hasattr(s, "command_dbinfo"):
with tempfile.TemporaryDirectory(prefix="apsw-test-shell-dbinfo-") as tmpd:
reset()
cmd(f".open { shlex.quote(tmpd) }/newdb")
s.cmdloop()
isempty(fh[2])
reset()
cmd(
textwrap.dedent("""
.dbinfo
pragma journal_mode=wal;
.dbinfo
pragma journal_mode=persist;
.dbinfo
pragma encoding="UTF-16";
.dbinfo
create table x(y);
.dbinfo
"""))
s.cmdloop()
isnotempty(fh[1])
isempty(fh[2])
reset()
cmd(".close\n.connection 0")
s.cmdloop()
###
### Commands - dump
###
reset()
cmd("create table foo(x); create table bar(x);\n.dump foox")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".dump foo")
s.cmdloop()
isempty(fh[2])
for i in "foo", "create table", "begin", "commit":
self.assertTrue(i in get(fh[1]).lower())
self.assertTrue("bar" not in get(fh[1]).lower())
# can we do virtual tables?
reset()
if self.checkOptionalExtension("fts3", "create virtual table foo using fts3()"):
reset()
cmd("CREATE virtual TaBlE fts3 using fts3(colA FRED , colB JOHN DOE);\n"
"insert into fts3 values('one', 'two');insert into fts3 values('onee', 'two');\n"
"insert into fts3 values('one', 'two two two');")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".dump")
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
for i in "pragma writable_schema", "create virtual table fts3", "cola fred", "colb john doe":
self.assertTrue(i in v.lower())
# analyze
reset()
cmd("drop table bar;create table bar(x unique,y);create index barf on bar(x,y);create index barff on bar(y);insert into bar values(3,4);\nanalyze;\n.dump bar"
)
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
for i in "analyze bar", "create index barf":
self.assertTrue(i in v.lower())
self.assertTrue("autoindex" not in v.lower()) # created by sqlite to do unique constraint
self.assertTrue("sqlite_sequence" not in v.lower()) # not autoincrements
# repeat but all tables
reset()
cmd(".dump")
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
for i in "analyze bar", "create index barf":
self.assertTrue(i in v.lower())
self.assertTrue("autoindex" not in v.lower()) # created by sqlite to do unique constraint
# foreign keys
reset()
cmd("create table xxx(z references bar(x));\n.dump")
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
for i in "foreign_keys", "references":
self.assertTrue(i in v.lower())
# views
reset()
cmd("create view noddy as select * from foo;\n.dump noddy")
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
for i in "drop view", "create view noddy":
self.assertTrue(i in v.lower())
# issue82 - view ordering
reset()
cmd("create table issue82(x);create view issue82_2 as select * from issue82; create view issue82_1 as select count(*) from issue82_2;\n.dump issue82%"
)
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
s.db.cursor().execute("drop table issue82 ; drop view issue82_1 ; drop view issue82_2")
reset()
cmd(v)
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
# autoincrement
reset()
cmd("create table abc(x INTEGER PRIMARY KEY AUTOINCREMENT); insert into abc values(null);insert into abc values(null);\n.dump"
)
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
for i in "sqlite_sequence", "'abc', 2":
self.assertTrue(i in v.lower())
# user version
self.assertTrue("user_version" not in v)
reset()
cmd("pragma user_version=27;\n.dump")
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
self.assertTrue("pragma user_version=27;" in v)
s.db.cursor().execute("pragma user_version=0")
# some nasty stuff
reset()
cmd("create table nastydata(x,y); insert into nastydata values(null,'xxx\\u1234\\uabcd\\U00012345yyy\r\n\t\"this is nasty\u0001stuff!');"
'create table "table"([except] int); create table [](""); create table [using]("&");')
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".dump")
s.cmdloop()
isempty(fh[2])
v = get(fh[1])
self.assertTrue("nasty" in v)
self.assertTrue("stuff" in v)
# sanity check the dumps
reset()
cmd(v) # should run just fine
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
# drop all the tables we made to do another dump and compare with before
for t in "abc", "bar", "foo", "fts3", "xxx", "noddy", "sqlite_sequence", "sqlite_stat1", \
"issue82", "issue82_1", "issue82_2":
reset()
cmd("drop table %s;drop view %s;" % (t, t))
s.cmdloop() # there will be errors which we ignore
reset()
cmd(v)
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
# another dump
reset()
cmd(".dump")
s.cmdloop()
isempty(fh[2])
v2 = get(fh[1])
v = re.sub("-- Date:.*", "", v)
v2 = re.sub("-- Date:.*", "", v2)
self.assertEqual(v, v2)
# clean database
reset()
s = shellclass(args=[':memory:'], **kwargs)
cmd(v)
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(v2 + "\n.dump")
s.cmdloop()
isempty(fh[2])
v3 = get(fh[1])
v3 = re.sub("-- Date:.*", "", v3)
self.assertEqual(v, v3)
# trailing comments
reset()
cmd("""create table xxblah(b -- ff
) -- xx
; create index xxfoo on xxblah(b -- ff
) -- xx
; create view xxbar as select * from xxblah -- ff
;
insert into xxblah values(3);
.dump
""")
s.cmdloop()
isempty(fh[2])
dump = get(fh[1])
reset()
cmd("drop table xxblah; drop view xxbar;")
s.cmdloop()
isempty(fh[2])
isempty(fh[1])
reset()
cmd(dump)
s.cmdloop()
isempty(fh[2])
isempty(fh[1])
self.assertEqual(s.db.cursor().execute("select * from xxbar").fetchall(), [(3, )])
# check index
reset()
cmd("drop index xxfoo;")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
###
### Command - echo
###
reset()
cmd(".echo")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".echo bananas")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".echo on on")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".echo off\nselect 3;")
s.cmdloop()
self.assertTrue("3" in get(fh[1]))
self.assertTrue("select 3" not in get(fh[2]))
reset()
cmd(".echo on\nselect 3;")
s.cmdloop()
self.assertTrue("3" in get(fh[1]))
self.assertTrue("select 3" in get(fh[2]))
# more complex testing is done earlier including multiple statements and errors
###
### Command - encoding
###
self.suppressWarning("ResourceWarning")
for i in ".encoding one two", ".encoding", ".encoding utf8 another":
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".encoding this-does-not-exist")
s.cmdloop()
isempty(fh[1])
self.assertTrue("no known encoding" in get(fh[2]).lower())
# use iso8859-1 to make sure data is read correctly - it
# differs from utf8
us = "unitestdata \xaa\x89 34"
write_whole_file(TESTFILEPREFIX + "test-shell-1",
"w",
f"insert into enctest values('{ us }');\n",
encoding="iso8859-1")
gc.collect()
reset()
cmd(".encoding iso8859-1\ncreate table enctest(x);\n.echo on\n.read %stest-shell-1\n.echo off" %
(TESTFILEPREFIX, ))
s.cmdloop()
self.assertEqual(s.db.cursor().execute("select * from enctest").fetchall()[0][0], us)
self.assertTrue(us in get(fh[2]))
reset()
write_whole_file(TESTFILEPREFIX + "test-shell-1", "w", us + "\n", encoding="iso8859-1")
cmd("drop table enctest;create table enctest(x);\n.import %stest-shell-1 enctest" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[2])
isempty(fh[1])
self.assertEqual(s.db.cursor().execute("select * from enctest").fetchall()[0][0], us)
reset()
cmd(".output %stest-shell-1\n.mode list\nselect * from enctest;" % (TESTFILEPREFIX, ))
s.cmdloop()
self.assertEqual(
read_whole_file(TESTFILEPREFIX + "test-shell-1", "rb").strip(), # skip eol
us.encode("iso8859-1"))
reset()
cmd(".output stdout\nselect '%s';\n" % (us, ))
s.cmdloop()
isempty(fh[2])
self.assertTrue(us in get(fh[1]))
### encoding specifying error handling - see issue 108
reset()
cmd(".encoding utf8:replace")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
# non-existent error
reset()
cmd(".encoding cp437:blahblah")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
self.assertTrue("blahblah" in get(fh[2]))
# check replace works
reset()
us = "\N{BLACK STAR}8\N{WHITE STAR}"
write_whole_file(TESTFILEPREFIX + "test-shell-1",
"w",
f"insert into enctest values('{ us }');",
encoding="utf8")
cmd(".encoding utf8\n.read %stest-shell-1\n.encoding cp437:replace\n.output %stest-shell-1\nselect * from enctest;\n.encoding utf8\n.output stdout"
% (TESTFILEPREFIX, TESTFILEPREFIX))
s.cmdloop()
isempty(fh[2])
isempty(fh[1])
self.assertTrue("?8?" in read_whole_file(TESTFILEPREFIX + "test-shell-1", "rt", "cp437"))
###
### Command - exceptions
###
reset()
cmd("syntax error;")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
self.assertTrue(len(get(fh[2]).split("\n")) < 5)
reset()
s.db.create_scalar_function("make_error", lambda: 1 / 0)
cmd(".exceptions on\nselect make_error();")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
self.assertTrue(len(get(fh[2]).split("\n")) > 10)
self.assertTrue("sql = " in get(fh[2]))
# deliberately leave exceptions on
###
### Command - exit
###
for i in (".exit", ):
reset()
cmd(i)
self.assertRaises(SystemExit, s.cmdloop)
isempty(fh[1])
isempty(fh[2])
reset()
cmd(i + " jjgflk")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
###
### Explain auto format
###
reset()
cmd(".mode tcl\n.header off\nselect 3;")
s.cmdloop()
isempty(fh[2])
self.assertIn('"3"', get(fh[1]))
reset()
cmd("explain select 3;")
s.cmdloop()
isempty(fh[2])
self.assertIn('opcode', get(fh[1]))
reset()
cmd("explain query plan select 3;")
s.cmdloop()
isempty(fh[2])
self.assertIn('detail', get(fh[1]))
reset()
cmd("select 3;")
s.cmdloop()
isempty(fh[2])
self.assertIn('"3"', get(fh[1]))
###
### Command find
###
reset()
cmd(".find one two three")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd("create table findtest([x\" x],y); insert into findtest values(3, 'xx3'); insert into findtest values(34, 'abcd');"
)
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".find 3")
s.cmdloop()
isempty(fh[2])
for text, present in (("findtest", True), ("xx3", True), ("34", False)):
if present:
self.assertTrue(text in get(fh[1]))
else:
self.assertTrue(text not in get(fh[1]))
reset()
cmd(".find does-not-exist")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".find ab_d")
s.cmdloop()
isempty(fh[2])
for text, present in (("findtest", True), ("xx3", False), ("34", True)):
if present:
self.assertTrue(text in get(fh[1]))
else:
self.assertTrue(text not in get(fh[1]))
reset()
cmd(".find 3 table-not-exist")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
###
### Command help
###
reset()
cmd(".help\n.help all\n.help import backup")
s.cmdloop()
isempty(fh[1])
for i in ".import", "Reads data from the file":
self.assertTrue(i in get(fh[2]))
reset()
cmd(".help backup notexist import")
s.cmdloop()
isempty(fh[1])
for i in "Copies the contents", "No such command":
self.assertTrue(i in get(fh[2]))
# screw up terminal width
origtw = s._terminal_width
def tw(minimum):
return minimum
s._terminal_width = tw
reset()
cmd(".bail on\n.help all\n.bail off")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
###
### Command - import
###
# check it fundamentally works
reset()
cmd(".encoding utf16\ncreate table imptest(x real, y char);\n"
"insert into imptest values(3.1, 'xabc');\n"
"insert into imptest values(3.2, 'xabfff\"ffffc');\n"
".output %stest-shell-1\n.mode csv\nselect * from imptest;\n"
".output stdout" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
# make sure encoding took
self.assertTrue(b"xab" not in read_whole_file(TESTFILEPREFIX + "test-shell-1", "rb"))
data = s.db.cursor().execute("select * from imptest; delete from imptest").fetchall()
self.assertEqual(2, len(data))
reset()
cmd(".import %stest-shell-1 imptest" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
newdata = s.db.cursor().execute("select * from imptest; drop table imptest").fetchall()
data.sort()
newdata.sort()
self.assertEqual(data, newdata)
# error handling
for i in ".import", ".import one", ".import one two three", ".import nosuchfile nosuchtable", ".import nosuchfile sqlite_schema":
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
# wrong number of columns
reset()
cmd("create table imptest(x,y);\n.mode tabs\n.output %stest-shell-1\nselect 3,4;select 5,6;select 7,8,9;" %
(TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".output stdout\n.import %stest-shell-1 imptest" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
# check it was done in a transaction and aborted
self.assertEqual(0, s.db.cursor().execute("select count(*) from imptest").fetchall()[0][0])
###
### Command - autoimport
###
# errors
for i in ".autoimport", ".autoimport 1 2 3", ".autoimport nosuchfile", ".autoimport %stest-shell-1 sqlite_schema" % (
TESTFILEPREFIX, ):
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
# check correct detection with each type of separator and that types are not mangled
c = s.db.cursor()
for row in (
('a,b', '21/1/20', '00'),
(' ', '1/1/20', 10),
('a"b', '1/1/01', '00'),
('+40', '01123', '2010 100 15'),
('2010//10//13', '2010/10/13 12', 2),
('2010/13/13 12:13', '13/13/2010 12:93', '13/2010/13'),
("+3", " 3", 3),
("03.03", "03.03.20", "03"),
(
(None, 2, 5.5),
(None, 4, 99),
),
):
c.execute("""drop table if exists aitest ; create table aitest("x y", ["], "3d")""")
if isinstance(row[0], tuple):
f = c.executemany
else:
f = c.execute
f("insert into aitest values(?,?,?)", row)
fname = TESTFILEPREFIX + "test-shell-1"
for sep in "\t", "|", ",", "X":
reset()
cmd(".mode csv\n.headers on\n.output %stest-shell-1\n.separator \"%s\"\nselect * from aitest;\n.output stdout\n.separator X\ndrop table if exists \"test-shell-1\";\n.autoimport %stest-shell-1"
% (TESTFILEPREFIX, sep, TESTFILEPREFIX))
s.cmdloop()
isnotempty(fh[1])
isempty(fh[2])
self.assertTablesEqual(s.db, "aitest", s.db, "test-shell-1")
# Change encoding back to sensible
reset()
cmd(".encoding utf8")
s.cmdloop()
# Check date detection
for expect, fmt, sequences in (("1999-10-13", "%d-%d:%d", (
(1999, 10, 13),
(13, 10, 1999),
(10, 13, 1999),
)), ("1999-10-13T12:14:17", "%d/%d/%d/%d/%d/%d", (
(1999, 10, 13, 12, 14, 17),
(13, 10, 1999, 12, 14, 17),
(10, 13, 1999, 12, 14, 17),
)), ("1999-10-13T12:14:00", "%dX%dX%dX%dX%d", (
(1999, 10, 13, 12, 14),
(13, 10, 1999, 12, 14),
(10, 13, 1999, 12, 14),
))):
for seq in sequences:
write_whole_file(TESTFILEPREFIX + "test-shell-1", "wt", ("a,b\nrow," + (fmt % seq) + "\n"))
reset()
cmd("drop table [test-shell-1];\n.autoimport %stest-shell-1" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[2])
imp = c.execute("select b from [test-shell-1] where a='row'").fetchall()[0][0]
self.assertEqual(imp, expect)
# Check diagnostics when unable to import
for err, content in (
("current encoding", b"\x81\x82\x83\tfoo\n\x84\x97\xff\tbar"),
("known type", "abcdef\nhiojklmnop\n"),
("more than one", 'ab,c\tdef\nqr,dd\t\n'),
("ambiguous data format", "a,b\n1/1/2001,3\n2001/4/4,4\n"),
):
if isinstance(content, bytes):
continue
write_whole_file(TESTFILEPREFIX + "test-shell-1", "wt", content)
reset()
cmd("drop table [test-shell-1];\n.autoimport %stest-shell-1" % (TESTFILEPREFIX, ))
s.cmdloop()
errmsg = get(fh[2])
self.assertTrue(err in errmsg)
###
### Command - indices
###
for i in ".indices", ".indices one two":
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd("create table indices(x unique, y unique); create index shouldseethis on indices(x,y);")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".indices indices")
s.cmdloop()
isempty(fh[2])
for i in "shouldseethis", "autoindex":
self.assertTrue(i in get(fh[1]))
###
### Command - load
###
if hasattr(APSW, "testLoadExtension"):
lf = LOADEXTENSIONFILENAME
for i in ".load", ".load one two three":
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".load nosuchfile")
s.cmdloop()
isempty(fh[1])
self.assertTrue("nosuchfile" in get(fh[2]) or "ExtensionLoadingError" in get(fh[2]))
reset()
cmd(".mode list\n.load " + lf + " alternate_sqlite3_extension_init\nselect doubleup(2);")
s.cmdloop()
isempty(fh[2])
self.assertTrue("4" in get(fh[1]))
reset()
cmd(".mode list\n.load " + lf + "\nselect half(2);")
s.cmdloop()
isempty(fh[2])
self.assertTrue("1" in get(fh[1]))
###
### Command - log
###
reset()
cmd(".log on\n+;")
s.cmdloop()
self.assertIn("SQLITE_ERROR", get(fh[2]))
reset()
cmd(".log off\n+;")
s.cmdloop()
self.assertNotIn("SQLITE_ERROR", get(fh[2]))
###
### Command - mode
###
# already thoroughly tested in code above
for i in ".mode", ".mode foo more", ".mode invalid":
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
###
### command nullvalue & separator
###
# already tested in code above
for i in ".nullvalue", ".nullvalue jkhkl lkjkj", ".separator", ".separator one two":
reset()
cmd(i)
b4 = s.nullvalue, s.separator
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
self.assertEqual(b4, (s.nullvalue, s.separator))
###
### command output
###
for i in ".output", ".output too many args", ".output " + os.sep:
reset()
cmd(i)
b4 = s.stdout
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
self.assertEqual(b4, s.stdout)
###
### Command - parameter
###
reset()
cmd("select :foo;")
s.cmdloop()
isempty(fh[1])
self.assertIn("No binding present for 'foo' -", get(fh[2]))
for val in ('orange', 'banana'):
reset()
cmd(f".parameter set foo '{ val }'\nselect $foo;")
s.cmdloop()
isempty(fh[2])
self.assertIn(val, get(fh[1]))
reset()
cmd(".parameter list")
s.cmdloop()
isempty(fh[2])
self.assertIn("banana", get(fh[1]))
reset()
cmd(".parameter unset foo\nselect $foo;")
s.cmdloop()
isempty(fh[1])
self.assertIn("No binding present for 'foo' -", get(fh[2]))
reset()
cmd(".parameter set bar 3\n.parameter clear\nselect $bar;")
s.cmdloop()
isempty(fh[1])
self.assertIn("No binding present for 'bar' -", get(fh[2]))
###
### Command prompt
###
# not much to test until pty testing is working
for i in ".prompt", ".prompt too many args":
reset()
cmd(i)
b4 = s.prompt, s.moreprompt
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
self.assertEqual(b4, (s.prompt, s.moreprompt))
###
### Command - py
###
reset()
cmd(".py None")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
reset()
cmd(".py [")
s.cmdloop()
self.assertIn("Incomplete", get(fh[2]))
# interactive use is tested interactively
###
### Command read
###
# pretty much thoroughly tested above
write_whole_file(TESTFILEPREFIX + "test-shell-1.py", "wt", """
assert apsw
assert shell
shell.write(shell.stdout, "hello world\\n")
""")
for i in ".read", ".read one two", ".read " + os.sep:
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
reset()
cmd(".read %stest-shell-1.py" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[2])
self.assertTrue("hello world" in get(fh[1]))
# restore tested with backup
###
### Command - schema
###
# make sure it works
reset()
cmd(".schema")
s.cmdloop()
isempty(fh[2])
isnotempty(fh[1])
reset()
cmd("create table schematest(x);create index unrelatedname on schematest(x);\n.schema schematest foo notexist foo"
)
s.cmdloop()
isempty(fh[2])
for i in "schematest", "unrelatedname":
self.assertTrue(i in get(fh[1]))
# separator done earlier
###
### Command - shell
###
reset()
# this uses the process stdout/err which we can't capture without heroics
cmd(".shell exit 1")
s.cmdloop()
self.assertIn("Exit code", get(fh[2]))
reset()
cmd(".shell %s > %s" % ("dir" if sys.platform == "win32" else "ls", os.devnull))
s.cmdloop()
# should always work on these platforms
if sys.platform in {"win32", "linux", "darwin"}:
self.assertNotIn("Exit code", get(fh[2]))
###
### Command - show
###
# set all settings to known values
resetcmd = ".echo off\n.changes off\n.headers off\n.mode list\n.nullvalue ''\n.output stdout\n.separator |\n.width 1 2 3\n.exceptions off"
reset()
cmd(resetcmd)
s.cmdloop()
isempty(fh[2])
isempty(fh[1])
reset()
cmd(".show")
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
baseline = get(fh[2])
for i in ".echo on", ".changes on", ".headers on", ".mode column", ".nullvalue T", ".separator %", ".width 8 9 1", ".exceptions on":
reset()
cmd(resetcmd)
s.cmdloop()
isempty(fh[1])
if not get(fh[2]).startswith(".echo off"):
isempty(fh[2])
reset()
cmd(i + "\n.show")
s.cmdloop()
isempty(fh[1])
# check size has not changed much
self.assertTrue(abs(len(get(fh[2])) - len(baseline)) < 14)
# output
reset()
cmd(".output %stest-shell-1\n.show" % (TESTFILEPREFIX, ))
s.cmdloop()
isempty(fh[1])
self.assertTrue("output: " + TESTFILEPREFIX + "test-shell-1" in get(fh[2]))
reset()
cmd(".output stdout\n.show")
s.cmdloop()
isempty(fh[1])
self.assertTrue("output: stdout" in get(fh[2]))
self.assertTrue(not os.path.exists("stdout"))
# errors
reset()
cmd(".show one two")
s.cmdloop()
isempty(fh[1])
self.assertTrue("at most one parameter" in get(fh[2]))
reset()
cmd(".show notexist")
s.cmdloop()
isempty(fh[1])
self.assertTrue("notexist: " not in get(fh[2]))
###
### Command tables
###
reset()
cmd(".tables")
s.cmdloop()
isempty(fh[2])
isnotempty(fh[1])
reset()
cmd("create table tabletest(x);create index tabletest1 on tabletest(x);create index noway on tabletest(x);\n.tables tabletest\n.tables"
)
s.cmdloop()
isempty(fh[2])
self.assertTrue("tabletest" in get(fh[1]))
self.assertTrue("tabletest1" not in get(fh[1]))
self.assertTrue("noway" not in get(fh[1]))
###
### Command timeout
###
for i in (".timeout", ".timeout ksdjfh", ".timeout 6576 78987"):
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
for i in (".timeout 1000", ".timeout 0", ".timeout -33"):
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
# timer is tested earlier
###
### Command - version
###
reset()
cmd(".version")
s.cmdloop()
isempty(fh[2])
self.assertIn(apsw.apsw_version(), get(fh[1]))
###
### Command - vfsname / vfsinfo / vfslist
###
name = s.db.open_vfs
reset()
cmd(".vfsname")
s.cmdloop()
self.assertEqual(s.db.vfsname('main') or "", get(fh[1]).strip())
reset()
cmd(".vfsname temp")
s.cmdloop()
self.assertEqual(s.db.vfsname('temp') or "", get(fh[1]).strip())
reset()
cmd(".vfsinfo")
s.cmdloop()
self.assertIn(name, get(fh[1]))
reset()
cmd(".vfslist")
s.cmdloop()
self.assertIn(name, get(fh[1]))
###
### Command width
###
# does it work?
reset()
cmd(".width 10 10 10 0")
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
def getw():
reset()
cmd(".show width")
s.cmdloop()
isempty(fh[1])
return [int(x) for x in get(fh[2]).split()[1:]]
self.assertEqual([10, 10, 10, 0], getw())
# some errors
for i in ".width", ".width foo", ".width 1 2 3 seven 3":
reset()
cmd(i)
s.cmdloop()
isempty(fh[1])
isnotempty(fh[2])
self.assertEqual([10, 10, 10, 0], getw())
for i, r in ("9 0 9", [9, 0, 9]), ("10 -3 10 -3", [10, -3, 10, -3]), ("0", [0]):
reset()
cmd(".width " + i)
s.cmdloop()
isempty(fh[1])
isempty(fh[2])
self.assertEqual(r, getw())
###
### Unicode output with all output modes
###
colname = "\N{BLACK STAR}8\N{WHITE STAR}"
val = 'xxx\u1234\uabcdyyy this\" is nasty\u0001stuff!'
noheadermodes = ('insert', )
# possible ways val can be represented (eg csv doubles up double quotes)
outputs = (val, val.replace('"', '""'), val.replace('"', '"'), val.replace('"', '\\"'))
for mode in [x[len("output_"):] for x in dir(shellclass) if x.startswith("output_")]:
if mode in ("qbox", "table", "box"):
# apsw.ext.format_query_table already tested elsewhere
continue
reset()
cmd(".separator |\n.width 999\n.encoding utf8\n.header on\n.mode %s\nselect '%s' as '%s';" %
(mode, val, colname))
s.cmdloop()
isempty(fh[2])
# modes too complicated to construct the correct string
if mode in ('python', 'tcl'):
continue
# all others
if mode not in noheadermodes:
self.assertIn(colname if "json" not in mode else json.dumps(colname), get(fh[1]))
cnt = 0
for o in outputs:
cnt += (o if "json" not in mode else json.dumps(o)) in get(fh[1])
self.assertTrue(cnt)
# clean up files
for f in fh:
f.close()
# Note that faults fire only once, so there is no need to reset
# them. The testing for objects bigger than 2GB is done in
# testLargeObjects
def testzzFaultInjection(self):
"Deliberately inject faults to exercise all code paths"
if not getattr(apsw, "test_fixtures_present", None):
return
apsw.faultdict = dict()
def ShouldFault(name, pending_exception):
r = apsw.faultdict.get(name, False)
apsw.faultdict[name] = False
return r
sys.apsw_should_fault = ShouldFault
# Verify we test all fault locations
code = []
for fn in glob.glob("*/*.c"):
with open(fn, encoding="utf8") as f:
code.append(f.read())
code = "\n".join(code)
with open(__file__, "rt", encoding="utf8") as f:
test_code = f.read()
seen = set()
for macro, faultname in re.findall(r"(APSW_FAULT_INJECT)\s*[(]\s*(?P<fault_name>.*?)\s*,", code):
if faultname == "faultName":
continue
if faultname not in test_code:
raise Exception(f"Fault injected { faultname } not found in tests.py")
if faultname in seen:
raise Exception(f"Fault { faultname } seen multiple times")
seen.add(faultname)
def dummy(*args):
1 / 0
def dummy2(*args):
return 7
# The 1/0 in these tests is to cause a ZeroDivisionError so
# that an exception is always thrown. If we catch that then
# it means earlier expected exceptions were not thrown.
## Virtual table code
class Source:
def Create(self, *args):
return "create table foo(x,y)", Table()
Connect = Create
class Table:
def __init__(self):
self.data = [ #("rowid", "x", "y"),
[0, 1, 2], [3, 4, 5]
]
def Open(self):
return Cursor(self)
def BestIndex(self, *args):
return None
def UpdateChangeRow(self, rowid, newrowid, fields):
for i, row in enumerate(self.data):
if row[0] == rowid:
self.data[i] = [newrowid] + list(fields)
def FindFunction(self, *args):
return lambda *args: 1
class Cursor:
def __init__(self, table):
self.table = table
self.row = 0
def Eof(self):
return self.row >= len(self.table.data)
def Rowid(self):
return self.table.data[self.row][0]
def Column(self, col):
return self.table.data[self.row][1 + col]
def Filter(self, *args):
self.row = 0
def Next(self):
self.row += 1
def Close(self):
pass
## BlobDeallocException
def f():
db = apsw.Connection(":memory:")
db.cursor().execute("create table foo(b);insert into foo(rowid,b) values(2,x'aabbccddee')")
blob = db.blob_open("main", "foo", "b", 2, False) # open read-only
# deliberately cause problem
try:
blob.write(b'a')
except apsw.ReadOnlyError:
pass
# garbage collect
del blob
gc.collect()
self.assertRaisesUnraisable(apsw.ReadOnlyError, f)
## ConnectionReadError
self.db.pragma("application_id", 0xdeadbeef)
self.db.read("main", 0, 0, 1)
apsw.faultdict["ConnectionReadError"] = True
self.assertRaises(apsw.IOError, self.db.read, "main", 0, 0, 1)
### vfs routines
class FaultVFS(apsw.VFS):
def __init__(self, name="faultvfs", inherit="", makedefault=False):
super().__init__(name, inherit, makedefault=makedefault)
def xGetLastErrorLong(self):
return "a" * 1024, None
def xOpen(self, name, flags):
return FaultVFSFile(name, flags)
class FaultVFSFile(apsw.VFSFile):
def __init__(self, name, flags):
super().__init__("", name, flags)
vfs = FaultVFS()
## APSWVFSBadVersion
apsw.faultdict["APSWVFSBadVersion"] = True
self.assertRaises(ValueError, apsw.VFS, "foo", "")
self.assertTrue("foo" not in apsw.vfs_names())
## xReadReadBufferFail
try:
# This will fail if we are using auto-WAL so we don't run
# the rest of the test in WAL mode.
apsw.Connection(TESTFILEPREFIX + "testdb", vfs="faultvfs").cursor().execute("create table dummy1(x,y)")
openok = True
except apsw.CantOpenError:
if len(apsw.connection_hooks) == 0:
raise
openok = False
# The following tests cause failures when making the
# connection because a connection hook turns on wal mode which
# causes database reads which then cause failures
if openok:
## xUnlockFails
apsw.faultdict["xUnlockFails"] = True
self.assertRaises(apsw.IOError,
apsw.Connection(TESTFILEPREFIX + "testdb", vfs="faultvfs").cursor().execute,
"select * from dummy1")
## xSyncFails
apsw.faultdict["xSyncFails"] = True
self.assertRaises(apsw.IOError,
apsw.Connection(TESTFILEPREFIX + "testdb", vfs="faultvfs").cursor().execute,
"insert into dummy1 values(3,4)")
## xFileSizeFails
apsw.faultdict["xFileSizeFails"] = True
self.assertRaises(apsw.IOError,
apsw.Connection(TESTFILEPREFIX + "testdb", vfs="faultvfs").cursor().execute,
"select * from dummy1")
## xCheckReservedLockFails
apsw.faultdict["xCheckReservedLockFails"] = True
self.assertRaises(apsw.IOError, vfstestdb, vfsname="faultvfs")
## xCheckReservedLockIsTrue
apsw.faultdict["xCheckReservedLockIsTrue"] = True
vfstestdb(vfsname="faultvfs")
## xCloseFails
t = apsw.VFSFile("", os.path.abspath(TESTFILEPREFIX + "testfile"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0])
apsw.faultdict["xCloseFails"] = True
self.assertRaises(apsw.IOError, t.xClose)
del t
# now catch it in the destructor
def foo():
t = apsw.VFSFile("", os.path.abspath(TESTFILEPREFIX + "testfile"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0])
apsw.faultdict["xCloseFails"] = True
del t
gc.collect()
self.assertRaisesUnraisable(apsw.IOError, foo)
## BlobWriteTooBig
apsw.faultdict["BlobWriteTooBig"] = True
self.db.execute("CREATE TABLE blobby(x); insert into blobby values (zeroblob(1000))")
blob = self.db.blob_open("main", "blobby", "x", self.db.last_insert_rowid(), True)
self.assertRaises(ValueError, blob.write, b"1234")
for k, v in apsw.faultdict.items():
assert v is False, f"faultdict { k } never fired"
def testFunctionFlags(self) -> None:
"Flags to registered SQLite functions"
self.db.create_scalar_function("donotcall", lambda x: x / 0, flags=apsw.SQLITE_DIRECTONLY)
self.db.execute("""
create table foo(y);
insert into foo values(7);
create view bar(z) as select donotcall(y) from foo;
""")
try:
self.db.execute("select * from bar")
1 / 0 # should not be reached
except apsw.SQLError as e:
self.assertIn("unsafe use of donotcall", str(e))
def testBestPractice(self) -> None:
"apsw.bestpractice module"
if sys.version_info >= (3, 10):
with self.assertNoLogs():
apsw.log(apsw.SQLITE_NOMEM, "Zebras are striped")
apsw.bestpractice.apply(apsw.bestpractice.recommended)
with self.assertLogs() as l:
apsw.log(apsw.SQLITE_NOMEM, "Zebras are striped")
self.assertIn("Zebras", str(l.output))
dqs = 'select "world"'
with self.assertLogs() as l:
# this connection was made before applying bestpractice
self.db.execute(dqs) # no error but does log
self.assertIn("double-quoted string literal", str(l.output))
con = apsw.Connection("")
# now fail
apsw.config(apsw.SQLITE_CONFIG_LOG, None)
if sys.version_info >= (3, 10):
with self.assertNoLogs():
self.assertRaises(apsw.SQLError, con.execute, dqs)
def testExtDataClassRowFactory(self) -> None:
"apsw.ext.DataClassRowFactory"
dcrf = apsw.ext.DataClassRowFactory()
self.db.set_row_trace(dcrf)
# sanity check
for row in self.db.execute("select 3 as three, 'four' as four"):
self.assertEqual(row.three, 3)
self.assertEqual(row.four, 'four')
row.four = "five" # not frozen
# rename check
for row in self.db.execute("select 3 as three, 'four' as [4]"):
self.assertEqual(row.three, 3)
self.assertEqual(row._1, 'four')
# no rename, kwargs
dcrf2 = apsw.ext.DataClassRowFactory(rename=False, dataclass_kwargs={"frozen": True})
self.db.set_row_trace(dcrf2)
self.assertRaises(TypeError, self.db.execute("select 4 as [4]").fetchall)
for row in self.db.execute("select 3 as three"):
try:
row.three = 4
except dataclasses.FrozenInstanceError:
pass
db = apsw.Connection("")
db.set_row_trace(dcrf)
for row in db.execute(
"create table foo([x y] some random typename here); insert into foo values(3); select * from foo"):
self.assertEqual(row.__description__, (('x y', 'some random typename here'), ))
# type annotations
self.db.set_row_trace(dcrf)
self.db.execute(
"create table foo(one [], two [an integer], three VARCHAR(17), four cblob, five doUBl, six [none of those]); insert into foo values(1,2,3,4,5,6)"
)
self.assertEqual(dcrf.get_type("an integer"), int)
for row in self.db.execute("select * from foo"):
a = row.__annotations__
self.assertEqual(a["one"], typing.Any)
self.assertEqual(a["two"], int)
self.assertEqual(a["three"], str)
self.assertEqual(a["four"], bytes)
self.assertEqual(a["five"], float)
self.assertEqual(a["six"], typing.Union[float, int])
def testExtTypesConverter(self) -> None:
"apsw.ext.TypesConverterCursorFactory"
tccf = apsw.ext.TypesConverterCursorFactory()
class Point(apsw.ext.SQLiteTypeAdapter):
def to_sqlite_value(self):
return 3
tccf.register_adapter(complex, lambda c: f"{ c.real };{ c.imag }")
tccf.register_converter("COMPLEX", lambda v: complex(*(float(part) for part in v.split(";"))))
self.db.cursor_factory = tccf
self.db.execute("create table foo(a POINT, b COMPLEX)")
self.db.execute("insert into foo values(?,?);", (Point(), 3 + 4j))
self.db.execute(" insert into foo values(:one, :two)", {"one": Point(), "two": 3 + 4j})
def datas():
for _ in range(10):
yield (Point(), 3 + 4j)
self.db.executemany("insert into foo values(?,?)", datas())
for row in self.db.execute("select * from foo"):
self.assertEqual(row[0], 3)
self.assertEqual(row[1], 3 + 4j)
self.assertRaises(TypeError, tccf.adapt_value, {})
self.assertEqual(tccf.convert_value("zebra", "zebra"), "zebra")
def builtin_types():
yield (None, )
yield (3, )
yield (b"aabbccddee", )
yield ("hello world", )
yield (3.1415, )
self.assertEqual(self.db.executemany("select ?", builtin_types()).fetchall(), list(builtin_types()))
class NotImplemented(apsw.ext.SQLiteTypeAdapter):
pass
self.assertRaises(TypeError, NotImplemented)
def testExtStuff(self):
"Various apsw.ext functions"
apsw.ext.make_virtual_module(self.db, "g1", apsw.ext.generate_series)
vals = {row[0] for row in self.db.execute("select value from g1 where start=1 and stop=10")}
self.assertEqual(vals, {i for i in range(1, 10 + 1)})
vals = {row[0] for row in self.db.execute("select value from g1 where start=10 and stop=1")}
self.assertEqual(vals, {i for i in range(1, 10 + 1)})
vals = {row[0] for row in self.db.execute("select value from g1(1, 10, 2)")}
self.assertEqual(vals, {i for i in range(1, 10 + 1, 2)})
self.assertRaises(ValueError, self.db.execute, "select *,start,stop,step from g1(1,10) where step=0")
vals = [row[0] for row in self.db.execute("select value from g1(0.0, 1, 0.1)")]
expected = [i / 10 for i in range(0, 10 + 1)]
for l, r in zip(vals, expected):
self.assertAlmostEqual(l, r)
self.assertRaises(TypeError, self.db.execute, "select * from g1()")
apsw.ext.make_virtual_module(self.db, "g2", apsw.ext.generate_series_sqlite)
vals = {row[0] for row in self.db.execute("select value from g2 where start=1 and stop=10")}
self.assertEqual(vals, {i for i in range(1, 10 + 1)})
vals = {row[0] for row in self.db.execute("select value from g2 where start=1 and stop=10 and step=-1")}
self.assertEqual(vals, {i for i in range(1, 10 + 1)})
vals = {row[0] for row in self.db.execute("select value from g2(1, 10, 2)")}
self.assertEqual(vals, {i for i in range(1, 10 + 1, 2)})
self.assertEqual(
self.db.execute("select *,start,stop from g2(1,10) where step=0").get,
self.db.execute("select *,start,stop from g2(1,10) where step=1").get)
self.assertRaises(ValueError, self.db.execute, "select * from g2 where stop=10 and step=1")
self.assertRaises(TypeError, self.db.execute, "select * from g2(0.1, 1, 1)")
# https://github.com/rogerbinns/apsw/issues/412
apsw.ext.make_virtual_module(self.db, "genseries", apsw.ext.generate_series)
apsw.ext.make_virtual_module(self.db, "genseries2", apsw.ext.generate_series)
self.db.execute("""select *,genseries.start,genseries2.stop,genseries.step from genseries(1,10)
inner join genseries2(10,1) on genseries.value=genseries2.value
group by genseries2.value""")
def stuff_dict():
for i in range(10):
yield {
"a a": i,
"bb": 3.1415,
" cc": b"aabbccddeeff",
"dd": None,
# the chr gives a codepoint that has no name in unicodedata
"eeeeeeeeeeeeeeeeeeee": f"\\\n\f\v\t\n\0{ i }" + chr(0x10ffff) + "\r \n\r\\ \nr\r \n\r \n\n",
"f": APSW.wikipedia_text,
}
def stuff_attr():
class x:
pass
for row in stuff_dict():
for k, v in row.items():
setattr(x, k, v)
yield x
def stuff_tuple():
for row in stuff_dict():
yield tuple(row.values())
@dataclasses.dataclass
class xx:
one: str
two: str
three: str
four: str
five: str
six: str
def method(self):
1 / 0
def stuff_dataclass():
for row in stuff_tuple():
yield xx(*row)
def stuff_stat_struct():
yield os.stat(".")
yield os.stat("..")
nt = collections.namedtuple("nt", next(stuff_dict()).keys(), rename=True)
def stuff_namedtuple():
for row in stuff_tuple():
yield nt(*row)
for func, access, names in (
(stuff_dict, apsw.ext.VTColumnAccess.By_Name, ('a a', 'bb', ' cc', 'dd', 'eeeeeeeeeeeeeeeeeeee', "f")),
(stuff_tuple, apsw.ext.VTColumnAccess.By_Index, ('column0', 'column1', 'column2', 'column3', 'column4',
"column5")),
(stuff_dataclass, apsw.ext.VTColumnAccess.By_Attr, ('one', 'two', 'three', 'four', 'five', 'six')),
(stuff_namedtuple, apsw.ext.VTColumnAccess.By_Index, ('_0', 'bb', '_2', 'dd', 'eeeeeeeeeeeeeeeeeeee', 'f')),
(stuff_stat_struct, apsw.ext.VTColumnAccess.By_Attr, None),
):
if func is stuff_stat_struct:
if sys.version_info < (3, 10):
a = apsw.ext.VTColumnAccess.By_Attr
n = names = tuple(member for member in dir(next(func())) if member.startswith("st_"))
else:
n, a = apsw.ext.get_column_names(next(func()))
names = n
self.assertEqual(names, os.stat(".").__match_args__)
else:
n, a = apsw.ext.get_column_names(next(func()))
self.assertEqual(access, a)
self.assertEqual(names, n)
func.columns = n
func.column_access = a
stuff_attr.columns = stuff_dict.columns
stuff_attr.column_access = apsw.ext.VTColumnAccess.By_Attr
self.assertRaises(TypeError, apsw.ext.get_column_names, object())
funcs = (stuff_attr, stuff_dataclass, stuff_dict, stuff_tuple, stuff_stat_struct, stuff_namedtuple)
def _nb():
while True:
yield random.choice((True, False))
_nb = _nb()
nb = lambda: next(_nb)
def _ns():
while True:
yield 0
yield 1
yield 2
yield lambda x: repr(x)
_ns = _ns()
ns = lambda: next(_ns)
for f in funcs:
apsw.ext.make_virtual_module(self.db, "workit", f, eponymous_only=nb(), repr_invalid=nb())
apsw.ext.format_query_table(self.db,
"select rowid, * from workit(); select * from workit()",
bindings=tuple() if nb() else None,
colour=nb(),
quote=nb(),
string_sanitize=ns(),
truncate=3 if nb() else 35,
text_width=30 if nb() else 130,
use_unicode=nb(),
word_wrap=nb())
for width in range(200, 1, -17):
try:
apsw.ext.format_query_table(self.db,
"select * from workit()",
bindings=tuple() if nb() else None,
colour=nb(),
quote=nb(),
string_sanitize=ns(),
truncate=3 if nb() else 35,
text_width=width,
use_unicode=nb(),
word_wrap=nb())
except ValueError as e:
self.assertIn("Results can't be fitted in text width", str(e))
self.assertEqual(apsw.ext.format_query_table(self.db, "-- comment"), "")
query = """
-- comment
select 3;
-- another comment
select 4;
"""
self.db.row_trace = lambda x: 1 / 0
apsw.ext.format_query_table(self.db, query)
self.db.row_trace = None
def messy(arg1, arg2, arg3=lambda x: 1 / 0):
yield (1, )
yield (object(), )
messy.columns = ["x"]
messy.column_access = "orange"
try:
apsw.ext.make_virtual_module(self.db, "messy", messy)
except ValueError as e:
self.assertIn("column_access", str(e))
messy.column_access = apsw.ext.VTColumnAccess.By_Index
messy.columns = ["arg1"]
try:
apsw.ext.make_virtual_module(self.db, "messy", messy)
except ValueError as e:
self.assertIn("Same name in ", str(e))
self.assertIn("arg1", str(e))
messy.columns = ["x"]
messy.primary_key = 7
try:
apsw.ext.make_virtual_module(self.db, "messy", messy)
except ValueError as e:
self.assertIn("column number", str(e))
messy.primary_key = 0
for ri in (True, False):
apsw.ext.make_virtual_module(self.db, "messy", messy, repr_invalid=True)
self.db.execute("select *, arg3 from messy(1,2)").fetchall()
try:
self.db.execute("select * from messy(1,2,3,4)").fetchall()
except apsw.SQLError as e:
self.assertIn("too many arguments", str(e))
self.assertRaises(TypeError, self.db.execute, "select * from messy(1)")
self.assertRaises(ValueError, self.db.execute, "create virtual table fail using messy(1,2,3,4,5)")
for query in (
"select * from messy where arg1>3",
"select * from messy(1,2) where arg2=7"
"select * from messy(1,2) where arg2=7 and arg2=8",
"select * from messy where arg1 in (1,2) and arg1=6",
):
self.assertRaises(apsw.SQLError, self.db.execute, query)
def testExtQueryInfo(self) -> None:
"apsw.ext.query_info"
qd = apsw.ext.query_info(self.db, "select 3; a syntax error")
self.assertEqual(qd.query, "select 3; a syntax error")
self.assertEqual(qd.bindings, None)
self.assertEqual(qd.first_query, "select 3; ")
self.assertEqual(qd.query_remaining, "a syntax error")
self.assertEqual(qd.is_explain, 0)
self.assertEqual(qd.is_readonly, True)
self.assertEqual(qd.description, (('3', None), ))
self.assertEqual(1, apsw.ext.query_info(self.db, "explain select 3").is_explain)
self.assertEqual(2, apsw.ext.query_info(self.db, "explain query plan select 3").is_explain)
self.db.execute(
"create table one(x up); create table two(x down); insert into one values(3); insert into two values(3)")
self.assertFalse(apsw.ext.query_info(self.db, "insert into two values(7)").is_readonly)
# actions
query = "select * from one join two"
self.assertIsNone(apsw.ext.query_info(self.db, query).actions)
qd = apsw.ext.query_info(self.db, query, actions=True)
self.assertTrue(
any(a.action_name == "SQLITE_READ" and a.table_name == "one" for a in qd.actions)
and any(a.action_name == "SQLITE_READ" and a.table_name == "two" for a in qd.actions))
# expanded_sql
self.assertEqual("select 3, 'three'",
apsw.ext.query_info(self.db, "select ?, ?", (3, "three"), expanded_sql=True).expanded_sql)
# bindings count/names
qd = apsw.ext.query_info(self.db, "select ?2, :three, ?5, $six")
self.assertEqual(qd.bindings_count, 6)
self.assertEqual(qd.bindings_names, (None, "2", "three", None, "5", "six"))
# explain / explain query_plan
# from https://sqlite.org/lang_with.html
query = """
WITH RECURSIVE
xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2),
yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0),
m(iter, cx, cy, x, y) AS (
SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis
UNION ALL
SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m
WHERE (x*x + y*y) < 4.0 AND iter<28
),
m2(iter, cx, cy) AS (
SELECT max(iter), cx, cy FROM m GROUP BY cx, cy
),
a(t) AS (
SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '')
FROM m2 GROUP BY cy
)
SELECT group_concat(rtrim(t),x'0a') FROM a;
"""
self.assertIsNone(apsw.ext.query_info(self.db, query).explain)
qd = apsw.ext.query_info(self.db, query, explain=True)
self.assertTrue(all(isinstance(e, apsw.ext.VDBEInstruction) for e in qd.explain))
# at time of writing it was 233 steps, so use ~10% of that
self.assertGreater(len(qd.explain), 25)
self.assertIsNone(apsw.ext.query_info(self.db, query).query_plan)
qd = apsw.ext.query_info(self.db, query, explain_query_plan=True)
def check_instance(node: apsw.ext.QueryPlan):
return isinstance(node, apsw.ext.QueryPlan) and all(check_instance(s) for s in (node.sub or []))
self.assertTrue(check_instance(qd.query_plan))
def count(node: apsw.ext.QueryPlan):
return 1 + sum(count(s) for s in (node.sub or []))
# at time of writing it was 24 nodes
self.assertGreater(count(qd.query_plan), 10)
def testVFSFcntlPragma(self):
"Test wrapping fcntl pragmas"
class testvfs(apsw.VFS):
name = "testingpragma"
def __init__(self):
super().__init__(self.name, "")
def xOpen(self, name, flags):
return testvfsfile(name, flags)
testvfs = testvfs()
seen_unraiseable = None
class testvfsfile(apsw.VFSFile):
def __init__(self, name, flags):
super().__init__("", name, flags)
def excepthook(self, etype, evalue, etraceback):
nonlocal seen_unraiseable
seen_unraiseable = (etype, evalue, etraceback)
# note 'self' is for the test not this file instance
def xFileControl(fileself, op, ptr):
if op != apsw.SQLITE_FCNTL_PRAGMA:
return super().xFileControl(op, ptr)
self.assertRaises(TypeError, apsw.VFSFcntlPragma, "a string")
p = apsw.VFSFcntlPragma(ptr)
self.assertRaisesRegex(RuntimeError, "__init__ has already been called, and cannot be called again",
p.__init__, "issue 488")
self.assertIn((p.name, p.value), test_pragmas)
self.assertRaises(TypeError, setattr, p, "result", 3)
v = test_pragmas[(p.name, p.value)]
if isinstance(v, Exception):
p.result = v.args[0]
p.result = "message: " + p.result
raise v
if v is not False:
p.result = v
return True
return False
con = apsw.Connection(self.db.db_filename("main"), vfs=testvfs.name)
test_pragmas = {
("once", None): "some text for the result",
("once", "valueeee"): "completely different text",
("donot", "understand"): False,
("autherror", None): apsw.AuthError("yet more different text"),
}
for (k, v), r in test_pragmas.items():
seen_unraiseable = res = exc = None
sql = f"pragma { k }"
if v:
sql += f"({ v })"
try:
res = con.execute(sql).get
except apsw.Error as e:
exc = e
if r is False:
self.assertIsNone(exc)
self.assertIsNone(res)
elif isinstance(r, apsw.Error):
self.assertIsInstance(exc, r.__class__)
self.assertEqual(exc.args[0], r.args[0])
self.assertIsNone(res)
else:
self.assertEqual(res, r)
def testTpStr(self):
"Helpful descriptions of str(apsw objects)"
self.db.execute("create table x(y); insert into x values(x'abcdef1012')")
blob =self.db.blob_open("main", "x", "y", self.db.last_insert_rowid(), 0)
blob2 =self.db.blob_open("main", "x", "y", self.db.last_insert_rowid(), 0)
blob2.close()
db2=apsw.Connection("")
cur2=db2.cursor()
cur2.close()
db3=apsw.Connection("")
cur3=db3.cursor()
db3.close()
backup2=db2.backup("main", self.db, "main")
backup2.close()
name_catch=[]
class TVFS(apsw.VFS):
def __init__(self):
apsw.VFS.__init__(self, "uritest", "")
def xOpen(self, name, flags):
assert isinstance(name, apsw.URIFilename)
name_catch.append((name, str(name)))
raise apsw.SQLError()
# MacOS fails the name we provide returning
# cantopen for this, so we avoid their code
def xFullPathname(self, name: str) -> str:
return name
t = TVFS()
with contextlib.suppress(apsw.SQLError):
with tempfile.NamedTemporaryFile() as n:
apsw.Connection(n.name, vfs="uritest")
self.assertEqual(len(name_catch), 1)
uriname, urinamestr = name_catch[0]
objects = (self.db,
db2,
db3,
self.db.cursor(),
cur2,
cur3,
apsw.zeroblob(3),
blob,
blob2,
uriname,
apsw.VFS("aname", ""),
apsw.VFSFile("", self.db.db_filename("main"),
[apsw.SQLITE_OPEN_MAIN_DB | apsw.SQLITE_OPEN_CREATE | apsw.SQLITE_OPEN_READWRITE, 0]),
db2.backup("main", self.db, "main"),
backup2,
)
for o in objects:
self.assertNotEqual(repr(o), str(o))
# issue 501
if isinstance(o, apsw.URIFilename):
self.assertNotEqual(repr(o), urinamestr)
self.assertNotEqual(str(o), urinamestr)
# more issue 501
with contextlib.suppress(ValueError):
uriname.filename()
1/0
with contextlib.suppress(ValueError):
uriname.parameters
1/0
with contextlib.suppress(ValueError):
uriname.uri_boolean("name", False)
1/0
with contextlib.suppress(ValueError):
uriname.uri_int("name", 0)
1/0
with contextlib.suppress(ValueError):
uriname.uri_parameter("name")
1/0
# This test is run last by deliberate name choice. If it did
# uncover any bugs there isn't much that can be done to turn the
# checker off.
def testzzForkChecker(self):
"Test detection of using objects across fork"
# need to free up everything that already exists
self.db.close()
self.db = None
gc.collect()
# install it
apsw.fork_checker()
# return some objects
def getstuff():
db = apsw.Connection(":memory:")
cur = db.cursor()
for row in cur.execute(
"create table foo(x);insert into foo values(1);insert into foo values(x'aabbcc'); select last_insert_rowid()"
):
blobid = row[0]
blob = db.blob_open("main", "foo", "x", blobid, 0)
db2 = apsw.Connection(":memory:")
if hasattr(db2, "backup"):
backup = db2.backup("main", db, "main")
else:
backup = None
return (db, cur, blob, backup)
# test the objects
def teststuff(db, cur, blob, backup):
if db:
db.cursor().execute("select 3")
if cur:
cur.execute("select 3")
if blob:
blob.read(1)
if backup:
backup.step()
# Sanity check
teststuff(*getstuff())
# get some to use in parent
parent = getstuff()
# to be used (and fail with error) in child
child = getstuff()
def childtest(*args):
# we can't use unittest methods here since we are in a different process
val = args[0]
args = args[1:]
# this should work
teststuff(*getstuff())
# ignore the unraisable stuff sent to sys.excepthook
def eh(*args):
pass
sys.excepthook = eh
# call with each separate item to check
try:
for i in range(len(args)):
a = [None] * len(args)
a[i] = args[i]
try:
teststuff(*a)
except apsw.ForkingViolationError:
pass
except apsw.ForkingViolationError:
# we get one final exception "between" line due to the
# nature of how the exception is raised
pass
# this should work again
teststuff(*getstuff())
val.value = 1
import multiprocessing
val = multiprocessing.Value("i", 0)
p = multiprocessing.Process(target=childtest, args=[val] + list(child))
self.suppressWarning("DeprecationWarning") # we are deliberately forking
p.start()
p.join()
self.assertEqual(1, val.value) # did child complete ok?
teststuff(*parent)
# we call shutdown to free mutexes used in fork checker,
# so clear out all the things first
del child
del parent
gc.collect()
apsw.shutdown()
testtimeout = False # timeout testing adds several seconds to each run
def vfstestdb(filename=TESTFILEPREFIX + "testdb2", vfsname="apswtest", closedb=True, mode=None, attachdb=None):
"This method causes all parts of a vfs to be executed"
gc.collect() # free any existing db handles
for suf in "", "-journal", "x", "x-journal":
deletefile(filename + suf)
db = apsw.Connection("file:" + filename + "?psow=0", vfs=vfsname, flags=openflags)
if mode:
db.cursor().execute("pragma journal_mode=" + mode)
db.cursor().execute(
"create table foo(x,y); insert into foo values(1,2); insert into foo values(date('now'), date('now'))")
db.vfsname("main")
if testtimeout:
# busy
db2 = apsw.Connection(filename, vfs=vfsname)
if mode:
db2.cursor().execute("pragma journal_mode=" + mode)
db.set_busy_timeout(1100)
db2.cursor().execute("begin exclusive")
try:
db.cursor().execute("begin immediate")
1 / 0 # should not be reached
except apsw.BusyError:
pass
db2.cursor().execute("end")
# cause truncate to be called
# see sqlite test/pager3.test where this (public domain) code is taken from
# I had to add the pragma journal_mode to get it to work
c = db.cursor()
for row in c.execute("pragma journal_mode=truncate"):
pass
c.execute("""
create table t1(a unique, b);
insert into t1 values(1, 'abcdefghijklmnopqrstuvwxyz');
insert into t1 values(2, 'abcdefghijklmnopqrstuvwxyz');
update t1 set b=b||a||b;
update t1 set b=b||a||b;
update t1 set b=b||a||b;
update t1 set b=b||a||b;
update t1 set b=b||a||b;
update t1 set b=b||a||b;
create temp table t2 as select * from t1;
begin;
create table t3(x);""")
try:
c.execute("insert into t1 select 4-a, b from t2")
except apsw.ConstraintError:
pass
c.execute("rollback")
if attachdb:
c.execute("attach '%s' as second" % (attachdb, ))
if hasattr(APSW, "testLoadExtension"):
# can we use loadextension?
db.enableloadextension(True)
try:
db.loadextension("./" * 128 + LOADEXTENSIONFILENAME + "xxx")
except apsw.ExtensionLoadingError:
pass
db.loadextension(LOADEXTENSIONFILENAME)
assert (1 == curnext(db.cursor().execute("select half(2)"))[0])
# Get the routine xCheckReservedLock to be called. We need a hot journal
# which this code adapted from SQLite's pager.test does
if not iswindows:
c.execute("create table abc(a,b,c)")
for i in range(20):
c.execute("insert into abc values(1,2,?)", (randomstring(200), ))
c.execute("begin; update abc set c=?", (randomstring(200), ))
write_whole_file(filename + "x", "wb", read_whole_file(filename, "rb"))
write_whole_file(filename + "x-journal", "wb", read_whole_file(filename + "-journal", "rb"))
f = open(filename + "x-journal", "ab")
f.seek(-1032, 2) # 1032 bytes before end of file
f.write(b"\x00\x00\x00\x00")
f.close()
hotdb = apsw.Connection(filename + "x", vfs=vfsname)
if mode:
hotdb.cursor().execute("pragma journal_mode=" + mode)
hotdb.cursor().execute("select sql from sqlite_schema")
hotdb.close()
if closedb:
db.close()
else:
return db
if not iswindows:
# note that a directory must be specified otherwise $LD_LIBRARY_PATH is used
LOADEXTENSIONFILENAME = "./testextension.sqlext"
else:
LOADEXTENSIONFILENAME = "testextension.sqlext"
MEMLEAKITERATIONS = 123
PROFILESTEPS = 250000
def setup():
"""Call this if importing this test suite as it will ensure tests
we can't run are removed etc. It will also print version
information."""
print_version_info()
try:
apsw.config(apsw.SQLITE_CONFIG_MEMSTATUS, True) # ensure memory tracking is on
except apsw.MisuseError:
# if using amalgamation then something went wrong
if apsw.using_amalgamation:
raise
# coverage uses sqlite and so the config call is too
# late
pass
apsw.initialize() # manual call for coverage
memdb = apsw.Connection(":memory:")
if not getattr(memdb, "enableloadextension", None):
del APSW.testLoadExtension
# earlier py versions make recursion error fatal
if sys.version_info < (3, 10):
del APSW.testIssue425
forkcheck = False
if hasattr(apsw, "fork_checker") and hasattr(os, "fork") and platform.python_implementation() != "PyPy":
try:
import multiprocessing
if hasattr(multiprocessing, "get_start_method"):
if multiprocessing.get_start_method() != "fork":
raise ImportError
# sometimes the import works but doing anything fails
val = multiprocessing.Value("i", 0)
forkcheck = True
except ImportError:
pass
# we also remove forkchecker if doing multiple iterations
if not forkcheck or "APSW_TEST_ITERATIONS" in os.environ:
del APSW.testzzForkChecker
if not is64bit or "APSW_TEST_LARGE" not in os.environ:
del APSW.testLargeObjects
# We can do extension loading but no extension present ...
if getattr(memdb, "enableloadextension", None) and not os.path.exists(LOADEXTENSIONFILENAME):
print("Not doing the optional LoadExtension test. You need to compile the extension first\n")
print(" python3 setup.py build_test_extension")
del APSW.testLoadExtension
del memdb
test_types_vals = (
"a simple string", # "ascii" string
"0123456789" * 200000, # a longer string
"a \u1234 unicode \ufe54 string \u0089", # simple unicode string
"\N{BLACK STAR} \N{WHITE STAR} \N{LIGHTNING} \N{COMET} ", # funky unicode or an episode of b5
"\N{MUSICAL SYMBOL G CLEF}", # https://www.cmlenz.net/archives/2008/07/the-truth-about-unicode-in-python
97, # integer
2147483647, # numbers on 31 bit boundary (32nd bit used for integer sign), and then
-2147483647, # start using 32nd bit (must be represented by 64bit to avoid losing
2147483648, # detail)
-2147483648,
2147483999,
-2147483999,
992147483999,
-992147483999,
9223372036854775807,
-9223372036854775808,
b"a set of bytes", # bag of bytes initialised from a string, but don't confuse it with a
b"".join([b"\\x%02x" % (x, ) for x in range(256)]), # string
b"".join([b"\\x%02x" % (x, ) for x in range(256)]) * 20000, # non-trivial size
None, # our good friend NULL/None
1.1, # floating point can't be compared exactly - assertAlmostEqual is used to check
10.2, # see Appendix B in the Python Tutorial
1.3,
1.45897589347E97,
5.987987 / 8.7678678687676786,
math.pi,
True, # derived from integer
False)
if __name__ == '__main__':
setup()
def runtests():
if os.getenv("PYTRACE"):
import trace
t = trace.Trace(count=0, trace=1, ignoredirs=[sys.prefix, sys.exec_prefix])
t.runfunc(unittest.main)
else:
unittest.main()
v = os.environ.get("APSW_TEST_ITERATIONS", None)
if v is None:
try:
runtests()
except SystemExit:
exitcode = sys.exc_info()[1].code
else:
# we run all the tests multiple times which has better coverage
# a larger value for MEMLEAKITERATIONS slows down everything else
MEMLEAKITERATIONS = 5
PROFILESTEPS = 1000
v = int(v)
for i in range(v):
print(f"Iteration { i + 1 } of { v }")
try:
runtests()
except SystemExit:
exitcode = sys.exc_info()[1].code
# Free up everything possible
del APSW
del ThreadRunner
del randomintegers
# clean up sqlite and apsw
del apsw.ext
del apsw.bestpractice
gc.collect() # all cursors & connections must be gone
apsw.shutdown()
apsw.config(apsw.SQLITE_CONFIG_LOG, None)
if hasattr(apsw, "_fini"):
apsw._fini()
gc.collect()
del apsw
gc.collect()
sys.exit(exitcode)
|