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
|
SEQUEL_ADAPTER_TEST = :postgres
require_relative 'spec_helper'
uses_pg = Sequel::Postgres::USES_PG if DB.adapter_scheme == :postgres
uses_pg_or_jdbc = uses_pg || DB.adapter_scheme == :jdbc
DB.extension :pg_array, :pg_range, :pg_row, :pg_inet, :pg_json, :pg_enum
begin
DB.extension :pg_interval
rescue LoadError
end
DB.extension :pg_hstore if DB.type_supported?('hstore')
DB.extension :pg_multirange if DB.server_version >= 140000
DB.extension :pg_auto_parameterize if uses_pg && ENV['SEQUEL_PG_AUTO_PARAMETERIZE']
describe 'PostgreSQL adapter' do
before do
@db = DB
@db.disconnect
end
after do
@db.disconnect
end
it "should handle case where status raises PGError" do
proc do
@db.synchronize do |c|
def c.status; raise Sequel::Postgres::PGError end
def c.execute_query(*); raise Sequel::Postgres::PGError end
c.execute('SELECT 1')
end
end.must_raise Sequel::DatabaseDisconnectError
end
it "should handle case where execute_query returns nil" do
@db.synchronize do |c|
def c.execute_query(*); super; nil end
c.execute('SELECT 1 AS v'){|v| v.must_be_nil}
end
end
it "should handle prepared statement case where exec_prepared returns nil" do
@db.synchronize do |c|
def c.exec_prepared(*); super; nil end
@db['SELECT 1 AS v'].prepare(:all, :test_prepared)
@db.execute(:test_prepared){|v| v.must_be_nil}
end
end
it "should handle prepared statement case where same variable is used more than once" do
@db['SELECT (?::integer + ?::integer) AS v', :$v, :$v].prepare(:single_value, :test_prepared).call(:v=>2).must_equal 4
end
end if uses_pg
describe 'A PostgreSQL database' do
before do
@db = DB
end
after do
@db.drop_table?(:test)
end
it "should provide a list of existing ordinary tables" do
@db.create_table(:test){Integer :id}
@db.tables.must_include :test
end
it "should handle providing a block to tables" do
@db.create_table(:test){Integer :id}
@db.tables{|ds| ds.where(:relkind=>'r').map{|r| r[:relname]}}.must_include 'test'
@db.tables{|ds| ds.where(:relkind=>'p').map{|r| r[:relname]}}.wont_include 'test'
end
it "should handle blobs" do
@db.create_table(:test){File :blob}
blob = Sequel.blob("\0\1\254\255").force_encoding('BINARY')
@db[:test].insert(blob)
@db[:test].get(:blob).force_encoding('BINARY').must_equal blob
end
it "should provide a list of existing partitioned tables" do
@db.create_table(:test, :partition_by => :id, :partition_type => :range){Integer :id}
@db.tables.must_include :test
end if DB.server_version >= 100000
it "should provide a list of existing ordinary and partitioned tables" do
@db.create_table(:test, :partition_by => :id, :partition_type => :range){Integer :id}
@db.create_table(:test_1, :partition_of => :test){from 1; to 3}
@db.create_table(:test_2, :partition_of => :test){from 3; to 4}
@db.tables.must_include :test
@db.tables.must_include :test_1
@db.tables.must_include :test_2
end if DB.server_version >= 100000
it "should provide a list of foreign keys on partitioned tables" do
begin
@db.create_table(:test){primary_key :i}
@db.create_table(:test2, :partition_by => :id, :partition_type => :range){foreign_key :id, :test}
fks = @db.foreign_key_list(:test2)
fks.length.must_equal 1
fks[0][:table].must_equal :test
fks[0][:columns].must_equal [:id]
fks[0][:key].must_equal [:i]
ensure
@db.drop_table?(:test2)
end
end if DB.server_version >= 110000
end
describe "PostgreSQL", '#create_table' do
before do
@db = DB
end
after do
@db.drop_table?(:tmp_dolls, :unlogged_dolls)
@db.default_string_column_size = nil
end
it "should support multiple types of string columns" do
@db.default_string_column_size = 50
@db.create_table(:tmp_dolls) do
String :a1
String :a2, :size=>10
String :a3, :fixed=>true
String :a4, :fixed=>true, :size=>10
String :a5, :text=>false
String :a6, :text=>false, :size=>10
String :a7, :text=>true
end
@db.schema(:tmp_dolls).map{|k, v| v[:max_length]}.must_equal [nil, 10, 50, 10, 50, 10, nil]
end
it "should support range partitioned tables for single columns with :partition_* options" do
@db.create_table(:tmp_dolls, :partition_by => :id, :partition_type=>:range){Integer :id}
@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){from 1; to 3}
@db.create_table(:tmp_dolls_2, :partition_of => :tmp_dolls){from 3; to 4}
@db[:tmp_dolls].insert(1)
@db[:tmp_dolls].insert(2)
@db[:tmp_dolls].insert(3)
proc{@db[:tmp_dolls].insert(0)}.must_raise Sequel::DatabaseError
proc{@db[:tmp_dolls].insert(5)}.must_raise Sequel::DatabaseError
@db.create_table(:tmp_dolls_0, :partition_of => :tmp_dolls){from minvalue; to 1}
@db.create_table(:tmp_dolls_3, :partition_of => :tmp_dolls){from 4; to maxvalue}
@db[:tmp_dolls].insert(0)
@db[:tmp_dolls].insert(5)
@db[:tmp_dolls].order(:id).select_order_map(:id).must_equal [0,1,2,3,5]
@db[:tmp_dolls_0].order(:id).select_order_map(:id).must_equal [0]
@db[:tmp_dolls_1].order(:id).select_order_map(:id).must_equal [1,2]
@db[:tmp_dolls_2].order(:id).select_order_map(:id).must_equal [3]
@db[:tmp_dolls_3].order(:id).select_order_map(:id).must_equal [5]
end if DB.server_version >= 100000
it "should support range partitioned tables for multiple columns with :partition_* options" do
@db.create_table(:tmp_dolls, :partition_by => [:id, :id2], :partition_type=>:range){Integer :id; Integer :id2}
@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){from 1, 1; to 3, 3}
@db.create_table(:tmp_dolls_2, :partition_of => :tmp_dolls){from 3, 3; to 4, 4}
@db[:tmp_dolls].insert(1, 1)
@db[:tmp_dolls].insert(2, 2)
@db[:tmp_dolls].insert(3, 3)
proc{@db[:tmp_dolls].insert(0, 0)}.must_raise Sequel::DatabaseError
proc{@db[:tmp_dolls].insert(5, 5)}.must_raise Sequel::DatabaseError
@db.create_table(:tmp_dolls_0, :partition_of => :tmp_dolls){from minvalue, minvalue; to 1, 1}
@db.create_table(:tmp_dolls_3, :partition_of => :tmp_dolls){from 4, 4; to maxvalue, maxvalue}
@db[:tmp_dolls].insert(0, 0)
@db[:tmp_dolls].insert(5, 5)
@db[:tmp_dolls].order(:id).select_order_map([:id, :id2]).must_equal [0,1,2,3,5].map{|x| [x,x]}
@db[:tmp_dolls_0].order(:id).select_order_map([:id, :id2]).must_equal [0].map{|x| [x,x]}
@db[:tmp_dolls_1].order(:id).select_order_map([:id, :id2]).must_equal [1,2].map{|x| [x,x]}
@db[:tmp_dolls_2].order(:id).select_order_map([:id, :id2]).must_equal [3].map{|x| [x,x]}
@db[:tmp_dolls_3].order(:id).select_order_map([:id, :id2]).must_equal [5].map{|x| [x,x]}
end if DB.server_version >= 100000
it "should support list partitioned tables for single column with :partition_* options" do
@db.create_table(:tmp_dolls, :partition_by => :id, :partition_type=>:list){Integer :id}
@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){values_in 1, 2}
@db.create_table(:tmp_dolls_2, :partition_of => :tmp_dolls){values_in 3}
@db[:tmp_dolls].insert(1)
@db[:tmp_dolls].insert(2)
@db[:tmp_dolls].insert(3)
proc{@db[:tmp_dolls].insert(0)}.must_raise Sequel::DatabaseError
proc{@db[:tmp_dolls].insert(5)}.must_raise Sequel::DatabaseError
@db.create_table(:tmp_dolls_0, :partition_of => :tmp_dolls){values_in 0}
@db.create_table(:tmp_dolls_3, :partition_of => :tmp_dolls){default}
@db[:tmp_dolls].insert(0)
@db[:tmp_dolls].insert(5)
@db[:tmp_dolls].order(:id).select_order_map(:id).must_equal [0,1,2,3,5]
@db[:tmp_dolls_0].order(:id).select_order_map(:id).must_equal [0]
@db[:tmp_dolls_1].order(:id).select_order_map(:id).must_equal [1,2]
@db[:tmp_dolls_2].order(:id).select_order_map(:id).must_equal [3]
@db[:tmp_dolls_3].order(:id).select_order_map(:id).must_equal [5]
end if DB.server_version >= 110000
it "should support hash partitioned tables for single column with :partition_* options" do
@db.create_table(:tmp_dolls, :partition_by => :id, :partition_type=>:hash){Integer :id}
@db.create_table(:tmp_dolls_0, :partition_of => :tmp_dolls){modulus 4; remainder 0}
@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){modulus 4; remainder 1}
@db.create_table(:tmp_dolls_2, :partition_of => :tmp_dolls){modulus 4; remainder 2}
@db.create_table(:tmp_dolls_3, :partition_of => :tmp_dolls){modulus 4; remainder 3}
@db[:tmp_dolls].insert(1)
@db[:tmp_dolls].insert(2)
@db[:tmp_dolls].insert(3)
@db[:tmp_dolls].insert(4)
@db[:tmp_dolls].order(:id).select_order_map(:id).must_equal [1,2,3,4]
[0,1,2,3].flat_map{|i| @db[:"tmp_dolls_#{i}"].select_order_map(:id)}.sort.must_equal [1,2,3,4]
end if DB.server_version >= 110000
it "should support partitioned tables with create_table?" do
@db.create_table(:tmp_dolls, :partition_by => :id, :partition_type=>:range){Integer :id}
@db.create_table?(:tmp_dolls_1, :partition_of => :tmp_dolls){from 1; to 3}
@db.create_table?(:tmp_dolls_2, :partition_of => :tmp_dolls){from 3; to 4}
@db[:tmp_dolls].insert(1)
@db[:tmp_dolls].insert(2)
@db[:tmp_dolls].insert(3)
proc{@db[:tmp_dolls].insert(0)}.must_raise Sequel::DatabaseError
proc{@db[:tmp_dolls].insert(5)}.must_raise Sequel::DatabaseError
@db.create_table?(:tmp_dolls_0, :partition_of => :tmp_dolls){from minvalue; to 1}
@db.create_table?(:tmp_dolls_3, :partition_of => :tmp_dolls){from 4; to maxvalue}
@db[:tmp_dolls].insert(0)
@db[:tmp_dolls].insert(5)
@db[:tmp_dolls].order(:id).select_order_map(:id).must_equal [0,1,2,3,5]
@db[:tmp_dolls_0].order(:id).select_order_map(:id).must_equal [0]
@db[:tmp_dolls_1].order(:id).select_order_map(:id).must_equal [1,2]
@db[:tmp_dolls_2].order(:id).select_order_map(:id).must_equal [3]
@db[:tmp_dolls_3].order(:id).select_order_map(:id).must_equal [5]
end if DB.server_version >= 100000
it "should support partitioned tables with create_table!" do
@db.create_table(:tmp_dolls, :partition_by => :id, :partition_type=>:range){Integer :id}
@db.create_table!(:tmp_dolls_1, :partition_of => :tmp_dolls){from 1; to 3}
@db.create_table!(:tmp_dolls_1, :partition_of => :tmp_dolls){from 1; to 3}
@db.create_table!(:tmp_dolls_2, :partition_of => :tmp_dolls){from 3; to 4}
@db.create_table!(:tmp_dolls_2, :partition_of => :tmp_dolls){from 3; to 4}
@db[:tmp_dolls].insert(1)
@db[:tmp_dolls].insert(2)
@db[:tmp_dolls].insert(3)
proc{@db[:tmp_dolls].insert(0)}.must_raise Sequel::DatabaseError
proc{@db[:tmp_dolls].insert(5)}.must_raise Sequel::DatabaseError
@db.create_table!(:tmp_dolls_0, :partition_of => :tmp_dolls){from minvalue; to 1}
@db.create_table!(:tmp_dolls_0, :partition_of => :tmp_dolls){from minvalue; to 1}
@db.create_table!(:tmp_dolls_3, :partition_of => :tmp_dolls){from 4; to maxvalue}
@db.create_table!(:tmp_dolls_3, :partition_of => :tmp_dolls){from 4; to maxvalue}
@db[:tmp_dolls].insert(0)
@db[:tmp_dolls].insert(5)
@db[:tmp_dolls].order(:id).select_order_map(:id).must_equal [0,1,2,3,5]
@db[:tmp_dolls_0].order(:id).select_order_map(:id).must_equal [0]
@db[:tmp_dolls_1].order(:id).select_order_map(:id).must_equal [1,2]
@db[:tmp_dolls_2].order(:id).select_order_map(:id).must_equal [3]
@db[:tmp_dolls_3].order(:id).select_order_map(:id).must_equal [5]
end if DB.server_version >= 100000
it "should raise for unsupported partition types" do
@db.create_table(:tmp_dolls, :partition_by => [:id, :id2], :partition_type=>:range){Integer :id; Integer :id2}
proc{@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){from 1, 1; to 3, 3; modulus 10}}.must_raise Sequel::Error
proc{@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){from 1, 1}}.must_raise Sequel::Error
proc{@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){modulus 10}}.must_raise Sequel::Error
proc{@db.create_table(:tmp_dolls_1, :partition_of => :tmp_dolls){}}.must_raise Sequel::Error
end if DB.server_version >= 100000
it "should not use a size for text columns" do
@db.create_table(:tmp_dolls){String :description, text: true, size: :long}
@db.tables.must_include :tmp_dolls
end
it "should create an unlogged table" do
@db.create_table(:unlogged_dolls, :unlogged => true){text :name}
end if DB.server_version >= 90100
it "should create a table inheriting from another table" do
@db.create_table(:unlogged_dolls){text :name}
@db.create_table(:tmp_dolls, :inherits=>:unlogged_dolls){}
@db[:tmp_dolls].insert('a')
@db[:unlogged_dolls].all.must_equal [{:name=>'a'}]
end
it "should create a table inheriting from multiple tables" do
begin
@db.create_table(:unlogged_dolls){text :name}
@db.create_table(:tmp_dolls){text :bar}
@db.create_table!(:items, :inherits=>[:unlogged_dolls, :tmp_dolls]){text :foo}
@db[:items].insert(:name=>'a', :bar=>'b', :foo=>'c')
@db[:unlogged_dolls].all.must_equal [{:name=>'a'}]
@db[:tmp_dolls].all.must_equal [{:bar=>'b'}]
@db[:items].all.must_equal [{:name=>'a', :bar=>'b', :foo=>'c'}]
ensure
@db.drop_table?(:items)
end
end
it "should have #check_constraints method for getting check constraints" do
@db.create_table(:tmp_dolls) do
Integer :i
Integer :j
constraint(:ic, Sequel[:i] > 2)
constraint(:jc, Sequel[:j] > 2)
constraint(:ijc, Sequel[:i] - Sequel[:j] > 2)
end
@db.check_constraints(:tmp_dolls).must_equal(:ic=>{:definition=>"CHECK ((i > 2))", :columns=>[:i]}, :jc=>{:definition=>"CHECK ((j > 2))", :columns=>[:j]}, :ijc=>{:definition=>"CHECK (((i - j) > 2))", :columns=>[:i, :j]})
end
it "should have #check_constraints return check constraints where columns are unknown" do
begin
@db.create_table(:tmp_dolls) do
Integer :i
Integer :j
end
@db.run "CREATE OR REPLACE FUNCTION valid_tmp_dolls(t1 tmp_dolls) RETURNS boolean AS 'SELECT false' LANGUAGE SQL;"
@db.alter_table(:tmp_dolls) do
add_constraint(:valid_tmp_dolls, Sequel.function(:valid_tmp_dolls, :tmp_dolls))
end
@db.check_constraints(:tmp_dolls).must_equal(:valid_tmp_dolls=>{:definition=>"CHECK (valid_tmp_dolls(tmp_dolls.*))", :columns=>[]})
ensure
@db.run "ALTER TABLE tmp_dolls DROP CONSTRAINT IF EXISTS valid_tmp_dolls"
@db.run "DROP FUNCTION IF EXISTS valid_tmp_dolls(tmp_dolls)"
end
end if DB.server_version >= 90000
it "should support :if_exists option to drop_column" do
@db.create_table(:tmp_dolls){Integer :a; Integer :b}
2.times do
@db.drop_column :tmp_dolls, :b, :if_exists=>true
@db[:tmp_dolls].columns.must_equal [:a]
end
end if DB.server_version >= 90000
it "should support primary_key with :type=>:smallserial, :type=>:serial or :type=>:bigserial" do
[:smallserial, :serial, :bigserial, 'smallserial', 'serial', 'bigserial'].each do |type|
@db.create_table!(:tmp_dolls){primary_key :id, :type=>type}
@db[:tmp_dolls].insert
@db[:tmp_dolls].get(:id).must_equal 1
end
end if DB.server_version >= 100002
it "should support primary_key with :serial=>true" do
@db.create_table!(:tmp_dolls){primary_key :id, :serial=>true}
@db[:tmp_dolls].insert
@db[:tmp_dolls].get(:id).must_equal 1
end if DB.server_version >= 100002
it "should support primary_key with :Bignum type and serial=>true" do
@db.create_table!(:tmp_dolls){primary_key :id, :type=>:Bignum, :serial=>true}
@db[:tmp_dolls].insert(2**48)
@db[:tmp_dolls].get(:id).must_equal(2**48)
end
it "should support Bignum column with serial=>true" do
@db.create_table!(:tmp_dolls){Bignum :id, :serial=>true, :primary_key=>true}
@db[:tmp_dolls].insert(2**48)
@db[:tmp_dolls].get(:id).must_equal(2**48)
end
it "should support creating identity columns on non-primary key tables" do
@db.create_table(:tmp_dolls){Integer :a, :identity=>true}
2.times do
@db[:tmp_dolls].insert
end
@db[:tmp_dolls].select_order_map(:a).must_equal [1, 2]
@db[:tmp_dolls].insert(:a=>2)
@db[:tmp_dolls].select_order_map(:a).must_equal [1, 2, 2]
@db[:tmp_dolls].insert(:a=>4)
@db[:tmp_dolls].select_order_map(:a).must_equal [1, 2, 2, 4]
@db[:tmp_dolls].overriding_user_value.insert(:a=>5)
@db[:tmp_dolls].select_order_map(:a).must_equal [1, 2, 2, 3, 4]
end if DB.server_version >= 100002
it "should support creating identity columns generated always" do
@db.create_table(:tmp_dolls){primary_key :id, :identity=>:always}
2.times do
@db[:tmp_dolls].insert
end
@db[:tmp_dolls].select_order_map(:id).must_equal [1, 2]
proc{@db[:tmp_dolls].insert(:id=>2)}.must_raise Sequel::DatabaseError
@db[:tmp_dolls].overriding_system_value.insert(:id=>4)
@db[:tmp_dolls].select_order_map(:id).must_equal [1, 2, 4]
@db[:tmp_dolls].insert
@db[:tmp_dolls].select_order_map(:id).must_equal [1, 2, 3, 4]
end if DB.server_version >= 100002
it "should handle generated column overrides using override value at time of merge_insert call" do
@db.create_table(:tmp_dolls){primary_key :id, :identity=>:always}
@db.create_table(:unlogged_dolls){Integer :i}
@db[:unlogged_dolls].insert(10)
@db[:unlogged_dolls].insert(20)
@db[:tmp_dolls].
merge_using(:unlogged_dolls, :id=>:i).
overriding_system_value.
merge_insert(:i){i > 15}.
overriding_user_value.
merge_insert(:i).
merge
@db[:tmp_dolls].select_order_map(:id).must_equal [1, 20]
end if DB.server_version >= 150000
it "should support converting serial columns to identity columns" do
@db.create_table(:tmp_dolls){primary_key :id, :identity=>false, :serial=>true}
sch = @db.schema(:tmp_dolls)[0][1]
sch[:default].must_match(/nextval/)
sch[:auto_increment].must_equal true
2.times do
@db[:tmp_dolls].insert
end
@db.convert_serial_to_identity(:tmp_dolls)
sch = @db.schema(:tmp_dolls)[0][1]
sch[:default].must_be_nil
sch[:auto_increment].must_equal true
@db[:tmp_dolls].insert
@db[:tmp_dolls].insert(5)
@db[:tmp_dolls].select_order_map(:id).must_equal [1, 2, 3, 5]
# Make sure it doesn't break if already converted
@db.convert_serial_to_identity(:tmp_dolls)
end if DB.server_version >= 100002 && DB.get{current_setting('is_superuser')} == 'on'
it "should support converting serial columns to identity columns when using the :column option" do
@db.create_table(:tmp_dolls){Integer :i, :primary_key=>true; serial :id}
sch = @db.schema(:tmp_dolls)[1][1]
sch[:default].must_match(/nextval/)
2.times do |i|
@db[:tmp_dolls].insert(:i=>-i)
end
# Automatic conversion should not work
proc{@db.convert_serial_to_identity(:tmp_dolls)}.must_raise Sequel::Error
# Conversion of type without related sequence should not work
proc{@db.convert_serial_to_identity(:tmp_dolls, :column=>:i)}.must_raise Sequel::Error
@db.convert_serial_to_identity(:tmp_dolls, :column=>:id)
sch = @db.schema(:tmp_dolls)[1][1]
sch[:default].must_be_nil
@db[:tmp_dolls].insert(:i=>200)
@db[:tmp_dolls].insert(:i=>300, :id=>5)
@db[:tmp_dolls].select_order_map(:id).must_equal [1, 2, 3, 5]
# Make sure it doesn't break if already converted
@db.convert_serial_to_identity(:tmp_dolls, :column=>:id)
end if DB.server_version >= 100002 && DB.get{current_setting('is_superuser')} == 'on'
it "should support creating generated columns" do
@db.create_table(:tmp_dolls){Integer :a; Integer :b; Integer :c, :generated_always_as=>Sequel[:a] * 2 + :b + 1}
@db[:tmp_dolls].insert(:a=>100, :b=>10)
@db[:tmp_dolls].select_order_map([:a, :b, :c]).must_equal [[100, 10, 211]]
end if DB.server_version >= 120000
it "should include :generated entry in schema for whether the column is generated" do
@db.create_table(:tmp_dolls){Integer :a; Integer :b; Integer :c, :generated_always_as=>Sequel[:a] * 2 + :b + 1}
@db.schema(:tmp_dolls).map{|_,v| v[:generated]}.must_equal [false, false, true]
end if DB.server_version >= 120000
it "should support deferred primary key and unique constraints on columns" do
@db.create_table(:tmp_dolls){primary_key :id, :primary_key_deferrable=>true; Integer :i, :unique=>true, :unique_deferrable=>true}
@db[:tmp_dolls].insert(:i=>10)
DB.transaction do
@db[:tmp_dolls].insert(:id=>1, :i=>1)
@db[:tmp_dolls].insert(:id=>10, :i=>10)
@db[:tmp_dolls].where(:i=>1).update(:id=>2)
@db[:tmp_dolls].where(:id=>10).update(:i=>2)
end
@db[:tmp_dolls].select_order_map([:id, :i]).must_equal [[1, 10], [2, 1], [10, 2]]
end if DB.server_version >= 90000
it "should support pg_loose_count extension" do
@db.extension :pg_loose_count
@db.create_table(:tmp_dolls){text :name}
@db.loose_count(:tmp_dolls).must_be_kind_of(Integer)
[0, -1].must_include @db.loose_count(:tmp_dolls)
[0, -1].must_include @db.loose_count(Sequel[:public][:tmp_dolls])
@db[:tmp_dolls].insert('a')
@db << 'VACUUM ANALYZE tmp_dolls'
@db.loose_count(:tmp_dolls).must_equal 1
@db.loose_count(Sequel[:public][:tmp_dolls]).must_equal 1
end
end
describe "PostgreSQL temporary table/view support" do
before(:all) do
@db = DB
@db.disconnect
end
after do
@db.drop_view(:tmp_dolls_view, :if_exists=>true, :cascade=>true) rescue nil
@db.drop_table?(:tmp_dolls)
end
it "should create a temporary table" do
@db.create_table(:tmp_dolls, :temp => true){text :name}
@db.table_exists?(:tmp_dolls).must_equal true
@db.disconnect
@db.table_exists?(:tmp_dolls).must_equal false
end
it "temporary table should support :on_commit option" do
@db.drop_table?(:some_table)
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :drop){text :name}
end
@db.table_exists?(:some_table).must_equal false
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :delete_rows){text :name}
@db[:some_table].insert('a')
end
@db.table_exists?(:some_table).must_equal true
@db[:some_table].empty?.must_equal true
@db.drop_table(:some_table)
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :preserve_rows){text :name}
@db[:some_table].insert('a')
end
@db.table_exists?(:some_table).must_equal true
@db[:some_table].count.must_equal 1
@db.drop_table(:some_table)
end
it "temporary table should accept :on_commit with :as option" do
@db.drop_table?(:some_table)
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :drop, :as => 'select 1')
end
@db.table_exists?(:some_table).must_equal false
end
it ":on_commit should raise error if not used on a temporary table" do
proc{@db.create_table(:some_table, :on_commit => :drop)}.must_raise(Sequel::Error)
end
it ":on_commit should raise error if given unsupported value" do
proc{@db.create_table(:some_table, :temp => true, :on_commit => :unsupported){text :name}}.must_raise(Sequel::Error)
end
it "should not allow to pass both :temp and :unlogged" do
proc do
@db.create_table(:temp_unlogged_dolls, :temp => true, :unlogged => true){text :name}
end.must_raise(Sequel::Error, "can't provide both :temp and :unlogged to create_table")
end
it "should support temporary views" do
@db.create_table(:tmp_dolls, :temp => true){Integer :number}
@db[:tmp_dolls].insert(10)
@db[:tmp_dolls].insert(20)
@db.create_view(:tmp_dolls_view, @db[:tmp_dolls].where(:number=>10), :temp=>true)
@db[:tmp_dolls_view].map(:number).must_equal [10]
@db.create_or_replace_view(:tmp_dolls_view, @db[:tmp_dolls].where(:number=>20), :temp=>true)
@db[:tmp_dolls_view].map(:number).must_equal [20]
end
it "should support security_invoker view option" do
@db.create_table(:tmp_dolls, :temp => true){Integer :number}
@db[:tmp_dolls].insert(10)
@db.create_view(:tmp_dolls_view, @db[:tmp_dolls].where(:number=>10), :temp=>true, :security_invoker=>true)
@db[:tmp_dolls_view].count.must_equal 1
end if DB.server_version >= 150000
end
describe "PostgreSQL views" do
before do
@db = DB
@db.drop_table?(:items, :cascade=>true)
@db.create_table(:items){Integer :number}
@db[:items].insert(10)
@db[:items].insert(20)
end
after do
@opts ||={}
@db.drop_view(:items_view, @opts.merge(:if_exists=>true, :cascade=>true)) rescue nil
@db.drop_table?(:items)
end
it "should support recursive views" do
@db.create_view(:items_view, @db[:items].where(:number=>10).union(@db[:items, :items_view].where(Sequel.-(:number, 5)=>:n).select(:number), :all=>true, :from_self=>false), :recursive=>[:n])
@db[:items_view].select_order_map(:n).must_equal [10]
@db[:items].insert(15)
@db[:items_view].select_order_map(:n).must_equal [10, 15, 20]
end if DB.server_version >= 90300
it "should support materialized views" do
@opts = {:materialized=>true}
@db.create_view(:items_view, @db[:items].where{number >= 10}, @opts)
@db[:items_view].select_order_map(:number).must_equal [10, 20]
@db[:items].insert(15)
@db[:items_view].select_order_map(:number).must_equal [10, 20]
@db.refresh_view(:items_view)
@db[:items_view].select_order_map(:number).must_equal [10, 15, 20]
@db.views.wont_include :items_view
@db.views(@opts).must_include :items_view
end if DB.server_version >= 90300
it "should support refreshing materialized views concurrently" do
@opts = {:materialized=>true}
@db.create_view(:items_view, @db[:items].where{number >= 10}, @opts)
@db.refresh_view(:items_view)
proc{@db.refresh_view(:items_view, :concurrently=>true)}.must_raise(Sequel::DatabaseError)
@db.add_index :items_view, :number, :unique=>true
@db.refresh_view(:items_view, :concurrently=>true)
end if DB.server_version >= 90400
it "should support specifying tablespaces for materialized views" do
@opts = {:materialized=>true}
@db.create_view(:items_view, @db[:items].where{number >= 10}, :materialized=>true, :tablespace=>:pg_default)
end if DB.server_version >= 90300
it "should support :if_exists=>true for not raising an error if the view does not exist" do
@db.drop_view(:items_view, :if_exists=>true)
end
end
describe "PostgreSQL", 'INSERT ON CONFLICT' do
before(:all) do
@db = DB
@db.create_table!(:ic_test){Integer :a; Integer :b; Integer :c; TrueClass :c_is_unique, :default=>false; unique :a, :name=>:ic_test_a_uidx; unique [:b, :c], :name=>:ic_test_b_c_uidx; index [:c], :where=>:c_is_unique, :unique=>true}
@ds = @db[:ic_test]
end
before do
@ds.delete
end
after(:all) do
@db.drop_table?(:ic_test)
end
it "Dataset#supports_insert_conflict? should be true" do
@ds.supports_insert_conflict?.must_equal true
end
it "Dataset#insert_ignore and insert_conflict should ignore uniqueness violations" do
@ds.insert(1, 2, 3)
@ds.insert(10, 11, 3, true)
proc{@ds.insert(1, 3, 4)}.must_raise Sequel::UniqueConstraintViolation
proc{@ds.insert(11, 12, 3, true)}.must_raise Sequel::UniqueConstraintViolation
@ds.insert_ignore.insert(1, 3, 4).must_be_nil
@ds.insert_conflict.insert(1, 3, 4).must_be_nil
@ds.insert_conflict.insert(11, 12, 3, true).must_be_nil
@ds.insert_conflict(:target=>:a).insert(1, 3, 4).must_be_nil
@ds.insert_conflict(:target=>:c, :conflict_where=>:c_is_unique).insert(11, 12, 3, true).must_be_nil
@ds.insert_conflict(:constraint=>:ic_test_a_uidx).insert(1, 3, 4).must_be_nil
@ds.all.must_equal [{:a=>1, :b=>2, :c=>3, :c_is_unique=>false}, {:a=>10, :b=>11, :c=>3, :c_is_unique=>true}]
end
it "Dataset#insert_ignore and insert_conflict should work with multi_insert/import" do
@ds.insert(1, 2, 3)
@ds.insert_ignore.multi_insert([{:a=>1, :b=>3, :c=>4}])
@ds.insert_ignore.import([:a, :b, :c], [[1, 3, 4]])
@ds.all.must_equal [{:a=>1, :b=>2, :c=>3, :c_is_unique=>false}]
@ds.insert_conflict(:target=>:a, :update=>{:b=>3}).import([:a, :b, :c], [[1, 3, 4]])
@ds.all.must_equal [{:a=>1, :b=>3, :c=>3, :c_is_unique=>false}]
@ds.insert_conflict(:target=>:a, :update=>{:b=>4}).multi_insert([{:a=>1, :b=>5, :c=>6}])
@ds.all.must_equal [{:a=>1, :b=>4, :c=>3, :c_is_unique=>false}]
end
it "Dataset#insert_conflict should handle upserts" do
@ds.insert(1, 2, 3)
@ds.insert_conflict(:target=>:a, :update=>{:b=>3}).insert(1, 3, 4).must_be_nil
@ds.all.must_equal [{:a=>1, :b=>3, :c=>3, :c_is_unique=>false}]
@ds.insert_conflict(:target=>[:b, :c], :update=>{:c=>5}).insert(5, 3, 3).must_be_nil
@ds.all.must_equal [{:a=>1, :b=>3, :c=>5, :c_is_unique=>false}]
@ds.insert_conflict(:constraint=>:ic_test_a_uidx, :update=>{:b=>4}).insert(1, 3).must_be_nil
@ds.all.must_equal [{:a=>1, :b=>4, :c=>5, :c_is_unique=>false}]
@ds.insert_conflict(:constraint=>:ic_test_a_uidx, :update=>{:b=>5}, :update_where=>{Sequel[:ic_test][:b]=>4}).insert(1, 3, 4).must_be_nil
@ds.all.must_equal [{:a=>1, :b=>5, :c=>5, :c_is_unique=>false}]
@ds.insert_conflict(:constraint=>:ic_test_a_uidx, :update=>{:b=>6}, :update_where=>{Sequel[:ic_test][:b]=>4}).insert(1, 3, 4).must_be_nil
@ds.all.must_equal [{:a=>1, :b=>5, :c=>5, :c_is_unique=>false}]
end
it "Dataset#insert_conflict should support table aliases" do
@ds = @db[Sequel[:ic_test].as(:foo)]
@ds.insert(1, 2, 5)
proc{@ds.insert(1, 3, 4)}.must_raise Sequel::UniqueConstraintViolation
@ds.insert_conflict(:target=>:a, :update=>{:b=>Sequel[:foo][:c] + Sequel[:excluded][:c]}).insert(1, 7, 10)
@ds.all.must_equal [{:a=>1, :b=>15, :c=>5, :c_is_unique=>false}]
end
end if DB.server_version >= 90500
describe "A PostgreSQL database" do
before(:all) do
@db = DB
@db.create_table!(Sequel[:public][:testfk]){primary_key :id; foreign_key :i, Sequel[:public][:testfk]}
end
after(:all) do
@db.drop_table?(Sequel[:public][:testfk])
end
it "should provide the server version" do
@db.server_version.must_be :>, 70000
end
it "should create a dataset using the VALUES clause via #values" do
@db.values([[1, 2], [3, 4]]).map([:column1, :column2]).must_equal [[1, 2], [3, 4]]
end
it "should correctly handle various numbers of columns" do
[1, 2, 15, 16, 17, 63, 64, 65, 255, 256, 257, 1663, 1664].each do |i|
DB.get((1..i).map{|j| Sequel.as(j, "c#{j}")}).must_equal((1..i).to_a)
end
proc{DB.get((1..1665).map{|j| Sequel.as(j, "c#{j}")})}.must_raise Sequel::DatabaseError
end
it "should support ordering in aggregate functions" do
@db.from(@db.values([['1'], ['2']]).as(:t, [:a])).get{string_agg(:a, '-').order(Sequel.desc(:a)).as(:c)}.must_equal '2-1'
end if DB.server_version >= 90000
it "should support ordering and limiting with #values" do
@db.values([[1, 2], [3, 4]]).reverse(:column2, :column1).limit(1).map([:column1, :column2]).must_equal [[3, 4]]
@db.values([[1, 2], [3, 4]]).reverse(:column2, :column1).offset(1).map([:column1, :column2]).must_equal [[1, 2]]
end
it "should support subqueries with #values" do
@db.values([[1, 2]]).from_self.cross_join(@db.values([[3, 4]]).as(:x, [:c1, :c2])).map([:column1, :column2, :c1, :c2]).must_equal [[1, 2, 3, 4]]
end
it "should support JOIN USING" do
@db.from(@db.values([[1, 2]]).as(:x, [:c1, :c2])).join(@db.values([[1, 2]]).as(:y, [:c1, :c2]), [:c1, :c2]).all.must_equal [{:c1=>1, :c2=>2}]
end
it "should support column aliases for JOIN USING" do
@db.from(@db.values([[1, 2]]).as(:x, [:c1, :c2])).join(@db.values([[1, 2]]).as(:y, [:c1, :c2]), Sequel.as([:c1, :c2], :foo)).select_all(:foo).all.must_equal [{:c1=>1, :c2=>2}]
end if DB.server_version >= 140000
it "should respect the :read_only option per-savepoint" do
proc{@db.transaction{@db.transaction(:savepoint=>true, :read_only=>true){@db[Sequel[:public][:testfk]].insert}}}.must_raise(Sequel::DatabaseError)
proc{@db.transaction(:auto_savepoint=>true, :read_only=>true){@db.transaction(:read_only=>false){@db[Sequel[:public][:testfk]].insert}}}.must_raise(Sequel::DatabaseError)
@db[Sequel[:public][:testfk]].delete
@db.transaction{@db[Sequel[:public][:testfk]].insert; @db.transaction(:savepoint=>true, :read_only=>true){@db[Sequel[:public][:testfk]].all;}}
@db.transaction{@db.transaction(:savepoint=>true, :read_only=>true){}; @db[Sequel[:public][:testfk]].insert}
@db.transaction{@db[Sequel[:public][:testfk]].all; @db.transaction(:savepoint=>true, :read_only=>true){@db[Sequel[:public][:testfk]].all;}}
end
it "should support disable_insert_returning" do
ds = @db[Sequel[:public][:testfk]].disable_insert_returning
ds.delete
ds.insert.must_be_nil
id = ds.max(:id)
ds.select_order_map([:id, :i]).must_equal [[id, nil]]
ds.insert(:i=>id).must_be_nil
ds.select_order_map([:id, :i]).must_equal [[id, nil], [id+1, id]]
ds.insert_select(:i=>ds.max(:id)).must_be_nil
ds.select_order_map([:id, :i]).must_equal [[id, nil], [id+1, id]]
c = Class.new(Sequel::Model(ds))
c.class_eval do
def before_create
self.id = model.max(:id)+1
super
end
end
c.create(:i=>id+1).must_equal c.load(:id=>id+2, :i=>id+1)
ds.select_order_map([:id, :i]).must_equal [[id, nil], [id+1, id], [id+2, id+1]]
ds.delete
end
it "should support functions with and without quoting" do
ds = @db[Sequel[:public][:testfk]]
ds.delete
ds.insert
ds.get{sum(1)}.must_equal 1
ds.get{Sequel.function('pg_catalog.sum', 1)}.must_equal 1
ds.get{sum.function(1)}.must_equal 1
ds.get{pg_catalog[:sum].function(1)}.must_equal 1
ds.delete
end
it "should support a :qualify option to tables and views" do
@db.tables(:qualify=>true).must_include(Sequel.qualify('public', 'testfk'))
begin
@db.create_view(:testfkv, @db[:testfk])
@db.views(:qualify=>true).must_include(Sequel.qualify('public', 'testfkv'))
ensure
@db.drop_view(:testfkv)
end
end
it "should handle double underscores in tables when using the qualify option" do
@db.create_table!(Sequel.qualify(:public, 'test__fk')){Integer :a}
@db.tables(:qualify=>true).must_include(Sequel.qualify('public', 'test__fk'))
@db.drop_table(Sequel.qualify(:public, 'test__fk'))
end
it "should not typecast the int2vector type incorrectly" do
@db.get(Sequel.cast('10 20', :int2vector)).wont_equal 10
end
it "should not typecast the money type incorrectly" do
@db.get(Sequel.cast('10.01', :money)).wont_equal 0
end
it "should correctly parse the schema" do
[[[:id, 23], [:i, 23]], [[:id, 20], [:i, 20]]].must_include @db.schema(Sequel[:public][:testfk], :reload=>true).map{|c,s| [c, s[:oid]]}
end
it "should parse foreign keys for tables in a schema" do
@db.foreign_key_list(Sequel[:public][:testfk]).must_equal [{:on_delete=>:no_action, :on_update=>:no_action, :columns=>[:i], :key=>[:id], :deferrable=>false, :table=>Sequel.qualify(:public, :testfk), :name=>:testfk_i_fkey}]
@db.foreign_key_list(Sequel[:public][:testfk], :schema=>false).must_equal [{:on_delete=>:no_action, :on_update=>:no_action, :columns=>[:i], :key=>[:id], :deferrable=>false, :table=>:testfk, :name=>:testfk_i_fkey, :schema=>:public}]
end
it "should return uuid fields as strings" do
@db.get(Sequel.cast('550e8400-e29b-41d4-a716-446655440000', :uuid)).must_equal '550e8400-e29b-41d4-a716-446655440000'
end
it "should handle inserts with placeholder literal string tables" do
ds = @db.from(Sequel.lit('?', :testfk))
ds.delete
ds.insert(:id=>1)
ds.select_map(:id).must_equal [1]
end
it "should have notice receiver receive notices" do
a = nil
Sequel.connect(DB.opts.merge(:notice_receiver=>proc{|r| a = r.result_error_message})){|db| db.do("BEGIN\nRAISE WARNING 'foo';\nEND;")}
a.must_equal "WARNING: foo\n"
end if uses_pg && DB.server_version >= 90000
end
describe "A PostgreSQL database " do
after do
DB.drop_table?(:b, :a)
end
it "should handle non-ASCII column aliases" do
s = String.new("\u00E4").force_encoding(DB.get(Sequel.cast('a', :text)).encoding)
k, v = DB.select(Sequel.as(Sequel.cast(s, :text), s)).first.shift
k.to_s.must_equal v
end
it "should parse foreign keys referencing current table using :reverse option" do
DB.create_table!(:a) do
primary_key :id
Integer :i
Integer :j
foreign_key :a_id, :a, :foreign_key_constraint_name=>:a_a
unique [:i, :j]
end
DB.create_table!(:b) do
foreign_key :a_id, :a, :foreign_key_constraint_name=>:a_a
Integer :c
Integer :d
foreign_key [:c, :d], :a, :key=>[:j, :i], :name=>:a_c_d
end
DB.foreign_key_list(:a, :reverse=>true).must_equal [
{:name=>:a_a, :columns=>[:a_id], :key=>[:id], :on_update=>:no_action, :on_delete=>:no_action, :deferrable=>false, :table=>:a, :schema=>:public},
{:name=>:a_a, :columns=>[:a_id], :key=>[:id], :on_update=>:no_action, :on_delete=>:no_action, :deferrable=>false, :table=>:b, :schema=>:public},
{:name=>:a_c_d, :columns=>[:c, :d], :key=>[:j, :i], :on_update=>:no_action, :on_delete=>:no_action, :deferrable=>false, :table=>:b, :schema=>:public}]
end
end
describe "A PostgreSQL database with domain types" do
before(:all) do
@db = DB
@db << "DROP DOMAIN IF EXISTS positive_number CASCADE"
@db << "CREATE DOMAIN positive_number AS numeric(10,2) CHECK (VALUE > 0)"
@db.create_table!(:testfk){positive_number :id, :primary_key=>true}
end
after(:all) do
@db.drop_table?(:testfk)
@db << "DROP DOMAIN positive_number"
end
it "should correctly parse the schema" do
sch = @db.schema(:testfk, :reload=>true)
sch.first.last.delete(:domain_oid).must_be_kind_of(Integer)
sch.first.last[:db_domain_type].must_equal 'positive_number'
end
end
describe "A PostgreSQL dataset" do
before(:all) do
@db = DB
@d = @db[:test]
@db.create_table! :test do
text :name
integer :value, :index => true
end
end
before do
@d.delete
end
after do
@db.drop_table?(:atest)
end
after(:all) do
@db.drop_table?(:test)
end
it "should support returning limited results with ties" do
@d.insert(:value => 1)
@d.insert(:value => 1)
@d.insert(:value => 2)
@d.insert(:value => 2)
@d.order(:value).select(:value).limit(1).select_order_map(:value).must_equal [1]
@d.order(:value).select(:value).limit(1).with_ties.select_order_map(:value).must_equal [1, 1]
@d.order(:value).select(:value).limit(2, 1).select_order_map(:value).must_equal [1, 2]
@d.order(:value).select(:value).limit(2, 1).with_ties.select_order_map(:value).must_equal [1, 2, 2]
@d.order(:value).select(:value).offset(1).select_order_map(:value).must_equal [1, 2, 2]
@d.order(:value).select(:value).offset(1).with_ties.select_order_map(:value).must_equal [1, 2, 2]
end if DB.server_version >= 130000
it "should support regexps" do
@d.insert(:name => 'abc', :value => 1)
@d.insert(:name => 'bcd', :value => 2)
@d.filter(:name => /bc/).count.must_equal 2
@d.filter(:name => /^bc/).count.must_equal 1
end
it "should support NULLS FIRST and NULLS LAST" do
@d.insert(:name => 'abc')
@d.insert(:name => 'bcd')
@d.insert(:name => 'bcd', :value => 2)
@d.order(Sequel.asc(:value, :nulls=>:first), :name).select_map(:name).must_equal %w[abc bcd bcd]
@d.order(Sequel.asc(:value, :nulls=>:last), :name).select_map(:name).must_equal %w[bcd abc bcd]
@d.order(Sequel.asc(:value, :nulls=>:first), :name).reverse.select_map(:name).must_equal %w[bcd bcd abc]
end
it "should support selecting from LATERAL functions" do
@d.from{[generate_series(1,3,1).as(:a), pow(:a, 2).lateral.as(:b)]}.select_map([:a, :b])== [[1, 1], [2, 4], [3, 9]]
end if DB.server_version >= 90300
it "should support ordered-set and hypothetical-set aggregate functions" do
@d.from{generate_series(1,3,1).as(:a)}.select{(a.sql_number % 2).as(:a)}.from_self.get{mode.function.within_group(:a)}.must_equal 1
end if DB.server_version >= 90400
it "should support functions with ordinality" do
@d.from{generate_series(1,10,3).with_ordinality}.select_map([:generate_series, :ordinality]).must_equal [[1, 1], [4, 2], [7, 3], [10, 4]]
end if DB.server_version >= 90400
it "#lock should lock tables and yield if a block is given" do
@d.lock('EXCLUSIVE'){@d.insert(:name=>'a')}
end
it "#lock should raise Error for unsupported lock mode" do
proc{@d.lock('BAD'){}}.must_raise Sequel::Error
end
it "should support exclusion constraints when creating or altering tables" do
@db.create_table!(:atest){Integer :t; exclude [[Sequel.desc(:t, :nulls=>:last), '=']], :using=>:btree, :where=>proc{t > 0}}
@db[:atest].insert(1)
@db[:atest].insert(2)
proc{@db[:atest].insert(2)}.must_raise(Sequel::Postgres::ExclusionConstraintViolation)
@db.create_table!(:atest){Integer :t}
@db.alter_table(:atest){add_exclusion_constraint [[:t, '=']], :using=>:btree, :name=>'atest_ex'}
@db[:atest].insert(1)
@db[:atest].insert(2)
proc{@db[:atest].insert(2)}.must_raise(Sequel::Postgres::ExclusionConstraintViolation)
@db.alter_table(:atest){drop_constraint 'atest_ex'}
end if DB.server_version >= 90000
it "should support deferrable exclusion constraints" do
@db.create_table!(:atest){Integer :t; exclude [[Sequel.desc(:t, :nulls=>:last), '=']], :using=>:btree, :where=>proc{t > 0}, :deferrable => true}
proc do
@db.transaction do
@db[:atest].insert(2)
@db[:atest].insert(2)
end
end.must_raise(Sequel::Postgres::ExclusionConstraintViolation)
end if DB.server_version >= 90000
it "should support Database#error_info for getting info hash on the given error" do
@db.create_table!(:atest){Integer :t; Integer :t2, :null=>false, :default=>1; constraint :f, :t=>0}
begin
@db[:atest].insert(1)
rescue => e
end
e.wont_equal nil
info = @db.error_info(e)
info[:schema].must_equal 'public'
info[:table].must_equal 'atest'
info[:constraint].must_equal 'f'
info[:column].must_be_nil
info[:type].must_be_nil
begin
@db[:atest].insert(0, nil)
rescue => e
end
e.wont_equal nil
info = @db.error_info(e.wrapped_exception)
info[:schema].must_equal 'public'
info[:table].must_equal 'atest'
info[:constraint].must_be_nil
info[:column].must_equal 't2'
info[:type].must_be_nil
end if DB.server_version >= 90300 && uses_pg && Object.const_defined?(:PG) && ::PG.const_defined?(:Constants) && ::PG::Constants.const_defined?(:PG_DIAG_SCHEMA_NAME)
it "should support Database#do for executing anonymous code blocks" do
@db.drop_table?(:btest)
@db.do "BEGIN EXECUTE 'CREATE TABLE btest (a INTEGER)'; EXECUTE 'INSERT INTO btest VALUES (1)'; END"
@db[:btest].select_map(:a).must_equal [1]
@db.do "BEGIN EXECUTE 'DROP TABLE btest; CREATE TABLE atest (a INTEGER)'; EXECUTE 'INSERT INTO atest VALUES (1)'; END", :language=>:plpgsql
@db[:atest].select_map(:a).must_equal [1]
end if DB.server_version >= 90000
it "should support adding foreign key constarints that are not yet valid, and validating them later" do
@db.create_table!(:atest){primary_key :id; Integer :fk}
@db[:atest].insert(1, 5)
@db.alter_table(:atest){add_foreign_key [:fk], :atest, :not_valid=>true, :name=>:atest_fk}
@db[:atest].insert(2, 1)
proc{@db[:atest].insert(3, 4)}.must_raise(Sequel::ForeignKeyConstraintViolation)
proc{@db.alter_table(:atest){validate_constraint :atest_fk}}.must_raise(Sequel::ForeignKeyConstraintViolation)
@db[:atest].where(:id=>1).update(:fk=>2)
@db.alter_table(:atest){validate_constraint :atest_fk}
@db.alter_table(:atest){validate_constraint :atest_fk}
end if DB.server_version >= 90200
it "should support adding check constarints that are not yet valid, and validating them later" do
@db.create_table!(:atest){Integer :a}
@db[:atest].insert(5)
@db.alter_table(:atest){add_constraint({:name=>:atest_check, :not_valid=>true}){a >= 10}}
@db[:atest].insert(10)
proc{@db[:atest].insert(6)}.must_raise(Sequel::CheckConstraintViolation)
proc{@db.alter_table(:atest){validate_constraint :atest_check}}.must_raise(Sequel::CheckConstraintViolation, Sequel::DatabaseError)
@db[:atest].where{a < 10}.update(:a=>Sequel.+(:a, 10))
@db.alter_table(:atest){validate_constraint :atest_check}
@db.alter_table(:atest){validate_constraint :atest_check}
end if DB.server_version >= 90200
it "should support :using when altering a column's type" do
@db.create_table!(:atest){Integer :t}
@db[:atest].insert(1262404000)
@db.alter_table(:atest){set_column_type :t, Time, :using=>Sequel.cast('epoch', Time) + Sequel.cast('1 second', :interval) * :t}
@db[:atest].get(Sequel.extract(:year, :t)).must_equal 2010
end
it "should support :using with a string when altering a column's type" do
@db.create_table!(:atest){Integer :t}
@db[:atest].insert(1262304000)
@db.alter_table(:atest){set_column_type :t, Time, :using=>"'epoch'::timestamp + '1 second'::interval * t"}
@db[:atest].get(Sequel.extract(:year, :t)).must_equal 2010
end
it "should have #transaction support various types of synchronous options" do
@db.transaction(:synchronous=>:on){}
@db.transaction(:synchronous=>true){}
@db.transaction(:synchronous=>:off){}
@db.transaction(:synchronous=>false){}
@db.transaction(:synchronous=>nil){}
if @db.server_version >= 90100
@db.transaction(:synchronous=>:local){}
if @db.server_version >= 90200
@db.transaction(:synchronous=>:remote_write){}
end
end
end
it "should have #transaction support read only transactions" do
@db.transaction(:read_only=>true){}
@db.transaction(:read_only=>false){}
@db.transaction(:isolation=>:serializable, :read_only=>true){}
@db.transaction(:isolation=>:serializable, :read_only=>false){}
end
it "should have #transaction support deferrable transactions" do
@db.transaction(:deferrable=>true){}
@db.transaction(:deferrable=>false){}
@db.transaction(:deferrable=>true, :read_only=>true){}
@db.transaction(:deferrable=>false, :read_only=>false){}
@db.transaction(:isolation=>:serializable, :deferrable=>true, :read_only=>true){}
@db.transaction(:isolation=>:serializable, :deferrable=>false, :read_only=>false){}
end if DB.server_version >= 90100
it "should support parsing partial indexes with :include_partial option" do
@db.add_index :test, [:name, :value], :where=>(Sequel[:value] > 10), :name=>:tnv_partial
@db.indexes(:test)[:tnv_partial].must_be_nil
@db.indexes(:test, :include_partial=>true)[:tnv_partial].must_equal(:columns=>[:name, :value], :unique=>false, :deferrable=>nil)
end
it "should support creating indexes concurrently" do
@db.add_index :test, [:name, :value], :concurrently=>true, :name=>'tnv0'
end
it "should support dropping indexes only if they already exist" do
proc{@db.drop_index :test, [:name, :value], :name=>'tnv1'}.must_raise Sequel::DatabaseError
@db.drop_index :test, [:name, :value], :if_exists=>true, :name=>'tnv1'
@db.add_index :test, [:name, :value], :name=>'tnv1'
@db.drop_index :test, [:name, :value], :if_exists=>true, :name=>'tnv1'
end
it "should support CASCADE when dropping indexes" do
@db.add_index :test, [:name, :value], :name=>'tnv2', :unique=>true
@db.create_table(:atest){text :name; integer :value; foreign_key [:name, :value], :test, :key=>[:name, :value]}
@db.foreign_key_list(:atest).length.must_equal 1
@db.drop_index :test, [:name, :value], :cascade=>true, :name=>'tnv2'
@db.foreign_key_list(:atest).length.must_equal 0
end
it "should support dropping indexes concurrently" do
@db.add_index :test, [:name, :value], :name=>'tnv2'
@db.drop_index :test, [:name, :value], :concurrently=>true, :name=>'tnv2'
end if DB.server_version >= 90200
it "should support creating indexes only if they do not exist" do
@db.add_index :test, [:name, :value], :name=>'tnv3'
proc{@db.add_index :test, [:name, :value], :name=>'tnv3'}.must_raise Sequel::DatabaseError
@db.add_index :test, [:name, :value], :if_not_exists=>true, :name=>'tnv3'
end if DB.server_version >= 90500
it "should support specifying whether NULLS are distinct in unique indexes" do
@db.create_table(:atest){Integer :a}
@db.add_index :atest, :a, :nulls_distinct=>true, :unique=>true
@db[:atest].insert
@db[:atest].insert
@db[:atest].count.must_equal 2
@db.create_table!(:atest){Integer :a}
@db.add_index :atest, :a, :nulls_distinct=>false, :unique=>true
@db[:atest].insert
proc{@db[:atest].insert}.must_raise Sequel::DatabaseError
end if DB.server_version >= 150000
it "should support including columns in indexes" do
@db.create_table(:atest){Integer :a; Integer :b; Integer :c}
@db.add_index :atest, :a, :include=>[:b, :c]
@db.add_index :atest, :b, :include=>:a
end if DB.server_version >= 110000
it "should support specifying tablespaces for tables" do
@db.create_table(:atest, :tablespace=>:pg_default){Integer :a}
end
it "should support specifying tablespaces for indexes" do
@db.create_table(:atest){Integer :a}
@db.add_index :atest, :a, :tablespace=>:pg_default
end
it "#lock should lock table if inside a transaction" do
@db.transaction{@d.lock('EXCLUSIVE'); @d.insert(:name=>'a')}
end
it "#lock should return nil" do
@d.lock('EXCLUSIVE'){@d.insert(:name=>'a')}.must_be_nil
@db.transaction{@d.lock('EXCLUSIVE').must_be_nil; @d.insert(:name=>'a')}
end
it "should raise an error if attempting to update a joined dataset with a single FROM table" do
proc{@db[:test].join(:test, [:name]).update(:name=>'a')}.must_raise(Sequel::Error, 'Need multiple FROM tables if updating/deleting a dataset with JOINs')
end
it "should truncate with options" do
@d.insert( :name => 'abc', :value => 1)
@d.count.must_equal 1
@d.truncate(:cascade => true)
@d.count.must_equal 0
if @d.db.server_version > 80400
@d.insert( :name => 'abc', :value => 1)
@d.truncate(:cascade => true, :only=>true, :restart=>true)
@d.count.must_equal 0
end
end
it "should truncate multiple tables at once" do
tables = [:test, :test]
tables.each{|t| @d.from(t).insert}
@d.from(:test, :test).truncate
tables.each{|t| @d.from(t).count.must_equal 0}
end
it "should not allow truncate for grouped or joined datasets" do
proc{@d.from(:test).cross_join(:test).truncate}.must_raise Sequel::InvalidOperation
proc{@d.from(:test).group(:test).truncate}.must_raise Sequel::InvalidOperation
end
it "should raise when attempting to insert with 0 or multiple tables" do
proc{@d.from(:test, :test).insert}.must_raise Sequel::InvalidOperation
proc{@d.from.insert}.must_raise Sequel::Error
end
end
describe "Dataset#distinct" do
before do
@db = DB
@db.create_table!(:a) do
Integer :a
Integer :b
end
@ds = @db[:a]
end
after do
@db.drop_table?(:a)
end
it "#distinct with arguments should return results distinct on those arguments" do
@ds.insert(20, 10)
@ds.insert(30, 10)
@ds.order(:b, :a).distinct.map(:a).must_equal [20, 30]
@ds.order(:b, Sequel.desc(:a)).distinct.map(:a).must_equal [30, 20]
@ds.order(:b, :a).distinct(:b).map(:a).must_equal [20]
@ds.order(:b, Sequel.desc(:a)).distinct(:b).map(:a).must_equal [30]
end
end
if DB.pool.respond_to?(:max_size) and DB.pool.max_size > 1
describe "Dataset#for_update support" do
before do
@db = DB.create_table!(:items) do
primary_key :id
Integer :number
String :name
end
@ds = DB[:items]
end
after do
DB.drop_table?(:items)
DB.disconnect
end
it "should handle FOR UPDATE" do
@ds.insert(:number=>20)
c, t = nil, nil
q = Queue.new
DB.transaction do
@ds.for_update.first(:id=>1)
t = Thread.new do
DB.transaction do
q.push nil
@ds.filter(:id=>1).update(:name=>'Jim')
c = @ds.first(:id=>1)
q.push nil
end
end
q.pop
@ds.filter(:id=>1).update(:number=>30)
end
q.pop
t.join
c.must_equal(:id=>1, :number=>30, :name=>'Jim')
end
it "should handle FOR SHARE" do
@ds.insert(:number=>20)
c, t = nil
q = Queue.new
DB.transaction do
@ds.for_share.first(:id=>1)
t = Thread.new do
DB.transaction do
c = @ds.for_share.filter(:id=>1).first
q.push nil
end
end
q.pop
@ds.filter(:id=>1).update(:name=>'Jim')
c.must_equal(:id=>1, :number=>20, :name=>nil)
end
t.join
end
end
end
describe "A PostgreSQL dataset with a timestamp field" do
before(:all) do
@db = DB
@db.create_table! :test3 do
Date :date
DateTime :time
end
@d = @db[:test3]
if @db.adapter_scheme == :postgres
@db.convert_infinite_timestamps.must_equal false
@db.convert_infinite_timestamps = false
@db.convert_infinite_timestamps = true
@db.convert_infinite_timestamps = false
else
@db.extension :pg_extended_date_support
end
end
before do
@d.delete
end
after do
@db.convert_infinite_timestamps = false
Sequel.datetime_class = Time
Sequel::SQLTime.date = nil
Sequel.default_timezone = nil
end
after(:all) do
@db.drop_table?(:test3)
end
it "should store milliseconds in time fields for Time objects" do
t = Time.now
@d.insert(:time=>t)
t2 = @d.get(:time)
@d.literal(t2).must_equal @d.literal(t)
t2.strftime('%Y-%m-%d %H:%M:%S').must_equal t.strftime('%Y-%m-%d %H:%M:%S')
(t2.is_a?(Time) ? t2.usec : t2.strftime('%N').to_i/1000).must_equal t.usec
end
it "should store milliseconds in time fields for DateTime objects" do
t = DateTime.now
@d.insert(:time=>t)
t2 = @d.get(:time)
@d.literal(t2).must_equal @d.literal(t)
t2.strftime('%Y-%m-%d %H:%M:%S').must_equal t.strftime('%Y-%m-%d %H:%M:%S')
(t2.is_a?(Time) ? t2.usec : t2.strftime('%N').to_i/1000).must_equal t.strftime('%N').to_i/1000
end
it "should respect SQLTime.date setting for time columns" do
Sequel::SQLTime.date = Time.local(2000, 1, 2)
d = Sequel::SQLTime.create(10, 11, 12)
@db.get(Sequel.cast(d, :time)).must_equal d
@db.get(Sequel.cast(d, :timetz)).must_equal d
end
it "should respect Sequel.application_timezone for time columns" do
d = Sequel::SQLTime.create(10, 11, 12)
Sequel.application_timezone = :local
@db.get(Sequel.cast(d, :time)).utc_offset.must_equal Time.now.utc_offset
@db.get(Sequel.cast(d, :timetz)).utc_offset.must_equal Time.now.utc_offset
Sequel.application_timezone = :utc
@db.get(Sequel.cast(d, :time)).utc_offset.must_equal 0
@db.get(Sequel.cast(d, :timetz)).utc_offset.must_equal 0
end
it "should handle parsing dates and timestamps in with 1, 2, and 3 digit years" do
[1, 10, 100, -2, -20, -200].each do |year|
d = Date.new(year, 2, 3)
@db.get(Sequel.cast(d, Date)).must_equal d
begin
Sequel.default_timezone = :utc
d = Time.utc(year, 2, 3, 10, 11, 12)
@db.get(Sequel.cast(d, Time)).must_equal d
Sequel.datetime_class = DateTime
d = DateTime.new(year, 2, 3, 10, 11, 12)
@db.get(Sequel.cast(d, Time)).must_equal d
ensure
Sequel.datetime_class = Time
Sequel.default_timezone = nil
end
end
end
it "should handle parsing dates and timestamps in the distant future" do
d = Date.new(5874896, 2, 3)
@db.get(Sequel.cast(d, Date)).must_equal d
d = Time.local(294275, 2, 3, 10, 11, 12)
@db.get(Sequel.cast(d, Time)).must_equal d
Sequel.datetime_class = DateTime
d = DateTime.new(294275, 2, 3, 10, 11, 12)
@db.get(Sequel.cast(d, Time)).must_equal d
end
it "should handle BC times and dates" do
d = Date.new(-1234, 2, 3)
@db.get(Sequel.cast(d, Date)).must_equal d
Sequel.default_timezone = :utc
t = Time.at(-100000000000).utc + 0.5
@db.get(Sequel.cast(t, Time)).must_equal t
@db.get(Sequel.cast(t, :timestamptz)).must_equal t
Sequel.datetime_class = DateTime
dt = DateTime.new(-1234, 2, 3, 10, 20, Rational(30, 20))
@db.get(Sequel.cast(dt, DateTime)).must_equal dt
@db.get(Sequel.cast(dt, :timestamptz)).must_equal dt
end
it "should handle BC times and dates in bound variables" do
d = Date.new(-1234, 2, 3)
@db.select(Sequel.cast(:$d, Date)).call(:single_value, :d=>d).must_equal d
Sequel.default_timezone = :utc
t = Time.at(-100000000000).utc + 0.5
@db.select(Sequel.cast(:$t, Time)).call(:single_value, :t=>t).must_equal t
@db.select(Sequel.cast(:$t, :timestamptz)).call(:single_value, :t=>t).must_equal t
Sequel.datetime_class = DateTime
dt = DateTime.new(-1234, 2, 3, 10, 20, Rational(30, 20))
@db.select(Sequel.cast(:$dt, DateTime)).call(:single_value, :dt=>dt).must_equal dt
@db.select(Sequel.cast(:$dt, :timestamptz)).call(:single_value, :dt=>dt).must_equal dt
end if uses_pg_or_jdbc
it "should handle infinite timestamps if convert_infinite_timestamps is set" do
@d.insert(:time=>Sequel.cast('infinity', DateTime))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:time).must_be_nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:time).must_equal 'infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(:time).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 'nil'
@db[:test3].get(:time).must_be_nil
@db.convert_infinite_timestamps = 'string'
@db[:test3].get(:time).must_equal 'infinity'
@db.convert_infinite_timestamps = 'date'
@db[:test3].get(:time).must_equal Date::Infinity.new
@db.convert_infinite_timestamps = 'float'
@db[:test3].get(:time).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 't'
@db[:test3].get(:time).must_equal 1.0/0.0
@db.convert_infinite_timestamps = true
@db[:test3].get(:time).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 'f'
proc{@db[:test3].get(:time)}.must_raise ArgumentError, Sequel::InvalidValue
@db.convert_infinite_timestamps = nil
proc{@db[:test3].get(:time)}.must_raise ArgumentError, Sequel::InvalidValue
@db.convert_infinite_timestamps = false
proc{@db[:test3].get(:time)}.must_raise ArgumentError, Sequel::InvalidValue
@d.update(:time=>Sequel.cast('-infinity', DateTime))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:time).must_be_nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:time).must_equal '-infinity'
@db.convert_infinite_timestamps = :date
@db[:test3].get(:time).must_equal(-Date::Infinity.new)
@db.convert_infinite_timestamps = :float
@db[:test3].get(:time).must_equal(-1.0/0.0)
end
it "should handle infinite dates if convert_infinite_timestamps is set" do
@d.insert(:time=>Sequel.cast('infinity', DateTime))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(Sequel.cast(:time, Date)).must_be_nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(Sequel.cast(:time, Date)).must_equal 'infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(Sequel.cast(:time, Date)).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 'nil'
@db[:test3].get(Sequel.cast(:time, Date)).must_be_nil
@db.convert_infinite_timestamps = 'string'
@db[:test3].get(Sequel.cast(:time, Date)).must_equal 'infinity'
@db.convert_infinite_timestamps = 'float'
@db[:test3].get(Sequel.cast(:time, Date)).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 't'
@db[:test3].get(Sequel.cast(:time, Date)).must_equal 1.0/0.0
@db.convert_infinite_timestamps = true
@db[:test3].get(Sequel.cast(:time, Date)).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 'f'
proc{@db[:test3].get(Sequel.cast(:time, Date))}.must_raise ArgumentError, Sequel::InvalidValue
@db.convert_infinite_timestamps = nil
proc{@db[:test3].get(Sequel.cast(:time, Date))}.must_raise ArgumentError, Sequel::InvalidValue
@db.convert_infinite_timestamps = false
proc{@db[:test3].get(Sequel.cast(:time, Date))}.must_raise ArgumentError, Sequel::InvalidValue
@d.update(:time=>Sequel.cast('-infinity', DateTime))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(Sequel.cast(:time, Date)).must_be_nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(Sequel.cast(:time, Date)).must_equal '-infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(Sequel.cast(:time, Date)).must_equal(-1.0/0.0)
end
it "should handle conversions from infinite strings/floats in models" do
c = Class.new(Sequel::Model(:test3))
@db.convert_infinite_timestamps = :float
c.new(:time=>'infinity').time.must_equal 'infinity'
c.new(:time=>'-infinity').time.must_equal '-infinity'
c.new(:time=>1.0/0.0).time.must_equal 1.0/0.0
c.new(:time=>-1.0/0.0).time.must_equal(-1.0/0.0)
end
it "should handle infinite dates if convert_infinite_timestamps is set" do
@d.insert(:date=>Sequel.cast('infinity', Date))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:date).must_be_nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:date).must_equal 'infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(:date).must_equal 1.0/0.0
@d.update(:date=>Sequel.cast('-infinity', :timestamp))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:date).must_be_nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:date).must_equal '-infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(:date).must_equal(-1.0/0.0)
end
it "should handle conversions from infinite strings/floats in models" do
c = Class.new(Sequel::Model(:test3))
@db.convert_infinite_timestamps = :float
c.new(:date=>'infinity').date.must_equal 'infinity'
c.new(:date=>'-infinity').date.must_equal '-infinity'
c.new(:date=>1.0/0.0).date.must_equal 1.0/0.0
c.new(:date=>-1.0/0.0).date.must_equal(-1.0/0.0)
end
it "explain and analyze should not raise errors" do
@d = DB[:test3]
@d.explain
@d.analyze
end
it "#locks should be a dataset returning database locks " do
@db.locks.must_be_kind_of(Sequel::Dataset)
@db.locks.all.must_be_kind_of(Array)
end
end
describe "A PostgreSQL database" do
before do
@db = DB
@db.create_table! :test2 do
text :name
integer :value
end
end
after do
@db.drop_table?(:test2)
end
it "should support column operations" do
@db.create_table!(:test2){text :name; integer :value}
@db[:test2].insert({})
@db[:test2].columns.must_equal [:name, :value]
@db.add_column :test2, :xyz, :text, :default => '000'
@db[:test2].columns.must_equal [:name, :value, :xyz]
@db[:test2].insert(:name => 'mmm', :value => 111)
@db[:test2].first[:xyz].must_equal '000'
@db[:test2].columns.must_equal [:name, :value, :xyz]
@db.drop_column :test2, :xyz
@db[:test2].columns.must_equal [:name, :value]
@db[:test2].delete
@db.add_column :test2, :xyz, :text, :default => '000'
@db[:test2].insert(:name => 'mmm', :value => 111, :xyz => 'qqqq')
@db[:test2].columns.must_equal [:name, :value, :xyz]
@db.rename_column :test2, :xyz, :zyx
@db[:test2].columns.must_equal [:name, :value, :zyx]
@db[:test2].first[:zyx].must_equal 'qqqq'
@db.add_column :test2, :xyz, :float
@db[:test2].delete
@db[:test2].insert(:name => 'mmm', :value => 111, :xyz => 56.78)
@db.set_column_type :test2, :xyz, :integer
@db[:test2].first[:xyz].must_equal 57
end
end
describe "A PostgreSQL database" do
before do
@db = DB
@db.drop_table?(:posts)
end
after do
@db.drop_table?(:posts)
end
it "should support resetting the primary key sequence" do
@db.create_table(:posts){primary_key :a}
@db[:posts].insert(:a=>20).must_equal 20
@db[:posts].insert.must_equal 1
@db[:posts].insert.must_equal 2
@db[:posts].insert(:a=>10).must_equal 10
@db.reset_primary_key_sequence(:posts).must_equal 21
@db[:posts].insert.must_equal 21
@db[:posts].order(:a).map(:a).must_equal [1, 2, 10, 20, 21]
end
it "should support specifying Integer/Bignum types in primary keys and have them be auto incrementing" do
@db.create_table(:posts){primary_key :a, :type=>Integer}
@db[:posts].insert.must_equal 1
@db[:posts].insert.must_equal 2
@db.create_table!(:posts){primary_key :a, :type=>:Bignum}
@db[:posts].insert.must_equal 1
@db[:posts].insert.must_equal 2
end
it "should not raise an error if attempting to resetting the primary key sequence for a table without a primary key" do
@db.create_table(:posts){Integer :a}
@db.reset_primary_key_sequence(:posts).must_be_nil
end
it "should support opclass specification" do
@db.create_table(:posts){text :title; text :body; integer :user_id; index(:user_id, :opclass => :int4_ops, :type => :btree)}
proc{@db.create_table(:posts){text :title; text :body; integer :user_id; index(:user_id, :opclass => :bogus_opclass, :type => :btree)}}.must_raise Sequel::DatabaseError
end
it "should support fulltext indexes and searching" do
@db.create_table(:posts){text :title; text :body; full_text_index [:title, :body]; full_text_index :title, :language => 'french', :index_type=>:gist}
@db[:posts].insert(:title=>'ruby rails', :body=>'yowsa')
@db[:posts].insert(:title=>'sequel', :body=>'ruby')
@db[:posts].insert(:title=>'ruby scooby', :body=>'x')
@db[:posts].full_text_search(:title, 'rails').all.must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
@db[:posts].full_text_search(:title, 'rails', :headline=>true).all.must_equal [{:title=>'ruby rails', :body=>'yowsa', :headline=>'ruby <b>rails</b>'}]
@db[:posts].full_text_search([:title, :body], ['yowsa', 'rails']).all.must_equal [:title=>'ruby rails', :body=>'yowsa']
@db[:posts].full_text_search(:title, 'scooby', :language => 'french').all.must_equal [{:title=>'ruby scooby', :body=>'x'}]
@db[:posts].full_text_search(:title, :$n).call(:select, :n=>'rails').must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
@db[:posts].full_text_search(:title, :$n).prepare(:select, :fts_select).call(:n=>'rails').must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
@db[:posts].insert(:title=>'jruby rubinius ruby maglev mri iron')
@db[:posts].insert(:title=>'ruby jruby maglev mri rubinius iron')
@db[:posts].full_text_search(:title, 'rubinius ruby', :phrase=>true).select_order_map(:title).must_equal ['jruby rubinius ruby maglev mri iron']
@db[:posts].full_text_search(:title, 'jruby maglev', :phrase=>true).select_order_map(:title).must_equal ['ruby jruby maglev mri rubinius iron']
@db[:posts].full_text_search(:title, 'rubinius ruby', :plain=>true).select_order_map(:title).must_equal ['jruby rubinius ruby maglev mri iron', 'ruby jruby maglev mri rubinius iron']
@db[:posts].full_text_search(:title, 'jruby maglev', :plain=>true).select_order_map(:title).must_equal ['jruby rubinius ruby maglev mri iron', 'ruby jruby maglev mri rubinius iron']
if DB.server_version >= 90600
@db[:posts].full_text_search(:title, 'rubinius ruby', :to_tsquery=>:phrase).select_order_map(:title).must_equal ['jruby rubinius ruby maglev mri iron']
@db[:posts].full_text_search(:title, 'jruby maglev', :to_tsquery=>:phrase).select_order_map(:title).must_equal ['ruby jruby maglev mri rubinius iron']
end
@db[:posts].full_text_search(Sequel.function(:to_tsvector, 'simple', :title), 'rails', :tsvector=>true).all.must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
@db[:posts].full_text_search(:title, Sequel.function(:to_tsquery, 'simple', 'rails'), :tsquery=>true).all.must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
proc{@db[:posts].full_text_search(Sequel.function(:to_tsvector, 'simple', :title), 'rubinius ruby', :tsvector=>true, :phrase=>true)}.must_raise(Sequel::Error)
proc{@db[:posts].full_text_search(:title, Sequel.function(:to_tsquery, 'simple', 'rails'), :tsquery=>true, :phrase=>true)}.must_raise(Sequel::Error)
@db[:posts].delete
t1 = "bork " * 1000 + "ruby sequel"
t2 = "ruby sequel " * 1000
@db[:posts].insert(:title=>t1)
@db[:posts].insert(:title=>t2)
@db[:posts].full_text_search(:title, 'ruby & sequel', :rank=>true).select_map(:title).must_equal [t2, t1]
end if DB.server_version >= 80300
it "should support spatial indexes" do
@db.create_table(:posts){box :geom; spatial_index [:geom]}
end
it "should support indexes with index type" do
@db.create_table(:posts){box :geom; index :geom, :type => 'gist'}
end
it "should support unique indexes with index type" do
@db.create_table(:posts){varchar :title, :size => 5; index :title, :type => 'btree', :unique => true, :name=>:post_index_foo}
@db.indexes(:posts).length.must_equal 1
@db.indexes(:posts)[:post_index_foo][:unique].must_equal true
end
it "should support partial indexes" do
@db.create_table(:posts){varchar :title, :size => 5; index :title, :where => {:title => '5'}}
end
it "should support identifiers for table names when creating indexes" do
@db.create_table(Sequel::SQL::Identifier.new(:posts)){varchar :title, :size => 5; index :title}
@db.indexes(:posts).length.must_equal 1
end
it "should support renaming tables" do
@db.create_table!(:posts1){primary_key :a}
@db.rename_table(:posts1, :posts)
end
it "should adding a primary key only if it does not already exists" do
@db.create_table(:posts){Integer :a}
@db.alter_table(:posts){add_column :b, Integer}
@db.alter_table(:posts){add_column :b, Integer, :if_not_exists=>true}
proc{@db.alter_table(:posts){add_column :b, Integer}}.must_raise Sequel::DatabaseError
end if DB.server_version >= 90600
end
describe "Sequel::Postgres::Database" do
before do
@db = DB
@db.create_table!(:posts){Integer :a}
end
after do
@db.run("DROP PROCEDURE test_procedure_posts(#{@args || "int, int"})")
@db.drop_table?(:posts)
end
it "#call_procedure should call a procedure that returns a row" do
@db.run <<SQL
CREATE OR REPLACE PROCEDURE test_procedure_posts(inout a int, inout b int)
LANGUAGE SQL
AS $$
INSERT INTO posts VALUES (a) RETURNING *;
INSERT INTO posts VALUES (a * 2) RETURNING *;
SELECT max(posts.a), min(posts.a) FROM posts;
$$;
SQL
@db.call_procedure(:test_procedure_posts, 1, nil).must_equal(:a=>2, :b=>1)
@db.call_procedure(:test_procedure_posts, 3, nil).must_equal(:a=>6, :b=>1)
end
# Remove after release of pg 1.3.5
skip_due_to_pg_bug = defined?(Sequel::Postgres::USES_PG) &&
Sequel::Postgres::USES_PG &&
DB.respond_to?(:stream_all_queries) &&
DB.stream_all_queries &&
defined?(PG::VERSION) &&
PG::VERSION == '1.3.4'
it "#call_procedure should call a procedure without arguments" do
@args = ''
@db.run <<SQL
CREATE OR REPLACE PROCEDURE test_procedure_posts()
LANGUAGE SQL
AS $$
INSERT INTO posts VALUES (1) RETURNING *;
INSERT INTO posts VALUES (2) RETURNING *;
SELECT max(posts.a), min(posts.a) FROM posts;
$$;
SQL
@db.call_procedure(:test_procedure_posts).must_be_nil
@db[:posts].select_order_map(:a).must_equal [1, 2]
end unless skip_due_to_pg_bug
it "#call_procedure should call a procedure with output parameters" do
@db.run <<SQL
CREATE OR REPLACE PROCEDURE test_procedure_posts(out a int, out b int)
LANGUAGE SQL
AS $$
INSERT INTO posts VALUES (1) RETURNING *;
INSERT INTO posts VALUES (2) RETURNING *;
SELECT max(posts.a), min(posts.a) FROM posts;
$$;
SQL
@db.call_procedure(:test_procedure_posts, nil, nil).must_equal(:a=>2, :b=>1)
end if DB.server_version >= 140000
it "#call_procedure should call a procedure that doesn't return a row" do
@db.run <<SQL
CREATE OR REPLACE PROCEDURE test_procedure_posts(int, int)
LANGUAGE SQL
AS $$
INSERT INTO posts VALUES ($1) RETURNING *;
INSERT INTO posts VALUES ($1 * 2) RETURNING *;
$$;
SQL
@db.call_procedure(:test_procedure_posts, 1, nil).must_be_nil
@db.call_procedure(:test_procedure_posts, 3, nil).must_be_nil
end unless skip_due_to_pg_bug
it "#call_procedure should call a procedure that accepts text" do
@args = 'text'
@db.run <<SQL
CREATE OR REPLACE PROCEDURE test_procedure_posts(inout t text)
LANGUAGE SQL
AS $$
SELECT 'a' || t;
$$;
SQL
@db.call_procedure(:test_procedure_posts, 'b').must_equal(:t=>'ab')
end
end if DB.adapter_scheme == :postgres && DB.server_version >= 110000
describe "Postgres::Dataset#import" do
before do
@db = DB
@db.create_table!(:test){primary_key :x; Integer :y}
@ds = @db[:test]
end
after do
@db.drop_table?(:test)
end
it "#import should a single insert statement" do
@ds.import([:x, :y], [[1, 2], [3, 4]])
@ds.all.must_equal [{:x=>1, :y=>2}, {:x=>3, :y=>4}]
end
it "#import should work correctly when returning primary keys" do
@ds.import([:x, :y], [[1, 2], [3, 4]], :return=>:primary_key).must_equal [1, 3]
@ds.all.must_equal [{:x=>1, :y=>2}, {:x=>3, :y=>4}]
end
it "#import should work correctly when returning primary keys with :slice option" do
@ds.import([:x, :y], [[1, 2], [3, 4]], :return=>:primary_key, :slice=>1).must_equal [1, 3]
@ds.all.must_equal [{:x=>1, :y=>2}, {:x=>3, :y=>4}]
end
it "#import should work correctly with an arbitrary returning value" do
@ds.returning(:y, :x).import([:x, :y], [[1, 2], [3, 4]]).must_equal [{:y=>2, :x=>1}, {:y=>4, :x=>3}]
@ds.all.must_equal [{:x=>1, :y=>2}, {:x=>3, :y=>4}]
end
end
describe "Postgres::Dataset#insert" do
before do
@db = DB
@db.create_table!(:test5){primary_key :xid; Integer :value}
@ds = @db[:test5]
end
after do
@db.drop_table?(:test5)
end
it "should work with static SQL" do
@ds.with_sql('INSERT INTO test5 (value) VALUES (10)').insert.must_be_nil
@db['INSERT INTO test5 (value) VALUES (20)'].insert.must_be_nil
@ds.all.must_equal [{:xid=>1, :value=>10}, {:xid=>2, :value=>20}]
end
it "should insert correctly if using a column array and a value array" do
@ds.insert([:value], [10]).must_equal 1
@ds.all.must_equal [{:xid=>1, :value=>10}]
end
it "should have insert return primary key value" do
@ds.insert(:value=>10).must_equal 1
end
it "should have insert_select insert the record and return the inserted record" do
h = @ds.insert_select(:value=>10)
h[:value].must_equal 10
@ds.first(:xid=>h[:xid])[:value].must_equal 10
end
it "should have insert_select respect existing returning clause" do
h = @ds.returning(Sequel[:value].as(:v), Sequel[:xid].as(:x)).insert_select(:value=>10)
h[:v].must_equal 10
@ds.first(:xid=>h[:x])[:value].must_equal 10
end
it "should have prepared insert_select respect existing returning clause" do
h = @ds.returning(Sequel[:value].as(:v), Sequel[:xid].as(:x)).prepare(:insert_select, :insert_select, :value=>10).call
h[:v].must_equal 10
@ds.first(:xid=>h[:x])[:value].must_equal 10
end
it "should correctly return the inserted record's primary key value" do
value1 = 10
id1 = @ds.insert(:value=>value1)
@ds.first(:xid=>id1)[:value].must_equal value1
value2 = 20
id2 = @ds.insert(:value=>value2)
@ds.first(:xid=>id2)[:value].must_equal value2
end
it "should return nil if the table has no primary key" do
@db.create_table!(:test5){String :name; Integer :value}
@ds.delete
@ds.insert(:name=>'a').must_be_nil
end
end
describe "Postgres::Database schema qualified tables" do
before do
@db = DB
@db << "CREATE SCHEMA schema_test"
@db.instance_variable_set(:@primary_keys, {})
@db.instance_variable_set(:@primary_key_sequences, {})
end
after do
@db << "DROP SCHEMA schema_test CASCADE"
end
it "should be able to create, drop, select and insert into tables in a given schema" do
@db.create_table(Sequel[:schema_test][:schema_test]){primary_key :i}
@db[Sequel[:schema_test][:schema_test]].first.must_be_nil
@db[Sequel[:schema_test][:schema_test]].insert(:i=>1).must_equal 1
@db[Sequel[:schema_test][:schema_test]].first.must_equal(:i=>1)
@db.from(Sequel.lit('schema_test.schema_test')).first.must_equal(:i=>1)
@db.drop_table(Sequel[:schema_test][:schema_test])
@db.create_table(Sequel.qualify(:schema_test, :schema_test)){integer :i}
@db[Sequel[:schema_test][:schema_test]].first.must_be_nil
@db.from(Sequel.lit('schema_test.schema_test')).first.must_be_nil
@db.drop_table(Sequel.qualify(:schema_test, :schema_test))
end
it "#tables should not include tables in a default non-public schema" do
@db.create_table(Sequel[:schema_test][:schema_test]){integer :i}
@db.tables(:schema=>:schema_test).must_include(:schema_test)
@db.tables.wont_include(:pg_am)
@db.tables.wont_include(:domain_udt_usage)
end
it "#tables should return tables in the schema provided by the :schema argument" do
@db.create_table(Sequel[:schema_test][:schema_test]){integer :i}
@db.tables(:schema=>:schema_test).must_equal [:schema_test]
end
it "#schema should not include columns from tables in a default non-public schema" do
@db.create_table(Sequel[:schema_test][:domains]){integer :i}
sch = @db.schema(Sequel[:schema_test][:domains])
cs = sch.map{|x| x.first}
cs.first.must_equal :i
cs.wont_include(:data_type)
end
it "#schema should only include columns from the table in the given :schema argument" do
@db.create_table!(:domains){integer :d}
@db.create_table(Sequel[:schema_test][:domains]){integer :i}
sch = @db.schema(:domains, :schema=>:schema_test)
cs = sch.map{|x| x.first}
cs.first.must_equal :i
cs.wont_include(:d)
@db.drop_table(:domains)
end
it "#schema should not include columns in tables from other domains by default" do
@db.create_table!(Sequel[:public][:domains]){integer :d}
@db.create_table(Sequel[:schema_test][:domains]){integer :i}
begin
@db.schema(:domains).map{|x| x.first}.must_equal [:d]
@db.schema(Sequel[:schema_test][:domains]).map{|x| x.first}.must_equal [:i]
ensure
@db.drop_table?(Sequel[:public][:domains])
end
end
it "#table_exists? should see if the table is in a given schema" do
@db.create_table(Sequel[:schema_test][:schema_test]){integer :i}
@db.table_exists?(Sequel[:schema_test][:schema_test]).must_equal true
end
it "should be able to add and drop indexes in a schema" do
@db.create_table(Sequel[:schema_test][:schema_test]){Integer :i, :index=>true}
@db.indexes(Sequel[:schema_test][:schema_test]).keys.must_equal [:schema_test_schema_test_i_index]
@db.drop_index Sequel[:schema_test][:schema_test], :i
@db.indexes(Sequel[:schema_test][:schema_test]).keys.must_equal []
end
it "should be able to get primary keys for tables in a given schema" do
@db.create_table(Sequel[:schema_test][:schema_test]){primary_key :i}
@db.primary_key(Sequel[:schema_test][:schema_test]).must_equal 'i'
end
it "should be able to get serial sequences for tables in a given schema" do
@db.create_table(Sequel[:schema_test][:schema_test]){primary_key :i}
2.times{@db.primary_key_sequence(Sequel[:schema_test][:schema_test]).must_equal '"schema_test"."schema_test_i_seq"'}
end
it "should be able to get serial sequences for tables that have spaces in the name in a given schema" do
@db.create_table(Sequel[:schema_test][:"schema test"]){primary_key :i}
@db.primary_key_sequence(Sequel[:schema_test][:"schema test"]).must_equal '"schema_test"."schema test_i_seq"'
end
it "should be able to get custom sequences for tables in a given schema" do
@db << "CREATE SEQUENCE schema_test.kseq"
@db.create_table(Sequel[:schema_test][:schema_test]){integer :j; primary_key :k, :type=>:integer, :default=>Sequel.lit("nextval('schema_test.kseq'::regclass)")}
@db.primary_key_sequence(Sequel[:schema_test][:schema_test]).must_equal '"schema_test".kseq'
end
it "should be able to get custom sequences for tables that have spaces in the name in a given schema" do
@db << "CREATE SEQUENCE schema_test.\"ks eq\""
@db.create_table(Sequel[:schema_test][:"schema test"]){integer :j; primary_key :k, :type=>:integer, :default=>Sequel.lit("nextval('schema_test.\"ks eq\"'::regclass)")}
@db.primary_key_sequence(Sequel[:schema_test][:"schema test"]).must_equal '"schema_test"."ks eq"'
end
it "should handle schema introspection cases with tables with same name in multiple schemas" do
begin
@db.create_table(Sequel[:schema_test][:schema_test]) do
primary_key :id
foreign_key :i, Sequel[:schema_test][:schema_test], :index=>{:name=>:schema_test_sti}
end
@db.create_table!(Sequel[:public][:schema_test]) do
primary_key :id
foreign_key :j, Sequel[:public][:schema_test], :index=>{:name=>:public_test_sti}
end
h = @db.schema(:schema_test)
h.length.must_equal 2
h.last.first.must_equal :j
@db.indexes(:schema_test).must_equal(:public_test_sti=>{:unique=>false, :columns=>[:j], :deferrable=>nil})
@db.foreign_key_list(:schema_test).must_equal [{:on_update=>:no_action, :columns=>[:j], :deferrable=>false, :key=>[:id], :table=>:schema_test, :on_delete=>:no_action, :name=>:schema_test_j_fkey, :schema=>:public}]
ensure
@db.drop_table?(Sequel[:public][:schema_test])
end
end
it "should support resetting the primary key sequence" do
name = Sequel[:schema_test][:schema_test]
@db.create_table(name){primary_key :id}
@db[name].insert(:id=>10).must_equal 10
@db.reset_primary_key_sequence(name).must_equal 11
@db[name].insert.must_equal 11
@db[name].select_order_map(:id).must_equal [10, 11]
end
end
describe "Postgres::Database schema qualified tables and eager graphing" do
before(:all) do
@db = DB
@db.run "DROP SCHEMA s CASCADE" rescue nil
@db.run "CREATE SCHEMA s"
@db.create_table(Sequel[:s][:bands]){primary_key :id; String :name}
@db.create_table(Sequel[:s][:albums]){primary_key :id; String :name; foreign_key :band_id, Sequel[:s][:bands]}
@db.create_table(Sequel[:s][:tracks]){primary_key :id; String :name; foreign_key :album_id, Sequel[:s][:albums]}
@db.create_table(Sequel[:s][:members]){primary_key :id; String :name; foreign_key :band_id, Sequel[:s][:bands]}
@Band = Class.new(Sequel::Model(Sequel[:s][:bands]))
@Album = Class.new(Sequel::Model(Sequel[:s][:albums]))
@Track = Class.new(Sequel::Model(Sequel[:s][:tracks]))
@Member = Class.new(Sequel::Model(Sequel[:s][:members]))
def @Band.name; :Band; end
def @Album.name; :Album; end
def @Track.name; :Track; end
def @Member.name; :Member; end
@Band.one_to_many :albums, :class=>@Album, :order=>:name
@Band.one_to_many :members, :class=>@Member, :order=>:name
@Album.many_to_one :band, :class=>@Band, :order=>:name
@Album.one_to_many :tracks, :class=>@Track, :order=>:name
@Track.many_to_one :album, :class=>@Album, :order=>:name
@Member.many_to_one :band, :class=>@Band, :order=>:name
@Member.many_to_many :members, :class=>@Member, :join_table=>Sequel[:s][:bands], :right_key=>:id, :left_key=>:id, :left_primary_key=>:band_id, :right_primary_key=>:band_id, :order=>:name
@Band.many_to_many :tracks, :class=>@Track, :join_table=>Sequel[:s][:albums], :right_key=>:id, :right_primary_key=>:album_id, :order=>:name
@b1 = @Band.create(:name=>"BM")
@b2 = @Band.create(:name=>"J")
@a1 = @Album.create(:name=>"BM1", :band=>@b1)
@a2 = @Album.create(:name=>"BM2", :band=>@b1)
@a3 = @Album.create(:name=>"GH", :band=>@b2)
@a4 = @Album.create(:name=>"GHL", :band=>@b2)
@t1 = @Track.create(:name=>"BM1-1", :album=>@a1)
@t2 = @Track.create(:name=>"BM1-2", :album=>@a1)
@t3 = @Track.create(:name=>"BM2-1", :album=>@a2)
@t4 = @Track.create(:name=>"BM2-2", :album=>@a2)
@m1 = @Member.create(:name=>"NU", :band=>@b1)
@m2 = @Member.create(:name=>"TS", :band=>@b1)
@m3 = @Member.create(:name=>"NS", :band=>@b2)
@m4 = @Member.create(:name=>"JC", :band=>@b2)
end
after(:all) do
@db.run "DROP SCHEMA s CASCADE"
end
it "should return all eager graphs correctly" do
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:albums).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.albums.map{|y| y.tracks}}.must_equal [[[@t1, @t2], [@t3, @t4]], [[], []]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph({:albums=>:tracks}, :members).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.albums.map{|y| y.tracks}}.must_equal [[[@t1, @t2], [@t3, @t4]], [[], []]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
end
it "should have eager graphs work with previous joins" do
bands = @Band.order(Sequel[:bands][:name]).select_all(Sequel[:s][:bands]).join(Sequel[:s][:members], :band_id=>:id).from_self(:alias=>:bands0).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.albums.map{|y| y.tracks}}.must_equal [[[@t1, @t2], [@t3, @t4]], [[], []]]
end
it "should have eager graphs work with joins with the same tables" do
bands = @Band.order(Sequel[:bands][:name]).select_all(Sequel[:s][:bands]).join(Sequel[:s][:members], :band_id=>:id).eager_graph({:albums=>:tracks}, :members).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.albums.map{|y| y.tracks}}.must_equal [[[@t1, @t2], [@t3, @t4]], [[], []]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
end
it "should have eager graphs work with self referential associations" do
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:tracks=>{:album=>:band}).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
bands.map{|x| x.tracks.map{|y| y.album}}.must_equal [[@a1, @a1, @a2, @a2], []]
bands.map{|x| x.tracks.map{|y| y.album.band}}.must_equal [[@b1, @b1, @b1, @b1], []]
members = @Member.order(Sequel[:members][:name]).eager_graph(:members).all
members.must_equal [@m4, @m3, @m1, @m2]
members.map{|x| x.members}.must_equal [[@m4, @m3], [@m4, @m3], [@m1, @m2], [@m1, @m2]]
members = @Member.order(Sequel[:members][:name]).eager_graph(:band, :members=>:band).all
members.must_equal [@m4, @m3, @m1, @m2]
members.map{|x| x.band}.must_equal [@b2, @b2, @b1, @b1]
members.map{|x| x.members}.must_equal [[@m4, @m3], [@m4, @m3], [@m1, @m2], [@m1, @m2]]
members.map{|x| x.members.map{|y| y.band}}.must_equal [[@b2, @b2], [@b2, @b2], [@b1, @b1], [@b1, @b1]]
end
it "should have eager graphs work with a from_self dataset" do
bands = @Band.order(Sequel[:bands][:name]).from_self.eager_graph(:tracks=>{:album=>:band}).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
bands.map{|x| x.tracks.map{|y| y.album}}.must_equal [[@a1, @a1, @a2, @a2], []]
bands.map{|x| x.tracks.map{|y| y.album.band}}.must_equal [[@b1, @b1, @b1, @b1], []]
end
it "should have eager graphs work with different types of aliased from tables" do
bands = @Band.order(Sequel[:tracks][:name]).from(Sequel[:s][:bands].as(:tracks)).eager_graph(:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
bands = @Band.order(Sequel[:tracks][:name]).from(Sequel.expr(Sequel[:s][:bands]).as(:tracks)).eager_graph(:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
bands = @Band.order(Sequel[:tracks][:name]).from(Sequel.expr(Sequel[:s][:bands]).as(Sequel.identifier(:tracks))).eager_graph(:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
bands = @Band.order(Sequel[:tracks][:name]).from(Sequel.expr(Sequel[:s][:bands]).as('tracks')).eager_graph(:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
end
it "should have eager graphs work with join tables with aliases" do
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:members).join(Sequel[:s][:albums].as(:tracks), :band_id=>Sequel.qualify(Sequel[:s][:bands], :id)).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:members).join(Sequel.as(Sequel[:s][:albums], :tracks), :band_id=>Sequel.qualify(Sequel[:s][:bands], :id)).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:members).join(Sequel.as(Sequel[:s][:albums], 'tracks'), :band_id=>Sequel.qualify(Sequel[:s][:bands], :id)).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:members).join(Sequel.as(Sequel[:s][:albums], Sequel.identifier(:tracks)), :band_id=>Sequel.qualify(Sequel[:s][:bands], :id)).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:members).join(Sequel[:s][:albums], {:band_id=>Sequel.qualify(Sequel[:s][:bands], :id)}, :table_alias=>:tracks).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:members).join(Sequel[:s][:albums], {:band_id=>Sequel.qualify(Sequel[:s][:bands], :id)}, :table_alias=>'tracks').eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
bands = @Band.order(Sequel[:bands][:name]).eager_graph(:members).join(Sequel[:s][:albums], {:band_id=>Sequel.qualify(Sequel[:s][:bands], :id)}, :table_alias=>Sequel.identifier(:tracks)).eager_graph(:albums=>:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.albums}.must_equal [[@a1, @a2], [@a3, @a4]]
bands.map{|x| x.members}.must_equal [[@m1, @m2], [@m4, @m3]]
end
it "should have eager graphs work with different types of qualified from tables" do
bands = @Band.order(Sequel[:bands][:name]).from(Sequel.qualify(:s, :bands)).eager_graph(:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
bands = @Band.order(Sequel[:bands][:name]).from(Sequel.identifier(:bands).qualify(:s)).eager_graph(:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
bands = @Band.order(Sequel[:bands][:name]).from(Sequel::SQL::QualifiedIdentifier.new(:s, 'bands')).eager_graph(:tracks).all
bands.must_equal [@b1, @b2]
bands.map{|x| x.tracks}.must_equal [[@t1, @t2, @t3, @t4], []]
end
end
if DB.server_version >= 80300
describe "PostgreSQL tsearch2" do
before(:all) do
DB.create_table! :test6 do
text :title
text :body
full_text_index [:title, :body]
end
@ds = DB[:test6]
end
after do
DB[:test6].delete
end
after(:all) do
DB.drop_table?(:test6)
end
it "should search by indexed column" do
record = {:title => "oopsla conference", :body => "test"}
@ds.insert(record)
@ds.full_text_search(:title, "oopsla").all.must_equal [record]
end
it "should join multiple coumns with spaces to search by last words in row" do
record = {:title => "multiple words", :body => "are easy to search"}
@ds.insert(record)
@ds.full_text_search([:title, :body], "words").all.must_equal [record]
end
it "should return rows with a NULL in one column if a match in another column" do
record = {:title => "multiple words", :body =>nil}
@ds.insert(record)
@ds.full_text_search([:title, :body], "words").all.must_equal [record]
end
end
end
describe "Postgres::Database functions, languages, schemas, and triggers" do
before do
@d = DB
end
after do
@d.drop_function('tf', :if_exists=>true, :cascade=>true)
@d.drop_function('tf', :if_exists=>true, :cascade=>true, :args=>%w'integer integer')
@d.drop_language(:plpgsql, :if_exists=>true, :cascade=>true) if @d.server_version < 90000
@d.drop_schema(:sequel, :if_exists=>true, :cascade=>true)
@d.drop_table?(:test)
end
it "#create_function and #drop_function should create and drop functions" do
proc{@d['SELECT tf()'].all}.must_raise(Sequel::DatabaseError)
@d.create_function('tf', 'SELECT 1', :returns=>:integer)
@d['SELECT tf()'].all.must_equal [{:tf=>1}]
@d.drop_function('tf')
proc{@d['SELECT tf()'].all}.must_raise(Sequel::DatabaseError)
end
it "#create_function and #drop_function should support options" do
args = ['tf', 'SELECT $1 + $2', {:args=>[[:integer, :a], :integer], :replace=>true, :returns=>:integer, :language=>'SQL', :behavior=>:immutable, :strict=>true, :security_definer=>true, :cost=>2, :parallel=>(:unsafe if @d.server_version >= 90600), :set=>{:search_path => 'public'}}]
@d.create_function(*args)
# Make sure replace works
@d.create_function(*args)
@d['SELECT tf(1, 2)'].all.must_equal [{:tf=>3}]
args = ['tf', {:if_exists=>true, :cascade=>true, :args=>[[:integer, :a], :integer]}]
@d.drop_function(*args)
# Make sure if exists works
@d.drop_function(*args)
end
it "#create_language and #drop_language should create and drop languages" do
@d.create_language(:plpgsql, :replace=>true) if @d.server_version < 90000
proc{@d.create_language(:plpgsql)}.must_raise(Sequel::DatabaseError)
@d.drop_language(:plpgsql) if @d.server_version < 90000
proc{@d.drop_language(:plpgsql)}.must_raise(Sequel::DatabaseError) if @d.server_version < 90000
# Make sure if exists works
@d.drop_language(:plpgsql, :if_exists=>true, :cascade=>true) if @d.server_version < 90000
end
it "#create_schema and #drop_schema should create and drop schemas" do
@d.create_schema(:sequel)
@d.create_schema(:sequel, :if_not_exists=>true) if @d.server_version >= 90300
@d.create_table(Sequel[:sequel][:test]){Integer :a}
@d.drop_schema(:sequel, :if_exists=>true, :cascade=>true)
end
it "#create_trigger and #drop_trigger should create and drop triggers" do
@d.create_language(:plpgsql) if @d.server_version < 90000
@d.create_function(:tf, 'BEGIN IF NEW.value IS NULL THEN RAISE EXCEPTION \'Blah\'; END IF; RETURN NEW; END;', :language=>:plpgsql, :returns=>:trigger)
@d.create_table(:test){String :name; Integer :value}
@d.create_trigger(:test, :identity, :tf, :each_row=>true)
@d[:test].insert(:name=>'a', :value=>1)
@d[:test].filter(:name=>'a').all.must_equal [{:name=>'a', :value=>1}]
proc{@d[:test].filter(:name=>'a').update(:value=>nil)}.must_raise(Sequel::DatabaseError)
@d[:test].filter(:name=>'a').all.must_equal [{:name=>'a', :value=>1}]
@d[:test].filter(:name=>'a').update(:value=>3)
@d[:test].filter(:name=>'a').all.must_equal [{:name=>'a', :value=>3}]
@d.drop_trigger(:test, :identity)
# Make sure if exists works
@d.drop_trigger(:test, :identity, :if_exists=>true, :cascade=>true)
if @d.supports_trigger_conditions?
@d.create_trigger(:test, :identity, :tf, :each_row=>true, :events => :update, :when=> {Sequel[:new][:name] => 'b'})
@d[:test].filter(:name=>'a').update(:value=>nil)
@d[:test].filter(:name=>'a').all.must_equal [{:name=>'a', :value=>nil}]
proc{@d[:test].filter(:name=>'a').update(:name=>'b')}.must_raise(Sequel::DatabaseError)
@d[:test].filter(:name=>'a').all.must_equal [{:name=>'a', :value=>nil}]
if @d.server_version >= 140000
proc{@d[:test].filter(:name=>'a').update(:name=>'b')}.must_raise(Sequel::DatabaseError)
@d[:test].filter(:name=>'a').update(:name=>'c')
@d.create_trigger(:test, :identity, :tf, :each_row=>true, :events => :update, :when=> {Sequel[:new][:name] => 'c'}, :replace=>true)
proc{@d[:test].filter(:name=>'c').update(:name=>'c')}.must_raise(Sequel::DatabaseError)
@d[:test].filter(:name=>'c').update(:name=>'b')
end
@d.drop_trigger(:test, :identity)
end
end
end
if DB.adapter_scheme == :postgres
describe "Postgres::Dataset #use_cursor" do
before(:all) do
@db = DB
@db.create_table!(:test_cursor){Integer :x}
@ds = @db[:test_cursor]
@db.transaction{1001.times{|i| @ds.insert(i)}}
end
after(:all) do
@db.drop_table?(:test_cursor)
end
it "should return the same results as the non-cursor use" do
@ds.all.must_equal @ds.use_cursor.all
end
it "should not swallow errors if closing cursor raises an error" do
proc do
@db.synchronize do |c|
@ds.use_cursor.each do |r|
@db.run "CLOSE sequel_cursor"
raise ArgumentError
end
end
end.must_raise(ArgumentError)
end
it "should handle errors raised while closing cursor after successful cursor fetch" do
proc do
closed = false
@db.synchronize do |c|
@ds.use_cursor(:rows_per_fetch=>2000).each do |r|
if closed == false
@db.run "CLOSE sequel_cursor"
closed = true
end
end
end
end.must_raise(Sequel::DatabaseError)
end
it "should respect the :rows_per_fetch option" do
i = 0
@ds = @ds.with_extend{define_method(:execute){|*a, &block| i+=1; super(*a, &block);}}
@ds.use_cursor.all
i.must_equal 2
i = 0
@ds.use_cursor(:rows_per_fetch=>100).all
i.must_equal 11
i = 0
@ds.use_cursor(:rows_per_fetch=>0).all
i.must_equal 2
i = 0
@ds.use_cursor(:rows_per_fetch=>2000).all
i.must_equal 1
end
it "should respect the :hold=>true option for creating the cursor WITH HOLD and not using a transaction" do
@ds.use_cursor.each{@db.in_transaction?.must_equal true}
@ds.use_cursor(:hold=>true).each{@db.in_transaction?.must_equal false}
end
it "should support updating individual rows based on a cursor" do
@db.transaction(:rollback=>:always) do
@ds.use_cursor(:rows_per_fetch=>1).each do |row|
@ds.where_current_of.update(:x=>Sequel.*(row[:x], 10))
end
@ds.select_order_map(:x).must_equal((0..1000).map{|x| x * 10})
end
@ds.select_order_map(:x).must_equal((0..1000).to_a)
end
it "should respect the :cursor_name option" do
one_rows = []
two_rows = []
@ds.order(:x).use_cursor(:cursor_name => 'cursor_one').each do |one|
one_rows << one
if one[:x] % 1000 == 500
two_rows = []
@ds.order(:x).use_cursor(:cursor_name => 'cursor_two').each do |two|
two_rows << two
end
end
end
one_rows.must_equal two_rows
end
it "should handle returning inside block" do
ds = @ds.with_extend do
def check_return
use_cursor.each{|r| return}
end
end
ds.check_return
ds.all.must_equal ds.use_cursor.all
end
end
describe "Database#add_named_conversion_proc" do
before(:all) do
@db = DB
@old_cp = @db.conversion_procs[1013]
@db.conversion_procs.delete(1013)
@db.add_named_conversion_proc(:oidvector, &:reverse)
end
after(:all) do
@db.conversion_procs.delete(30)
@db.conversion_procs[1013] = @old_cp
@db.drop_table?(:foo)
@db.drop_enum(:foo_enum) rescue nil
end
it "should work for scalar types" do
@db.create_table!(:foo){oidvector :bar}
@db[:foo].insert(Sequel.cast('21', :oidvector))
@db[:foo].get(:bar).must_equal '12'
end
it "should work for array types" do
@db.create_table!(:foo){column :bar, 'oidvector[]'}
@db[:foo].insert(Sequel.pg_array(['21'], :oidvector))
@db[:foo].get(:bar).must_equal ['12']
end
it "should work with for enums" do
@db.drop_enum(:foo_enum) rescue nil
@db.create_enum(:foo_enum, %w(foo bar))
@db.add_named_conversion_proc(:foo_enum, &:reverse)
@db.create_table!(:foo){foo_enum :bar}
@db[:foo].insert(:bar => 'foo')
@db[:foo].get(:bar).must_equal 'foo'.reverse
end
it "should raise error for unsupported type" do
proc{@db.add_named_conversion_proc(:nonexistant_type, &:reverse)}.must_raise Sequel::Error
end
end
end
if uses_pg_or_jdbc && DB.server_version >= 90000
describe "Postgres::Database#copy_into" do
before(:all) do
@db = DB
@db.create_table!(:test_copy){Integer :x; Integer :y}
@ds = @db[:test_copy].order(:x, :y)
end
before do
@db[:test_copy].delete
end
after(:all) do
@db.drop_table?(:test_copy)
end
it "should work with a :data option containing data in PostgreSQL text format" do
@db.copy_into(:test_copy, :data=>"1\t2\n3\t4\n")
@ds.select_map([:x, :y]).must_equal [[1, 2], [3, 4]]
end
it "should work with :format=>:csv option and :data option containing data in CSV format" do
@db.copy_into(:test_copy, :format=>:csv, :data=>"1,2\n3,4\n")
@ds.select_map([:x, :y]).must_equal [[1, 2], [3, 4]]
end
it "should respect given :options" do
@db.copy_into(:test_copy, :options=>"FORMAT csv, HEADER TRUE", :data=>"x,y\n1,2\n3,4\n")
@ds.select_map([:x, :y]).must_equal [[1, 2], [3, 4]]
end
it "should respect given :options options when :format is used" do
@db.copy_into(:test_copy, :options=>"QUOTE '''', DELIMITER '|'", :format=>:csv, :data=>"'1'|'2'\n'3'|'4'\n")
@ds.select_map([:x, :y]).must_equal [[1, 2], [3, 4]]
end
it "should accept :columns option to online copy the given columns" do
@db.copy_into(:test_copy, :data=>"1\t2\n3\t4\n", :columns=>[:y, :x])
@ds.select_map([:x, :y]).must_equal [[2, 1], [4, 3]]
end
it "should accept a block and use returned values for the copy in data stream" do
buf = ["1\t2\n", "3\t4\n"]
@db.copy_into(:test_copy){buf.shift}
@ds.select_map([:x, :y]).must_equal [[1, 2], [3, 4]]
end
it "should work correctly with a block and :format=>:csv" do
buf = ["1,2\n", "3,4\n"]
@db.copy_into(:test_copy, :format=>:csv){buf.shift}
@ds.select_map([:x, :y]).must_equal [[1, 2], [3, 4]]
end
it "should accept an enumerable as the :data option" do
@db.copy_into(:test_copy, :data=>["1\t2\n", "3\t4\n"])
@ds.select_map([:x, :y]).must_equal [[1, 2], [3, 4]]
end
it "should have an exception, cause a rollback of copied data and still have a usable connection" do
2.times do
sent = false
proc{@db.copy_into(:test_copy){raise ArgumentError if sent; sent = true; "1\t2\n"}}.must_raise(ArgumentError)
@ds.select_map([:x, :y]).must_equal []
end
end
it "should handle database errors with a rollback of copied data and still have a usable connection" do
2.times do
proc{@db.copy_into(:test_copy, :data=>["1\t2\n", "3\ta\n"])}.must_raise(Sequel::DatabaseError)
@ds.select_map([:x, :y]).must_equal []
end
end
it "should raise an Error if both :data and a block are provided" do
proc{@db.copy_into(:test_copy, :data=>["1\t2\n", "3\t4\n"]){}}.must_raise(Sequel::Error)
end
it "should raise an Error if neither :data or a block are provided" do
proc{@db.copy_into(:test_copy)}.must_raise(Sequel::Error)
end
end
describe "Postgres::Database#copy_into using UTF-8 encoding" do
before(:all) do
@db = DB
@db.create_table!(:test_copy){String :t}
@ds = @db[:test_copy].order(:t)
end
before do
@db[:test_copy].delete
end
after(:all) do
@db.drop_table?(:test_copy)
end
it "should work with UTF-8 characters using the :data option" do
@db.copy_into(:test_copy, :data=>(["\u00E4\n"]*2))
@ds.select_map([:t]).map{|a| a.map{|s| s.force_encoding('UTF-8')}}.must_equal([["\u00E4"]] * 2)
end
it "should work with UTF-8 characters using a block" do
buf = (["\u00E4\n"]*2)
@db.copy_into(:test_copy){buf.shift}
@ds.select_map([:t]).map{|a| a.map{|s| s.force_encoding('UTF-8')}}.must_equal([["\u00E4"]] * 2)
end
end
describe "Postgres::Database#copy_table" do
before(:all) do
@db = DB
@db.create_table!(:test_copy){Integer :x; Integer :y}
ds = @db[:test_copy]
ds.insert(1, 2)
ds.insert(3, 4)
end
after(:all) do
@db.drop_table?(:test_copy)
end
it "without a block or options should return a text version of the table as a single string" do
@db.copy_table(:test_copy).must_equal "1\t2\n3\t4\n"
end
it "without a block and with :format=>:csv should return a csv version of the table as a single string" do
@db.copy_table(:test_copy, :format=>:csv).must_equal "1,2\n3,4\n"
end
it "should treat string as SQL code" do
@db.copy_table('COPY "test_copy" TO STDOUT').must_equal "1\t2\n3\t4\n"
end
it "should respect given :options options" do
@db.copy_table(:test_copy, :options=>"FORMAT csv, HEADER TRUE").must_equal "x,y\n1,2\n3,4\n"
end
it "should respect given :options options when :format is used" do
@db.copy_table(:test_copy, :format=>:csv, :options=>"QUOTE '''', FORCE_QUOTE *").must_equal "'1','2'\n'3','4'\n"
end
it "should accept dataset as first argument" do
@db.copy_table(@db[:test_copy].cross_join(Sequel[:test_copy].as(:tc)).order(Sequel[:test_copy][:x], Sequel[:test_copy][:y], Sequel[:tc][:x], Sequel[:tc][:y])).must_equal "1\t2\t1\t2\n1\t2\t3\t4\n3\t4\t1\t2\n3\t4\t3\t4\n"
end
it "with a block and no options should yield each row as a string in text format" do
buf = []
@db.copy_table(:test_copy){|b| buf << b}
buf.must_equal ["1\t2\n", "3\t4\n"]
end
it "with a block and :format=>:csv should yield each row as a string in csv format" do
buf = []
@db.copy_table(:test_copy, :format=>:csv){|b| buf << b}
buf.must_equal ["1,2\n", "3,4\n"]
end
it "should work fine when using a block that is terminated early with a following copy_table" do
buf = []
proc{@db.copy_table(:test_copy, :format=>:csv){|b| buf << b; break}}.must_raise(Sequel::DatabaseDisconnectError)
buf.must_equal ["1,2\n"]
buf.clear
proc{@db.copy_table(:test_copy, :format=>:csv){|b| buf << b; raise ArgumentError}}.must_raise(Sequel::DatabaseDisconnectError)
buf.must_equal ["1,2\n"]
buf.clear
@db.copy_table(:test_copy){|b| buf << b}
buf.must_equal ["1\t2\n", "3\t4\n"]
end
it "should work fine when using a block that is terminated early with a following regular query" do
buf = []
proc{@db.copy_table(:test_copy, :format=>:csv){|b| buf << b; break}}.must_raise(Sequel::DatabaseDisconnectError)
buf.must_equal ["1,2\n"]
buf.clear
proc{@db.copy_table(:test_copy, :format=>:csv){|b| buf << b; raise ArgumentError}}.must_raise(Sequel::DatabaseDisconnectError)
buf.must_equal ["1,2\n"]
@db[:test_copy].select_order_map(:x).must_equal [1, 3]
end
it "should not swallow error raised by block" do
begin
@db.copy_table(:test_copy){|b| raise ArgumentError, "foo"}
rescue => e
end
e.must_be_kind_of Sequel::DatabaseDisconnectError
e.wrapped_exception.must_be_kind_of ArgumentError
e.message.must_include "foo"
end
it "should handle errors raised during row processing" do
proc{@db.copy_table(@db[:test_copy].select(Sequel[1]/(Sequel[:x] - 3)))}.must_raise Sequel::DatabaseError
@db.get(1).must_equal 1
end
it "should disconnect if the result status after the copy is not expected" do
proc do
@db.synchronize do |c|
def c.get_last_result; end
@db.copy_table(:test_copy)
end
end.must_raise Sequel::DatabaseDisconnectError
end if uses_pg
end
end
if uses_pg && DB.server_version >= 90000
describe "Postgres::Database LISTEN/NOTIFY" do
before(:all) do
@db = DB
end
it "should support listen and notify" do
# Spec assumes only one connection currently in the pool (otherwise notify_pid will not be deterministic)
@db.disconnect
notify_pid = @db.synchronize{|conn| conn.backend_pid}
called = false
@db.listen('foo', :after_listen=>proc{@db.notify('foo')}) do |ev, pid, payload|
ev.must_equal 'foo'
pid.must_equal notify_pid
['', nil].must_include(payload)
called = true
end.must_equal 'foo'
called.must_equal true
# Check weird identifier names
called = false
@db.listen('FOO bar', :after_listen=>proc{@db.notify('FOO bar')}) do |ev, pid, payload|
ev.must_equal 'FOO bar'
pid.must_equal notify_pid
['', nil].must_include(payload)
called = true
end.must_equal 'FOO bar'
called.must_equal true
# Check identifier symbols
called = false
@db.listen(:foo, :after_listen=>proc{@db.notify(:foo)}) do |ev, pid, payload|
ev.must_equal 'foo'
pid.must_equal notify_pid
['', nil].must_include(payload)
called = true
end.must_equal 'foo'
called.must_equal true
called = false
@db.listen('foo', :after_listen=>proc{@db.notify('foo', :payload=>'bar')}) do |ev, pid, payload|
ev.must_equal 'foo'
pid.must_equal notify_pid
payload.must_equal 'bar'
called = true
end.must_equal 'foo'
called.must_equal true
@db.listen('foo', :after_listen=>proc{@db.notify('foo')}).must_equal 'foo'
called = false
called2 = false
i = 0
@db.listen(['foo', 'bar'], :after_listen=>proc{@db.notify('foo', :payload=>'bar'); @db.notify('bar', :payload=>'foo')}, :loop=>proc{i+=1}) do |ev, pid, payload|
if !called
ev.must_equal 'foo'
pid.must_equal notify_pid
payload.must_equal 'bar'
called = true
else
ev.must_equal 'bar'
pid.must_equal notify_pid
payload.must_equal 'foo'
called2 = true
break
end
end.must_be_nil
called.must_equal true
called2.must_equal true
i.must_equal 1
proc{@db.listen('foo', :loop=>true)}.must_raise Sequel::Error
end
it "should accept a :timeout option in listen" do
@db.listen('foo2', :timeout=>0.001).must_be_nil
called = false
@db.listen('foo2', :timeout=>0.001){|ev, pid, payload| called = true}.must_be_nil
called.must_equal false
i = 0
@db.listen('foo2', :timeout=>0.001, :loop=>proc{i+=1; throw :stop if i > 3}){|ev, pid, payload| called = true}.must_be_nil
i.must_equal 4
called = false
i = 0
@db.listen('foo2', :timeout=>proc{i+=1; 0.001}){|ev, pid, payload| called = true}.must_be_nil
called.must_equal false
i.must_equal 1
i = 0
t = 0
@db.listen('foo2', :timeout=>proc{t+=1; 0.001}, :loop=>proc{i+=1; throw :stop if i > 3}){|ev, pid, payload| called = true}.must_be_nil
called.must_equal false
t.must_equal 4
t = 0
@db.listen('foo2', :timeout=>proc{t+=1; throw :stop if t == 4; 0.001}, :loop=>true){|ev, pid, payload| called = true}.must_be_nil
called.must_equal false
t.must_equal 4
end unless RUBY_PLATFORM =~ /mingw/ # Ruby freezes on this spec on this platform/version
end
end
describe 'PostgreSQL special float handling' do
before do
@db = DB
@db.create_table!(:test5){Float :value}
@ds = @db[:test5]
end
after do
@db.drop_table?(:test5)
end
it 'inserts NaN' do
nan = 0.0/0.0
@ds.insert(:value=>nan)
@ds.all[0][:value].nan?.must_equal true
end
it 'inserts +Infinity' do
inf = 1.0/0.0
@ds.insert(:value=>inf)
@ds.all[0][:value].infinite?.must_be :>, 0
end
it 'inserts -Infinity' do
inf = -1.0/0.0
@ds.insert(:value=>inf)
@ds.all[0][:value].infinite?.must_be :<, 0
end
end if DB.adapter_scheme == :postgres
describe 'PostgreSQL array handling' do
before(:all) do
@db = DB
@ds = @db[:items]
@tp = lambda{@db.schema(:items).map{|a| a.last[:type]}}
end
after do
@db.drop_table?(:items)
end
it 'insert and retrieve integer and float arrays of various sizes' do
@db.create_table!(:items) do
column :i2, 'int2[]'
column :i4, 'int4[]'
column :i8, 'int8[]'
column :r, 'real[]'
column :dp, 'double precision[]'
end
@tp.call.must_equal [:smallint_array, :integer_array, :bigint_array, :real_array, :float_array]
@ds.insert(Sequel.pg_array([1], :int2), Sequel.pg_array([nil, 2], :int4), Sequel.pg_array([3, nil], :int8), Sequel.pg_array([4, nil, 4.5], :real), Sequel.pg_array([5, nil, 5.5], "double precision"))
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:i2=>[1], :i4=>[nil, 2], :i8=>[3, nil], :r=>[4.0, nil, 4.5], :dp=>[5.0, nil, 5.5]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
@ds.insert(Sequel.pg_array([[1], [2]], :int2), Sequel.pg_array([[nil, 2], [3, 4]], :int4), Sequel.pg_array([[3, nil], [nil, nil]], :int8), Sequel.pg_array([[4, nil], [nil, 4.5]], :real), Sequel.pg_array([[5, nil], [nil, 5.5]], "double precision"))
rs = @ds.all
rs.must_equal [{:i2=>[[1], [2]], :i4=>[[nil, 2], [3, 4]], :i8=>[[3, nil], [nil, nil]], :r=>[[4, nil], [nil, 4.5]], :dp=>[[5, nil], [nil, 5.5]]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'insert and retrieve decimal arrays' do
@db.create_table!(:items) do
column :n, 'numeric[]'
end
@tp.call.must_equal [:decimal_array]
@ds.insert(Sequel.pg_array([BigDecimal('1.000000000000000000001'), nil, BigDecimal('1')], :numeric))
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:n=>[BigDecimal('1.000000000000000000001'), nil, BigDecimal('1')]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
@ds.insert(Sequel.pg_array([[BigDecimal('1.0000000000000000000000000000001'), nil], [nil, BigDecimal('1')]], :numeric))
rs = @ds.all
rs.must_equal [{:n=>[[BigDecimal('1.0000000000000000000000000000001'), nil], [nil, BigDecimal('1')]]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'insert and retrieve string arrays' do
@db.create_table!(:items) do
column :c, 'char(4)[]'
column :vc, 'varchar[]'
column :t, 'text[]'
end
@tp.call.must_equal [:character_array, :varchar_array, :string_array]
@ds.insert(Sequel.pg_array(['a', nil, 'NULL', 'b"\'c'], 'char(4)'), Sequel.pg_array(['a', nil, 'NULL', 'b"\'c', '', ''], :varchar), Sequel.pg_array(['a', nil, 'NULL', 'b"\'c'], :text))
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:c=>['a ', nil, 'NULL', 'b"\'c'], :vc=>['a', nil, 'NULL', 'b"\'c', '', ''], :t=>['a', nil, 'NULL', 'b"\'c']}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
@ds.insert(Sequel.pg_array([[['a'], [nil]], [['NULL'], ['b"\'c']]], 'char(4)'), Sequel.pg_array([[['a[],\\[\\]\\,\\""NULL",'], ['']], [['NULL'], ['b"\'c']]], :varchar), Sequel.pg_array([[['a'], [nil]], [['NULL'], ['b"\'c']]], :text))
rs = @ds.all
rs.must_equal [{:c=>[[['a '], [nil]], [['NULL'], ['b"\'c']]], :vc=>[[['a[],\\[\\]\\,\\""NULL",'], ['']], [['NULL'], ['b"\'c']]], :t=>[[['a'], [nil]], [['NULL'], ['b"\'c']]]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'insert and retrieve arrays of other types' do
@db.create_table!(:items) do
column :b, 'bool[]'
column :d, 'date[]'
column :t, 'time[]'
column :ts, 'timestamp[]'
column :tstz, 'timestamptz[]'
end
@tp.call.must_equal [:boolean_array, :date_array, :time_array, :datetime_array, :datetime_timezone_array]
d = Date.today
t = Sequel::SQLTime.create(10, 20, 30)
ts = Time.local(2011, 1, 2, 3, 4, 5)
@ds.insert(Sequel.pg_array([true, false], :bool), Sequel.pg_array([d, nil], :date), Sequel.pg_array([t, nil], :time), Sequel.pg_array([ts, nil], :timestamp), Sequel.pg_array([ts, nil], :timestamptz))
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:b=>[true, false], :d=>[d, nil], :t=>[t, nil], :ts=>[ts, nil], :tstz=>[ts, nil]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@db.create_table!(:items) do
column :ba, 'bytea[]'
column :tz, 'timetz[]'
column :o, 'oid[]'
end
@tp.call.must_equal [:blob_array, :time_timezone_array, :oid_array]
@ds.insert(Sequel.pg_array([Sequel.blob("a\0"), nil], :bytea), Sequel.pg_array([t, nil], :timetz), Sequel.pg_array([1, 2, 3], :oid))
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:ba=>[Sequel.blob("a\0"), nil], :tz=>[t, nil], :o=>[1, 2, 3]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@db.create_table!(:items) do
column :x, 'xml[]'
column :m, 'money[]'
column :b, 'bit[]'
column :vb, 'bit varying[]'
column :u, 'uuid[]'
column :xi, 'xid[]'
column :c, 'cid[]'
column :n, 'name[]'
column :o, 'oidvector[]'
end
@tp.call.must_equal [:xml_array, :money_array, :bit_array, :varbit_array, :uuid_array, :xid_array, :cid_array, :name_array, :oidvector_array]
@ds.insert(Sequel.pg_array(['<a></a>'], :xml),
Sequel.pg_array(['1'], :money),
Sequel.pg_array(['1'], :bit),
Sequel.pg_array(['10'], :varbit),
Sequel.pg_array(['c0f24910-39e7-11e4-916c-0800200c9a66'], :uuid),
Sequel.pg_array(['12'], :xid),
Sequel.pg_array(['12'], :cid),
Sequel.pg_array(['N'], :name),
Sequel.pg_array(['1 2'], :oidvector))
@ds.count.must_equal 1
rs = @ds.all
r = rs.first
m = r.delete(:m)
m.class.must_equal(Sequel::Postgres::PGArray)
m.to_a.must_be_kind_of(Array)
m.first.must_be_kind_of(String)
r.must_be(:==, :x=>['<a></a>'], :b=>['1'], :vb=>['10'], :u=>['c0f24910-39e7-11e4-916c-0800200c9a66'], :xi=>['12'], :c=>['12'], :n=>['N'], :o=>['1 2'])
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
r[:m] = m
@ds.delete
@ds.insert(r)
@ds.all.must_equal rs
end
it 'insert and retrieve empty arrays' do
@db.create_table!(:items) do
column :n, 'integer[]'
end
@ds.insert(:n=>Sequel.pg_array([], :integer))
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:n=>[]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'convert ruby array :default values' do
@db.create_table!(:items) do
column :n, 'integer[]', :default=>[]
end
@ds.insert
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:n=>[]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'insert and retrieve custom array types' do
point= Class.new do
attr_reader :array
def initialize(array)
@array = array
end
def sql_literal_append(ds, sql)
sql << "'(#{array.join(',')})'"
end
def ==(other)
if other.is_a?(self.class)
array == other.array
else
super
end
end
end
@db.register_array_type(:point){|s| point.new(s[1...-1].split(',').map{|i| i.to_i})}
@db.create_table!(:items) do
column :b, 'point[]'
end
@tp.call.must_equal [:point_array]
pv = point.new([1, 2])
@ds.insert(Sequel.pg_array([pv], :point))
@ds.count.must_equal 1
rs = @ds.all
rs.must_equal [{:b=>[pv]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'retrieve arrays with explicit bounds' do
@db.create_table!(:items) do
column :n, 'integer[]'
end
@ds.insert(:n=>"[0:1]={2,3}")
rs = @ds.all
rs.must_equal [{:n=>[2,3]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
@ds.insert(:n=>"[0:1][0:0]={{2},{3}}")
rs = @ds.all
rs.must_equal [{:n=>[[2], [3]]}]
rs.first.values.each{|v| v.class.must_equal(Sequel::Postgres::PGArray)}
rs.first.values.each{|v| v.to_a.must_be_kind_of(Array)}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'use arrays in bound variables' do
@db.create_table!(:items) do
column :i, 'int4[]'
end
@ds.call(:insert, {:i=>[1,2]}, :i=>:$i)
@ds.get(:i).must_equal [1, 2]
@ds.filter(:i=>:$i).call(:first, :i=>[1,2]).must_equal(:i=>[1,2])
@ds.filter(:i=>:$i).call(:first, :i=>[1,3]).must_be_nil
# NULL values
@ds.delete
@ds.call(:insert, {:i=>[nil,nil]}, :i=>:$i)
@ds.first.must_equal(:i=>[nil, nil])
@db.create_table!(:items) do
column :i, 'text[]'
end
a = ["\"\\\\\"{}\n\t\r \v\b123afP", 'NULL', nil, '']
@ds.call(:insert, {:i=>Sequel.pg_array(a)}, :i=>:$i)
@ds.get(:i).must_equal a
@ds.filter(:i=>:$i).call(:first, :i=>a).must_equal(:i=>a)
@ds.filter(:i=>:$i).call(:first, :i=>['', nil, nil, 'a']).must_be_nil
@db.create_table!(:items) do
column :i, 'date[]'
end
a = [Date.today]
@ds.call(:insert, {:i=>Sequel.pg_array(a, 'date')}, :i=>:$i)
@ds.get(:i).must_equal a
@ds.filter(:i=>:$i).call(:first, :i=>a).must_equal(:i=>a)
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.pg_array([Date.today-1], 'date')).must_be_nil
@db.create_table!(:items) do
column :i, 'timestamp[]'
end
a = [Time.local(2011, 1, 2, 3, 4, 5)]
@ds.call(:insert, {:i=>Sequel.pg_array(a, 'timestamp')}, :i=>:$i)
@ds.get(:i).must_equal a
@ds.filter(:i=>:$i).call(:first, :i=>a).must_equal(:i=>a)
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.pg_array([a.first-1], 'timestamp')).must_be_nil
@db.create_table!(:items) do
column :i, 'boolean[]'
end
a = [true, false]
@ds.call(:insert, {:i=>Sequel.pg_array(a, 'boolean')}, :i=>:$i)
@ds.get(:i).must_equal a
@ds.filter(:i=>:$i).call(:first, :i=>a).must_equal(:i=>a)
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.pg_array([false, true], 'boolean')).must_be_nil
@db.create_table!(:items) do
column :i, 'bytea[]'
end
a = [Sequel.blob("a\0'\"")]
@ds.call(:insert, {:i=>Sequel.pg_array(a, 'bytea')}, :i=>:$i)
@ds.get(:i).must_equal a
@ds.filter(:i=>:$i).call(:first, :i=>a).must_equal(:i=>a)
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.pg_array([Sequel.blob("b\0")], 'bytea')).must_be_nil
end if uses_pg_or_jdbc
it 'with models' do
@db.create_table!(:items) do
primary_key :id
column :i, 'integer[]'
column :f, 'double precision[]'
column :d, 'numeric[]'
column :t, 'text[]'
end
c = Class.new(Sequel::Model(@db[:items]))
h = {:i=>[1,2, nil], :f=>[[1, 2.5], [3, 4.5]], :d=>[1, BigDecimal('1.000000000000000000001')], :t=>[%w'a b c', ['NULL', nil, '1']]}
o = c.create(h)
o.i.must_equal [1, 2, nil]
o.f.must_equal [[1, 2.5], [3, 4.5]]
o.d.must_equal [BigDecimal('1'), BigDecimal('1.000000000000000000001')]
o.t.must_equal [%w'a b c', ['NULL', nil, '1']]
c.where(:i=>o.i, :f=>o.f, :d=>o.d, :t=>o.t).all.must_equal [o]
o2 = c.new(h)
c.where(:i=>o2.i, :f=>o2.f, :d=>o2.d, :t=>o2.t).all.must_equal [o]
@db.create_table!(:items) do
primary_key :id
column :i, 'int2[]'
column :f, 'real[]'
column :d, 'numeric(30,28)[]'
column :t, 'varchar[]'
end
c = Class.new(Sequel::Model(@db[:items]))
o = c.create(:i=>[1,2, nil], :f=>[[1, 2.5], [3, 4.5]], :d=>[1, BigDecimal('1.000000000000000000001')], :t=>[%w'a b c', ['NULL', nil, '1']])
o.i.must_equal [1, 2, nil]
o.f.must_equal [[1, 2.5], [3, 4.5]]
o.d.must_equal [BigDecimal('1'), BigDecimal('1.000000000000000000001')]
o.t.must_equal [%w'a b c', ['NULL', nil, '1']]
c.where(:i=>o.i, :f=>o.f, :d=>o.d, :t=>o.t).all.must_equal [o]
o2 = c.new(h)
c.where(:i=>o2.i, :f=>o2.f, :d=>o2.d, :t=>o2.t).all.must_equal [o]
end
it 'with empty array default values and defaults_setter plugin' do
@db.create_table!(:items) do
column :n, 'integer[]', :default=>[]
end
c = Class.new(Sequel::Model(@db[:items]))
c.plugin :defaults_setter, :cache=>true
o = c.new
o.n.class.must_equal(Sequel::Postgres::PGArray)
o.n.to_a.must_be_same_as(o.n.to_a)
o.n << 1
o.save.n.must_equal [1]
end
it 'operations/functions with pg_array_ops' do
Sequel.extension :pg_array_ops
@db.create_table!(:items){column :i, 'integer[]'; column :i2, 'integer[]'; column :i3, 'integer[]'; column :i4, 'integer[]'; column :i5, 'integer[]'}
@ds.insert(Sequel.pg_array([1, 2, 3]), Sequel.pg_array([2, 1]), Sequel.pg_array([4, 4]), Sequel.pg_array([[5, 5], [4, 3]]), Sequel.pg_array([1, nil, 5]))
@ds.get(Sequel.pg_array(:i) > :i3).must_equal false
@ds.get(Sequel.pg_array(:i3) > :i).must_equal true
@ds.get(Sequel.pg_array(:i) >= :i3).must_equal false
@ds.get(Sequel.pg_array(:i) >= :i).must_equal true
@ds.get(Sequel.pg_array(:i3) < :i).must_equal false
@ds.get(Sequel.pg_array(:i) < :i3).must_equal true
@ds.get(Sequel.pg_array(:i3) <= :i).must_equal false
@ds.get(Sequel.pg_array(:i) <= :i).must_equal true
@ds.get(Sequel.expr(5=>Sequel.pg_array(:i).any)).must_equal false
@ds.get(Sequel.expr(1=>Sequel.pg_array(:i).any)).must_equal true
@ds.get(Sequel.expr(1=>Sequel.pg_array(:i3).all)).must_equal false
@ds.get(Sequel.expr(4=>Sequel.pg_array(:i3).all)).must_equal true
@ds.get(Sequel.expr(1=>Sequel.pg_array(:i)[1..1].any)).must_equal true
@ds.get(Sequel.expr(2=>Sequel.pg_array(:i)[1..1].any)).must_equal false
@ds.get(Sequel.pg_array(:i2)[1]).must_equal 2
@ds.get(Sequel.pg_array(:i2)[1]).must_equal 2
@ds.get(Sequel.pg_array(:i2)[2]).must_equal 1
@ds.get(Sequel.pg_array(:i4)[2][1]).must_equal 4
@ds.get(Sequel.pg_array(:i4)[2][2]).must_equal 3
@ds.get(Sequel.pg_array(:i).contains(:i2)).must_equal true
@ds.get(Sequel.pg_array(:i).contains(:i3)).must_equal false
@ds.get(Sequel.pg_array(:i2).contained_by(:i)).must_equal true
@ds.get(Sequel.pg_array(:i).contained_by(:i2)).must_equal false
@ds.get(Sequel.pg_array(:i).overlaps(:i2)).must_equal true
@ds.get(Sequel.pg_array(:i2).overlaps(:i3)).must_equal false
@ds.get(Sequel.pg_array(:i).dims).must_equal '[1:3]'
@ds.get(Sequel.pg_array(:i).length).must_equal 3
@ds.get(Sequel.pg_array(:i).lower).must_equal 1
if @db.server_version >= 80400
@ds.select(Sequel.pg_array(:i).unnest).from_self.count.must_equal 3
end
if @db.server_version >= 90000
@ds.get(Sequel.pg_array(:i5).join).must_equal '15'
@ds.get(Sequel.pg_array(:i5).join(':')).must_equal '1:5'
end
if @db.server_version >= 90100
@ds.get(Sequel.pg_array(:i5).join(':', '*')).must_equal '1:*:5'
end
if @db.server_version >= 90300
@ds.get(Sequel.pg_array(:i5).remove(1).length).must_equal 2
@ds.get(Sequel.pg_array(:i5).replace(1, 4).contains([1])).must_equal false
@ds.get(Sequel.pg_array(:i5).replace(1, 4).contains([4])).must_equal true
end
if @db.server_version >= 90400
@ds.get(Sequel.pg_array(:i).cardinality).must_equal 3
@ds.get(Sequel.pg_array(:i4).cardinality).must_equal 4
@ds.get(Sequel.pg_array(:i5).cardinality).must_equal 3
@ds.from{Sequel.pg_array([1,2,3]).op.unnest([4,5,6], [7,8]).as(:t1, [:a, :b, :c])}.select_order_map([:a, :b, :c]).must_equal [[1, 4, 7], [2, 5, 8], [3, 6, nil]]
end
@ds.get(Sequel.pg_array(:i).push(4)).must_equal [1, 2, 3, 4]
@ds.get(Sequel.pg_array(:i).unshift(4)).must_equal [4, 1, 2, 3]
@ds.get(Sequel.pg_array(:i).concat(:i2)).must_equal [1, 2, 3, 2, 1]
if @db.type_supported?(:hstore)
Sequel.extension :pg_hstore_ops
@db.get(Sequel.pg_array(['a', 'b']).op.hstore['a']).must_equal 'b'
@db.get(Sequel.pg_array(['a', 'b']).op.hstore(['c', 'd'])['a']).must_equal 'c'
end
end
end
describe 'PostgreSQL hstore handling' do
before(:all) do
@db = DB
@ds = @db[:items]
@h = {'a'=>'b', 'c'=>nil, 'd'=>'NULL', 'e'=>'\\\\" \\\' ,=>'}
end
after do
@db.drop_table?(:items)
end
it 'insert and retrieve hstore values' do
@db.create_table!(:items) do
column :h, :hstore
end
@ds.insert(Sequel.hstore(@h))
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:h]
v.must_equal @h
v.class.must_equal(Sequel::Postgres::HStore)
v.to_hash.must_be_kind_of(Hash)
v.to_hash.must_equal @h
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'insert and retrieve hstore[] values' do
@db.create_table!(:items) do
column :h, 'hstore[]'
end
@ds.insert(Sequel.pg_array([Sequel.hstore(@h)], :hstore))
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:h].first
v.class.must_equal(Sequel::Postgres::HStore)
v.to_hash.must_be_kind_of(Hash)
v.to_hash.must_equal @h
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'use hstore in bound variables' do
@db.create_table!(:items) do
column :i, :hstore
end
@ds.call(:insert, {:i=>Sequel.hstore(@h)}, {:i=>:$i})
@ds.get(:i).must_equal @h
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.hstore(@h)).must_equal(:i=>@h)
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.hstore({})).must_be_nil
@ds.delete
@ds.call(:insert, {:i=>Sequel.hstore('a'=>nil)}, {:i=>:$i})
@ds.get(:i).must_equal Sequel.hstore('a'=>nil)
@ds.delete
@ds.call(:insert, {:i=>@h}, {:i=>:$i})
@ds.get(:i).must_equal @h
@ds.filter(:i=>:$i).call(:first, :i=>@h).must_equal(:i=>@h)
@ds.filter(:i=>:$i).call(:first, :i=>{}).must_be_nil
@db.create_table!(:items) do
column :i, 'hstore[]'
end
@ds.call(:insert, {:i=>Sequel.pg_array([Sequel.hstore(@h)], :hstore)}, {:i=>:$i})
@ds.get(:i).must_equal [@h]
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.pg_array([Sequel.hstore(@h)], :hstore)).must_equal(:i=>[@h])
@ds.filter(:i=>:$i).call(:first, :i=>Sequel.pg_array([Sequel.hstore({})], :hstore)).must_be_nil
@ds.delete
@ds.call(:insert, {:i=>Sequel.pg_array([Sequel.hstore('a'=>nil)], :hstore)}, {:i=>:$i})
@ds.get(:i).must_equal [Sequel.hstore('a'=>nil)]
end if uses_pg_or_jdbc
it 'with models and associations' do
@db.create_table!(:items) do
primary_key :id
column :h, :hstore
end
c = Class.new(Sequel::Model(@db[:items])) do
def self.name
'Item'
end
unrestrict_primary_key
def item_id
h['item_id'].to_i if h
end
def left_item_id
h['left_item_id'].to_i if h
end
end
Sequel.extension :pg_hstore_ops
c.plugin :many_through_many
h = {'item_id'=>"2", 'left_item_id'=>"1"}
o2 = c.create(:id=>2)
o = c.create(:id=>1, :h=>h)
o.h.must_equal h
c.many_to_one :item, :class=>c, :key_column=>Sequel.cast(Sequel.hstore(:h)['item_id'], Integer)
c.one_to_many :items, :class=>c, :key=>Sequel.cast(Sequel.hstore(:h)['item_id'], Integer), :key_method=>:item_id
c.many_to_many :related_items, :class=>c, :join_table=>Sequel[:items].as(:i), :left_key=>Sequel.cast(Sequel.hstore(:h)['left_item_id'], Integer), :right_key=>Sequel.cast(Sequel.hstore(:h)['item_id'], Integer)
c.many_to_one :other_item, :class=>c, :key=>:id, :primary_key_method=>:item_id, :primary_key=>Sequel.cast(Sequel.hstore(:h)['item_id'], Integer), :reciprocal=>:other_items
c.one_to_many :other_items, :class=>c, :primary_key=>:item_id, :key=>:id, :primary_key_column=>Sequel.cast(Sequel.hstore(:h)['item_id'], Integer), :reciprocal=>:other_item
c.many_to_many :other_related_items, :class=>c, :join_table=>Sequel[:items].as(:i), :left_key=>:id, :right_key=>:id,
:left_primary_key_column=>Sequel.cast(Sequel.hstore(:h)['left_item_id'], Integer),
:left_primary_key=>:left_item_id,
:right_primary_key=>Sequel.cast(Sequel.hstore(:h)['left_item_id'], Integer),
:right_primary_key_method=>:left_item_id
c.many_through_many :mtm_items, [
[:items, Sequel.cast(Sequel.hstore(:h)['item_id'], Integer), Sequel.cast(Sequel.hstore(:h)['left_item_id'], Integer)],
[:items, Sequel.cast(Sequel.hstore(:h)['left_item_id'], Integer), Sequel.cast(Sequel.hstore(:h)['left_item_id'], Integer)]
],
:class=>c,
:left_primary_key_column=>Sequel.cast(Sequel.hstore(:h)['item_id'], Integer),
:left_primary_key=>:item_id,
:right_primary_key=>Sequel.cast(Sequel.hstore(:h)['left_item_id'], Integer),
:right_primary_key_method=>:left_item_id
# Lazily Loading
o.item.must_equal o2
o2.items.must_equal [o]
o.related_items.must_equal [o2]
o2.other_item.must_equal o
o.other_items.must_equal [o2]
o.other_related_items.must_equal [o]
o.mtm_items.must_equal [o]
# Eager Loading via eager
os = c.eager(:item, :related_items, :other_items, :other_related_items, :mtm_items).where(:id=>1).all.first
os.item.must_equal o2
os.related_items.must_equal [o2]
os.other_items.must_equal [o2]
os.other_related_items.must_equal [o]
os.mtm_items.must_equal [o]
os = c.eager(:items, :other_item).where(:id=>2).all.first
os.items.must_equal [o]
os.other_item.must_equal o
# Eager Loading via eager_graph
c.eager_graph(:item).where(Sequel[:items][:id]=>1).all.first.item.must_equal o2
c.eager_graph(:items).where(Sequel[:items][:id]=>2).all.first.items.must_equal [o]
c.eager_graph(:related_items).where(Sequel[:items][:id]=>1).all.first.related_items.must_equal [o2]
c.eager_graph(:other_item).where(Sequel[:items][:id]=>2).all.first.other_item.must_equal o
c.eager_graph(:other_items).where(Sequel[:items][:id]=>1).all.first.other_items.must_equal [o2]
c.eager_graph(:other_related_items).where(Sequel[:items][:id]=>1).all.first.other_related_items.must_equal [o]
c.eager_graph(:mtm_items).where(Sequel[:items][:id]=>1).all.first.mtm_items.must_equal [o]
# Filter By Associations - Model Instances
c.filter(:item=>o2).all.must_equal [o]
c.filter(:items=>o).all.must_equal [o2]
c.filter(:related_items=>o2).all.must_equal [o]
c.filter(:other_item=>o).all.must_equal [o2]
c.filter(:other_items=>o2).all.must_equal [o]
c.filter(:other_related_items=>o).all.must_equal [o]
c.filter(:mtm_items=>o).all.must_equal [o]
# Filter By Associations - Model Datasets
c.filter(:item=>c.filter(:id=>o2.id)).all.must_equal [o]
c.filter(:items=>c.filter(:id=>o.id)).all.must_equal [o2]
c.filter(:related_items=>c.filter(:id=>o2.id)).all.must_equal [o]
c.filter(:other_item=>c.filter(:id=>o.id)).all.must_equal [o2]
c.filter(:other_items=>c.filter(:id=>o2.id)).all.must_equal [o]
c.filter(:other_related_items=>c.filter(:id=>o.id)).all.must_equal [o]
c.filter(:mtm_items=>c.filter(:id=>o.id)).all.must_equal [o]
end
it 'with empty hstore default values and defaults_setter plugin' do
@db.create_table!(:items) do
hstore :h, :default=>Sequel.hstore({})
end
c = Class.new(Sequel::Model(@db[:items]))
c.plugin :defaults_setter, :cache=>true
o = c.new
o.h.class.must_equal(Sequel::Postgres::HStore)
o.h.to_hash.must_be_same_as(o.h.to_hash)
o.h['a'] = 'b'
o.save.h.must_equal('a'=>'b')
end
it 'operations/functions with pg_hstore_ops' do
Sequel.extension :pg_hstore_ops, :pg_array_ops
@db.create_table!(:items){hstore :h1; hstore :h2; hstore :h3; String :t}
@ds.insert(Sequel.hstore('a'=>'b', 'c'=>nil), Sequel.hstore('a'=>'b'), Sequel.hstore('d'=>'e'))
h1 = Sequel.hstore(:h1)
h2 = Sequel.hstore(:h2)
h3 = Sequel.hstore(:h3)
@ds.get(h1['a']).must_equal 'b'
@ds.get(h1['d']).must_be_nil
@ds.get(h2.concat(h3).keys.length).must_equal 2
@ds.get(h1.concat(h3).keys.length).must_equal 3
@ds.get(h2.merge(h3).keys.length).must_equal 2
@ds.get(h1.merge(h3).keys.length).must_equal 3
@ds.get(h1.contain_all(%w'a c')).must_equal true
@ds.get(h1.contain_all(%w'a d')).must_equal false
@ds.get(h1.contain_any(%w'a d')).must_equal true
@ds.get(h1.contain_any(%w'e d')).must_equal false
@ds.get(h1.contains(h2)).must_equal true
@ds.get(h1.contains(h3)).must_equal false
@ds.get(h2.contained_by(h1)).must_equal true
@ds.get(h2.contained_by(h3)).must_equal false
@ds.get(h1.defined('a')).must_equal true
@ds.get(h1.defined('c')).must_equal false
@ds.get(h1.defined('d')).must_equal false
@ds.get(h1.delete('a')['c']).must_be_nil
@ds.get(h1.delete(%w'a d')['c']).must_be_nil
@ds.get(h1.delete(h2)['c']).must_be_nil
@ds.from(Sequel.hstore('a'=>'b', 'c'=>nil).op.each).order(:key).all.must_equal [{:key=>'a', :value=>'b'}, {:key=>'c', :value=>nil}]
@ds.get(h1.has_key?('c')).must_equal true
@ds.get(h1.include?('c')).must_equal true
@ds.get(h1.key?('c')).must_equal true
@ds.get(h1.member?('c')).must_equal true
@ds.get(h1.exist?('c')).must_equal true
@ds.get(h1.has_key?('d')).must_equal false
@ds.get(h1.include?('d')).must_equal false
@ds.get(h1.key?('d')).must_equal false
@ds.get(h1.member?('d')).must_equal false
@ds.get(h1.exist?('d')).must_equal false
@ds.get(h1.hstore.hstore.hstore.keys.length).must_equal 2
@ds.get(h1.keys.length).must_equal 2
@ds.get(h2.keys.length).must_equal 1
@ds.get(h1.akeys.length).must_equal 2
@ds.get(h2.akeys.length).must_equal 1
@ds.from(Sequel.hstore('t'=>'s').op.populate(Sequel::SQL::Cast.new(nil, :items))).select_map(:t).must_equal ['s']
@ds.from(Sequel[:items].as(:i)).select(Sequel.hstore('t'=>'s').op.record_set(:i).as(:r)).from_self(:alias=>:s).select(Sequel.lit('(r).*')).from_self.select_map(:t).must_equal ['s']
@ds.from(Sequel.hstore('t'=>'s', 'a'=>'b').op.skeys.as(:s)).select_order_map(:s).must_equal %w'a t'
@ds.from((Sequel.hstore('t'=>'s', 'a'=>'b').op - 'a').skeys.as(:s)).select_order_map(:s).must_equal %w't'
@ds.get(h1.slice(%w'a c').keys.length).must_equal 2
@ds.get(h1.slice(%w'd c').keys.length).must_equal 1
@ds.get(h1.slice(%w'd e').keys.length).must_be_nil
@ds.from(Sequel.hstore('t'=>'s', 'a'=>'b').op.svals.as(:s)).select_order_map(:s).must_equal %w'b s'
@ds.get(h1.to_array.length).must_equal 4
@ds.get(h2.to_array.length).must_equal 2
@ds.get(h1.to_matrix.length).must_equal 2
@ds.get(h2.to_matrix.length).must_equal 1
@ds.get(h1.values.length).must_equal 2
@ds.get(h2.values.length).must_equal 1
@ds.get(h1.avals.length).must_equal 2
@ds.get(h2.avals.length).must_equal 1
if DB.server_version >= 140000
@ds.update(h1['a'] => '2', h1['b'] => '3', h1['c'] => '4')
@ds.get(:h1).must_equal("a"=>"2", "b"=>"3", "c"=>"4")
end
end
end if DB.type_supported?(:hstore)
describe 'PostgreSQL' do
before(:all) do
@db = DB
@ds = @db[:items]
@a = [1, 2, {'a'=>'b'}, 3.0]
@h = {'a'=>'b', '1'=>[3, 4, 5]}
end
after do
@db.wrap_json_primitives = nil
@db.typecast_json_strings = nil
@db.drop_table?(:items)
end
json_types = [:json]
json_types << :jsonb if DB.server_version >= 90400
json_types.each do |json_type|
json_array_type = "#{json_type}[]"
pg_json = Sequel.method(:"pg_#{json_type}")
pg_json_wrap = Sequel.method(:"pg_#{json_type}_wrap")
hash_class = json_type == :jsonb ? Sequel::Postgres::JSONBHash : Sequel::Postgres::JSONHash
array_class = json_type == :jsonb ? Sequel::Postgres::JSONBArray : Sequel::Postgres::JSONArray
str_class = json_type == :jsonb ? Sequel::Postgres::JSONBString : Sequel::Postgres::JSONString
object_class = json_type == :jsonb ? Sequel::Postgres::JSONBObject : Sequel::Postgres::JSONObject
Sequel.extension :pg_json_ops
jo = pg_json.call('a'=>1, 'b'=>{'c'=>2, 'd'=>{'e'=>3}}).op
ja = pg_json.call([2, 3, %w'a b']).op
it "insert and retrieve #{json_type} values" do
@db.create_table!(:items){column :j, json_type}
@ds.insert(pg_json.call(@h))
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:j]
v.class.must_equal(hash_class)
v.to_hash.must_be_kind_of(Hash)
v.must_equal @h
v.to_hash.must_equal @h
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
@ds.insert(pg_json.call(@a))
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:j]
v.class.must_equal(array_class)
v.to_a.must_be_kind_of(Array)
v.must_equal @a
v.to_a.must_equal @a
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it "insert and retrieve #{json_type} primitive values" do
@db.create_table!(:items){column :j, json_type}
['str', 1, 2.5, nil, true, false].each do |rv|
@ds.delete
@ds.insert(pg_json_wrap.call(rv))
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:j]
v.class.must_equal(rv.class)
if rv.nil?
v.must_be_nil
else
v.must_equal rv
end
end
@db.wrap_json_primitives = true
['str', 1, 2.5, nil, true, false].each do |rv|
@ds.delete
@ds.insert(pg_json_wrap.call(rv))
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:j]
v.class.ancestors.must_include(object_class)
v.__getobj__.must_be_kind_of(rv.class)
if rv.nil?
v.must_be_nil
v.__getobj__.must_be_nil
else
v.must_equal rv
v.__getobj__.must_equal rv
end
@ds.delete
@ds.insert(rs.first)
@ds.all[0][:j].must_equal rs[0][:j]
end
end
it "insert and retrieve #{json_type}[] values" do
@db.create_table!(:items){column :j, json_array_type}
j = Sequel.pg_array([pg_json.call('a'=>1), pg_json.call(['b', 2])])
@ds.insert(j)
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:j]
v.class.must_equal(Sequel::Postgres::PGArray)
v.to_a.must_be_kind_of(Array)
v.must_equal j
v.to_a.must_equal j
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it "insert and retrieve #{json_type}[] values with json primitives" do
@db.create_table!(:items){column :j, json_array_type}
raw = ['str', 1, 2.5, nil, true, false]
j = Sequel.pg_array(raw.map(&pg_json_wrap), json_type)
@ds.insert(j)
@ds.count.must_equal 1
rs = @ds.all
v = rs.first[:j]
v.class.must_equal(Sequel::Postgres::PGArray)
v.to_a.must_be_kind_of(Array)
v.map(&:class).must_equal raw.map(&:class)
v.must_equal raw
v.to_a.must_equal raw
@db.wrap_json_primitives = true
j = Sequel.pg_array(raw.map(&pg_json_wrap), json_type)
@ds.insert(j)
rs = @ds.all
v = rs.first[:j]
v.class.must_equal(Sequel::Postgres::PGArray)
v.to_a.must_be_kind_of(Array)
v.map(&:class).each{|c| c.ancestors.must_include(object_class)}
[v, v.to_a].each do |v0|
v0.zip(raw) do |v1, r1|
if r1.nil?
v1.must_be_nil
v1.__getobj__.must_be_nil
else
v1.must_equal r1
v1.__getobj__.must_equal r1
end
end
end
@ds.delete
@ds.insert(rs.first)
@ds.all[0][:j].zip(rs[0][:j]) do |v1, r1|
if v1.__getobj__.nil?
v1.must_be_nil
v1.__getobj__.must_be_nil
else
v1.must_equal r1
v1.must_equal r1.__getobj__
v1.__getobj__.must_equal r1
v1.__getobj__.must_equal r1.__getobj__
end
end
end
it "models with #{json_type} columns" do
@db.create_table!(:items) do
primary_key :id
column :h, json_type
end
c = Class.new(Sequel::Model(@db[:items]))
c.create(:h=>@h).h.must_equal @h
c.create(:h=>@a).h.must_equal @a
c.create(:h=>pg_json.call(@h)).h.must_equal @h
c.create(:h=>pg_json.call(@a)).h.must_equal @a
end
it "models with #{json_type} primitives" do
@db.create_table!(:items) do
primary_key :id
column :h, json_type
end
c = Class.new(Sequel::Model(@db[:items]))
['str', 1, 2.5, nil, true, false].each do |v|
@db.wrap_json_primitives = nil
cv = c[c.insert(:h=>pg_json_wrap.call(v))]
cv.h.class.ancestors.wont_include(object_class)
if v.nil?
cv.h.must_be_nil
else
cv.h.must_equal v
end
@db.wrap_json_primitives = true
cv.refresh
cv.h.class.ancestors.must_include(object_class)
cv.save
cv.refresh
cv.h.class
if v.nil?
cv.h.must_be_nil
else
cv.h.must_equal v
end
c.new(:h=>cv.h).h.class.ancestors.must_include(object_class)
end
v = c.new(:h=>'{}').h
v.class.must_equal hash_class
v.must_equal({})
@db.typecast_json_strings = true
v = c.new(:h=>'{}').h
v.class.must_equal str_class
v.must_equal '{}'
c.new(:h=>'str').h.class.ancestors.must_include(object_class)
c.new(:h=>'str').h.must_equal 'str'
c.new(:h=>1).h.class.ancestors.must_include(object_class)
c.new(:h=>1).h.must_equal 1
c.new(:h=>2.5).h.class.ancestors.must_include(object_class)
c.new(:h=>2.5).h.must_equal 2.5
c.new(:h=>true).h.class.ancestors.must_include(object_class)
c.new(:h=>true).h.must_equal true
c.new(:h=>false).h.class.ancestors.must_include(object_class)
c.new(:h=>false).h.must_equal false
c.new(:h=>nil).h.class.ancestors.wont_include(object_class)
c.new(:h=>nil).h.must_be_nil
end
it "with empty #{json_type} default values and defaults_setter plugin" do
@db.create_table!(:items) do
column :h, json_type, :default=>hash_class.new({})
column :a, json_type, :default=>array_class.new([])
end
c = Class.new(Sequel::Model(@db[:items]))
c.plugin :defaults_setter, :cache=>true
o = c.new
o.h.class.must_equal(hash_class)
o.a.class.must_equal(array_class)
o.h.to_hash.must_be_same_as(o.h.to_hash)
o.a.to_a.must_be_same_as(o.a.to_a)
o.h['a'] = 'b'
o.a << 1
o.save
o.h.must_equal('a'=>'b')
o.a.must_equal([1])
end
it "use #{json_type} in bound variables" do
@db.create_table!(:items){column :i, json_type}
@ds.call(:insert, {:i=>pg_json.call(@h)}, {:i=>:$i})
@ds.get(:i).must_equal @h
@ds.delete
@ds.call(:insert, {:i=>pg_json.call('a'=>nil)}, {:i=>:$i})
@ds.get(:i).must_equal pg_json.call('a'=>nil)
@db.create_table!(:items){column :i, json_array_type}
j = Sequel.pg_array([pg_json.call('a'=>1), pg_json.call(['b', 2])], json_type)
@ds.call(:insert, {:i=>j}, {:i=>:$i})
@ds.get(:i).must_equal j
end if uses_pg_or_jdbc
it "use #{json_type} primitives in bound variables" do
@db.create_table!(:items){column :i, json_type}
@db.wrap_json_primitives = true
raw = ['str', 1, 2.5, nil, true, false]
raw.each do |v|
@ds.delete
@ds.call(:insert, {:i=>@db.get(pg_json_wrap.call(v))}, {:i=>:$i})
rv = @ds.get(:i)
rv.class.ancestors.must_include(object_class)
if v.nil?
rv.must_be_nil
else
rv.must_equal v
end
end
@db.create_table!(:items){column :i, json_array_type}
j = Sequel.pg_array(raw.map(&pg_json_wrap), json_type)
@ds.call(:insert, {:i=>j}, {:i=>:$i})
@ds.all[0][:i].zip(raw) do |v1, r1|
if v1.__getobj__.nil?
v1.must_be_nil
v1.__getobj__.must_be_nil
else
v1.must_equal r1
v1.__getobj__.must_equal r1
end
end
end if uses_pg_or_jdbc
it "9.3 #{json_type} operations/functions with pg_json_ops" do
@db.get(jo['a']).must_equal 1
@db.get(jo['b']['c']).must_equal 2
@db.get(jo[%w'b c']).must_equal 2
@db.get(jo['b'].get_text(%w'd e')).must_equal "3"
@db.get(jo[%w'b d'].get_text('e')).must_equal "3"
@db.get(ja[1]).must_equal 3
@db.get(ja[%w'2 1']).must_equal 'b'
@db.get(jo.extract('a')).must_equal 1
@db.get(jo.extract('b').extract('c')).must_equal 2
@db.get(jo.extract('b', 'c')).must_equal 2
@db.get(jo.extract('b', 'd', 'e')).must_equal 3
@db.get(jo.extract_text('b', 'd')).gsub(' ', '').must_equal '{"e":3}'
@db.get(jo.extract_text('b', 'd', 'e')).must_equal '3'
@db.get(ja.array_length).must_equal 3
@db.from(ja.array_elements.as(:v)).select_map(:v).must_equal [2, 3, %w'a b']
@db.from(jo.keys.as(:k)).select_order_map(:k).must_equal %w'a b'
@db.from(jo.each).select_order_map(:key).must_equal %w'a b'
@db.from(jo.each).order(:key).select_map(:value).must_equal [1, {'c'=>2, 'd'=>{'e'=>3}}]
@db.from(jo.each_text).select_order_map(:key).must_equal %w'a b'
@db.from(jo.each_text).order(:key).where(:key=>'b').get(:value).gsub(' ', '').must_match(/\{"d":\{"e":3\},"c":2\}|\{"c":2,"d":\{"e":3\}\}/)
Sequel.extension :pg_row_ops
@db.create_table!(:items) do
Integer :a
String :b
end
j = Sequel.pg_json('a'=>1, 'b'=>'c').op
@db.get(j.populate(Sequel.cast(nil, :items)).pg_row[:a]).must_equal 1
@db.get(j.populate(Sequel.cast(nil, :items)).pg_row[:b]).must_equal 'c'
j = Sequel.pg_json([{'a'=>1, 'b'=>'c'}, {'a'=>2, 'b'=>'d'}]).op
@db.from(j.populate_set(Sequel.cast(nil, :items))).select_order_map(:a).must_equal [1, 2]
@db.from(j.populate_set(Sequel.cast(nil, :items))).select_order_map(:b).must_equal %w'c d'
end if DB.server_version >= 90300
it "9.4 #{json_type} operations/functions with pg_json_ops" do
@db.get(jo.typeof).must_equal 'object'
@db.get(ja.typeof).must_equal 'array'
@db.from(ja.array_elements_text.as(:v)).select_map(:v).map{|s| s.gsub(' ', '')}.must_equal ['2', '3', '["a","b"]']
@db.from(jo.to_record.as(:v, [Sequel.lit('a integer'), Sequel.lit('b text')])).select_map(:a).must_equal [1]
@db.from(pg_json.call([{'a'=>1, 'b'=>1}]).op.to_recordset.as(:v, [Sequel.lit('a integer'), Sequel.lit('b integer')])).select_map(:a).must_equal [1]
if json_type == :jsonb
@db.get(jo.has_key?('a')).must_equal true
@db.get(jo.has_key?('c')).must_equal false
@db.get(pg_json.call(['2', '3', %w'a b']).op.include?('2')).must_equal true
@db.get(pg_json.call(['2', '3', %w'a b']).op.include?('4')).must_equal false
@db.get(jo.contain_all(['a', 'b'])).must_equal true
@db.get(jo.contain_all(['a', 'c'])).must_equal false
@db.get(jo.contain_all(['d', 'c'])).must_equal false
@db.get(jo.contain_any(['a', 'b'])).must_equal true
@db.get(jo.contain_any(['a', 'c'])).must_equal true
@db.get(jo.contain_any(['d', 'c'])).must_equal false
@db.get(jo.contains(jo)).must_equal true
@db.get(jo.contained_by(jo)).must_equal true
@db.get(jo.contains('a'=>1)).must_equal true
@db.get(jo.contained_by('a'=>1)).must_equal false
@db.get(pg_json.call('a'=>1).op.contains(jo)).must_equal false
@db.get(pg_json.call('a'=>1).op.contained_by(jo)).must_equal true
@db.get(ja.contains(ja)).must_equal true
@db.get(ja.contained_by(ja)).must_equal true
@db.get(ja.contains([2,3])).must_equal true
@db.get(ja.contained_by([2,3])).must_equal false
@db.get(pg_json.call([2,3]).op.contains(ja)).must_equal false
@db.get(pg_json.call([2,3]).op.contained_by(ja)).must_equal true
end
end if DB.server_version >= 90400
it '9.5 jsonb operations/functions with pg_json_ops' do
@db.get(pg_json.call([nil, 2]).op.strip_nulls[1]).must_equal 2
@db.get(pg_json.call([nil, 2]).op.pretty).must_equal "[\n null,\n 2\n]"
@db.from((jo - 'b').keys.as(:k)).select_order_map(:k).must_equal %w'a'
@db.from(jo.delete_path(['b','c'])['b'].keys.as(:k)).select_order_map(:k).must_equal %w'd'
@db.from(jo.concat('c'=>'d').keys.as(:k)).select_order_map(:k).must_equal %w'a b c'
@db.get(jo.set(%w'a', 'f'=>'g')['a']['f']).must_equal 'g'
end if DB.server_version >= 90500 && json_type == :jsonb
it '9.6 jsonb operations/functions with pg_json_ops' do
@db.get(pg_json.call([3]).op.insert(['0'], {'a'=>2})[0]['a']).must_equal 2
@db.get(pg_json.call([3]).op.insert(['0'], {'a'=>2}, false)[0]['a']).must_equal 2
@db.get(pg_json.call([3]).op.insert(['0'], {'a'=>2}, true)[0]).must_equal 3
@db.get(pg_json.call([3]).op.insert(['0'], {'a'=>2}, true)[1]['a']).must_equal 2
end if DB.server_version >= 90600 && json_type == :jsonb
it '12 jsonb operations/functions with pg_json_ops' do
@db.get(jo.path_exists('$.b.d.e')).must_equal true
@db.get(jo.path_exists('$.b.d.f')).must_equal false
@db.get(jo.path_exists!('$.b.d.e')).must_equal true
@db.get(jo.path_exists!('$.b.d.f')).must_equal false
@db.get(jo.path_exists!('$.b.d.e ? (@ > $x)', '{"x":2}')).must_equal true
@db.get(jo.path_exists!('$.b.d.e ? (@ > $x)', '{"x":4}')).must_equal false
@db.get(jo.path_exists!('$.b.d.e ? (@ > $x)', x: 2)).must_equal true
@db.get(jo.path_exists!('$.b.d.e ? (@ > $x)', x: 4)).must_equal false
@db.get(jo.path_exists!('$.b.d.e ? (@ > $x)', {x: 2}, true)).must_equal true
@db.get(jo.path_exists!('$.b.d.e ? (@ > $x)', {x: 4}, false)).must_equal false
@db.get(jo.path_match('$.b.d.e')).must_be_nil
@db.get(jo.path_match('$.b.d.f')).must_be_nil
@db.get(pg_json.call('b'=>{'d'=>{'e'=>true}}).op.path_match('$.b.d.e')).must_equal true
@db.get(pg_json.call('b'=>{'d'=>{'e'=>false}}).op.path_match('$.b.d.e')).must_equal false
proc{@db.get(jo.path_match!('$.b.d.e'))}.must_raise(Sequel::DatabaseError)
proc{@db.get(jo.path_match!('$.b.d.f'))}.must_raise(Sequel::DatabaseError)
@db.get(jo.path_match!('$.b.d.e', {}, true)).must_be_nil
@db.get(jo.path_match!('$.b.d.f', {}, true)).must_be_nil
@db.get(pg_json.call('b'=>{'d'=>{'e'=>true}}).op.path_match!('$.b.d.e')).must_equal true
@db.get(pg_json.call('b'=>{'d'=>{'e'=>false}}).op.path_match!('$.b.d.e')).must_equal false
@db.get(jo.path_match!('$.b.d.e > $x', '{"x":2}')).must_equal true
@db.get(jo.path_match!('$.b.d.e > $x', '{"x":4}')).must_equal false
@db.get(jo.path_match!('$.b.d.e > $x', x: 2)).must_equal true
@db.get(jo.path_match!('$.b.d.e > $x', x: 4)).must_equal false
@db.get(jo.path_match!('$.b.d.e > $x', {x: 2}, false)).must_equal true
@db.get(jo.path_match!('$.b.d.e > $x', {x: 4}, true)).must_equal false
@db.get(jo.path_query_first('$.b.d.e')).must_equal 3
@db.get(jo.path_query_first('$.b.d.f')).must_be_nil
@db.get(jo.path_query_first('$.b.d.e ? (@ > $x)', '{"x":2}')).must_equal 3
@db.get(jo.path_query_first('$.b.d.e ? (@ > $x)', '{"x":4}')).must_be_nil
@db.get(jo.path_query_first('$.b.d.e ? (@ > $x)', x: 2)).must_equal 3
@db.get(jo.path_query_first('$.b.d.e ? (@ > $x)', x: 4)).must_be_nil
@db.get(jo.path_query_first('$.b.d.e ? (@ > $x)', {x: 2}, true)).must_equal 3
@db.get(jo.path_query_first('$.b.d.e ? (@ > $x)', {x: 4}, false)).must_be_nil
@db.get(jo.path_query_array('$.b.d.e')).must_equal [3]
@db.get(jo.path_query_array('$.b.d.f')).must_equal []
@db.get(jo.path_query_array('$.b.d.e ? (@ > $x)', '{"x":2}')).must_equal [3]
@db.get(jo.path_query_array('$.b.d.e ? (@ > $x)', '{"x":4}')).must_equal []
@db.get(jo.path_query_array('$.b.d.e ? (@ > $x)', x: 2)).must_equal [3]
@db.get(jo.path_query_array('$.b.d.e ? (@ > $x)', x: 4)).must_equal []
@db.get(jo.path_query_array('$.b.d.e ? (@ > $x)', {x: 2}, true)).must_equal [3]
@db.get(jo.path_query_array('$.b.d.e ? (@ > $x)', {x: 4}, false)).must_equal []
@db.from(jo.path_query('$.b.d.e').as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query('$.b.d.f').as(:a, [:b])).get(:b).must_be_nil
@db.from(jo.path_query('$.b.d.e ? (@ > $x)', '{"x":2}').as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query('$.b.d.e ? (@ > $x)', '{"x":4}').as(:a, [:b])).get(:b).must_be_nil
@db.from(jo.path_query('$.b.d.e ? (@ > $x)', x: 2).as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query('$.b.d.e ? (@ > $x)', x: 4).as(:a, [:b])).get(:b).must_be_nil
@db.from(jo.path_query('$.b.d.e ? (@ > $x)', {x: 2}, true).as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query('$.b.d.e ? (@ > $x)', {x: 4}, false).as(:a, [:b])).get(:b).must_be_nil
end if DB.server_version >= 120000 && json_type == :jsonb
it '13 jsonb operations/functions with pg_json_ops' do
@db.get(jo.set_lax(%w'a', 'f'=>'g')['a']['f']).must_equal 'g'
@db.get(jo.path_exists_tz!('$.b.d.e')).must_equal true
@db.get(jo.path_exists_tz!('$.b.d.f')).must_equal false
@db.get(jo.path_exists_tz!('$.b.d.e ? (@ > $x)', '{"x":2}')).must_equal true
@db.get(jo.path_exists_tz!('$.b.d.e ? (@ > $x)', '{"x":4}')).must_equal false
@db.get(jo.path_exists_tz!('$.b.d.e ? (@ > $x)', x: 2)).must_equal true
@db.get(jo.path_exists_tz!('$.b.d.e ? (@ > $x)', x: 4)).must_equal false
@db.get(jo.path_exists_tz!('$.b.d.e ? (@ > $x)', {x: 2}, true)).must_equal true
@db.get(jo.path_exists_tz!('$.b.d.e ? (@ > $x)', {x: 4}, false)).must_equal false
proc{@db.get(jo.path_match_tz!('$.b.d.e'))}.must_raise(Sequel::DatabaseError)
proc{@db.get(jo.path_match_tz!('$.b.d.f'))}.must_raise(Sequel::DatabaseError)
@db.get(jo.path_match_tz!('$.b.d.e', {}, true)).must_be_nil
@db.get(jo.path_match_tz!('$.b.d.f', {}, true)).must_be_nil
@db.get(pg_json.call('b'=>{'d'=>{'e'=>true}}).op.path_match_tz!('$.b.d.e')).must_equal true
@db.get(pg_json.call('b'=>{'d'=>{'e'=>false}}).op.path_match_tz!('$.b.d.e')).must_equal false
@db.get(jo.path_match_tz!('$.b.d.e > $x', '{"x":2}')).must_equal true
@db.get(jo.path_match_tz!('$.b.d.e > $x', '{"x":4}')).must_equal false
@db.get(jo.path_match_tz!('$.b.d.e > $x', x: 2)).must_equal true
@db.get(jo.path_match_tz!('$.b.d.e > $x', x: 4)).must_equal false
@db.get(jo.path_match_tz!('$.b.d.e > $x', {x: 2}, false)).must_equal true
@db.get(jo.path_match_tz!('$.b.d.e > $x', {x: 4}, true)).must_equal false
@db.get(jo.path_query_first_tz('$.b.d.e')).must_equal 3
@db.get(jo.path_query_first_tz('$.b.d.f')).must_be_nil
@db.get(jo.path_query_first_tz('$.b.d.e ? (@ > $x)', '{"x":2}')).must_equal 3
@db.get(jo.path_query_first_tz('$.b.d.e ? (@ > $x)', '{"x":4}')).must_be_nil
@db.get(jo.path_query_first_tz('$.b.d.e ? (@ > $x)', x: 2)).must_equal 3
@db.get(jo.path_query_first_tz('$.b.d.e ? (@ > $x)', x: 4)).must_be_nil
@db.get(jo.path_query_first_tz('$.b.d.e ? (@ > $x)', {x: 2}, true)).must_equal 3
@db.get(jo.path_query_first_tz('$.b.d.e ? (@ > $x)', {x: 4}, false)).must_be_nil
@db.get(jo.path_query_array_tz('$.b.d.e')).must_equal [3]
@db.get(jo.path_query_array_tz('$.b.d.f')).must_equal []
@db.get(jo.path_query_array_tz('$.b.d.e ? (@ > $x)', '{"x":2}')).must_equal [3]
@db.get(jo.path_query_array_tz('$.b.d.e ? (@ > $x)', '{"x":4}')).must_equal []
@db.get(jo.path_query_array_tz('$.b.d.e ? (@ > $x)', x: 2)).must_equal [3]
@db.get(jo.path_query_array_tz('$.b.d.e ? (@ > $x)', x: 4)).must_equal []
@db.get(jo.path_query_array_tz('$.b.d.e ? (@ > $x)', {x: 2}, true)).must_equal [3]
@db.get(jo.path_query_array_tz('$.b.d.e ? (@ > $x)', {x: 4}, false)).must_equal []
@db.from(jo.path_query_tz('$.b.d.e').as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query_tz('$.b.d.f').as(:a, [:b])).get(:b).must_be_nil
@db.from(jo.path_query_tz('$.b.d.e ? (@ > $x)', '{"x":2}').as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query_tz('$.b.d.e ? (@ > $x)', '{"x":4}').as(:a, [:b])).get(:b).must_be_nil
@db.from(jo.path_query_tz('$.b.d.e ? (@ > $x)', x: 2).as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query_tz('$.b.d.e ? (@ > $x)', x: 4).as(:a, [:b])).get(:b).must_be_nil
@db.from(jo.path_query_tz('$.b.d.e ? (@ > $x)', {x: 2}, true).as(:a, [:b])).get(:b).must_equal 3
@db.from(jo.path_query_tz('$.b.d.e ? (@ > $x)', {x: 4}, false).as(:a, [:b])).get(:b).must_be_nil
end if DB.server_version >= 130000 && json_type == :jsonb
it '14 jsonb operations/functions with pg_json_ops' do
@db.create_table!(:items){column :i, json_type}
@db[:items].delete
@db[:items].insert(:i=>Sequel.pg_jsonb('a'=>{'b'=>1}))
@db[:items].update(Sequel.pg_jsonb_op(:i)['a']['b'] => '2',
Sequel.pg_jsonb_op(:i)['a']['c'] => '3',
Sequel.pg_jsonb_op(Sequel[:i])['d'] => Sequel.pg_jsonb('e'=>4))
@db[:items].all.must_equal [{:i=>{'a'=>{'b'=>2, 'c'=>3}, 'd'=>{'e'=>4}}}]
end if DB.server_version >= 140000 && json_type == :jsonb
end
end if DB.server_version >= 90200
describe 'PostgreSQL inet/cidr types' do
ipv6_broken = (IPAddr.new('::1'); false) rescue true
before(:all) do
@db = DB
@ds = @db[:items]
@v4 = '127.0.0.1'
@v4nm = '127.0.0.0/8'
@v6 = '2001:4f8:3:ba:2e0:81ff:fe22:d1f1'
@v6nm = '2001:4f8:3:ba::/64'
@ipv4 = IPAddr.new(@v4)
@ipv4nm = IPAddr.new(@v4nm)
unless ipv6_broken
@ipv6 = IPAddr.new(@v6)
@ipv6nm = IPAddr.new(@v6nm)
end
end
after do
@db.drop_table?(:items)
end
it 'insert and retrieve inet/cidr values' do
@db.create_table!(:items){inet :i; cidr :c}
@ds.insert(@ipv4, @ipv4nm)
@ds.count.must_equal 1
rs = @ds.all
rs.first[:i].must_equal @ipv4
rs.first[:c].must_equal @ipv4nm
rs.first[:i].must_be_kind_of(IPAddr)
rs.first[:c].must_be_kind_of(IPAddr)
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
unless ipv6_broken
@ds.delete
@ds.insert(@ipv6, @ipv6nm)
@ds.count.must_equal 1
rs = @ds.all
rs.first[:j]
rs.first[:i].must_equal @ipv6
rs.first[:c].must_equal @ipv6nm
rs.first[:i].must_be_kind_of(IPAddr)
rs.first[:c].must_be_kind_of(IPAddr)
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
end
it 'allow overridding type conversions of inet/cidr types' do
cp = @db.conversion_procs[869]
acp = @db.conversion_procs[1041]
begin
@db.add_conversion_proc(1041, lambda{|s| s})
@db.add_conversion_proc(869, lambda{|s| s})
@db.get(Sequel.cast('127.0.0.1', :inet)).must_equal '127.0.0.1'
@db.get(Sequel.pg_array(['127.0.0.1'], :inet)).must_equal '{127.0.0.1}'
ensure
@db.add_conversion_proc(869, cp)
@db.add_conversion_proc(1041, acp)
end
end
it 'insert and retrieve inet/cidr/macaddr array values' do
@db.create_table!(:items){column :i, 'inet[]'; column :c, 'cidr[]'; column :m, 'macaddr[]'}
@ds.insert(Sequel.pg_array([@ipv4], 'inet'), Sequel.pg_array([@ipv4nm], 'cidr'), Sequel.pg_array(['12:34:56:78:90:ab'], 'macaddr'))
@ds.count.must_equal 1
rs = @ds.all
rs.first.values.all?{|c| c.is_a?(Sequel::Postgres::PGArray)}.must_equal true
rs.first[:i].first.must_equal @ipv4
rs.first[:c].first.must_equal @ipv4nm
rs.first[:m].first.must_equal '12:34:56:78:90:ab'
rs.first[:i].first.must_be_kind_of(IPAddr)
rs.first[:c].first.must_be_kind_of(IPAddr)
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'use ipaddr in bound variables' do
@db.create_table!(:items){inet :i; cidr :c}
@ds.call(:insert, {:i=>@ipv4, :c=>@ipv4nm}, {:i=>:$i, :c=>:$c})
@ds.get(:i).must_equal @ipv4
@ds.get(:c).must_equal @ipv4nm
@ds.filter(:i=>:$i, :c=>:$c).call(:first, :i=>@ipv4, :c=>@ipv4nm).must_equal(:i=>@ipv4, :c=>@ipv4nm)
@ds.filter(:i=>:$i, :c=>:$c).call(:first, :i=>@ipv6, :c=>@ipv6nm).must_be_nil
@ds.filter(:i=>:$i, :c=>:$c).call(:delete, :i=>@ipv4, :c=>@ipv4nm).must_equal 1
unless ipv6_broken
@ds.call(:insert, {:i=>@ipv6, :c=>@ipv6nm}, {:i=>:$i, :c=>:$c})
@ds.get(:i).must_equal @ipv6
@ds.get(:c).must_equal @ipv6nm
@ds.filter(:i=>:$i, :c=>:$c).call(:first, :i=>@ipv6, :c=>@ipv6nm).must_equal(:i=>@ipv6, :c=>@ipv6nm)
@ds.filter(:i=>:$i, :c=>:$c).call(:first, :i=>@ipv4, :c=>@ipv4nm).must_be_nil
@ds.filter(:i=>:$i, :c=>:$c).call(:delete, :i=>@ipv6, :c=>@ipv6nm).must_equal 1
end
@db.create_table!(:items){column :i, 'inet[]'; column :c, 'cidr[]'; column :m, 'macaddr[]'}
@ds.call(:insert, {:i=>[@ipv4], :c=>[@ipv4nm], :m=>['12:34:56:78:90:ab']}, {:i=>:$i, :c=>:$c, :m=>:$m})
@ds.filter(:i=>:$i, :c=>:$c, :m=>:$m).call(:first, :i=>[@ipv4], :c=>[@ipv4nm], :m=>['12:34:56:78:90:ab']).must_equal(:i=>[@ipv4], :c=>[@ipv4nm], :m=>['12:34:56:78:90:ab'])
@ds.filter(:i=>:$i, :c=>:$c, :m=>:$m).call(:first, :i=>[], :c=>[], :m=>[]).must_be_nil
@ds.filter(:i=>:$i, :c=>:$c, :m=>:$m).call(:delete, :i=>[@ipv4], :c=>[@ipv4nm], :m=>['12:34:56:78:90:ab']).must_equal 1
end if uses_pg_or_jdbc
it 'parse default values for schema' do
@db.create_table!(:items) do
inet :i, :default=>IPAddr.new('127.0.0.1')
cidr :c, :default=>IPAddr.new('127.0.0.1')
end
@db.schema(:items)[0][1][:ruby_default].must_equal IPAddr.new('127.0.0.1')
@db.schema(:items)[1][1][:ruby_default].must_equal IPAddr.new('127.0.0.1')
end
it 'with models' do
@db.create_table!(:items) do
primary_key :id
inet :i
cidr :c
end
c = Class.new(Sequel::Model(@db[:items]))
c.create(:i=>@v4, :c=>@v4nm).values.values_at(:i, :c).must_equal [@ipv4, @ipv4nm]
unless ipv6_broken
c.create(:i=>@ipv6, :c=>@ipv6nm).values.values_at(:i, :c).must_equal [@ipv6, @ipv6nm]
end
end
it 'operations/functions with pg_inet_ops' do
Sequel.extension :pg_inet_ops
@db.get(Sequel.pg_inet_op('1.2.3.4') << '1.2.3.0/24').must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.4') << '1.2.3.4/32').must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.4') << '1.2.2.0/24').must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.4').contained_by('1.2.3.0/24')).must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.4').contained_by('1.2.3.4/32')).must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.4').contained_by('1.2.2.0/24')).must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.4').contained_by_or_equals('1.2.3.0/24')).must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.4').contained_by_or_equals('1.2.3.4/32')).must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.4').contained_by_or_equals('1.2.2.0/24')).must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.0/24') >> '1.2.3.4').must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.0/24') >> '1.2.2.4').must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.0/24').contains('1.2.3.4')).must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.0/24').contains('1.2.2.4')).must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.0/24').contains_or_equals('1.2.3.4')).must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.0/24').contains_or_equals('1.2.2.4')).must_equal false
@db.get(Sequel.pg_inet_op('1.2.3.0/24').contains_or_equals('1.2.3.0/24')).must_equal true
@db.get(Sequel.pg_inet_op('1.2.3.0/32') + 1).must_equal IPAddr.new('1.2.3.1/32')
@db.get(Sequel.pg_inet_op('1.2.3.1/32') - 1).must_equal IPAddr.new('1.2.3.0/32')
@db.get(Sequel.pg_inet_op('1.2.3.1/32') - '1.2.3.0/32').must_equal 1
@db.get(Sequel.pg_inet_op('1.2.3.0/32') & '1.2.0.4/32').must_equal IPAddr.new('1.2.0.0/32')
@db.get(Sequel.pg_inet_op('1.2.0.0/32') | '0.0.3.4/32').must_equal IPAddr.new('1.2.3.4/32')
@db.get(~Sequel.pg_inet_op('0.0.0.0/32')).must_equal IPAddr.new('255.255.255.255/32')
@db.get(Sequel.pg_inet_op('1.2.3.4/24').abbrev).must_equal '1.2.3.4/24'
@db.get(Sequel.pg_inet_op('1.2.3.4/24').broadcast).must_equal IPAddr.new('1.2.3.255/24')
@db.get(Sequel.pg_inet_op('1234:3456:5678:789a:9abc:bced:edf0:f012/96').broadcast).must_equal IPAddr.new('1234:3456:5678:789a:9abc:bced::/96')
@db.get(Sequel.pg_inet_op('1234:3456:5678:789a:9abc:bced:edf0:f012/128').broadcast).must_equal IPAddr.new('1234:3456:5678:789a:9abc:bced:edf0:f012/128')
@db.get(Sequel.pg_inet_op('1234:3456:5678:789a:9abc:bced:edf0:f012/64').broadcast).must_equal IPAddr.new('1234:3456:5678:789a::/64')
@db.get(Sequel.pg_inet_op('1234:3456:5678:789a:9abc:bced:edf0:f012/32').broadcast).must_equal IPAddr.new('1234:3456::/32')
@db.get(Sequel.pg_inet_op('1234:3456:5678:789a:9abc:bced:edf0:f012/0').broadcast).must_equal IPAddr.new('::/0')
@db.get(Sequel.pg_inet_op('1.2.3.4/24').family).must_equal 4
@db.get(Sequel.pg_inet_op('1.2.3.4/24').host).must_equal '1.2.3.4'
@db.get(Sequel.pg_inet_op('1.2.3.4/24').hostmask).must_equal IPAddr.new('0.0.0.255/32')
@db.get(Sequel.pg_inet_op('1.2.3.4/24').masklen).must_equal 24
@db.get(Sequel.pg_inet_op('1.2.3.4/24').netmask).must_equal IPAddr.new('255.255.255.0/32')
@db.get(Sequel.pg_inet_op('1.2.3.4/24').network).must_equal IPAddr.new('1.2.3.0/24')
@db.get(Sequel.pg_inet_op('1.2.3.4/24').set_masklen(16)).must_equal IPAddr.new('1.2.3.4/16')
@db.get(Sequel.pg_inet_op('1.2.3.4/32').text).must_equal '1.2.3.4/32'
if @db.server_version >= 90400
@db.get(Sequel.pg_inet_op('1.2.3.0/24').contains_or_contained_by('1.2.0.0/16')).must_equal true
@db.get(Sequel.pg_inet_op('1.2.0.0/16').contains_or_contained_by('1.2.3.0/24')).must_equal true
@db.get(Sequel.pg_inet_op('1.3.0.0/16').contains_or_contained_by('1.2.3.0/24')).must_equal false
end
end
end
describe 'PostgreSQL custom range types' do
after do
@db.run "DROP TYPE timerange";
end
it "should allow registration and use" do
@db = DB
@db.run "CREATE TYPE timerange AS range (subtype = time)"
@db.register_range_type('timerange')
r = Sequel::SQLTime.create(10, 11, 12)..Sequel::SQLTime.create(11, 12, 13)
@db.get(Sequel.pg_range(r, :timerange)).to_range.must_equal r
if DB.server_version >= 140000
@db.register_multirange_type('timemultirange')
r = [Sequel::SQLTime.create(1, 2, 3)..Sequel::SQLTime.create(7, 8, 9), Sequel::SQLTime.create(10, 11, 12)..Sequel::SQLTime.create(11, 12, 13)]
@db.get(Sequel.pg_multirange(r, :timemultirange)).map(&:to_range).must_equal r
end
end
end if DB.server_version >= 90200
describe 'PostgreSQL range types' do
before(:all) do
@db = DB
@ds = @db[:items]
@map = {:i4=>'int4range', :i8=>'int8range', :n=>'numrange', :d=>'daterange', :t=>'tsrange', :tz=>'tstzrange'}
@r = {:i4=>1...2, :i8=>2...3, :n=>BigDecimal('1.0')..BigDecimal('2.0'), :d=>Date.today...(Date.today+1), :t=>Time.local(2011, 1)..Time.local(2011, 2), :tz=>Time.local(2011, 1)..Time.local(2011, 2)}
@ra = {}
@pgr = {}
@pgra = {}
@r.each{|k, v| @ra[k] = Sequel.pg_array([v], @map[k])}
@r.each{|k, v| @pgr[k] = Sequel.pg_range(v)}
@r.each{|k, v| @pgra[k] = Sequel.pg_array([Sequel.pg_range(v)], @map[k])}
end
after do
@db.drop_table?(:items)
end
it 'insert and retrieve range type values' do
@db.create_table!(:items){int4range :i4; int8range :i8; numrange :n; daterange :d; tsrange :t; tstzrange :tz}
[@r, @pgr].each do |input|
h = {}
input.each{|k, v| h[k] = Sequel.cast(v, @map[k])}
@ds.insert(h)
@ds.count.must_equal 1
rs = @ds.all
rs.first.each do |k, v|
v.class.must_equal(Sequel::Postgres::PGRange)
v.to_range.must_be_kind_of(Range)
v.must_be :==, @r[k]
v.to_range.must_equal @r[k]
end
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
end
end
it 'insert and retrieve arrays of range type values' do
@db.create_table!(:items){column :i4, 'int4range[]'; column :i8, 'int8range[]'; column :n, 'numrange[]'; column :d, 'daterange[]'; column :t, 'tsrange[]'; column :tz, 'tstzrange[]'}
[@ra, @pgra].each do |input|
@ds.insert(input)
@ds.count.must_equal 1
rs = @ds.all
rs.first.each do |k, v|
v.class.must_equal(Sequel::Postgres::PGArray)
v.to_a.must_be_kind_of(Array)
v.first.class.must_equal(Sequel::Postgres::PGRange)
v.first.to_range.must_be_kind_of(Range)
v.must_be :==, @ra[k].to_a
v.first.must_be :==, @r[k]
end
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
end
end
it 'use range types in bound variables' do
@db.create_table!(:items){int4range :i4; int8range :i8; numrange :n; daterange :d; tsrange :t; tstzrange :tz}
h = {}
@r.keys.each{|k| h[k] = :"$#{k}"}
r2 = {}
@r.each{|k, v| r2[k] = Range.new(v.begin, v.end+2)}
@ds.call(:insert, @r, h)
@ds.first.must_be :==, @r
@ds.filter(h).call(:first, @r).must_be :==, @r
@ds.filter(h).call(:first, @pgr).must_be :==, @r
@ds.filter(h).call(:first, r2).must_be_nil
@ds.filter(h).call(:delete, @r).must_equal 1
@db.create_table!(:items){column :i4, 'int4range[]'; column :i8, 'int8range[]'; column :n, 'numrange[]'; column :d, 'daterange[]'; column :t, 'tsrange[]'; column :tz, 'tstzrange[]'}
@r.each{|k, v| r2[k] = [Range.new(v.begin, v.end+2)]}
@ds.call(:insert, @ra, h)
@ds.filter(h).call(:first, @ra).each{|k, v| v.must_be :==, @ra[k].to_a}
@ds.filter(h).call(:first, @pgra).each{|k, v| v.must_be :==, @ra[k].to_a}
@ds.filter(h).call(:first, r2).must_be_nil
@ds.filter(h).call(:delete, @ra).must_equal 1
end if uses_pg_or_jdbc
it 'handle endless ranges' do
@db.get(Sequel.cast(eval('(1...)'), :int4range)).must_be :==, eval('(1...)')
@db.get(Sequel.cast(eval('(1...)'), :int4range)).wont_be :==, eval('(2...)')
@db.get(Sequel.cast(eval('(1...)'), :int4range)).wont_be :==, eval('(1..)')
@db.get(Sequel.cast(eval('(2...)'), :int4range)).must_be :==, eval('(2...)')
@db.get(Sequel.cast(eval('(2...)'), :int4range)).wont_be :==, eval('(2..)')
@db.get(Sequel.cast(eval('(2...)'), :int4range)).wont_be :==, eval('(1...)')
end if RUBY_VERSION >= '2.6'
it 'handle startless ranges' do
@db.get(Sequel.cast(eval('(...1)'), :int4range)).must_be :==, Sequel::Postgres::PGRange.new(nil, 1, :exclude_begin=>true, :exclude_end=>true, :db_type=>"int4range")
@db.get(Sequel.cast(eval('(...1)'), :int4range)).wont_be :==, Sequel::Postgres::PGRange.new(nil, 2, :exclude_begin=>true, :exclude_end=>true, :db_type=>"int4range")
@db.get(Sequel.cast(eval('(...1)'), :int4range)).wont_be :==, Sequel::Postgres::PGRange.new(nil, 1, :exclude_end=>true, :db_type=>"int4range")
@db.get(Sequel.cast(eval('(...1)'), :int4range)).wont_be :==, Sequel::Postgres::PGRange.new(nil, 1, :exclude_begin=>true, :db_type=>"int4range")
end if RUBY_VERSION >= '2.7'
it 'handle startless, endless ranges' do
@db.get(Sequel.cast(eval('nil...nil'), :int4range)).must_be :==, Sequel::Postgres::PGRange.new(nil, nil, :exclude_begin=>true, :exclude_end=>true, :db_type=>"int4range")
@db.get(Sequel.cast(eval('nil...nil'), :int4range)).wont_be :==, Sequel::Postgres::PGRange.new(nil, nil, :exclude_begin=>true, :db_type=>"int4range")
@db.get(Sequel.cast(eval('nil...nil'), :int4range)).wont_be :==, Sequel::Postgres::PGRange.new(nil, nil, :exclude_end=>true, :db_type=>"int4range")
@db.get(Sequel.cast(eval('nil...nil'), :int4range)).wont_be :==, Sequel::Postgres::PGRange.new(1, nil, :exclude_begin=>true, :db_type=>"int4range")
@db.get(Sequel.cast(eval('nil...nil'), :int4range)).wont_be :==, Sequel::Postgres::PGRange.new(nil, 1, :exclude_begin=>true, :db_type=>"int4range")
end if RUBY_VERSION >= '2.7'
it 'parse default values for schema' do
@db.create_table!(:items) do
Integer :j
int4range :i, :default=>1..4
end
@db.schema(:items)[0][1][:ruby_default].must_be_nil
@db.schema(:items)[1][1][:ruby_default].must_equal Sequel::Postgres::PGRange.new(1, 5, :exclude_end=>true, :db_type=>'int4range')
end
it 'with models' do
@db.create_table!(:items){primary_key :id; int4range :i4; int8range :i8; numrange :n; daterange :d; tsrange :t; tstzrange :tz}
c = Class.new(Sequel::Model(@db[:items]))
v = c.create(@r).values
v.delete(:id)
v.must_be :==, @r
@db.create_table!(:items){primary_key :id; column :i4, 'int4range[]'; column :i8, 'int8range[]'; column :n, 'numrange[]'; column :d, 'daterange[]'; column :t, 'tsrange[]'; column :tz, 'tstzrange[]'}
c = Class.new(Sequel::Model(@db[:items]))
v = c.create(@ra).values
v.delete(:id)
v.each{|k,v1| v1.must_be :==, @ra[k].to_a}
end
it 'works with current_datetime_timestamp extension' do
ds = @db.dataset.extension(:current_datetime_timestamp)
tsr = ds.get(Sequel.pg_range(ds.current_datetime..ds.current_datetime, :tstzrange))
tsr.begin.must_be_kind_of Time
tsr.end.must_be_kind_of Time
end
it 'operations/functions with pg_range_ops' do
Sequel.extension :pg_range_ops
@db.get(Sequel.pg_range(1..5, :int4range).op.contains(2..4)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.contains(3..6)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.contains(0..6)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.contained_by(0..6)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.contained_by(3..6)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.contained_by(2..4)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.overlaps(5..6)).must_equal true
@db.get(Sequel.pg_range(1...5, :int4range).op.overlaps(5..6)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.left_of(6..10)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.left_of(5..10)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.left_of(-1..0)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.left_of(-1..3)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.right_of(6..10)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.right_of(5..10)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.right_of(-1..0)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.right_of(-1..3)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.ends_before(6..10)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.ends_before(5..10)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.ends_before(-1..0)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.ends_before(-1..3)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.ends_before(-1..7)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(6..10)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(5..10)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(3..10)).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(-1..10)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(-1..0)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(-1..3)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(-5..-1)).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.adjacent_to(6..10)).must_equal true
@db.get(Sequel.pg_range(1...5, :int4range).op.adjacent_to(6..10)).must_equal false
@db.get((Sequel.pg_range(1..5, :int4range).op + (6..10)).adjacent_to(6..10)).must_equal false
@db.get((Sequel.pg_range(1..5, :int4range).op + (6..10)).adjacent_to(11..20)).must_equal true
@db.get((Sequel.pg_range(1..5, :int4range).op * (2..6)).adjacent_to(6..10)).must_equal true
@db.get((Sequel.pg_range(1..4, :int4range).op * (2..6)).adjacent_to(6..10)).must_equal false
@db.get((Sequel.pg_range(1..5, :int4range).op - (2..6)).adjacent_to(2..10)).must_equal true
@db.get((Sequel.pg_range(0..4, :int4range).op - (3..6)).adjacent_to(4..10)).must_equal false
@db.get(Sequel.pg_range(0..4, :int4range).op.lower).must_equal 0
@db.get(Sequel.pg_range(0..4, :int4range).op.upper).must_equal 5
@db.get(Sequel.pg_range(0..4, :int4range).op.isempty).must_equal false
@db.get(Sequel::Postgres::PGRange.empty(:int4range).op.isempty).must_equal true
@db.get(Sequel.pg_range(1..5, :numrange).op.lower_inc).must_equal true
@db.get(Sequel::Postgres::PGRange.new(1, 5, :exclude_begin=>true, :db_type=>:numrange).op.lower_inc).must_equal false
@db.get(Sequel.pg_range(1..5, :numrange).op.upper_inc).must_equal true
@db.get(Sequel.pg_range(1...5, :numrange).op.upper_inc).must_equal false
@db.get(Sequel::Postgres::PGRange.new(1, 5, :db_type=>:int4range).op.lower_inf).must_equal false
@db.get(Sequel::Postgres::PGRange.new(nil, 5, :db_type=>:int4range).op.lower_inf).must_equal true
@db.get(Sequel::Postgres::PGRange.new(1, 5, :db_type=>:int4range).op.upper_inf).must_equal false
@db.get(Sequel::Postgres::PGRange.new(1, nil, :db_type=>:int4range).op.upper_inf).must_equal true
end
end if DB.server_version >= 90200
describe 'PostgreSQL multirange types' do
before(:all) do
@db = DB
@ds = @db[:items]
@map = {:i4=>'int4multirange', :i8=>'int8multirange', :n=>'nummultirange', :d=>'datemultirange', :t=>'tsmultirange', :tz=>'tstzmultirange'}
@r = {
:i4=>[1...2, 4...5],
:i8=>[2...3, 5...6],
:n=>[BigDecimal('1.0')..BigDecimal('2.0'), BigDecimal('4.0')..BigDecimal('5.0')],
:d=>[Date.today...(Date.today+1), (Date.today+3)...(Date.today+4)],
:t=>[Time.local(2011, 1)..Time.local(2011, 2), Time.local(2011, 4)..Time.local(2011, 5)],
:tz=>[Time.local(2011, 1)..Time.local(2011, 2), Time.local(2011, 4)..Time.local(2011, 5)],
}
@pgr = {}
@pgra = {}
@r.each do |k, v|
type = @map[k]
val = @pgr[k] = Sequel.pg_multirange(v.map{|range| Sequel::Postgres::PGRange.new(range.begin, range.end, :exclude_end => range.exclude_end?, :db_type => type.sub('multi', ''))}, type)
@pgra[k] = Sequel.pg_array([val], type)
end
end
after do
@db.drop_table?(:items)
end
it 'insert and retrieve multirange type values' do
@db.create_table!(:items){int4multirange :i4; int8multirange :i8; nummultirange :n; datemultirange :d; tsmultirange :t; tstzmultirange :tz}
h = {}
@pgr.each{|k, v| h[k] = Sequel.cast(v, @map[k])}
@ds.insert(h)
@ds.count.must_equal 1
rs = @ds.all
rs.first.each do |k, v|
v.class.must_equal(Sequel::Postgres::PGMultiRange)
v.each{|range| range.must_be_kind_of(Sequel::Postgres::PGRange)}
v.must_be :==, @r[k]
end
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
end
it 'insert and retrieve arrays of multirange type values' do
@db.create_table!(:items){column :i4, 'int4multirange[]'; column :i8, 'int8multirange[]'; column :n, 'nummultirange[]'; column :d, 'datemultirange[]'; column :t, 'tsmultirange[]'; column :tz, 'tstzmultirange[]'}
@ds.insert(@pgra)
@ds.count.must_equal 1
rs = @ds.all
rs.first.each do |k, v|
v.class.must_equal(Sequel::Postgres::PGArray)
v.to_a.must_be_kind_of(Array)
v.first.class.must_equal(Sequel::Postgres::PGMultiRange)
v.first.first.class.must_equal(Sequel::Postgres::PGRange)
v.first.first.to_range.must_be_kind_of(Range)
v.must_be :==, @pgra[k]
v.first.must_be :==, @pgr[k]
v.first.must_be :==, @r[k]
end
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
end
it 'use multirange types in bound variables' do
@db.create_table!(:items){int4multirange :i4; int8multirange :i8; nummultirange :n; datemultirange :d; tsmultirange :t; tstzmultirange :tz}
h = {}
@r.keys.each{|k| h[k] = :"$#{k}"}
r2 = {}
@r.each{|k, v| r2[k] = Sequel.pg_multirange(v.map{|range| Range.new(range.begin+2, range.end+2)}, @map[k])}
@ds.call(:insert, @pgr, h)
@ds.first.must_be :==, @r
@ds.filter(h).call(:first, @pgr).must_be :==, @r
@ds.filter(h).call(:first, r2).must_be_nil
@ds.filter(h).call(:delete, @pgr).must_equal 1
@db.create_table!(:items){column :i4, 'int4multirange[]'; column :i8, 'int8multirange[]'; column :n, 'nummultirange[]'; column :d, 'datemultirange[]'; column :t, 'tsmultirange[]'; column :tz, 'tstzmultirange[]'}
@r.each{|k, v| r2[k] = Sequel.pg_array([Sequel.pg_multirange(v.map{|range| Range.new(range.begin+2, range.end+2)}, @map[k])])}
@ds.call(:insert, @pgra, h)
@ds.filter(h).call(:first, @pgra).each{|k, v| v.must_be :==, @pgra[k].to_a}
@ds.filter(h).call(:first, r2).must_be_nil
@ds.filter(h).call(:delete, @pgra).must_equal 1
end if uses_pg_or_jdbc
it 'parse multiranges containing empty ranges' do
@db.get(Sequel::Postgres::PGMultiRange.new([], 'int4multirange')).must_be_empty
@db.get(Sequel::Postgres::PGMultiRange.new([Sequel::Postgres::PGRange.empty('int4range')], 'int4multirange')).must_be_empty
end
it 'parse default values for schema' do
@db.create_table!(:items) do
Integer :j
int4multirange :i, :default=>Sequel.pg_multirange([1..4], :int4multirange)
end
@db.schema(:items)[0][1][:ruby_default].must_be_nil
@db.schema(:items)[1][1][:ruby_default].must_equal Sequel::Postgres::PGMultiRange.new([Sequel::Postgres::PGRange.new(1, 5, :exclude_end=>true, :db_type=>'int4range')], 'int4multirange')
@db.schema(:items)[1][1][:ruby_default].first.must_equal Sequel::Postgres::PGRange.new(1, 5, :exclude_end=>true, :db_type=>'int4range')
end
it 'with models' do
@db.create_table!(:items){primary_key :id; int4multirange :i4; int8multirange :i8; nummultirange :n; datemultirange :d; tsmultirange :t; tstzmultirange :tz}
c = Class.new(Sequel::Model(@db[:items]))
v = c.create(@pgr).values
v.delete(:id)
v.must_be :==, @pgr
@db.create_table!(:items){primary_key :id; column :i4, 'int4multirange[]'; column :i8, 'int8multirange[]'; column :n, 'nummultirange[]'; column :d, 'datemultirange[]'; column :t, 'tsmultirange[]'; column :tz, 'tstzmultirange[]'}
c = Class.new(Sequel::Model(@db[:items]))
v = c.create(@pgra).values
v.delete(:id)
v.each{|k,v1| v1.must_be :==, @pgra[k].to_a}
end
it 'operations/functions with pg_range_ops' do
Sequel.extension :pg_range_ops
mr = lambda do |range|
type = (Range === range ? 'int4multirange' : range.db_type.to_s.sub('range', 'multirange'))
Sequel.pg_multirange([range], type)
end
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(mr[2..4])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(mr[3..6])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(mr[0..6])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(Sequel.pg_range(1..5, :int4range))).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(Sequel.pg_range(3..6, :int4range))).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(Sequel.pg_range(0..6, :int4range))).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(3)).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contains(6)).must_equal false
@db.get(Sequel.pg_range(0..6, :int4range).op.contains(mr[Sequel.pg_range(1..5, :int4range)])).must_equal true
@db.get(Sequel.pg_range(3..6, :int4range).op.contains(mr[Sequel.pg_range(1..5, :int4range)])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contained_by(mr[0..6])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contained_by(mr[3..6])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contained_by(mr[2..4])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contained_by(Sequel.pg_range(0..6, :int4range))).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.contained_by(Sequel.pg_range(3..6, :int4range))).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.contained_by(mr[Sequel.pg_range(0..6, :int4range)])).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.contained_by(mr[Sequel.pg_range(3..6, :int4range)])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.overlaps(mr[5..6])).must_equal true
@db.get(mr[Sequel.pg_range(1...5, :int4range)].op.overlaps(mr[5..6])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.overlaps(Sequel.pg_range(5..6, :int4range))).must_equal true
@db.get(mr[Sequel.pg_range(1...5, :int4range)].op.overlaps(Sequel.pg_range(5..6, :int4range))).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.overlaps(mr[Sequel.pg_range(5..6, :int4range)])).must_equal true
@db.get(Sequel.pg_range(1...5, :int4range).op.overlaps(mr[Sequel.pg_range(5..6, :int4range)])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.left_of(mr[6..10])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.left_of(mr[5..10])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.left_of(mr[-1..0])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.left_of(mr[-1..3])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.left_of(Sequel.pg_range(6..10, :int4range))).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.left_of(Sequel.pg_range(5..10, :int4range))).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.left_of(mr[Sequel.pg_range(6..10, :int4range)])).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.left_of(mr[Sequel.pg_range(5..10, :int4range)])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.right_of(mr[6..10])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.right_of(mr[5..10])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.right_of(mr[-1..0])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.right_of(mr[-1..3])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.right_of(Sequel.pg_range(6..10, :int4range))).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.right_of(Sequel.pg_range(-1..0, :int4range))).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.right_of(mr[Sequel.pg_range(6..10, :int4range)])).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.right_of(mr[Sequel.pg_range(-1..0, :int4range)])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.ends_before(mr[6..10])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.ends_before(mr[5..10])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.ends_before(mr[-1..0])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.ends_before(mr[-1..3])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.ends_before(mr[-1..7])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.ends_before(Sequel.pg_range(5..10, :int4range))).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.ends_before(Sequel.pg_range(-1..0, :int4range))).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.ends_before(mr[Sequel.pg_range(5..10, :int4range)])).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.ends_before(mr[Sequel.pg_range(-1..0, :int4range)])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(mr[6..10])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(mr[5..10])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(mr[3..10])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(mr[-1..10])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(mr[-1..0])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(mr[-1..3])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(mr[-5..-1])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(Sequel.pg_range(3..10, :int4range))).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.starts_after(Sequel.pg_range(-1..10, :int4range))).must_equal true
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(mr[Sequel.pg_range(3..10, :int4range)])).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.starts_after(mr[Sequel.pg_range(-1..10, :int4range)])).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.adjacent_to(mr[6..10])).must_equal true
@db.get(mr[Sequel.pg_range(1...5, :int4range)].op.adjacent_to(mr[6..10])).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.adjacent_to(Sequel.pg_range(6..10, :int4range))).must_equal true
@db.get(mr[Sequel.pg_range(1...5, :int4range)].op.adjacent_to(Sequel.pg_range(6..10, :int4range))).must_equal false
@db.get(Sequel.pg_range(1..5, :int4range).op.adjacent_to(mr[Sequel.pg_range(6..10, :int4range)])).must_equal true
@db.get(Sequel.pg_range(1...5, :int4range).op.adjacent_to(mr[Sequel.pg_range(6..10, :int4range)])).must_equal false
@db.get((mr[Sequel.pg_range(1..5, :int4range)].op + (mr[6..10])).adjacent_to(mr[6..10])).must_equal false
@db.get((mr[Sequel.pg_range(1..5, :int4range)].op + (mr[6..10])).adjacent_to(mr[11..20])).must_equal true
@db.get((mr[Sequel.pg_range(1..5, :int4range)].op * (mr[2..6])).adjacent_to(mr[6..10])).must_equal true
@db.get((mr[Sequel.pg_range(1..4, :int4range)].op * (mr[2..6])).adjacent_to(mr[6..10])).must_equal false
@db.get((mr[Sequel.pg_range(1..5, :int4range)].op - (mr[2..6])).adjacent_to(mr[2..10])).must_equal true
@db.get((mr[Sequel.pg_range(0..4, :int4range)].op - (mr[3..6])).adjacent_to(mr[4..10])).must_equal false
@db.get(mr[Sequel.pg_range(0..4, :int4range)].op.lower).must_equal 0
@db.get(mr[Sequel.pg_range(0..4, :int4range)].op.upper).must_equal 5
@db.get(mr[Sequel.pg_range(0..4, :int4range)].op.isempty).must_equal false
@db.get(mr[Sequel::Postgres::PGRange.empty(:int4range)].op.isempty).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :numrange)].op.lower_inc).must_equal true
@db.get(mr[Sequel::Postgres::PGRange.new(1, 5, :exclude_begin=>true, :db_type=>:numrange)].op.lower_inc).must_equal false
@db.get(mr[Sequel.pg_range(1..5, :numrange)].op.upper_inc).must_equal true
@db.get(mr[Sequel.pg_range(1...5, :numrange)].op.upper_inc).must_equal false
@db.get(mr[Sequel::Postgres::PGRange.new(1, 5, :db_type=>:int4range)].op.lower_inf).must_equal false
@db.get(mr[Sequel::Postgres::PGRange.new(nil, 5, :db_type=>:int4range)].op.lower_inf).must_equal true
@db.get(mr[Sequel::Postgres::PGRange.new(1, 5, :db_type=>:int4range)].op.upper_inf).must_equal false
@db.get(mr[Sequel::Postgres::PGRange.new(1, nil, :db_type=>:int4range)].op.upper_inf).must_equal true
@db.get(mr[Sequel.pg_range(1..5, :int4range)].op.range_merge.adjacent_to(mr[6..10])).must_equal true
@db.get(Sequel.pg_range(1...5, :int4range).op.multirange.adjacent_to(mr[6..10])).must_equal false
@db.get(Sequel.pg_range(1...5, :int4range).op.multirange.unnest).to_range.must_equal 1...5
end
end if DB.server_version >= 140000
describe 'PostgreSQL interval types' do
before(:all) do
@db = DB
@ds = @db[:items]
m = Sequel::Postgres::IntervalDatabaseMethods::Parser
@year = m::SECONDS_PER_YEAR
@month = m::SECONDS_PER_MONTH
end
after do
@db.drop_table?(:items)
end
it 'insert and retrieve interval values' do
@db.create_table!(:items){interval :i}
[
['0', '00:00:00', 0, []],
['1', '00:00:01', 1, [[:seconds, 1]]],
['1 microsecond', '00:00:00.000001', 0.000001, [[:seconds, 0.000001]]],
['1 millisecond', '00:00:00.001', 0.001, [[:seconds, 0.001]]],
['1 second', '00:00:01', 1, [[:seconds, 1]]],
['1 minute', '00:01:00', 60, [[:seconds, 60]]],
['1 hour', '01:00:00', 3600, [[:seconds, 3600]]],
['123000 hours', '123000:00:00', 442800000, [[:seconds, 442800000]]],
['1 day', '1 day', 86400, [[:days, 1]]],
['1 week', '7 days', 86400*7, [[:days, 7]]],
['1 month', '1 mon', @month, [[:months, 1]]],
['1 year', '1 year', @year, [[:years, 1]]],
['1 decade', '10 years', @year*10, [[:years, 10]]],
['1 century', '100 years', @year*100, [[:years, 100]]],
['1 millennium', '1000 years', @year*1000, [[:years, 1000]]],
['1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds', '1 year 2 mons 25 days 05:06:07', @year + 2*@month + 3*86400*7 + 4*86400 + 5*3600 + 6*60 + 7, [[:years, 1], [:months, 2], [:days, 25], [:seconds, 18367]]],
['-1 year +2 months -3 weeks +4 days -5 hours +6 minutes -7 seconds', '-10 mons -17 days -04:54:07', -10*@month - 3*86400*7 + 4*86400 - 5*3600 + 6*60 - 7, [[:months, -10], [:days, -17], [:seconds, -17647]]],
['+2 years -1 months +3 weeks -4 days +5 hours -6 minutes +7 seconds', '1 year 11 mons 17 days 04:54:07', @year + 11*@month + 3*86400*7 - 4*86400 + 5*3600 - 6*60 + 7, [[:years, 1], [:months, 11], [:days, 17], [:seconds, 17647]]],
].each do |instr, outstr, value, parts|
@ds.insert(instr)
@ds.count.must_equal 1
@ds.get(Sequel.cast(:i, String)).must_equal outstr
rs = @ds.all
rs.first[:i].is_a?(ActiveSupport::Duration).must_equal true
rs.first[:i].must_equal ActiveSupport::Duration.new(value, parts)
rs.first[:i].parts.sort_by{|k,v| k.to_s}.reject{|k,v| v == 0}.must_equal parts.sort_by{|k,v| k.to_s}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
@ds.delete
end
end
it 'insert and retrieve interval array values' do
@db.create_table!(:items){column :i, 'interval[]'}
@ds.insert(Sequel.pg_array(['1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds'], 'interval'))
@ds.count.must_equal 1
rs = @ds.all
rs.first[:i].is_a?(Sequel::Postgres::PGArray).must_equal true
rs.first[:i].first.is_a?(ActiveSupport::Duration).must_equal true
rs.first[:i].first.must_equal ActiveSupport::Duration.new(@year + 2*@month + 3*86400*7 + 4*86400 + 5*3600 + 6*60 + 7, [[:years, 1], [:months, 2], [:days, 25], [:seconds, 18367]])
rs.first[:i].first.parts.sort_by{|k,v| k.to_s}.must_equal [[:years, 1], [:months, 2], [:days, 25], [:seconds, 18367]].sort_by{|k,v| k.to_s}
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
end
it 'use intervals in bound variables' do
@db.create_table!(:items){interval :i}
@ds.insert('1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds')
d = @ds.get(:i)
@ds.delete
@ds.call(:insert, {:i=>d}, {:i=>:$i})
@ds.get(:i).must_equal d
@ds.filter(:i=>:$i).call(:first, :i=>d).must_equal(:i=>d)
@ds.filter(:i=>Sequel.cast(:$i, :interval)).call(:first, :i=>'0').must_be_nil
@ds.filter(:i=>:$i).call(:delete, :i=>d).must_equal 1
@db.create_table!(:items){column :i, 'interval[]'}
@ds.call(:insert, {:i=>[d]}, {:i=>:$i})
@ds.filter(:i=>:$i).call(:first, :i=>[d]).must_equal(:i=>[d])
@ds.filter(:i=>:$i).call(:first, :i=>[]).must_be_nil
@ds.filter(:i=>:$i).call(:delete, :i=>[d]).must_equal 1
end if uses_pg_or_jdbc
it 'parse default values for schema' do
@db.create_table!(:items) do
Integer :j
interval :i, :default=>ActiveSupport::Duration.new(3*86400, :days=>3)
end
@db.schema(:items)[0][1][:ruby_default].must_be_nil
@db.schema(:items)[1][1][:ruby_default].must_equal ActiveSupport::Duration.new(3*86400, :days=>3)
end
it 'correctly handles round tripping interval values' do
@db.create_table!(:items) do
interval :i
interval :j
end
d = Sequel.cast(Date.new(2020, 2, 1), Date)
{'30 days'=>Date.new(2020, 3, 2), '1 month'=>Date.new(2020, 3, 1)}.each do |interval, result|
@ds.insert(:i=>interval)
@ds.update(:j=>@ds.get(:i))
@ds.where(:i=>:j).count.must_equal 1
@ds.get{(d+:i).cast(Date).as(:v)}.must_equal result
@ds.get{(d+:j).cast(Date).as(:v)}.must_equal result
@ds.delete
end
end
it 'with models' do
@db.create_table!(:items) do
primary_key :id
interval :i
end
c = Class.new(Sequel::Model(@db[:items]))
v = c.create(:i=>'1 year 2 mons 25 days 05:06:07').i
v.is_a?(ActiveSupport::Duration).must_equal true
v.must_equal ActiveSupport::Duration.new(@year + 2*@month + 3*86400*7 + 4*86400 + 5*3600 + 6*60 + 7, [[:years, 1], [:months, 2], [:days, 25], [:seconds, 18367]])
v.parts.sort_by{|k,_| k.to_s}.must_equal [[:years, 1], [:months, 2], [:days, 25], [:seconds, 18367]].sort_by{|k,_| k.to_s}
end
end if (begin require 'active_support'; require 'active_support/duration'; require 'active_support/inflector'; require 'active_support/core_ext/string/inflections'; true; rescue LoadError; false end)
describe 'PostgreSQL row-valued/composite types' do
before(:all) do
@db = DB
Sequel.extension :pg_array_ops, :pg_row_ops
@ds = @db[:person]
@db.drop_table?(:company, :person, :address)
@db.create_table(:address) do
String :street
String :city
String :zip
end
@db.create_table(:person) do
Integer :id
address :address
end
@db.create_table(:company) do
Integer :id
column :employees, 'person[]'
end
oids = @db.conversion_procs.keys
@db.register_row_type(:address)
@db.register_row_type(Sequel.qualify(:public, :person))
@db.register_row_type(Sequel[:public][:company])
@new_oids = @db.conversion_procs.keys - oids
end
after(:all) do
@new_oids.each{|oid| @db.conversion_procs.delete(oid)}
@db.row_types.clear
@db.drop_table?(:company, :person, :address)
end
after do
[:company, :person, :address].each{|t| @db[t].delete}
end
it 'insert and retrieve row types' do
@ds.insert(:id=>1, :address=>Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345']))
@ds.count.must_equal 1
# Single row valued type
rs = @ds.all
v = rs.first[:address]
v.class.superclass.must_equal(Sequel::Postgres::PGRow::HashRow)
v.to_hash.must_be_kind_of(Hash)
v.to_hash.must_equal(:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345')
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
# Nested row value type
p = @ds.get(:person)
p[:id].must_equal 1
p[:address].must_equal v
end
it 'insert and retrieve row types containing domains' do
begin
@db << "DROP DOMAIN IF EXISTS positive_integer CASCADE"
@db << "CREATE DOMAIN positive_integer AS integer CHECK (VALUE > 0)"
@db.create_table!(:domain_check) do
positive_integer :id
end
@db.register_row_type(:domain_check)
@db.get(@db.row_type(:domain_check, [1])).must_equal(:id=>1)
@db.register_row_type(Sequel[:public][:domain_check])
@db.get(@db.row_type(Sequel[:public][:domain_check], [1])).must_equal(:id=>1)
@db.get(@db.row_type(Sequel.qualify(:public, :domain_check), [1])).must_equal(:id=>1)
ensure
@db.drop_table(:domain_check)
@db << "DROP DOMAIN positive_integer"
end
end
it 'insert and retrieve arrays of row types' do
@ds = @db[:company]
@ds.insert(:id=>1, :employees=>Sequel.pg_array([@db.row_type(:person, [1, Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345'])])]))
@ds.count.must_equal 1
v = @ds.get(:company)
v.class.superclass.must_equal(Sequel::Postgres::PGRow::HashRow)
v.to_hash.must_be_kind_of(Hash)
v[:id].must_equal 1
employees = v[:employees]
employees.class.must_equal(Sequel::Postgres::PGArray)
employees.to_a.must_be_kind_of(Array)
employees.must_equal [{:id=>1, :address=>{:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345'}}]
@ds.delete
@ds.insert(v[:id], v[:employees])
@ds.get(:company).must_equal v
end
it 'use row types in bound variables' do
@ds.call(:insert, {:address=>Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345'])}, {:address=>:$address, :id=>1})
@ds.get(:address).must_equal(:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345')
@ds.filter(:address=>Sequel.cast(:$address, :address)).call(:first, :address=>Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345']))[:id].must_equal 1
@ds.filter(:address=>Sequel.cast(:$address, :address)).call(:first, :address=>Sequel.pg_row(['123 Sesame St', 'Somewhere', '12356'])).must_be_nil
@ds.delete
@ds.call(:insert, {:address=>Sequel.pg_row([nil, nil, nil])}, {:address=>:$address, :id=>1})
@ds.get(:address).must_equal(:street=>nil, :city=>nil, :zip=>nil)
end if uses_pg_or_jdbc
it 'use arrays of row types in bound variables' do
@ds = @db[:company]
@ds.call(:insert, {:employees=>Sequel.pg_array([@db.row_type(:person, [1, Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345'])])])}, {:employees=>:$employees, :id=>1})
@ds.get(:company).must_equal(:id=>1, :employees=>[{:id=>1, :address=>{:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345'}}])
@ds.filter(:employees=>Sequel.cast(:$employees, 'person[]')).call(:first, :employees=>Sequel.pg_array([@db.row_type(:person, [1, Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345'])])]))[:id].must_equal 1
@ds.filter(:employees=>Sequel.cast(:$employees, 'person[]')).call(:first, :employees=>Sequel.pg_array([@db.row_type(:person, [1, Sequel.pg_row(['123 Sesame St', 'Somewhere', '12356'])])])).must_be_nil
@ds.delete
@ds.call(:insert, {:employees=>Sequel.pg_array([@db.row_type(:person, [1, Sequel.pg_row([nil, nil, nil])])])}, {:employees=>:$employees, :id=>1})
@ds.get(:employees).must_equal [{:address=>{:city=>nil, :zip=>nil, :street=>nil}, :id=>1}]
end if uses_pg_or_jdbc
it 'operations/functions with pg_row_ops' do
@ds.insert(:id=>1, :address=>Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345']))
@ds.get(Sequel.pg_row(:address)[:street]).must_equal '123 Sesame St'
@ds.get(Sequel.pg_row(:address)[:city]).must_equal 'Somewhere'
@ds.get(Sequel.pg_row(:address)[:zip]).must_equal '12345'
@ds = @db[:company]
@ds.insert(:id=>1, :employees=>Sequel.pg_array([@db.row_type(:person, [1, Sequel.pg_row(['123 Sesame St', 'Somewhere', '12345'])])]))
@ds.get(Sequel.pg_row(:company)[:id]).must_equal 1
@ds.get(Sequel.pg_row(:company)[:employees]).must_equal [{:id=>1, :address=>{:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345'}}]
@ds.get(Sequel.pg_row(:company)[:employees][1]).must_equal(:id=>1, :address=>{:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345'})
@ds.get(Sequel.pg_row(:company)[:employees][1][:address]).must_equal(:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345')
@ds.get(Sequel.pg_row(:company)[:employees][1][:id]).must_equal 1
@ds.get(Sequel.pg_row(:company)[:employees][1][:address][:street]).must_equal '123 Sesame St'
@ds.get(Sequel.pg_row(:company)[:employees][1][:address][:city]).must_equal 'Somewhere'
@ds.get(Sequel.pg_row(:company)[:employees][1][:address][:zip]).must_equal '12345'
@db.get(@ds.get(Sequel.pg_row(:company)[:employees][1][:address]).op[:street]).must_equal '123 Sesame St'
if DB.server_version >= 130000
@db.get(Sequel.pg_row([1,2,3]).op[:f1]).must_equal 1
end
end
describe "#splat and #*" do
before(:all) do
@db.create_table!(:a){Integer :a}
@db.create_table!(:b){a :b; Integer :a}
@db.register_row_type(:a)
@db.register_row_type(:b)
@db[:b].insert(:a=>1, :b=>@db.row_type(:a, [2]))
end
after(:all) do
@db.drop_table?(:b, :a)
end
it "splat should reference the table type" do
@db[:b].select(:a).first.must_equal(:a=>1)
@db[:b].select(Sequel[:b][:a]).first.must_equal(:a=>1)
@db[:b].select(Sequel.pg_row(:b)[:a]).first.must_equal(:a=>2)
@db[:b].select(Sequel.pg_row(:b).splat[:a]).first.must_equal(:a=>1)
@db[:b].select(:b).first.must_equal(:b=>{:a=>2})
@db[:b].select(Sequel.pg_row(:b).splat).first.must_equal(:a=>1, :b=>{:a=>2})
@db[:b].select(Sequel.pg_row(:b).splat(:b)).first.must_equal(:b=>{:a=>1, :b=>{:a=>2}})
end
it "* should expand the table type into separate columns" do
ds = @db[:b].select(Sequel.pg_row(:b).splat(:b)).from_self(:alias=>:t)
ds.first.must_equal(:b=>{:a=>1, :b=>{:a=>2}})
ds.select(Sequel.pg_row(:b).*).first.must_equal(:a=>1, :b=>{:a=>2})
ds.select(Sequel.pg_row(:b)[:b]).first.must_equal(:b=>{:a=>2})
ds.select(Sequel.pg_row(Sequel[:t][:b]).*).first.must_equal(:a=>1, :b=>{:a=>2})
ds.select(Sequel.pg_row(Sequel[:t][:b])[:b]).first.must_equal(:b=>{:a=>2})
ds.select(Sequel.pg_row(:b)[:a]).first.must_equal(:a=>1)
ds.select(Sequel.pg_row(Sequel[:t][:b])[:a]).first.must_equal(:a=>1)
end
end
describe "with models" do
before(:all) do
class Address < Sequel::Model(:address)
plugin :pg_row
end
class Person < Sequel::Model(:person)
plugin :pg_row
end
class Company < Sequel::Model(:company)
plugin :pg_row
end
@a = Address.new(:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345')
@es = Sequel.pg_array([Person.new(:id=>1, :address=>@a)])
end
after(:all) do
Object.send(:remove_const, :Address) rescue nil
Object.send(:remove_const, :Person) rescue nil
Object.send(:remove_const, :Company) rescue nil
end
it 'create model objects whose values are model instances using pg_row' do
person = Person.create(:id=>1, :address=>@a)
person.address.must_equal @a
Person.count.must_equal 1
company = Company.create(:employees=>[person])
company.employees.must_equal [person]
Company.count.must_equal 1
end
it 'insert and retrieve row types as model objects' do
@ds.insert(:id=>1, :address=>@a)
@ds.count.must_equal 1
# Single row valued type
rs = @ds.all
v = rs.first[:address]
v.must_be_kind_of(Address)
v.must_equal @a
@ds.delete
@ds.insert(rs.first)
@ds.all.must_equal rs
# Nested row value type
p = @ds.get(:person)
p.must_be_kind_of(Person)
p.id.must_equal 1
p.address.must_be_kind_of(Address)
p.address.must_equal @a
end
it 'insert and retrieve arrays of row types as model objects' do
@ds = @db[:company]
@ds.insert(:id=>1, :employees=>@es)
@ds.count.must_equal 1
v = @ds.get(:company)
v.must_be_kind_of(Company)
v.id.must_equal 1
employees = v[:employees]
employees.class.must_equal(Sequel::Postgres::PGArray)
employees.to_a.must_be_kind_of(Array)
employees.must_equal @es
@ds.delete
@ds.insert(v.id, v.employees)
@ds.get(:company).must_equal v
end
it 'use model objects in bound variables' do
@ds.call(:insert, {:address=>@a}, {:address=>:$address, :id=>1})
@ds.get(:address).must_equal @a
@ds.filter(:address=>Sequel.cast(:$address, :address)).call(:first, :address=>@a)[:id].must_equal 1
@ds.filter(:address=>Sequel.cast(:$address, :address)).call(:first, :address=>Address.new(:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12356')).must_be_nil
end if uses_pg_or_jdbc
it 'use arrays of model objects in bound variables' do
@ds = @db[:company]
@ds.call(:insert, {:employees=>@es}, {:employees=>:$employees, :id=>1})
@ds.get(:company).must_equal Company.new(:id=>1, :employees=>@es)
@ds.filter(:employees=>Sequel.cast(:$employees, 'person[]')).call(:first, :employees=>@es)[:id].must_equal 1
@ds.filter(:employees=>Sequel.cast(:$employees, 'person[]')).call(:first, :employees=>Sequel.pg_array([@db.row_type(:person, [1, Sequel.pg_row(['123 Sesame St', 'Somewhere', '12356'])])])).must_be_nil
end if uses_pg_or_jdbc
it 'model typecasting' do
a = Address.new(:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345')
o = Person.create(:id=>1, :address=>['123 Sesame St', 'Somewhere', '12345'])
o.address.must_equal a
o = Person.create(:id=>1, :address=>{:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345'})
o.address.must_equal a
o = Person.create(:id=>1, :address=>a)
o.address.must_equal a
e = Person.new(:id=>1, :address=>a)
o = Company.create(:id=>1, :employees=>[{:id=>1, :address=>{:street=>'123 Sesame St', :city=>'Somewhere', :zip=>'12345'}}])
o.employees.must_equal [e]
o = Company.create(:id=>1, :employees=>[e])
o.employees.must_equal [e]
end
end
end
describe 'pg_static_cache_updater extension' do
before(:all) do
@db = DB
@db.extension :pg_static_cache_updater
@db.drop_function(@db.default_static_cache_update_name, :cascade=>true, :if_exists=>true)
@db.create_static_cache_update_function
@db.create_table!(:things) do
primary_key :id
String :name
end
@Thing = Class.new(Sequel::Model(:things))
@Thing.plugin :static_cache
@db.create_static_cache_update_trigger(:things)
end
after(:all) do
@db.drop_table(:things)
@db.drop_function(@db.default_static_cache_update_name)
end
it "should reload model static cache when underlying table changes" do
@Thing.all.must_equal []
q = Queue.new
q1 = Queue.new
@db.listen_for_static_cache_updates(@Thing, :timeout=>0, :loop=>proc{q.push(nil); q1.pop.call}, :before_thread_exit=>proc{q.push(nil)})
q.pop
q1.push(proc{@db[:things].insert(1, 'A')})
q.pop
@Thing.all.must_equal [@Thing.load(:id=>1, :name=>'A')]
q1.push(proc{@db[:things].update(:name=>'B')})
q.pop
@Thing.all.must_equal [@Thing.load(:id=>1, :name=>'B')]
q1.push(proc{@db[:things].delete})
q.pop
@Thing.all.must_equal []
q1.push(proc{throw :stop})
q.pop
end
end if uses_pg && DB.server_version >= 90000
describe 'PostgreSQL enum types' do
before do
@db = DB
@initial_enum_values = %w'a b c d'
@db.create_enum(:test_enum, @initial_enum_values)
@db.create_table!(:test_enumt) do
test_enum :t
end
end
after do
@db.drop_table?(:test_enumt)
@db.drop_enum(:test_enum)
end
it "should return correct entries in the schema" do
s = @db.schema(:test_enumt)
s.first.last[:type].must_equal :enum
s.first.last[:enum_values].must_equal @initial_enum_values
end
it "should add array parsers for enum values" do
@db.get(Sequel.pg_array(%w'a b', :test_enum)).must_equal %w'a b'
end
it "should set up model typecasting correctly" do
c = Class.new(Sequel::Model(:test_enumt))
o = c.new
o.t = :a
o.t.must_equal 'a'
end
it "should add values to existing enum" do
@db.add_enum_value(:test_enum, 'e')
@db.add_enum_value(:test_enum, 'f', :after=>'a')
@db.add_enum_value(:test_enum, 'g', :before=>'b')
@db.add_enum_value(:test_enum, 'a', :if_not_exists=>true) if @db.server_version >= 90300
@db.schema(:test_enumt, :reload=>true).first.last[:enum_values].must_equal %w'a f g b c d e'
end if DB.server_version >= 90100
it "should rename existing enum" do
@db.rename_enum(:test_enum, :new_enum)
@db.schema(:test_enumt, :reload=>true).first.last[:db_type].must_equal 'new_enum'
@db.schema(:test_enumt, :reload=>true).first.last[:enum_values].must_equal @initial_enum_values
@db.rename_enum(:new_enum, :test_enum)
end
it "should rename enum values" do
@db.rename_enum_value(:test_enum, :b, :x)
new_enum_values = @initial_enum_values
new_enum_values[new_enum_values.index('b')] = 'x'
@db.schema(:test_enumt, :reload=>true).first.last[:enum_values].must_equal new_enum_values
@db.rename_enum_value(:test_enum, :x, :b)
end if DB.server_version >= 100000
end
describe "PostgreSQL stored procedures for datasets" do
before do
@db = DB
@db.create_table!(:items) do
primary_key :id
integer :numb
end
@db.execute(<<-SQL)
create or replace function insert_item(numb bigint)
returns items.id%type
as $$
declare
l_id items.id%type;
begin
l_id := 1;
insert into items(id, numb) values(l_id, numb);
return l_id;
end;
$$ language plpgsql;
SQL
@ds = @db[:items]
end
after do
@db.drop_function("insert_item", :if_exists=>true)
@db.drop_table?(:items)
end
it "should correctly call stored procedure for inserting record" do
result = @ds.call_sproc(:insert, :insert_item, 100)
result.must_be_nil
@ds.call(:all).must_equal [{:id=>1, :numb=>100}]
end
end if DB.adapter_scheme == :jdbc
describe "pg_auto_constraint_validations plugin" do
before(:all) do
@db = DB
@db.create_table!(:test1) do
Integer :id, :primary_key=>true
Integer :i, :unique=>true, :null=>false
constraint :valid_i, Sequel[:i] < 10
constraint(:valid_i_id, Sequel[:i] + Sequel[:id] < 20)
end
@db.run "CREATE OR REPLACE FUNCTION valid_test1(t1 test1) RETURNS boolean AS 'SELECT t1.i != -100' LANGUAGE SQL;"
@db.alter_table(:test1) do
add_constraint(:valid_test1, Sequel.function(:valid_test1, :test1))
end
@db.create_table!(:test2) do
Integer :test2_id, :primary_key=>true
foreign_key :test1_id, :test1
index [:test1_id], :unique=>true, :where=>(Sequel[:test1_id] < 10)
end
@c1 = Sequel::Model(:test1)
@c2 = Sequel::Model(:test2)
@c1.plugin :update_primary_key
@c1.plugin :pg_auto_constraint_validations
@c2.plugin :pg_auto_constraint_validations
@c1.unrestrict_primary_key
@c2.unrestrict_primary_key
end
before do
@c2.dataset.delete
@c1.dataset.delete
@c1.insert(:id=>1, :i=>2)
@c2.insert(:test2_id=>3, :test1_id=>1)
end
after(:all) do
@db.run "ALTER TABLE test1 DROP CONSTRAINT IF EXISTS valid_test1"
@db.run "DROP FUNCTION IF EXISTS valid_test1(test1)"
@db.drop_table?(:test2, :test1)
end
it "should handle check constraint failures as validation errors when creating" do
o = @c1.new(:id=>5, :i=>12)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is invalid'])
end
it "should handle check constraint failures where the columns are unknown, if columns are explicitly specified" do
o = @c1.new(:id=>5, :i=>-100)
proc{o.save}.must_raise Sequel::CheckConstraintViolation
@c1.pg_auto_constraint_validation_override(:valid_test1, :i, "should not be -100")
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['should not be -100'])
end
it "should handle check constraint failures as validation errors when updating" do
o = @c1.new(:id=>5, :i=>3)
o.save
proc{o.update(:i=>12)}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is invalid'])
end
it "should handle unique constraint failures as validation errors when creating" do
o = @c1.new(:id=>5, :i=>2)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is already taken'])
end
it "should handle unique constraint failures as validation errors when updating" do
o = @c1.new(:id=>5, :i=>3)
o.save
proc{o.update(:i=>2)}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is already taken'])
end
it "should handle unique constraint failures as validation errors for partial unique indexes" do
@c1.create(:id=>2, :i=>3)
@c2.create(:test2_id=>6, :test1_id=>2)
o = @c2.new(:test2_id=>5, :test1_id=>2)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:test1_id=>['is already taken'])
end
it "should handle not null constraint failures as validation errors when creating" do
o = @c1.new(:id=>5)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is not present'])
end
it "should handle not null constraint failures as validation errors when updating" do
o = @c1.new(:id=>5, :i=>3)
o.save
proc{o.update(:i=>nil)}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is not present'])
end
it "should handle foreign key constraint failures as validation errors when creating" do
o = @c2.new(:test2_id=>4, :test1_id=>2)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:test1_id=>['is invalid'])
end
it "should handle foreign key constraint failures as validation errors when updating" do
o = @c2.first
proc{o.update(:test1_id=>2)}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:test1_id=>['is invalid'])
end
it "should handle foreign key constraint failures in other tables as validation errors when updating" do
o = @c1[1]
proc{o.update(:id=>2)}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:id=>['cannot be changed currently'])
end
it "should handle multi-column constraint failures as validation errors" do
c = Class.new(@c1)
o = c.new(:id=>18, :i=>8)
proc{o.save}.must_raise Sequel::ValidationFailed
[{[:i, :id]=>['is invalid']}, {[:id, :i]=>['is invalid']}].must_include o.errors
end
it "should handle multi-column constraint failures as validation errors when using the error_splitter plugin" do
c = Class.new(@c1)
c.plugin :error_splitter
o = c.new(:id=>18, :i=>8)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is invalid'], :id=>['is invalid'])
end
it "should handle dumping cached metadata and loading metadata from cache" do
cache_file = "spec/files/pgacv-#{$$}.cache"
begin
c = Class.new(Sequel::Model)
c.plugin :pg_auto_constraint_validations, :cache_file=>cache_file
c1 = Class.new(c)
def c1.name; 'Foo' end
c1.dataset = DB[:test1]
c2 = Class.new(c)
def c2.name; 'Bar' end
c2.dataset = DB[:test2]
c1.unrestrict_primary_key
c2.unrestrict_primary_key
o = c1.new(:id=>5, :i=>12)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is invalid'])
o = c2.new(:test2_id=>4, :test1_id=>2)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:test1_id=>['is invalid'])
c.dump_pg_auto_constraint_validations_cache
c = Class.new(Sequel::Model)
c.plugin :pg_auto_constraint_validations, :cache_file=>cache_file
c1 = Class.new(c)
def c1.name; 'Foo' end
c1.dataset = DB[:test1]
c2 = Class.new(c)
def c2.name; 'Bar' end
c2.dataset = DB[:test2]
c1.unrestrict_primary_key
c2.unrestrict_primary_key
o = c1.new(:id=>5, :i=>12)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:i=>['is invalid'])
o = c2.new(:test2_id=>4, :test1_id=>2)
proc{o.save}.must_raise Sequel::ValidationFailed
o.errors.must_equal(:test1_id=>['is invalid'])
ensure
File.delete(cache_file) if File.file?(cache_file)
end
end
end if DB.respond_to?(:error_info) && DB.server_version >= 90300
describe "Common Table Expression SEARCH" do
before(:all) do
@db = DB
@db.create_table!(:i1){Integer :id; Integer :parent_id}
@ds = @db[:i1]
@ds.insert(:id=>1)
@ds.insert(:id=>2)
@ds.insert(:id=>3, :parent_id=>1)
@ds.insert(:id=>4, :parent_id=>1)
@ds.insert(:id=>5, :parent_id=>3)
@ds.insert(:id=>6, :parent_id=>5)
end
after(:all) do
@db.drop_table?(:i1)
end
it "should support :search option for depth/breadth first ordering" do
@db[:t].with_recursive(:t, @ds.filter(:parent_id=>nil), @ds.join(:t, :id=>:parent_id).select_all(:i1), :search=>{:by=>:id}).
order(:ordercol, :id).
select_map([:id, :ordercol]).must_equal [
[1, [["1"]]],
[3, [["1"], ["3"]]],
[5, [["1"], ["3"], ["5"]]],
[6, [["1"], ["3"], ["5"], ["6"]]],
[4, [["1"], ["4"]]],
[2, [["2"]]]
]
@db[:t].with_recursive(:t, @ds.filter(:parent_id=>nil), @ds.join(:t, :id=>:parent_id).select_all(:i1), :search=>{:type=>:breadth, :by=>[:id, :parent_id], :set=>:c}, :args=>[:id, :parent_id]).
order(:c, :id).
select_map([:id, :c]).must_equal [
[1, ["0", "1", nil]],
[2, ["0", "2", nil]],
[3, ["1", "3", "1"]],
[4, ["1", "4", "1"]],
[5, ["2", "5", "3"]],
[6, ["3", "6", "5"]]
]
end
end if DB.server_version >= 140000
describe "Common Table Expression CYCLE" do
before(:all) do
@db = DB
@db.create_table!(:i1){Integer :id; Integer :parent_id}
@ds = @db[:i1]
@ds.insert(:id=>1, :parent_id=>6)
@ds.insert(:id=>2)
@ds.insert(:id=>3, :parent_id=>1)
@ds.insert(:id=>4, :parent_id=>1)
@ds.insert(:id=>5, :parent_id=>3)
@ds.insert(:id=>6, :parent_id=>5)
end
after(:all) do
@db.drop_table?(:i1)
end
it "should support :cycle option for detecting cycles" do
@db[:t].with_recursive(:t, @ds.filter(:id=>[1,2]), @ds.join(:t, :id=>:parent_id).select_all(:i1), :cycle=>{:columns=>:id}, :args=>[:id, :parent_id]).
order(:id).
exclude(:is_cycle).
select_map([:id, :is_cycle, :path]).must_equal [
[1, false, [["1"]]],
[2, false, [["2"]]],
[3, false, [["1"], ["3"]]],
[4, false, [["1"], ["4"]]],
[5, false, [["1"], ["3"], ["5"]]],
[6, false, [["1"], ["3"], ["5"], ["6"]]]
]
@db[:t].with_recursive(:t, @ds.filter(:id=>[1,2]), @ds.join(:t, :id=>:parent_id).select_all(:i1), :cycle=>{:columns=>[:id, :parent_id], :path_column=>:pc, :cycle_column=>:cc, :cycle_value=>1, :noncycle_value=>0}).
order(:id).
where(:cc=>0).
select_map([:id, :cc, :pc]).must_equal [
[1, 0, [["1", "6"]]],
[2, 0, [["2", nil]]],
[3, 0, [["1", "6"], ["3", "1"]]],
[4, 0, [["1", "6"], ["4", "1"]]],
[5, 0, [["1", "6"], ["3", "1"], ["5", "3"]]],
[6, 0, [["1", "6"], ["3", "1"], ["5", "3"], ["6", "5"]]]
]
end
it "should support both :search and :cycle options together" do
@db[:t].with_recursive(:t, @ds.filter(:id=>[1,2]), @ds.join(:t, :id=>:parent_id).select_all(:i1), :cycle=>{:columns=>:id}, :search=>{:by=>:id}, :args=>[:id, :parent_id]).
order(:ordercol, :id).
exclude(:is_cycle).
select_map([:id, :is_cycle, :path, :ordercol]).must_equal [
[1, false, [["1"]], [["1"]]],
[3, false, [["1"], ["3"]], [["1"], ["3"]]],
[5, false, [["1"], ["3"], ["5"]], [["1"], ["3"], ["5"]]],
[6, false, [["1"], ["3"], ["5"], ["6"]], [["1"], ["3"], ["5"], ["6"]]],
[4, false, [["1"], ["4"]], [["1"], ["4"]]],
[2, false, [["2"]], [["2"]]]
]
@db[:t].with_recursive(:t, @ds.filter(:id=>[1,2]), @ds.join(:t, :id=>:parent_id).select_all(:i1), :cycle=>{:columns=>:id}, :search=>{:type=>:breadth, :by=>:id}, :args=>[:id, :parent_id]).
order(:ordercol, :id).
exclude(:is_cycle).
select_map([:id, :is_cycle, :path, :ordercol]).must_equal [
[1, false, [["1"]], ["0", "1"]],
[2, false, [["2"]], ["0", "2"]],
[3, false, [["1"], ["3"]], ["1", "3"]],
[4, false, [["1"], ["4"]], ["1", "4"]],
[5, false, [["1"], ["3"], ["5"]], ["2", "5"]],
[6, false, [["1"], ["3"], ["5"], ["6"]], ["3", "6"]]
]
end
end if DB.server_version >= 140000
describe "MERGE DO NOTHING" do
before(:all) do
@db = DB
@db.create_table!(:m1){Integer :i1; Integer :a}
@db.create_table!(:m2){Integer :i2; Integer :b}
@m1 = @db[:m1]
@m2 = @db[:m2]
end
after do
@m1.delete
@m2.delete
end
after(:all) do
@db.drop_table?(:m1, :m2)
end
it "should allow inserts, updates, do nothings, and deletes based on conditions in a single MERGE statement" do
ds = @m1.
merge_using(:m2, :i1=>:i2).
merge_do_nothing_when_not_matched{b > 50}.
merge_insert(:i1=>Sequel[:i2], :a=>Sequel[:b]+11).
merge_do_nothing_when_matched{a > 50}.
merge_delete{a > 30}.
merge_update(:i1=>Sequel[:i1]+:i2+10, :a=>Sequel[:a]+:b+20)
@m2.insert(1, 2)
@m1.all.must_equal []
# INSERT
ds.merge
@m1.all.must_equal [{:i1=>1, :a=>13}]
# UPDATE
ds.merge
@m1.all.must_equal [{:i1=>12, :a=>35}]
# DELETE MATCHING current row, INSERT NOT MATCHED new row
@m2.insert(12, 3)
ds.merge
@m1.all.must_equal [{:i1=>1, :a=>13}]
# MATCHED DO NOTHING
@m2.where(:i2=>12).delete
@m1.update(:a=>51)
ds.merge
@m1.all.must_equal [{:i1=>1, :a=>51}]
# NOT MATCHED DO NOTHING
@m1.delete
@m2.update(:b=>51)
ds.merge
@m1.all.must_equal []
@m2.insert(1, 2)
@m2.insert(11, 22)
ds = @m1.
merge_using(:m2, :i1=>:i2).
merge_insert(:i2, :b){b <= 10}.
merge_update(:a=>Sequel[:a]+1){false}.
merge_delete{false}
ds.merge
@m1.all.must_equal [{:i1=>1, :a=>2}]
ds.merge
@m1.all.must_equal [{:i1=>1, :a=>2}]
end
it "should consider condition blocks that return nil as NULL" do
@m2.insert(1, 2)
@m1.
merge_using(:m2, :i1=>:i2).
merge_insert(Sequel[:i2], Sequel[:b]+11){nil}.
merge
@m1.all.must_equal []
end
it "supports static SQL" do
@m2.insert(1, 2)
@m1.with_sql(<<SQL).merge
MERGE INTO m1 USING m2 ON (i1 = i2)
WHEN NOT MATCHED AND (b > 50) THEN DO NOTHING
WHEN NOT MATCHED THEN INSERT (i1, a) VALUES (i2, (b + 11))
WHEN MATCHED AND (a > 50) THEN DO NOTHING
WHEN MATCHED AND (a > 30) THEN DELETE
WHEN MATCHED THEN UPDATE SET i1 = (i1 + i2 + 10), a = (a + b + 20)
SQL
@m1.all.must_equal [{:i1=>1, :a=>13}]
end
it "should support merge_do_nothing_* without blocks" do
@m2.insert(1, 2)
ds = @m1.
merge_using(:m2, :i1=>:i2).
merge_do_nothing_when_matched.
merge_do_nothing_when_not_matched
ds.merge
@m1.all.must_equal []
@m1.insert(1, 3)
ds.merge
@m1.all.must_equal [{:i1=>1, :a=>3}]
end
end if DB.server_version >= 150000
|