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
|
require_relative "spec_helper"
describe Sequel::Model, "associate" do
it "should use explicit class if given a class, symbol, or string" do
begin
klass = Class.new(Sequel::Model(:nodes))
class ::ParParent < Sequel::Model; end
klass.associate :many_to_one, :par_parent0, :class=>ParParent
klass.associate :one_to_many, :par_parent1s, :class=>'ParParent'
klass.associate :many_to_many, :par_parent2s, :class=>:ParParent
klass.association_reflection(:"par_parent0").associated_class.must_equal ParParent
klass.association_reflection(:"par_parent1s").associated_class.must_equal ParParent
klass.association_reflection(:"par_parent2s").associated_class.must_equal ParParent
ensure
Object.send(:remove_const, :ParParent)
end
end
it "should default to associating to other models in the same scope" do
begin
class ::AssociationModuleTest
class Album < Sequel::Model
many_to_one :artist
many_to_many :tags
end
class Artist< Sequel::Model
one_to_many :albums
end
class Tag < Sequel::Model
many_to_many :albums
end
end
::AssociationModuleTest::Album.association_reflection(:artist).associated_class.must_equal ::AssociationModuleTest::Artist
::AssociationModuleTest::Album.association_reflection(:tags).associated_class.must_equal ::AssociationModuleTest::Tag
::AssociationModuleTest::Artist.association_reflection(:albums).associated_class.must_equal ::AssociationModuleTest::Album
::AssociationModuleTest::Tag.association_reflection(:albums).associated_class.must_equal ::AssociationModuleTest::Album
ensure
Object.send(:remove_const, :AssociationModuleTest)
end
end
it "should add a model_object and association_reflection accessors to the dataset, and return it with the current model object" do
klass = Class.new(Sequel::Model(:nodes)) do
columns :id, :a_id
end
mod = Module.new do
def blah
filter{|o| o.__send__(association_reflection[:key]) > model_object.id*2}
end
end
klass.associate :many_to_one, :a, :class=>klass
klass.associate :one_to_many, :bs, :key=>:b_id, :class=>klass, :extend=>mod
klass.associate :many_to_many, :cs, :class=>klass
node = klass.load(:id=>1)
node.a_dataset.model_object.must_equal node
node.bs_dataset.model_object.must_equal node
node.cs_dataset.model_object.must_equal node
node.a_dataset.association_reflection.must_equal klass.association_reflection(:a)
node.bs_dataset.association_reflection.must_equal klass.association_reflection(:bs)
node.cs_dataset.association_reflection.must_equal klass.association_reflection(:cs)
node.bs_dataset.blah.sql.must_equal 'SELECT * FROM nodes WHERE ((nodes.b_id = 1) AND (b_id > 2))'
end
it "should allow extending the dataset with :extend option" do
klass = Class.new(Sequel::Model(:nodes)) do
columns :id, :a_id
end
mod = Module.new do
def blah
1
end
end
mod2 = Module.new do
def blar
2
end
end
klass.associate :many_to_one, :a, :class=>klass, :extend=>mod
klass.associate :one_to_many, :bs, :class=>klass, :extend=>[mod]
klass.associate :many_to_many, :cs, :class=>klass, :extend=>[mod, mod2]
node = klass.load(:id=>1)
node.a_dataset.blah.must_equal 1
node.bs_dataset.blah.must_equal 1
node.cs_dataset.blah.must_equal 1
node.cs_dataset.blar.must_equal 2
end
it "should clone an existing association with the :clone option" do
begin
class ::ParParent < Sequel::Model; end
class ::ParParent2 < Sequel::Model; end
klass = Class.new(Sequel::Model(:nodes))
klass.many_to_one(:par_parent, :order=>:a){|ds| 1}
klass.one_to_many(:par_parent1s, :class=>'ParParent', :limit=>12){|ds| 4}
klass.many_to_many(:par_parent2s, :class=>:ParParent, :uniq=>true){|ds| 2}
klass.many_to_one :par, :clone=>:par_parent, :select=>:b
klass.one_to_many :par1s, :clone=>:par_parent1s, :order=>:b, :limit=>10, :block=>nil
klass.many_to_many(:par2s, :clone=>:par_parent2s, :order=>:c, :class=>:ParParent2){|ds| 3}
klass.many_to_one :par3, :clone=>:par
klass.association_reflection(:par).associated_class.must_equal ParParent
klass.association_reflection(:par1s).associated_class.must_equal ParParent
klass.association_reflection(:par2s).associated_class.must_equal ParParent2
klass.association_reflection(:par)[:order].must_equal :a
klass.association_reflection(:par).select.must_equal :b
klass.association_reflection(:par)[:block].call.must_equal 1
klass.association_reflection(:par)[:eager_block].call.must_equal 1
klass.association_reflection(:par1s)[:limit].must_equal 10
klass.association_reflection(:par1s)[:order].must_equal :b
klass.association_reflection(:par1s)[:block].must_be_nil
klass.association_reflection(:par2s)[:after_load].length.must_equal 1
klass.association_reflection(:par2s)[:order].must_equal :c
klass.association_reflection(:par2s)[:block].call.must_equal 3
klass.association_reflection(:par3)[:block].call.must_equal 1
klass.association_reflection(:par3)[:eager_block].call.must_equal 1
ensure
Object.send(:remove_const, :ParParent)
Object.send(:remove_const, :ParParent2)
end
end
it "should respect :no_dataset_method option to not create a dataset method" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c, :no_dataset_method=>true
c.method_defined?(:c_dataset).must_equal false
c.method_defined?(:c).must_equal true
end
it "should respect :no_association_method option to not create an association method" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c, :no_association_method=>true
c.method_defined?(:c_dataset).must_equal true
c.method_defined?(:c).must_equal false
end
it "should respect :setter=>nil option to not create a setter method" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c, :setter=>nil
c.method_defined?(:c).must_equal true
c.method_defined?(:c=).must_equal false
c = Class.new(Sequel::Model(:c))
c.one_to_one :c, :setter=>nil
c.method_defined?(:c).must_equal true
c.method_defined?(:c=).must_equal false
c = Class.new(Sequel::Model(:c))
c.one_through_one :c, :setter=>nil
c.method_defined?(:c).must_equal true
c.method_defined?(:c=).must_equal false
end
it "should respect :adder=>nil option to not create a add_* method" do
c = Class.new(Sequel::Model(:c))
c.one_to_many :cs, :adder=>nil
c.method_defined?(:add_c).must_equal false
c.method_defined?(:remove_c).must_equal true
c.method_defined?(:remove_all_cs).must_equal true
c = Class.new(Sequel::Model(:c))
c.many_to_many :cs, :adder=>nil
c.method_defined?(:add_c).must_equal false
c.method_defined?(:remove_c).must_equal true
c.method_defined?(:remove_all_cs).must_equal true
end
it "should respect :remover=>nil option to not create a remove_* method" do
c = Class.new(Sequel::Model(:c))
c.one_to_many :cs, :remover=>nil
c.method_defined?(:add_c).must_equal true
c.method_defined?(:remove_c).must_equal false
c.method_defined?(:remove_all_cs).must_equal true
c = Class.new(Sequel::Model(:c))
c.many_to_many :cs, :remover=>nil
c.method_defined?(:add_c).must_equal true
c.method_defined?(:remove_c).must_equal false
c.method_defined?(:remove_all_cs).must_equal true
end
it "should respect :clearer=>nil option to not create a remove_all_* method" do
c = Class.new(Sequel::Model(:c))
c.one_to_many :cs, :clearer=>nil
c.method_defined?(:add_c).must_equal true
c.method_defined?(:remove_c).must_equal true
c.method_defined?(:remove_all_cs).must_equal false
c = Class.new(Sequel::Model(:c))
c.many_to_many :cs, :clearer=>nil
c.method_defined?(:add_c).must_equal true
c.method_defined?(:remove_c).must_equal true
c.method_defined?(:remove_all_cs).must_equal false
end
it "should raise an error if attempting to clone an association of differing type" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c
proc{c.one_to_many :cs, :clone=>:c}.must_raise(Sequel::Error)
end
it "should allow overriding the :instance_specific option" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c, :instance_specific=>true
c.association_reflection(:c)[:instance_specific].must_equal true
c.many_to_one :c, :instance_specific=>false do |ds| ds end
c.association_reflection(:c)[:instance_specific].must_equal false
end
it "should set :allow_eager false by default if :instance_specific option is set and eager loading not otherwise allowed" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c, :instance_specific=>true
c.association_reflection(:c)[:allow_eager].must_equal false
c.many_to_one :c, :instance_specific=>true, :allow_eager=>true
c.association_reflection(:c)[:allow_eager].must_equal true
c.many_to_one :c, :instance_specific=>true, :eager_loader=>proc{}
c.association_reflection(:c)[:allow_eager].must_be_nil
c.many_to_one :c do |_| end
c.association_reflection(:c)[:allow_eager].must_be_nil
c.many_to_one :c, :dataset=>proc{|_|}
c.association_reflection(:c)[:allow_eager].must_equal false
end
it "should allow cloning of one_to_many to one_to_one associations and vice-versa" do
c = Class.new(Sequel::Model(:c))
c.one_to_one :c
c.one_to_many :cs, :clone=>:c
c.one_to_one :c2, :clone=>:cs
end
it "should allow cloning of many_to_many to one_through_one associations and vice-versa" do
c = Class.new(Sequel::Model(:c))
c.many_to_many :c
c.one_through_one :cs, :clone=>:c
c.many_to_many :c2, :clone=>:cs
end
it "should clear associations cache when refreshing object manually" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c
o = c.new
o.associations[:c] = 1
o.refresh
o.associations.must_equal({})
end
it "should not clear associations cache when refreshing object after save" do
c = Class.new(Sequel::Model(:c))
c.many_to_one :c
o = c.new
o.associations[:c] = 1
o.save
o.associations.must_equal(:c=>1)
end
it "should not clear associations cache when saving with insert_select" do
ds = Sequel::Model.db[:c].with_extend do
def supports_insert_select?; true end
def insert_select(*) {:id=>1} end
end
c = Class.new(Sequel::Model(ds))
c.many_to_one :c
o = c.new
o.associations[:c] = 1
o.save
o.associations.must_equal(:c=>1)
end
it "should not autoreload associations when the current foreign key value is nil" do
c = Class.new(Sequel::Model(Sequel::Model.db[:c]))
c.many_to_one :c
o = c.new
o.associations[:c] = 1
o[:c_id] = 2
o.associations[:c].must_equal 1
o = c.load({})
o.associations[:c] = 1
o[:c_id] = 2
o.associations[:c].must_equal 1
end
it "should autoreload associations when the current foreign key is nil and the current associated value is nil" do
c = Class.new(Sequel::Model(Sequel::Model.db[:c]))
c.many_to_one :c
o = c.new
o.associations[:c] = nil
o[:c_id] = 2
o.associations.must_be_empty
o = c.load({})
o.associations[:c] = nil
o[:c_id] = 2
o.associations.must_be_empty
end
it "should handle autoreloading for multiple associations when the current foreign key is nil" do
c = Class.new(Sequel::Model(Sequel::Model.db[:c]))
c.many_to_one :c
c.many_to_one :d, :key=>:c_id
o = c.new
o.associations[:c] = nil
o.associations[:d] = 1
o[:c_id] = nil
o.associations.must_equal(:c=>nil, :d=>1)
o[:c_id] = 2
o.associations.must_equal(:d=>1)
o[:c_id] = 2
o.associations.must_equal(:d=>1)
o[:c_id] = nil
o.associations.must_be_empty
o = c.load({:c_id=>nil})
o.associations[:c] = nil
o.associations[:d] = 1
o[:c_id] = nil
o.associations.must_equal(:c=>nil, :d=>1)
o[:c_id] = 2
o.associations.must_equal(:d=>1)
o[:c_id] = 2
o.associations.must_equal(:d=>1)
o[:c_id] = nil
o.associations.must_be_empty
end
it "should raise error for unsupported type" do
klass = Class.new(Sequel::Model(:nodes))
proc{klass.associate(:foo, :bar)}.must_raise Sequel::Error
end
it "should raise error for unsupported name" do
klass = Class.new(Sequel::Model(:nodes))
proc{klass.associate(:many_to_one, "bar")}.must_raise Sequel::Error
end
end
describe Sequel::Model, "many_to_one" do
before do
@c2 = Class.new(Sequel::Model(:nodes)) do
unrestrict_primary_key
columns :id, :parent_id, :par_parent_id, :blah
end
@dataset = @c2.dataset
DB.reset
end
it "should raise an error if associated class does not have a primary key, and :primary_key is not specified" do
@c2.no_primary_key
@c2.many_to_one :parent, :class => @c2
d = @c2.new(:id => 1, :parent_id => 234)
proc{d.parent}.must_raise(Sequel::Error)
DB.sqls.must_equal []
end
it "should raise an error if associated class does not have a primary key, and :primary_key is not specified, with an association block" do
@c2.no_primary_key
@c2.many_to_one :parent, :class => @c2 do |ds| ds end
d = @c2.new(:id => 1, :parent_id => 234)
proc{d.parent}.must_raise(Sequel::Error)
DB.sqls.must_equal []
end
it "should use implicit key if omitted" do
@c2.many_to_one :parent, :class => @c2
d = @c2.new(:id => 1, :parent_id => 234)
p = d.parent
p.class.must_equal @c2
p.values.must_equal(:x => 1, :id => 1)
DB.sqls.must_equal ["SELECT * FROM nodes WHERE id = 234"]
end
it "should allow association with the same name as the key if :key_column is given" do
@c2.def_column_alias(:parent_id_id, :parent_id)
@c2.many_to_one :parent_id, :key_column=>:parent_id, :class => @c2
d = @c2.load(:id => 1, :parent_id => 234)
d.parent_id_dataset.sql.must_equal "SELECT * FROM nodes WHERE (nodes.id = 234) LIMIT 1"
d.parent_id.must_equal @c2.load(:x => 1, :id => 1)
d.parent_id_id.must_equal 234
d[:parent_id].must_equal 234
DB.sqls.must_equal ["SELECT * FROM nodes WHERE id = 234"]
d.parent_id_id = 3
d.parent_id_id.must_equal 3
d[:parent_id].must_equal 3
end
it "should use implicit class if omitted" do
begin
class ::ParParent < Sequel::Model; end
@c2.many_to_one :par_parent
@c2.new(:id => 1, :par_parent_id => 234).par_parent.class.must_equal ParParent
DB.sqls.must_equal ["SELECT * FROM par_parents WHERE id = 234"]
ensure
Object.send(:remove_const, :ParParent)
end
end
it "should use class inside module if given as a string" do
begin
module ::Par
class Parent < Sequel::Model; end
end
@c2.many_to_one :par_parent, :class=>"Par::Parent"
@c2.new(:id => 1, :par_parent_id => 234).par_parent.class.must_equal Par::Parent
DB.sqls.must_equal ["SELECT * FROM parents WHERE id = 234"]
ensure
Object.send(:remove_const, :Par)
end
end
it "should use explicit key if given" do
@c2.many_to_one :parent, :class => @c2, :key => :blah
d = @c2.new(:id => 1, :blah => 567)
p = d.parent
p.class.must_equal @c2
p.values.must_equal(:x => 1, :id => 1)
DB.sqls.must_equal ["SELECT * FROM nodes WHERE id = 567"]
end
it "should use explicit composite key if given" do
@c2.set_primary_key [:blah, :id]
@c2.many_to_one :parent, :class => @c2, :key => [:id, :blah], :primary_key=>[:blah, :id]
d = @c2.new(:id => 1, :blah => 567)
p = d.parent
p.class.must_equal @c2
p.values.must_equal(:x => 1, :id => 1)
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((blah = 1) AND (id = 567)) LIMIT 1"]
end
it "should respect :qualify => false option" do
@c2.many_to_one :parent, :class => @c2, :key => :blah, :qualify=>false
@c2.new(:id => 1, :blah => 567).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE id = 567"]
end
it "should use :primary_key option if given" do
@c2.many_to_one :parent, :class => @c2, :key => :blah, :primary_key => :pk
@c2.new(:id => 1, :blah => 567).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.pk = 567) LIMIT 1"]
end
it "should support composite keys" do
@c2.many_to_one :parent, :class => @c2, :key=>[:id, :parent_id], :primary_key=>[:parent_id, :id]
@c2.new(:id => 1, :parent_id => 234).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.parent_id = 1) AND (nodes.id = 234)) LIMIT 1"]
end
it "should not issue query if not all keys have values" do
@c2.many_to_one :parent, :class => @c2, :key=>[:id, :parent_id], :primary_key=>[:parent_id, :id]
@c2.new(:id => 1, :parent_id => nil).parent.must_be_nil
DB.sqls.must_equal []
end
it "should raise an Error unless same number of composite keys used" do
proc{@c2.many_to_one :parent, :class => @c2, :primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_one :parent, :class => @c2, :key=>[:id, :parent_id], :primary_key=>:id}.must_raise(Sequel::Error)
proc{@c2.many_to_one :parent, :class => @c2, :key=>:id, :primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_one :parent, :class => @c2, :key=>[:id, :parent_id, :blah], :primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
end
it "should use :select option if given" do
@c2.many_to_one :parent, :class => @c2, :key => :blah, :select=>[:id, :name]
@c2.new(:id => 1, :blah => 567).parent
DB.sqls.must_equal ["SELECT id, name FROM nodes WHERE (nodes.id = 567) LIMIT 1"]
end
it "should use :conditions option if given" do
@c2.many_to_one :parent, :class => @c2, :key => :blah, :conditions=>{:a=>32}
@c2.new(:id => 1, :blah => 567).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((a = 32) AND (nodes.id = 567)) LIMIT 1"]
@c2.many_to_one :parent, :class => @c2, :key => :blah, :conditions=>:a
@c2.new(:id => 1, :blah => 567).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (a AND (nodes.id = 567)) LIMIT 1"]
end
it "should support :order, :limit (only for offset), and :dataset options, as well as a block" do
@c2.many_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{model.filter(:parent_id=>pk)}, :limit=>[10,20], :order=>:name do |ds|
ds.filter{x > 1}
end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((parent_id = 100) AND (x > 1)) ORDER BY name LIMIT 1 OFFSET 20"]
end
it "should return nil if key value is nil" do
@c2.many_to_one :parent, :class => @c2
@c2.new(:id => 1).parent.must_be_nil
DB.sqls.must_equal []
end
it "should cache negative lookup" do
@c2.many_to_one :parent, :class => @c2
@c2.dataset = @c2.dataset.with_fetch([])
d = @c2.new(:id => 1, :parent_id=>555)
DB.sqls.must_equal []
d.parent.must_be_nil
DB.sqls.must_equal ['SELECT * FROM nodes WHERE id = 555']
d.parent.must_be_nil
DB.sqls.must_equal []
end
it "should define a setter method" do
@c2.many_to_one :parent, :class => @c2
d = @c2.new(:id => 1)
d.parent = @c2.new(:id => 4321)
d.values.must_equal(:id => 1, :parent_id => 4321)
d.parent = nil
d.values.must_equal(:id => 1, :parent_id => nil)
e = @c2.new(:id => 6677)
d.parent = e
d.values.must_equal(:id => 1, :parent_id => 6677)
end
it "should have the setter method respect the :primary_key option" do
@c2.many_to_one :parent, :class => @c2, :primary_key=>:blah
d = @c2.new(:id => 1)
d.parent = @c2.new(:id => 4321, :blah=>444)
d.values.must_equal(:id => 1, :parent_id => 444)
d.parent = nil
d.values.must_equal(:id => 1, :parent_id => nil)
e = @c2.new(:id => 6677, :blah=>8)
d.parent = e
d.values.must_equal(:id => 1, :parent_id => 8)
end
it "should have the setter method respect composite keys" do
@c2.many_to_one :parent, :class => @c2, :key=>[:id, :parent_id], :primary_key=>[:parent_id, :id]
d = @c2.new(:id => 1, :parent_id=> 234)
d.parent = @c2.new(:id => 4, :parent_id=>52)
d.values.must_equal(:id => 52, :parent_id => 4)
d.parent = nil
d.values.must_equal(:id => nil, :parent_id => nil)
e = @c2.new(:id => 6677, :parent_id=>8)
d.parent = e
d.values.must_equal(:id => 8, :parent_id => 6677)
end
it "should not persist changes until saved" do
@c2.many_to_one :parent, :class => @c2
d = @c2.load(:id => 1)
DB.reset
d.parent = @c2.new(:id => 345)
DB.sqls.must_equal []
d.save_changes
DB.sqls.must_equal ['UPDATE nodes SET parent_id = 345 WHERE (id = 1)']
end
it "should populate cache when accessed" do
@c2.many_to_one :parent, :class => @c2
d = @c2.load(:id => 1)
d.parent_id = 234
d.associations[:parent].must_be_nil
@c2.dataset = @c2.dataset.with_fetch(:id=>234)
e = d.parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE id = 234"]
d.associations[:parent].must_equal e
end
it "should populate cache when assigned" do
@c2.many_to_one :parent, :class => @c2
d = @c2.create(:id => 1)
DB.reset
d.associations[:parent].must_be_nil
d.parent = @c2.new(:id => 234)
e = d.parent
d.associations[:parent].must_equal e
DB.sqls.must_equal []
end
it "should use cache if available" do
@c2.many_to_one :parent, :class => @c2
d = @c2.create(:id => 1, :parent_id => 234)
DB.reset
d.associations[:parent] = 42
d.parent.must_equal 42
DB.sqls.must_equal []
end
it "should not use cache if asked to reload" do
@c2.many_to_one :parent, :class => @c2
d = @c2.create(:id => 1)
DB.reset
d.parent_id = 234
d.associations[:parent] = 42
d.parent(:reload=>true).wont_equal 42
DB.sqls.must_equal ["SELECT * FROM nodes WHERE id = 234"]
end
it "should use a callback if given one as the argument" do
@c2.many_to_one :parent, :class => @c2
d = @c2.create(:id => 1)
DB.reset
d.parent_id = 234
d.associations[:parent] = 42
d.parent{|ds| ds.where{name > 'M'}}.wont_equal 42
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 234) AND (name > 'M')) LIMIT 1"]
end
it "should use a block given to the association method as a callback" do
@c2.many_to_one :parent, :class => @c2
d = @c2.create(:id => 1)
DB.reset
d.parent_id = 234
d.associations[:parent] = 42
d.parent{|ds| ds.filter{name > 'M'}}.wont_equal 42
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 234) AND (name > 'M')) LIMIT 1"]
end
it "should have the setter add to the reciprocal one_to_many cached association array if it exists" do
@c2.many_to_one :parent, :class => @c2
@c2.one_to_many :children, :class => @c2, :key=>:parent_id
@c2.dataset = @c2.dataset.with_fetch([])
d = @c2.new(:id => 1)
e = @c2.new(:id => 2)
DB.sqls.must_equal []
d.parent = e
e.children.wont_include(d)
DB.sqls.must_equal ['SELECT * FROM nodes WHERE (nodes.parent_id = 2)']
d = @c2.new(:id => 1)
e = @c2.new(:id => 2)
e.children.wont_include(d)
DB.sqls.must_equal ['SELECT * FROM nodes WHERE (nodes.parent_id = 2)']
d.parent = e
e.children.must_include(d)
DB.sqls.must_equal []
end
it "should have setter deal with a one_to_one reciprocal" do
@c2.many_to_one :parent, :class => @c2, :key=>:parent_id
@c2.one_to_one :child, :class => @c2, :key=>:parent_id
d = @c2.new(:id => 1)
e = @c2.new(:id => 2)
e.associations[:child] = nil
d.parent = e
e.child.must_equal d
d.parent = nil
e.child.must_be_nil
d.parent = e
e.child.must_equal d
f = @c2.new(:id => 3)
d.parent = nil
e.child.must_be_nil
e.associations[:child] = f
d.parent = e
e.child.must_equal d
end
it "should have the setter remove the object from the previous associated object's reciprocal one_to_many cached association array if it exists" do
@c2.many_to_one :parent, :class => @c2
@c2.one_to_many :children, :class => @c2, :key=>:parent_id
@c2.dataset = @c2.dataset.with_fetch([])
d = @c2.new(:id => 1)
e = @c2.new(:id => 2)
f = @c2.new(:id => 3)
e.children.wont_include(d)
f.children.wont_include(d)
DB.reset
d.parent = e
e.children.must_include(d)
d.parent = f
f.children.must_include(d)
e.children.wont_include(d)
d.parent = nil
f.children.wont_include(d)
DB.sqls.must_equal []
end
it "should have the setter not modify the reciprocal if set to same value as current" do
@c2.many_to_one :parent, :class => @c2
@c2.one_to_many :children, :class => @c2, :key=>:parent_id
c1 = @c2.load(:id => 1, :parent_id=>nil)
c2 = @c2.load(:id => 2, :parent_id=>1)
c3 = @c2.load(:id => 3, :parent_id=>1)
c1.associations[:children] = [c2, c3]
c2.associations[:parent] = c1
c2.parent = c1
c1.children.must_equal [c2, c3]
DB.sqls.must_equal []
end
it "should get all matching records and only return the first if :key option is set to nil" do
@c2.dataset = @c2.dataset.with_fetch([{:id=>1, :parent_id=>0, :par_parent_id=>3, :blah=>4, :children_id=>2, :children_parent_id=>1, :children_par_parent_id=>5, :children_blah=>6}, {}])
@c2.dataset.columns(:id, :parent_id, :par_parent_id, :blah)
@c2.one_to_many :children, :class => @c2, :key=>:parent_id
@c2.many_to_one :first_grand_parent, :class => @c2, :key=>nil, :eager_graph=>:children, :dataset=>proc{model.filter(:children_id=>parent_id)}
p = @c2.new(:parent_id=>2)
fgp = p.first_grand_parent
DB.sqls.must_equal ["SELECT nodes.id, nodes.parent_id, nodes.par_parent_id, nodes.blah, children.id AS children_id, children.parent_id AS children_parent_id, children.par_parent_id AS children_par_parent_id, children.blah AS children_blah FROM nodes LEFT OUTER JOIN nodes AS children ON (children.parent_id = nodes.id) WHERE (children_id = 2)"]
fgp.values.must_equal(:id=>1, :parent_id=>0, :par_parent_id=>3, :blah=>4)
fgp.children.first.values.must_equal(:id=>2, :parent_id=>1, :par_parent_id=>5, :blah=>6)
end
it "should not create the setter method if :read_only option is used" do
@c2.many_to_one :parent, :class => @c2, :read_only=>true
@c2.instance_methods.must_include(:parent)
@c2.instance_methods.wont_include(:parent=)
end
it "should not add associations methods directly to class" do
@c2.many_to_one :parent, :class => @c2
@c2.instance_methods.must_include(:parent)
@c2.instance_methods.must_include(:parent=)
@c2.instance_methods(false).wont_include(:parent)
@c2.instance_methods(false).wont_include(:parent=)
end
it "should add associations methods to the :methods_module option" do
m = Module.new
@c2.many_to_one :parent, :class => @c2, :methods_module=>m
m.instance_methods.must_include(:parent)
m.instance_methods.must_include(:parent=)
@c2.instance_methods.wont_include(:parent)
@c2.instance_methods.wont_include(:parent=)
end
it "should add associations methods directly to class if :methods_module is the class itself" do
@c2.many_to_one :parent, :class => @c2, :methods_module=>@c2
@c2.instance_methods(false).must_include(:parent)
@c2.instance_methods(false).must_include(:parent=)
end
it "should raise an error if trying to set a model object that doesn't have a valid primary key" do
@c2.many_to_one :parent, :class => @c2
p = @c2.new
c = @c2.load(:id=>123)
proc{c.parent = p}.must_raise(Sequel::Error)
end
it "should make the change to the foreign_key value inside a _association= method" do
@c2.many_to_one :parent, :class => @c2
@c2.private_instance_methods.must_include(:_parent=)
p = @c2.new
c = @c2.load(:id=>123)
def p._parent=(x)
@x = x
end
def p.parent_id=; raise; end
p.parent = c
p.instance_variable_get(:@x).must_equal c
end
it "should have the :setter option define the _association= method" do
@c2.many_to_one :parent, :class => @c2, :setter=>proc{|x| @x = x}
p = @c2.new
c = @c2.load(:id=>123)
def p.parent_id=; raise; end
p.parent = c
p.instance_variable_get(:@x).must_equal c
end
it "should support (before|after)_set callbacks" do
h = []
@c2.many_to_one :parent, :class => @c2, :before_set=>[proc{|x,y| h << x.pk; h << (y ? -y.pk : :y)}, :blah], :after_set=>proc{h << 3}
@c2.class_eval do
self::Foo = h
def []=(a, v)
a == :parent_id ? (model::Foo << (v ? 4 : 5)) : super
end
def blah(x)
model::Foo << (x ? x.pk : :x)
end
def blahr(x)
model::Foo << 6
end
end
p = @c2.load(:id=>10)
c = @c2.load(:id=>123)
h.must_equal []
p.parent = c
h.must_equal [10, -123, 123, 4, 3]
p.parent = nil
h.must_equal [10, -123, 123, 4, 3, 10, :y, :x, 5, 3]
end
it "should support after_load association callback" do
h = []
@c2.many_to_one :parent, :class => @c2, :after_load=>[proc{|x,y| h << [x.pk, y.pk]}, :al]
@c2.class_eval do
self::Foo = h
def al(v)
model::Foo << v.pk
end
set_dataset dataset.with_fetch(:id=>20)
end
p = @c2.load(:id=>10, :parent_id=>20)
parent = p.parent
h.must_equal [[10, 20], 20]
parent.pk.must_equal 20
end
it "should support after_load association callback that changes the cached object" do
@c2.many_to_one :parent, :class => @c2, :after_load=>:al
@c2.class_eval do
def al(v)
associations[:parent] = :foo
end
end
p = @c2.load(:id=>10, :parent_id=>20)
p.parent.must_equal :foo
p.associations[:parent].must_equal :foo
end
it "should raise error and not call internal add or remove method if before callback calls cancel_action, even if raise_on_save_failure is false" do
p = @c2.new
c = @c2.load(:id=>123)
p.raise_on_save_failure = false
@c2.many_to_one :parent, :class => @c2, :before_set=>:bs
def p.bs(x) cancel_action end
def p._parent=; raise; end
proc{p.parent = c}.must_raise(Sequel::HookFailed)
p.parent.must_be_nil
p.associations[:parent] = c
p.parent.must_equal c
proc{p.parent = nil}.must_raise(Sequel::HookFailed)
end
it "should raise an error if a callback is not a proc or symbol" do
@c2.many_to_one :parent, :class => @c2, :before_set=>Object.new
proc{@c2.new.parent = @c2.load(:id=>1)}.must_raise(Sequel::Error)
end
it "should have association dataset use false condition if any key is nil" do
@c2.many_to_one :parent, :class => @c2
@c2.load({}).parent_dataset.sql.must_equal "SELECT * FROM nodes WHERE 'f' LIMIT 1"
end
end
describe Sequel::Model, "one_to_one" do
before do
@c1 = Class.new(Sequel::Model(:attributes)) do
unrestrict_primary_key
columns :id, :node_id, :y
end
@c2 = Class.new(Sequel::Model(:nodes)) do
unrestrict_primary_key
attr_accessor :xxx
def self.name; 'Node'; end
def self.to_s; 'Node'; end
columns :id, :x, :parent_id, :par_parent_id, :blah, :node_id
end
@dataset = @c2.dataset
@dataset = @dataset.with_fetch({})
@c1.dataset = @c1.dataset.with_fetch({})
DB.reset
end
it "should have the getter method return a single object" do
@c2.one_to_one :attribute, :class => @c1
att = @c2.new(:id => 1234).attribute
DB.sqls.must_equal ['SELECT * FROM attributes WHERE (attributes.node_id = 1234) LIMIT 1']
att.must_be_kind_of(@c1)
att.values.must_equal({})
end
it "should not add a setter method if the :read_only option is true" do
@c2.one_to_one :attribute, :class => @c1, :read_only=>true
im = @c2.instance_methods
im.must_include(:attribute)
im.wont_include(:attribute=)
end
it "should add a setter method" do
@c2.one_to_one :attribute, :class => @c1
attrib = @c1.new(:id=>3)
@c1.dataset = @c1.dataset.with_fetch(:id=>3)
@c2.new(:id => 1234).attribute = attrib
DB.sqls.must_equal ['UPDATE attributes SET node_id = NULL WHERE (node_id = 1234)',
'INSERT INTO attributes (id, node_id) VALUES (3, 1234)',
"SELECT * FROM attributes WHERE id = 3"]
@c2.new(:id => 1234).attribute.must_equal attrib
attrib = @c1.load(:id=>3)
@c2.new(:id => 1234).attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes WHERE (attributes.node_id = 1234) LIMIT 1",
'UPDATE attributes SET node_id = NULL WHERE ((node_id = 1234) AND (id != 3))',
"UPDATE attributes SET node_id = 1234 WHERE (id = 3)"]
end
it "should use a transaction in the setter method" do
@c2.one_to_one :attribute, :class => @c1
@c2.use_transactions = true
attrib = @c1.load(:id=>3)
@c2.new(:id => 1234).attribute = attrib
DB.sqls.must_equal ['BEGIN',
'UPDATE attributes SET node_id = NULL WHERE ((node_id = 1234) AND (id != 3))',
"UPDATE attributes SET node_id = 1234 WHERE (id = 3)",
'COMMIT']
end
it "should have setter method respect association filters" do
@c2.one_to_one :attribute, :class => @c1, :conditions=>{:a=>1} do |ds|
ds.filter(:b=>2)
end
attrib = @c1.load(:id=>3)
@c2.new(:id => 1234).attribute = attrib
DB.sqls.must_equal ['UPDATE attributes SET node_id = NULL WHERE ((a = 1) AND (node_id = 1234) AND (b = 2) AND (id != 3))',
"UPDATE attributes SET node_id = 1234 WHERE (id = 3)"]
end
it "should have the setter method respect the :primary_key option" do
@c2.one_to_one :attribute, :class => @c1, :primary_key=>:xxx
attrib = @c1.new(:id=>3)
@c1.dataset = @c1.dataset.with_fetch(:id=>3)
@c2.new(:id => 1234, :xxx=>5).attribute = attrib
DB.sqls.must_equal ['UPDATE attributes SET node_id = NULL WHERE (node_id = 5)',
'INSERT INTO attributes (id, node_id) VALUES (3, 5)',
"SELECT * FROM attributes WHERE id = 3"]
@c2.new(:id => 321, :xxx=>5).attribute.must_equal attrib
attrib = @c1.load(:id=>3)
@c2.new(:id => 621, :xxx=>5).attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes WHERE (attributes.node_id = 5) LIMIT 1",
'UPDATE attributes SET node_id = NULL WHERE ((node_id = 5) AND (id != 3))',
'UPDATE attributes SET node_id = 5 WHERE (id = 3)']
end
it "should have the setter method respect composite keys" do
@c2.one_to_one :attribute, :class => @c1, :key=>[:node_id, :y], :primary_key=>[:id, :x]
attrib = @c1.load(:id=>3, :y=>6)
@c1.dataset = @c1.dataset.with_fetch(:id=>3, :y=>6)
@c2.load(:id => 1234, :x=>5).attribute = attrib
DB.sqls.must_equal ["UPDATE attributes SET node_id = NULL, y = NULL WHERE ((node_id = 1234) AND (y = 5) AND (id != 3))",
"UPDATE attributes SET y = 5, node_id = 1234 WHERE (id = 3)"]
end
it "should have setter method handle associations to models with joined datasets" do
db = Sequel.mock
c = Class.new(Sequel::Model(db)) do
set_dataset(db[:attributes].join(:foo, :attribute_id=>:id))
def _insert_dataset
db[:attributes]
end
def _update_dataset
db[:attributes].where(pk_hash)
end
@instance_dataset = dataset.limit(1).naked.skip_limit_check
unrestrict_primary_key
columns :id, :node_id, :y
end
@c2.one_to_one :attribute, :class => c
attrib = c.new(:id=>3)
db.fetch = [[], {:id=>3}]
@c2.load(:id => 1234).attribute = attrib
DB.sqls.must_equal []
db.sqls.must_equal [
"SELECT * FROM (SELECT * FROM attributes INNER JOIN foo ON (foo.attribute_id = attributes.id)) AS attributes LIMIT 0",
"SELECT * FROM (SELECT * FROM attributes INNER JOIN foo ON (foo.attribute_id = attributes.id)) AS attributes WHERE (node_id = 1234) LIMIT 1",
"INSERT INTO attributes (id, node_id) VALUES (3, 1234)",
"SELECT * FROM (SELECT * FROM attributes INNER JOIN foo ON (foo.attribute_id = attributes.id)) AS attributes WHERE (id = 3) LIMIT 1"]
db.fetch = [[{:id=>4}], {:id=>3, :node_id=>1234}]
db.numrows = 1
@c2.load(:id => 1234).attribute = c.load(:id=>3)
db.sqls.must_equal [
"SELECT * FROM (SELECT * FROM attributes INNER JOIN foo ON (foo.attribute_id = attributes.id)) AS attributes WHERE (node_id = 1234) LIMIT 1",
"UPDATE attributes SET node_id = NULL WHERE (id = 4)",
"UPDATE attributes SET node_id = 1234 WHERE (id = 3)"]
db.fetch = [[{:id=>4}], {:id=>3, :node_id=>1234}]
@c2.load(:id => 1234).attribute = c.new(:id=>3)
db.sqls.must_equal [
"SELECT * FROM (SELECT * FROM attributes INNER JOIN foo ON (foo.attribute_id = attributes.id)) AS attributes WHERE (node_id = 1234) LIMIT 1",
"UPDATE attributes SET node_id = NULL WHERE (id = 4)",
"INSERT INTO attributes (id, node_id) VALUES (3, 1234)",
"SELECT * FROM (SELECT * FROM attributes INNER JOIN foo ON (foo.attribute_id = attributes.id)) AS attributes WHERE (id = 3) LIMIT 1"]
end
it "should use implicit key if omitted" do
@c2.dataset = @c2.dataset.with_fetch({})
@c2.one_to_one :parent, :class => @c2
d = @c2.new(:id => 234)
p = d.parent
p.class.must_equal @c2
p.values.must_equal({})
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.node_id = 234) LIMIT 1"]
end
it "should use implicit class if omitted" do
begin
class ::ParParent < Sequel::Model; end
@c2.one_to_one :par_parent
@c2.new(:id => 234).par_parent.class.must_equal ParParent
DB.sqls.must_equal ["SELECT * FROM par_parents WHERE (par_parents.node_id = 234) LIMIT 1"]
ensure
Object.send(:remove_const, :ParParent)
end
end
it "should use class inside module if given as a string" do
begin
module ::Par
class Parent < Sequel::Model; end
end
@c2.one_to_one :par_parent, :class=>"Par::Parent"
@c2.new(:id => 234).par_parent.class.must_equal Par::Parent
DB.sqls.must_equal ["SELECT * FROM parents WHERE (parents.node_id = 234) LIMIT 1"]
ensure
Object.send(:remove_const, :Par)
end
end
it "should use explicit key if given" do
@c2.dataset = @c2.dataset.with_fetch({})
@c2.one_to_one :parent, :class => @c2, :key => :blah
d = @c2.new(:id => 234)
p = d.parent
p.class.must_equal @c2
p.values.must_equal({})
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.blah = 234) LIMIT 1"]
end
it "should use :primary_key option if given" do
@c2.one_to_one :parent, :class => @c2, :key => :pk, :primary_key => :blah
@c2.new(:id => 1, :blah => 567).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.pk = 567) LIMIT 1"]
end
it "should support composite keys" do
@c2.one_to_one :parent, :class => @c2, :primary_key=>[:id, :parent_id], :key=>[:parent_id, :id]
@c2.new(:id => 1, :parent_id => 234).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.parent_id = 1) AND (nodes.id = 234)) LIMIT 1"]
end
it "should not issue query if not all keys have values" do
@c2.one_to_one :parent, :class => @c2, :key=>[:id, :parent_id], :primary_key=>[:parent_id, :id]
@c2.new(:id => 1, :parent_id => nil).parent.must_be_nil
DB.sqls.must_equal []
end
it "should raise an Error unless same number of composite keys used" do
proc{@c2.one_to_one :parent, :class => @c2, :primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_to_one :parent, :class => @c2, :key=>[:id, :parent_id], :primary_key=>:id}.must_raise(Sequel::Error)
proc{@c2.one_to_one :parent, :class => @c2, :key=>:id, :primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_to_one :parent, :class => @c2, :key=>[:id, :parent_id, :blah], :primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
end
it "should use :select option if given" do
@c2.one_to_one :parent, :class => @c2, :select=>[:id, :name]
@c2.new(:id => 567).parent
DB.sqls.must_equal ["SELECT id, name FROM nodes WHERE (nodes.node_id = 567) LIMIT 1"]
end
it "should use :conditions option if given" do
@c2.one_to_one :parent, :class => @c2, :conditions=>{:a=>32}
@c2.new(:id => 567).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((a = 32) AND (nodes.node_id = 567)) LIMIT 1"]
@c2.one_to_one :parent, :class => @c2, :conditions=>:a
@c2.new(:id => 567).parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (a AND (nodes.node_id = 567)) LIMIT 1"]
end
it "should support :order, :limit (only for offset), and :dataset options, as well as a block" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{model.filter(:parent_id=>pk)}, :limit=>[10,20], :order=>:name do |ds|
ds.filter{x > 1}
end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((parent_id = 100) AND (x > 1)) ORDER BY name LIMIT 1 OFFSET 20"]
end
it "should support :dataset options with different types of arity" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{model.filter(:parent_id=>pk)}
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id = 100) LIMIT 1"]
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{|_| model.filter(:parent_id=>pk)}
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id = 100) LIMIT 1"]
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{|_, *| model.filter(:parent_id=>pk)}
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id = 100) LIMIT 1"]
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{|*| model.filter(:parent_id=>pk)}
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id = 100) LIMIT 1"]
end
deprecated "should support :dataset option that requires multiple arguments" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{|_, _| model.filter(:parent_id=>pk)}
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id = 100) LIMIT 1"]
end
deprecated "should support association block requires no arguments" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id do model.filter(:parent_id=>pk) end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id = 100)"]
end
deprecated "should support association block requires multiple arguments" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id do |_, _| model.filter(:parent_id=>pk) end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (parent_id = 100)"]
end
it "should support instance specific behavior in association block" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id do |ds| ds.where(:x=>pk) end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
end
it "should support instance specific behavior in association block when finalizing associations" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id do |ds| ds.where(:x=>pk) end
@c2.load(:id => 100).child_20
@c2.finalize_associations
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
end
it "should handle associations with blocks that are marked as instance specific" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>true do |ds| ds.where(:x=>100) end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
end
it "should handle associations with blocks that are marked as not-instance specific" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>false do |ds| ds.where(:x=>100) end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
end
it "should handle associations with blocks that are marked as instance specific when finalizing associations" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>true do |ds| ds.where(:x=>100) end
@c2.finalize_associations
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
end
it "should handle associations with blocks that are marked as not-instance specific when finalizing associations" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>false do |ds| ds.where(:x=>100) end
@c2.finalize_associations
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
end
it "should handle associations with :dataset option that are marked as instance specific" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>true, :dataset=>proc{|r| r.associated_dataset.where(:x=>100)}
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (x = 100) LIMIT 1"]
end
it "should handle associations with :dataset option that are marked as not-instance specific" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>false, :dataset=>proc{|r| r.associated_dataset.where(:x=>100)}
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (x = 100) LIMIT 1"]
end
it "should handle associations with :dataset option that are marked as instance specific when finalizing associations" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>true, :dataset=>proc{|r| r.associated_dataset.where(:x=>100)}
@c2.finalize_associations
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (x = 100) LIMIT 1"]
end
it "should handle associations with :dataset option that are marked as not-instance specific when finalizing associations" do
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>false, :dataset=>proc{|r| r.associated_dataset.where(:x=>100)}
@c2.finalize_associations
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (x = 100) LIMIT 1"]
end
it "should handle associations with blocks that are marked as not-instance specific and use Sequel.delay" do
x = 100
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>false do |ds| ds.where(:x=>Sequel.delay{x}) end
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
x = 2
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 2)) LIMIT 1"]
end
it "should handle associations with blocks that are marked as not-instance specific and use Sequel.delay when finalizing associations" do
x = 100
@c2.one_to_one :child_20, :class => @c2, :key=>:id, :instance_specific=>false do |ds| ds.where(:x=>Sequel.delay{x}) end
@c2.finalize_associations
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 100)) LIMIT 1"]
x = 2
@c2.load(:id => 100).child_20
DB.sqls.must_equal ["SELECT * FROM nodes WHERE ((nodes.id = 100) AND (x = 2)) LIMIT 1"]
end
it "should return nil if primary_key value is nil" do
@c2.one_to_one :parent, :class => @c2, :primary_key=>:node_id
@c2.new(:id => 1).parent.must_be_nil
DB.sqls.must_equal []
end
it "should cache negative lookup" do
@c2.one_to_one :parent, :class => @c2
@c2.dataset = @c2.dataset.with_fetch([])
d = @c2.new(:id => 555)
DB.sqls.must_equal []
d.parent.must_be_nil
DB.sqls.must_equal ['SELECT * FROM nodes WHERE (nodes.node_id = 555) LIMIT 1']
d.parent.must_be_nil
DB.sqls.must_equal []
end
it "should have the setter method respect the :key option" do
@c2.one_to_one :parent, :class => @c2, :key=>:blah
d = @c2.new(:id => 3)
e = @c2.new(:id => 4321, :blah=>444)
@c2.dataset = @c2.dataset.with_fetch(:id => 4321, :blah => 3)
d.parent = e
e.values.must_equal(:id => 4321, :blah => 3)
DB.sqls.must_equal ["UPDATE nodes SET blah = NULL WHERE (blah = 3)",
"INSERT INTO nodes (id, blah) VALUES (4321, 3)",
"SELECT * FROM nodes WHERE id = 4321"]
end
it "should persist changes to associated object when the setter is called" do
@c2.one_to_one :parent, :class => @c2
d = @c2.load(:id => 1)
d.parent = @c2.load(:id => 3, :node_id=>345)
DB.sqls.must_equal ["UPDATE nodes SET node_id = NULL WHERE ((node_id = 1) AND (id != 3))",
"UPDATE nodes SET node_id = 1 WHERE (id = 3)"]
end
it "should populate cache when accessed" do
@c2.one_to_one :parent, :class => @c2
d = @c2.load(:id => 1)
d.associations[:parent].must_be_nil
@c2.dataset = @c2.dataset.with_fetch(:id=>234)
e = d.parent
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.node_id = 1) LIMIT 1"]
d.parent
DB.sqls.must_equal []
d.associations[:parent].must_equal e
end
it "should populate cache when assigned" do
@c2.one_to_one :parent, :class => @c2
d = @c2.load(:id => 1)
d.associations[:parent].must_be_nil
e = @c2.load(:id => 234)
d.parent = e
f = d.parent
d.associations[:parent].must_equal e
e.must_equal f
end
it "should use cache if available" do
@c2.one_to_one :parent, :class => @c2
d = @c2.load(:id => 1, :parent_id => 234)
d.associations[:parent] = 42
d.parent.must_equal 42
DB.sqls.must_equal []
end
it "should not use cache if asked to reload" do
@c2.one_to_one :parent, :class => @c2
d = @c2.load(:id => 1)
d.associations[:parent] = [42]
d.parent(:reload=>true).wont_equal 42
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.node_id = 1) LIMIT 1"]
end
it "should have the setter set the reciprocal many_to_one cached association" do
@c2.one_to_one :parent, :class => @c2, :key=>:parent_id
@c2.many_to_one :child, :class => @c2, :key=>:parent_id
d = @c2.load(:id => 1)
e = @c2.load(:id => 2)
d.parent = e
e.child.must_equal d
DB.sqls.must_equal ["UPDATE nodes SET parent_id = NULL WHERE ((parent_id = 1) AND (id != 2))",
"UPDATE nodes SET parent_id = 1 WHERE (id = 2)"]
d.parent = nil
e.child.must_be_nil
DB.sqls.must_equal ["UPDATE nodes SET parent_id = NULL WHERE (parent_id = 1)"]
end
it "should have the setter remove the object from the previous associated object's reciprocal many_to_one cached association array if it exists" do
@c2.one_to_one :parent, :class => @c2, :key=>:parent_id
@c2.many_to_one :child, :class => @c2, :key=>:parent_id
@c2.dataset = @c2.dataset.with_fetch([])
d = @c2.load(:id => 1)
e = @c2.load(:id => 2)
f = @c2.load(:id => 3)
e.child.must_be_nil
f.child.must_be_nil
d.parent = e
e.child.must_equal d
d.parent = f
f.child.must_equal d
e.child.must_be_nil
d.parent = nil
f.child.must_be_nil
end
it "should have the setter not modify the reciprocal if set to same value as current" do
@c2.one_to_one :parent, :class => @c2, :key=>:parent_id
@c2.many_to_one :child, :class => @c2, :key=>:parent_id
c1 = @c2.load(:id => 1, :parent_id=>nil)
c2 = @c2.load(:id => 2, :parent_id=>1)
c1.associations[:child] = c2
c2.associations[:parent] = c1
c2.parent = c1
c1.child.must_equal c2
DB.sqls.must_equal []
end
it "should have setter not unset reciprocal during save if reciprocal is the same as current" do
@c2.many_to_one :parent, :class => @c2, :key=>:parent_id
@c2.one_to_one :child, :class => @c2, :key=>:parent_id, :reciprocal=>:parent
d = @c2.new(:id => 1)
e = @c2.new(:id => 2)
e2 = @c2.new(:id => 3)
e3 = @c2.new(:id => 4)
d.associations[:parent] = e
e.associations[:child] = d
e2.associations[:child] = d
e3.associations[:child] = e
assoc = nil
d.define_singleton_method(:after_save) do
super()
assoc = associations
end
def e.set_associated_object_if_same?; true; end
e.child = d
assoc.must_equal(:parent=>e)
def e2.set_associated_object_if_same?; true; end
e2.child = e
assoc.must_equal(:parent=>nil)
d.associations.clear
e3.child = d
assoc.must_equal({})
end
it "should have setter method handle case where there is no reciprocal" do
@c2.many_to_one :parent, :class => @c2, :key=>:parent_id, :reciprocal=>nil
@c2.one_to_one :child, :class => @c2, :key=>:parent_id, :reciprocal=>nil
d = @c2.new(:id => 1)
e = @c2.new(:id => 2)
e2 = @c2.new(:id => 3)
e3 = @c2.new(:id => 4)
d.associations[:parent] = e
e.associations[:child] = d
e2.associations[:child] = d
e3.associations[:child] = e
def e.set_associated_object_if_same?; true; end
e.child = d
d.parent.must_equal e
end
it "should not add associations methods directly to class" do
@c2.one_to_one :parent, :class => @c2
@c2.instance_methods.must_include(:parent)
@c2.instance_methods.must_include(:parent=)
@c2.instance_methods(false).wont_include(:parent)
@c2.instance_methods(false).wont_include(:parent=)
end
it "should raise an error if the current model object that doesn't have a valid primary key" do
@c2.one_to_one :parent, :class => @c2
p = @c2.new
c = @c2.load(:id=>123)
proc{p.parent = c}.must_raise(Sequel::Error)
end
it "should make the change to the foreign_key value inside a _association= method" do
@c2.one_to_one :parent, :class => @c2
@c2.private_instance_methods.must_include(:_parent=)
c = @c2.new
p = @c2.load(:id=>123)
def p._parent=(x)
@x = x
end
def p.parent_id=; raise; end
p.parent = c
p.instance_variable_get(:@x).must_equal c
end
it "should have a :setter option define the _association= method" do
@c2.one_to_one :parent, :class => @c2, :setter=>proc{|x| @x = x}
c = @c2.new
p = @c2.load(:id=>123)
def p.parent_id=; raise; end
p.parent = c
p.instance_variable_get(:@x).must_equal c
end
it "should support (before|after)_set callbacks" do
h = []
@c2.one_to_one :parent, :class => @c2, :before_set=>[proc{|x,y| h << x.pk; h << (y ? -y.pk : :y)}, :blah], :after_set=>proc{h << 3}
@c2.class_eval do
self::Foo = h
def blah(x)
model::Foo << (x ? x.pk : :x)
end
def blahr(x)
model::Foo << 6
end
end
p = @c2.load(:id=>10)
c = @c2.load(:id=>123)
h.must_equal []
p.parent = c
h.must_equal [10, -123, 123, 3]
p.parent = nil
h.must_equal [10, -123, 123, 3, 10, :y, :x, 3]
end
it "should support after_load association callback" do
h = []
@c2.one_to_one :parent, :class => @c2, :after_load=>[proc{|x,y| h << [x.pk, y.pk]}, :al]
@c2.class_eval do
self::Foo = h
def al(v)
model::Foo << v.pk
end
@dataset = @dataset.with_fetch(:id=>20)
end
p = @c2.load(:id=>10)
parent = p.parent
h.must_equal [[10, 20], 20]
parent.pk.must_equal 20
end
it "should raise error and not call internal add or remove method if before callback calls cancel_action, even if raise_on_save_failure is false" do
p = @c2.load(:id=>321)
c = @c2.load(:id=>123)
p.raise_on_save_failure = false
@c2.one_to_one :parent, :class => @c2, :before_set=>:bs
def p.bs(x) cancel_action end
def p._parent=; raise; end
proc{p.parent = c}.must_raise(Sequel::HookFailed)
p.associations[:parent].must_be_nil
p.associations[:parent] = c
p.parent.must_equal c
proc{p.parent = nil}.must_raise(Sequel::HookFailed)
end
it "should not validate the associated object in setter if the :validate=>false option is used" do
@c2.one_to_one :parent, :class => @c2, :validate=>false
n = @c2.new(:id => 1234)
a = @c2.new(:id => 2345)
def a.validate() errors.add(:id, 'foo') end
(n.parent = a).must_equal a
end
it "should raise an error if a callback is not a proc or symbol" do
@c2.one_to_one :parent, :class => @c2, :before_set=>Object.new
proc{@c2.new.parent = @c2.load(:id=>1)}.must_raise(Sequel::Error)
end
it "should work_correctly when used with associate" do
@c2.dataset = @c2.dataset.with_fetch({})
@c2.associate :one_to_one, :parent, :class => @c2
@c2.load(:id => 567).parent.must_equal @c2.load({})
DB.sqls.must_equal ["SELECT * FROM nodes WHERE (nodes.node_id = 567) LIMIT 1"]
end
it "should have association dataset use false condition if any key is nil" do
@c2.one_to_one :parent, :class => @c2, :primary_key=>:parent_id
@c2.load(:id=>1).parent_dataset.sql.must_equal "SELECT * FROM nodes WHERE 'f' LIMIT 1"
end
end
describe Sequel::Model, "one_to_many" do
before do
@c1 = Class.new(Sequel::Model(:attributes)) do
unrestrict_primary_key
columns :id, :node_id, :y, :z
end
@c2 = Class.new(Sequel::Model(:nodes)) do
def _refresh(ds); end
unrestrict_primary_key
attr_accessor :xxx
def self.name; 'Node'; end
def self.to_s; 'Node'; end
columns :id, :x
end
@dataset = @c2.dataset = @c2.dataset.with_fetch({})
@c1.dataset = @c1.dataset.with_fetch(proc{|sql| sql =~ /SELECT 1/ ? {:a=>1} : {}})
DB.reset
end
it "should raise an error if current class does not have a primary key, and :primary_key is not specified" do
@c2.no_primary_key
proc{@c2.one_to_many :attributes, :class => @c1}.must_raise(Sequel::Error)
DB.sqls.must_equal []
end
it "should use implicit key if omitted" do
@c2.one_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE (attributes.node_id = 1234)'
end
it "should use implicit class if omitted" do
begin
class ::HistoricalValue < Sequel::Model; end
@c2.one_to_many :historical_values
v = @c2.new(:id => 1234).historical_values_dataset
v.must_be_kind_of(Sequel::Dataset)
v.sql.must_equal 'SELECT * FROM historical_values WHERE (historical_values.node_id = 1234)'
v.model.must_equal HistoricalValue
ensure
Object.send(:remove_const, :HistoricalValue)
end
end
it "should use class inside a module if given as a string" do
begin
module ::Historical
class Value < Sequel::Model; end
end
@c2.one_to_many :historical_values, :class=>'Historical::Value'
v = @c2.new(:id => 1234).historical_values_dataset
v.must_be_kind_of(Sequel::Dataset)
v.sql.must_equal 'SELECT * FROM values WHERE (values.node_id = 1234)'
v.model.must_equal Historical::Value
ensure
Object.send(:remove_const, :Historical)
end
end
it "should use a callback if given one as a block" do
@c2.one_to_many :attributes, :class => @c1, :key => :nodeid
d = @c2.load(:id => 1234)
d.associations[:attributes] = []
d.attributes{|ds| ds.where{name > 'M'}}.wont_equal []
DB.sqls.must_equal ["SELECT * FROM attributes WHERE ((attributes.nodeid = 1234) AND (name > 'M'))"]
end
it "should use explicit key if given" do
@c2.one_to_many :attributes, :class => @c1, :key => :nodeid
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE (attributes.nodeid = 1234)'
end
it "should support_composite keys" do
@c2.one_to_many :attributes, :class => @c1, :key =>[:node_id, :id], :primary_key=>[:id, :x]
@c2.load(:id => 1234, :x=>234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (attributes.id = 234))'
end
it "should not issue query if not all keys have values" do
@c2.one_to_many :attributes, :class => @c1, :key =>[:node_id, :id], :primary_key=>[:id, :x]
@c2.load(:id => 1234, :x=>nil).attributes.must_equal []
DB.sqls.must_equal []
end
it "should raise an Error unless same number of composite keys used" do
proc{@c2.one_to_many :attributes, :class => @c1, :key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_to_many :attributes, :class => @c1, :primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_to_many :attributes, :class => @c1, :key=>[:node_id, :id], :primary_key=>:id}.must_raise(Sequel::Error)
proc{@c2.one_to_many :attributes, :class => @c1, :key=>:id, :primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_to_many :attributes, :class => @c1, :key=>[:node_id, :id, :x], :primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
end
it "should define an add_ method that works on existing records" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345)
a.must_equal n.add_attribute(a)
a.values.must_equal(:node_id => 1234, :id => 2345)
DB.sqls.must_equal ['UPDATE attributes SET node_id = 1234 WHERE (id = 2345)']
end
it "should define an add_ method that works on new records" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a = @c1.new(:id => 234)
@c1.dataset = @c1.dataset.with_fetch(:id=>234, :node_id=>1234)
a.must_equal n.add_attribute(a)
DB.sqls.must_equal ["INSERT INTO attributes (id, node_id) VALUES (234, 1234)",
"SELECT * FROM attributes WHERE id = 234"]
a.values.must_equal(:node_id => 1234, :id => 234)
end
it "should define a remove_ method that works on existing records" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345, :node_id => 1234)
a.must_equal n.remove_attribute(a)
a.values.must_equal(:node_id => nil, :id => 2345)
DB.sqls.must_equal ["SELECT 1 AS one FROM attributes WHERE ((attributes.node_id = 1234) AND (id = 2345)) LIMIT 1", 'UPDATE attributes SET node_id = NULL WHERE (id = 2345)']
end
it "should have the remove_ method raise an error if the passed object is not already associated" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a = @c1.load(:id => 2345, :node_id => 1234)
@c1.dataset = @c1.dataset.with_fetch([])
proc{n.remove_attribute(a)}.must_raise(Sequel::Error)
DB.sqls.must_equal ["SELECT 1 AS one FROM attributes WHERE ((attributes.node_id = 1234) AND (id = 2345)) LIMIT 1"]
end
it "should have the remove_ method raise an error if the passed object id is not already associated" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a = @c1.load(:id => 2345, :node_id => 1234)
@c1.dataset = @c1.dataset.with_fetch([])
proc{n.remove_attribute(a.id)}.must_raise(Sequel::Error)
DB.sqls.must_equal ["SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (attributes.id = 2345)) LIMIT 1"]
end
it "should accept a hash for the add_ method and create a new record" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
DB.reset
@c1.dataset = @c1.dataset.with_fetch(:node_id => 1234, :id => 234)
n.add_attribute(:id => 234).must_equal @c1.load(:node_id => 1234, :id => 234)
DB.sqls.must_equal ["INSERT INTO attributes (id, node_id) VALUES (234, 1234)",
"SELECT * FROM attributes WHERE id = 234"]
end
it "should accept a primary key for the add_ method" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch(:node_id => nil, :id => 234)
n.add_attribute(234).must_equal @c1.load(:node_id => 1234, :id => 234)
DB.sqls.must_equal ["SELECT * FROM attributes WHERE id = 234", "UPDATE attributes SET node_id = 1234 WHERE (id = 234)"]
end
it "should raise an error if the primary key passed to the add_ method does not match an existing record" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch([])
proc{n.add_attribute(234)}.must_raise(Sequel::NoMatchingRow)
DB.sqls.must_equal ["SELECT * FROM attributes WHERE id = 234"]
end
it "should raise an error in the add_ method if the passed associated object is not of the correct type" do
@c2.one_to_many :attributes, :class => @c1
proc{@c2.new(:id => 1234).add_attribute(@c2.new)}.must_raise(Sequel::Error)
end
it "should accept a primary key for the remove_ method and remove an existing record" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch(:id=>234, :node_id=>1234)
n.remove_attribute(234).must_equal @c1.load(:node_id => nil, :id => 234)
DB.sqls.must_equal ['SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (attributes.id = 234)) LIMIT 1',
'UPDATE attributes SET node_id = NULL WHERE (id = 234)']
end
it "should raise an error in the remove_ method if the passed associated object is not of the correct type" do
@c2.one_to_many :attributes, :class => @c1
proc{@c2.new(:id => 1234).remove_attribute(@c2.new)}.must_raise(Sequel::Error)
end
it "should have add_ method respect the :primary_key option" do
@c2.one_to_many :attributes, :class => @c1, :primary_key=>:xxx
n = @c2.new(:id => 1234, :xxx=>5)
a = @c1.load(:id => 2345)
n.add_attribute(a).must_equal a
DB.sqls.must_equal ['UPDATE attributes SET node_id = 5 WHERE (id = 2345)']
end
it "should have add_ method not add the same object to the cached association array if the object is already in the array" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a = @c1.load(:id => 2345)
n.associations[:attributes] = []
a.must_equal n.add_attribute(a)
a.must_equal n.add_attribute(a)
a.values.must_equal(:node_id => 1234, :id => 2345)
n.attributes.must_equal [a]
DB.sqls.must_equal ['UPDATE attributes SET node_id = 1234 WHERE (id = 2345)'] * 2
end
it "should have add_ method respect composite keys" do
@c2.one_to_many :attributes, :class => @c1, :key =>[:node_id, :y], :primary_key=>[:id, :x]
n = @c2.load(:id => 1234, :x=>5)
a = @c1.load(:id => 2345)
n.add_attribute(a).must_equal a
DB.sqls.must_equal ["UPDATE attributes SET node_id = 1234, y = 5 WHERE (id = 2345)"]
end
it "should have add_ method accept a composite key" do
@c1.dataset = @c1.dataset.with_fetch(:id=>2345, :node_id=>1234, :z=>8, :y=>5)
@c1.set_primary_key [:id, :z]
@c2.one_to_many :attributes, :class => @c1, :key =>[:node_id, :y], :primary_key=>[:id, :x]
n = @c2.load(:id => 1234, :x=>5)
a = @c1.load(:id => 2345, :z => 8, :node_id => 1234, :y=>5)
n.add_attribute([2345, 8]).must_equal a
DB.sqls.must_equal ["SELECT * FROM attributes WHERE ((id = 2345) AND (z = 8)) LIMIT 1",
"UPDATE attributes SET node_id = 1234, y = 5 WHERE ((id = 2345) AND (z = 8))"]
end
it "should have remove_ method respect composite keys" do
@c2.one_to_many :attributes, :class => @c1, :key =>[:node_id, :y], :primary_key=>[:id, :x]
n = @c2.load(:id => 1234, :x=>5)
a = @c1.load(:id => 2345, :node_id=>1234, :y=>5)
n.remove_attribute(a).must_equal a
DB.sqls.must_equal ["SELECT 1 AS one FROM attributes WHERE ((attributes.node_id = 1234) AND (attributes.y = 5) AND (id = 2345)) LIMIT 1",
"UPDATE attributes SET node_id = NULL, y = NULL WHERE (id = 2345)"]
end
it "should accept a array of composite primary key values for the remove_ method and remove an existing record" do
@c1.dataset = @c1.dataset.with_fetch(:id=>234, :node_id=>123, :y=>5)
@c1.set_primary_key [:id, :y]
@c2.one_to_many :attributes, :class => @c1, :key=>:node_id, :primary_key=>:id
n = @c2.new(:id => 123)
n.remove_attribute([234, 5]).must_equal @c1.load(:node_id => nil, :y => 5, :id => 234)
DB.sqls.must_equal ["SELECT * FROM attributes WHERE ((attributes.node_id = 123) AND (attributes.id = 234) AND (attributes.y = 5)) LIMIT 1",
"UPDATE attributes SET node_id = NULL WHERE ((id = 234) AND (y = 5))"]
end
it "should raise an error in add_ and remove_ if the passed object returns false to save (is not valid)" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a = @c1.new(:id => 2345)
def a.validate() errors.add(:id, 'foo') end
proc{n.add_attribute(a)}.must_raise(Sequel::ValidationFailed)
proc{n.remove_attribute(a)}.must_raise(Sequel::ValidationFailed)
end
it "should not validate the associated object in add_ and remove_ if the :validate=>false option is used" do
@c2.one_to_many :attributes, :class => @c1, :validate=>false
n = @c2.new(:id => 1234)
a = @c1.new(:id => 2345)
def a.validate() errors.add(:id, 'foo') end
n.add_attribute(a).must_equal a
n.remove_attribute(a).must_equal a
end
it "should not raise exception in add_ and remove_ if the :raise_on_save_failure=>false option is used" do
@c2.one_to_many :attributes, :class => @c1, :raise_on_save_failure=>false
n = @c2.new(:id => 1234)
a = @c1.new(:id => 2345)
def a.validate() errors.add(:id, 'foo') end
n.associations[:attributes] = []
n.add_attribute(a).must_be_nil
n.associations[:attributes].must_equal []
n.remove_attribute(a).must_be_nil
n.associations[:attributes].must_equal []
end
it "should raise an error if the model object doesn't have a valid primary key" do
@c2.one_to_many :attributes, :class => @c1
a = @c2.new
n = @c1.load(:id=>123)
proc{a.attributes_dataset}.must_raise(Sequel::Error)
proc{a.add_attribute(n)}.must_raise(Sequel::Error)
proc{a.remove_attribute(n)}.must_raise(Sequel::Error)
proc{a.remove_all_attributes}.must_raise(Sequel::Error)
end
it "should use :primary_key option if given" do
@c1.one_to_many :nodes, :class => @c2, :primary_key => :node_id, :key=>:id
@c1.load(:id => 1234, :node_id=>4321).nodes_dataset.sql.must_equal "SELECT * FROM nodes WHERE (nodes.id = 4321)"
end
it "should support a select option" do
@c2.one_to_many :attributes, :class => @c1, :select => [:id, :name]
@c2.new(:id => 1234).attributes_dataset.sql.must_equal "SELECT id, name FROM attributes WHERE (attributes.node_id = 1234)"
end
it "should support a conditions option" do
@c2.one_to_many :attributes, :class => @c1, :conditions => {:a=>32}
@c2.new(:id => 1234).attributes_dataset.sql.must_equal "SELECT * FROM attributes WHERE ((a = 32) AND (attributes.node_id = 1234))"
@c2.one_to_many :attributes, :class => @c1, :conditions => Sequel.~(:a)
@c2.new(:id => 1234).attributes_dataset.sql.must_equal "SELECT * FROM attributes WHERE (NOT a AND (attributes.node_id = 1234))"
@c2.one_to_many :attributes, :class => @c1, :conditions => [Sequel.~(:a)]
@c2.new(:id => 1234).attributes_dataset.sql.must_equal "SELECT * FROM attributes WHERE (NOT a AND (attributes.node_id = 1234))"
end
it "should support an order option" do
@c2.one_to_many :attributes, :class => @c1, :order => :kind
@c2.new(:id => 1234).attributes_dataset.sql.must_equal "SELECT * FROM attributes WHERE (attributes.node_id = 1234) ORDER BY kind"
end
it "should support an array for the order option" do
@c2.one_to_many :attributes, :class => @c1, :order => [:kind1, :kind2]
@c2.new(:id => 1234).attributes_dataset.sql.must_equal "SELECT * FROM attributes WHERE (attributes.node_id = 1234) ORDER BY kind1, kind2"
end
it "should have a dataset method for the associated object dataset" do
@c2.one_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE (attributes.node_id = 1234)'
end
it "should accept a block" do
@c2.one_to_many :attributes, :class => @c1 do |ds|
ds.filter(:xxx => nil)
end
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (xxx IS NULL))'
end
it "should support :order option with block" do
@c2.one_to_many :attributes, :class => @c1, :order => :kind do |ds|
ds.filter(:xxx => nil)
end
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (xxx IS NULL)) ORDER BY kind'
end
it "should have the block argument affect the _dataset method" do
@c2.one_to_many :attributes, :class => @c1 do |ds|
ds.filter(:xxx => 456)
end
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (xxx = 456))'
end
it "should support a :dataset option that is used instead of the default" do
c1 = @c1
@c1.dataset = @c1.dataset.with_fetch({})
@c2.one_to_many :all_other_attributes, :class => @c1, :dataset=>proc{c1.exclude(:nodeid=>pk)}, :order=>:a, :limit=>10 do |ds|
ds.filter(:xxx => 5)
end
@c2.new(:id => 1234).all_other_attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE ((nodeid != 1234) AND (xxx = 5)) ORDER BY a LIMIT 10'
@c2.new(:id => 1234).all_other_attributes.must_equal [@c1.load({})]
DB.sqls.must_equal ['SELECT * FROM attributes WHERE ((nodeid != 1234) AND (xxx = 5)) ORDER BY a LIMIT 10']
end
it "should support a :limit option" do
@c2.one_to_many :attributes, :class => @c1 , :limit=>10
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE (attributes.node_id = 1234) LIMIT 10'
@c2.one_to_many :attributes, :class => @c1 , :limit=>[10,10]
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT * FROM attributes WHERE (attributes.node_id = 1234) LIMIT 10 OFFSET 10'
end
it "should have the :eager option affect the _dataset method" do
@c2.one_to_many :attributes, :class => @c2 , :eager=>:attributes
@c2.new(:id => 1234).attributes_dataset.opts[:eager].must_equal(:attributes=>nil)
end
it "should populate cache when accessed" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
n.associations.include?(:attributes).must_equal false
atts = n.attributes
atts.must_equal n.associations[:attributes]
DB.sqls.must_equal ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
end
it "should use cache if available" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
n.associations[:attributes] = 42
n.attributes.must_equal 42
DB.sqls.must_equal []
end
it "should not use cache if asked to reload" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
n.associations[:attributes] = 42
n.attributes(:reload=>true).wont_equal 42
DB.sqls.must_equal ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
end
it "should add item to cache if it exists when calling add_" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
att = @c1.load(:id => 345)
a = []
n.associations[:attributes] = a
n.add_attribute(att)
a.must_equal [att]
end
it "should set object to item's reciprocal cache when calling add_" do
@c2.one_to_many :attributes, :class => @c1
@c1.many_to_one :node, :class => @c2
n = @c2.new(:id => 1234)
att = @c1.new(:id => 345)
n.add_attribute(att)
att.node.must_equal n
end
it "should remove item from cache if it exists when calling remove_" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
att = @c1.load(:id => 345)
a = [att]
n.associations[:attributes] = a
n.remove_attribute(att)
a.must_equal []
end
it "should remove item's reciprocal cache calling remove_" do
@c2.one_to_many :attributes, :class => @c1
@c1.many_to_one :node, :class => @c2
n = @c2.new(:id => 1234)
att = @c1.new(:id => 345)
att.associations[:node] = n
att.node.must_equal n
n.remove_attribute(att)
att.node.must_be_nil
end
it "should not create the add_, remove_, or remove_all_ methods if :read_only option is used" do
@c2.one_to_many :attributes, :class => @c1, :read_only=>true
im = @c2.instance_methods
im.must_include(:attributes)
im.must_include(:attributes_dataset)
im.wont_include(:add_attribute)
im.wont_include(:remove_attribute)
im.wont_include(:remove_all_attributes)
end
it "should not add associations methods directly to class" do
@c2.one_to_many :attributes, :class => @c1
im = @c2.instance_methods
im.must_include(:attributes)
im.must_include(:attributes_dataset)
im.must_include(:add_attribute)
im.must_include(:remove_attribute)
im.must_include(:remove_all_attributes)
im2 = @c2.instance_methods(false)
im2.wont_include(:attributes)
im2.wont_include(:attributes_dataset)
im2.wont_include(:add_attribute)
im2.wont_include(:remove_attribute)
im2.wont_include(:remove_all_attributes)
end
it "should populate the reciprocal many_to_one cache when loading the one_to_many association" do
@c2.one_to_many :attributes, :class => @c1, :key => :node_id
@c1.many_to_one :node, :class => @c2, :key => :node_id
n = @c2.new(:id => 1234)
atts = n.attributes
DB.sqls.must_equal ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
atts.must_equal [@c1.load({})]
atts.map{|a| a.node}.must_equal [n]
DB.sqls.must_equal []
end
it "should use an explicit :reciprocal option if given" do
@c2.one_to_many :attributes, :class => @c1, :key => :node_id, :reciprocal=>:wxyz
n = @c2.new(:id => 1234)
atts = n.attributes
DB.sqls.must_equal ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
atts.must_equal [@c1.load({})]
atts.map{|a| a.associations[:wxyz]}.must_equal [n]
DB.sqls.must_equal []
end
it "should have an remove_all_ method that removes all associated objects" do
@c2.one_to_many :attributes, :class => @c1
@c2.new(:id => 1234).remove_all_attributes
DB.sqls.must_equal ['UPDATE attributes SET node_id = NULL WHERE (node_id = 1234)']
end
it "should have remove_all method respect association filters" do
@c2.one_to_many :attributes, :class => @c1, :conditions=>{:a=>1} do |ds|
ds.filter(:b=>2)
end
@c2.new(:id => 1234).remove_all_attributes
DB.sqls.must_equal ['UPDATE attributes SET node_id = NULL WHERE ((a = 1) AND (node_id = 1234) AND (b = 2))']
end
it "should have the remove_all_ method respect the :primary_key option" do
@c2.one_to_many :attributes, :class => @c1, :primary_key=>:xxx
@c2.new(:id => 1234, :xxx=>5).remove_all_attributes
DB.sqls.must_equal ['UPDATE attributes SET node_id = NULL WHERE (node_id = 5)']
end
it "should have the remove_all_ method respect composite keys" do
@c2.one_to_many :attributes, :class => @c1, :key=>[:node_id, :y], :primary_key=>[:id, :x]
@c2.new(:id => 1234, :x=>5).remove_all_attributes
DB.sqls.must_equal ["UPDATE attributes SET node_id = NULL, y = NULL WHERE ((node_id = 1234) AND (y = 5))"]
end
it "remove_all should set the cache to []" do
@c2.one_to_many :attributes, :class => @c1
node = @c2.new(:id => 1234)
node.remove_all_attributes
node.associations[:attributes].must_equal []
end
it "remove_all should return the array of previously associated items if the cache is populated" do
@c2.one_to_many :attributes, :class => @c1
attrib = @c1.new(:id=>3)
node = @c2.new(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch([[], [{:id=>3, :node_id=>1234}]])
node.attributes.must_equal []
node.add_attribute(attrib)
node.associations[:attributes].must_equal [attrib]
node.remove_all_attributes.must_equal [attrib]
end
it "remove_all should return nil if the cache is not populated" do
@c2.one_to_many :attributes, :class => @c1
@c2.new(:id => 1234).remove_all_attributes.must_be_nil
end
it "remove_all should remove the current item from all reciprocal association caches if they are populated" do
@c2.one_to_many :attributes, :class => @c1
@c1.many_to_one :node, :class => @c2
@c2.dataset = @c2.dataset.with_fetch([])
@c1.dataset = @c1.dataset.with_fetch([[], [{:id=>3, :node_id=>1234}]])
attrib = @c1.new(:id=>3)
node = @c2.load(:id => 1234)
node.attributes.must_equal []
attrib.node.must_be_nil
node.add_attribute(attrib)
attrib.associations[:node].must_equal node
node.remove_all_attributes
attrib.associations.fetch(:node, 2).must_be_nil
end
it "should call an _add_ method internally to add attributes" do
@c2.one_to_many :attributes, :class => @c1
@c2.private_instance_methods.must_include(:_add_attribute)
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._add_attribute(x)
@x = x
end
def c._node_id=; raise; end
p.add_attribute(c)
p.instance_variable_get(:@x).must_equal c
end
it "should support an :adder option for defining the _add_ method" do
@c2.one_to_many :attributes, :class => @c1, :adder=>proc{|x| @x = x}
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def c._node_id=; raise; end
p.add_attribute(c)
p.instance_variable_get(:@x).must_equal c
end
it "should support an :adder option that accepts keywords" do
@c2.one_to_many :attributes, :class => @c1, :adder=>eval('proc{|x, foo: nil| @x = [x, foo]}')
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def c._node_id=; raise; end
p.add_attribute(c, foo: 1)
p.instance_variable_get(:@x).must_equal [c, 1]
end if RUBY_VERSION >= '2.0'
it "should allow additional arguments given to the add_ method and pass them onwards to the _add_ method" do
@c2.one_to_many :attributes, :class => @c1
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._add_attribute(x,*y)
@x = x
@y = y
end
def c._node_id=; raise; end
p.add_attribute(c,:foo,:bar=>:baz)
p.instance_variable_get(:@x).must_equal c
p.instance_variable_get(:@y).must_equal [:foo,{:bar=>:baz}]
end
it "should call a _remove_ method internally to remove attributes" do
@c2.one_to_many :attributes, :class => @c1
@c2.private_instance_methods.must_include(:_remove_attribute)
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._remove_attribute(x)
@x = x
end
def c._node_id=; raise; end
p.remove_attribute(c)
p.instance_variable_get(:@x).must_equal c
end
it "should support a :remover option for defining the _remove_ method" do
@c2.one_to_many :attributes, :class => @c1, :remover=>proc{|x| @x = x}
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def c._node_id=; raise; end
p.remove_attribute(c)
p.instance_variable_get(:@x).must_equal c
end
it "should support a :remover option that accepts keywords" do
@c2.one_to_many :attributes, :class => @c1, :remover=>eval('proc{|x, foo: nil| @x = [x, foo]}')
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def c._node_id=; raise; end
p.remove_attribute(c, foo: 1)
p.instance_variable_get(:@x).must_equal [c, 1]
end if RUBY_VERSION >= '2.0'
it "should allow additional arguments given to the remove_ method and pass them onwards to the _remove_ method" do
@c2.one_to_many :attributes, :class => @c1, :reciprocal=>nil
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._remove_attribute(x,*y)
@x = x
@y = y
end
def c._node_id=; raise; end
p.remove_attribute(c,:foo,:bar=>:baz)
p.instance_variable_get(:@x).must_equal c
p.instance_variable_get(:@y).must_equal [:foo,{:bar=>:baz}]
end
it "should allow additional arguments given to the remove_all_ method and pass them onwards to the _remove_all_ method" do
@c2.one_to_many :attributes, :class => @c1
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._remove_all_attributes(*y)
@y = y
end
def c._node_id=; raise; end
p.remove_all_attributes(:foo,:bar=>:baz)
p.instance_variable_get(:@y).must_equal [:foo,{:bar=>:baz}]
end
it "should call a _remove_all_ method internally to remove attributes" do
@c2.one_to_many :attributes, :class => @c1
@c2.private_instance_methods.must_include(:_remove_all_attributes)
p = @c2.load(:id=>10)
def p._remove_all_attributes
@x = :foo
end
p.remove_all_attributes
p.instance_variable_get(:@x).must_equal :foo
end
it "should support a :clearer option for defining the _remove_all_ method" do
@c2.one_to_many :attributes, :class => @c1, :clearer=>proc{@x = :foo}
p = @c2.load(:id=>10)
p.remove_all_attributes
p.instance_variable_get(:@x).must_equal :foo
end
it "should support a :clearer option that supports keywords" do
@c2.one_to_many :attributes, :class => @c1, :clearer=>eval('proc{|foo: nil| @x = foo}')
p = @c2.load(:id=>10)
p.remove_all_attributes(foo: 1)
p.instance_variable_get(:@x).must_equal 1
end if RUBY_VERSION >= '2.0'
it "should support (before|after)_(add|remove) callbacks" do
h = []
@c2.one_to_many :attributes, :class => @c1, :before_add=>[proc{|x,y| h << x.pk; h << -y.pk}, :blah], :after_add=>proc{h << 3}, :before_remove=>:blah, :after_remove=>[:blahr]
@c2.class_eval do
self::Foo = h
def _add_attribute(v)
model::Foo << 4
end
def _remove_attribute(v)
model::Foo << 5
end
def blah(x)
model::Foo << x.pk
end
def blahr(x)
model::Foo << 6
end
end
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
h.must_equal []
p.add_attribute(c)
h.must_equal [10, -123, 123, 4, 3]
p.remove_attribute(c)
h.must_equal [10, -123, 123, 4, 3, 123, 5, 6]
end
it "should support after_load association callback" do
h = []
@c2.one_to_many :attributes, :class => @c1, :after_load=>[proc{|x,y| h << [x.pk, y.collect{|z|z.pk}]}, :al]
@c2.class_eval do
self::Foo = h
def al(v)
v.each{|x| model::Foo << x.pk}
end
end
@c1.dataset = @c1.dataset.with_fetch([{:id=>20}, {:id=>30}])
p = @c2.load(:id=>10, :parent_id=>20)
attributes = p.attributes
h.must_equal [[10, [20, 30]], 20, 30]
attributes.collect{|a| a.pk}.must_equal [20, 30]
end
it "should raise error and not call internal add or remove method if before callback calls cancel_action if raise_on_save_failure is true" do
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
@c2.one_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
def p.ba(o); cancel_action; end
def p._add_attribute; raise; end
def p._remove_attribute; raise; end
p.associations[:attributes] = []
proc{p.add_attribute(c)}.must_raise(Sequel::HookFailed)
p.attributes.must_equal []
p.associations[:attributes] = [c]
def p.br(o); cancel_action; end
proc{p.remove_attribute(c)}.must_raise(Sequel::HookFailed)
p.attributes.must_equal [c]
end
it "should return nil and not call internal add or remove method if before callback calls cancel_action if raise_on_save_failure is false" do
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
p.raise_on_save_failure = false
@c2.one_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
def p.ba(o); cancel_action; end
def p._add_attribute; raise; end
def p._remove_attribute; raise; end
p.associations[:attributes] = []
p.add_attribute(c).must_be_nil
p.attributes.must_equal []
p.associations[:attributes] = [c]
def p.br(o); cancel_action; end
p.remove_attribute(c).must_be_nil
p.attributes.must_equal [c]
end
it "should have association dataset use false condition if any key is nil" do
@c1.one_to_many :children, :class => @c1, :primary_key=>:node_id
@c1.load(:id=>1).children_dataset.sql.must_equal "SELECT * FROM attributes WHERE 'f'"
end
end
describe Sequel::Model, "many_to_many" do
before do
@c1 = Class.new(Sequel::Model(:attributes)) do
unrestrict_primary_key
attr_accessor :yyy
def self.name; 'Attribute'; end
def self.to_s; 'Attribute'; end
columns :id, :y, :z
end
@c2 = Class.new(Sequel::Model(:nodes)) do
unrestrict_primary_key
attr_accessor :xxx
def self.name; 'Node'; end
def self.to_s; 'Node'; end
columns :id, :x
end
@dataset = @c2.dataset
@c1.dataset = @c1.dataset.with_autoid(1)
[@c1, @c2].each{|c| c.dataset = c.dataset.with_fetch({})}
DB.reset
end
it "should raise an error if current class does not have a primary key, and :left_primary_key is not specified" do
@c2.no_primary_key
proc{@c2.many_to_many :attributes, :class => @c1}.must_raise(Sequel::Error)
DB.sqls.must_equal []
end
it "should raise an error if associated class does not have a primary key, and :right_primary_key is not specified" do
@c1.no_primary_key
@c2.many_to_many :attributes, :class => @c1
d = @c2.new(:id => 1234)
proc{d.attributes}.must_raise(Sequel::Error)
DB.sqls.must_equal []
end
it "should use implicit key values and join table if omitted" do
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
it "should use implicit key values and join table if omitted" do
@c2.one_through_one :attribute, :class => @c1
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1'
end
it "should use implicit class if omitted" do
begin
class ::Tag < Sequel::Model; end
@c2.many_to_many :tags
@c2.new(:id => 1234).tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN nodes_tags ON (nodes_tags.tag_id = tags.id) WHERE (nodes_tags.node_id = 1234)'
ensure
Object.send(:remove_const, :Tag)
end
end
it "should use class inside module if given as a string" do
begin
module ::Historical
class Tag < Sequel::Model; end
end
@c2.many_to_many :tags, :class=>'::Historical::Tag'
@c2.new(:id => 1234).tags_dataset.sql.must_equal 'SELECT tags.* FROM tags INNER JOIN nodes_tags ON (nodes_tags.tag_id = tags.id) WHERE (nodes_tags.node_id = 1234)'
ensure
Object.send(:remove_const, :Historical)
end
end
it "should not override a selection consisting completely of qualified columns using Sequel::SQL::QualifiedIdentifier" do
@c1.dataset = @c1.dataset.select(Sequel.qualify(:attributes, :id), Sequel.qualify(:attributes, :b))
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.id, attributes.b FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
with_symbol_splitting "should not override a selection consisting completely of qualified columns using symbols" do
@c1.dataset = @c1.dataset.select(:attributes__id, :attributes__b)
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.id, attributes.b FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
it "should not override a selection consisting completely of qualified columns using Sequel::SQL::AliasedExpression" do
@c1.dataset = @c1.dataset.select(Sequel.qualify(:attributes, :id).as(:a), Sequel[:attributes][:b].as(:c))
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.id AS a, attributes.b AS c FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
with_symbol_splitting "should not override a selection consisting completely of qualified columns using Sequel::SQL::AliasedExpression with qualified symbol" do
@c1.dataset = @c1.dataset.select(Sequel.qualify(:attributes, :id).as(:a), Sequel.as(:attributes__b, :c))
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.id AS a, attributes.b AS c FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
it "should override a selection consisting of non qualified columns" do
@c1.dataset = @c1.dataset.select{foo(:bar)}
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
it "should respect :predicate_key when lazily loading" do
@c2.many_to_many :attributes, :class => @c1, :predicate_key=>Sequel.subscript(Sequel[:attributes_nodes][:node_id], 0)
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id[0] = 1234)'
end
it "should use explicit key values and join table if given" do
@c2.many_to_many :attributes, :class => @c1, :left_key => :nodeid, :right_key => :attributeid, :join_table => :attribute2node
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attribute2node ON (attribute2node.attributeid = attributes.id) WHERE (attribute2node.nodeid = 1234)'
end
it "should support a conditions option" do
@c2.many_to_many :attributes, :class => @c1, :conditions => {:a=>32}
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((a = 32) AND (attributes_nodes.node_id = 1234))'
@c2.many_to_many :attributes, :class => @c1, :conditions => Sequel.lit('a = ?', 32)
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((a = 32) AND (attributes_nodes.node_id = 1234))'
@c2.new(:id => 1234).attributes.must_equal [@c1.load({})]
end
it "should support an order option" do
@c2.many_to_many :attributes, :class => @c1, :order => :blah
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) ORDER BY blah'
end
it "should support an array for the order option" do
@c2.many_to_many :attributes, :class => @c1, :order => [:blah1, :blah2]
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) ORDER BY blah1, blah2'
end
it "should support :left_primary_key and :right_primary_key options" do
@c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
@c2.new(:id => 1234, :xxx=>5).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.yyy) WHERE (attributes_nodes.node_id = 5)'
end
it "should support composite keys" do
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
@c2.load(:id => 1234, :x=>5).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.r1 = attributes.id) AND (attributes_nodes.r2 = attributes.y)) WHERE ((attributes_nodes.l1 = 1234) AND (attributes_nodes.l2 = 5))'
end
it "should not issue query if not all keys have values" do
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
@c2.load(:id => 1234, :x=>nil).attributes.must_equal []
DB.sqls.must_equal []
end
it "should raise an Error unless same number of composite keys used" do
proc{@c2.many_to_many :attributes, :class => @c1, :left_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :left_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :left_key=>[:node_id, :id], :left_primary_key=>:id}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :left_key=>:id, :left_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :left_key=>[:node_id, :id, :x], :left_primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :right_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :right_key=>[:node_id, :id], :right_primary_key=>:id}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :right_key=>:id, :left_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.many_to_many :attributes, :class => @c1, :right_key=>[:node_id, :id, :x], :right_primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
end
it "should support a select option" do
@c2.many_to_many :attributes, :class => @c1, :select => :blah
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT blah FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
it "should support an array for the select option" do
@c2.many_to_many :attributes, :class => @c1, :select => [Sequel::SQL::ColumnAll.new(:attributes), Sequel[:attribute_nodes][:blah2]]
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.*, attribute_nodes.blah2 FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)'
end
it "should accept a block" do
@c2.many_to_many :attributes, :class => @c1 do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (xxx = 555))'
end
it "should allow the :order option while accepting a block" do
@c2.many_to_many :attributes, :class => @c1, :order=>[:blah1, :blah2] do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (xxx = 555)) ORDER BY blah1, blah2'
end
it "should support a :dataset option that is used instead of the default" do
c1 = @c1
@c2.many_to_many :attributes, :class => @c1, :dataset=>proc{c1.join_table(:natural, :an).filter(Sequel[:an][:nodeid]=>pk)}, :order=> :a, :limit=>10, :select=>nil do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attributes_dataset.sql.must_equal 'SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 10'
n.attributes.must_equal [@c1.load({})]
DB.sqls.must_equal ['SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 10']
end
it "should support a :dataset option that accepts the reflection as an argument" do
@c2.many_to_many :attributes, :class => @c1, :dataset=>lambda{|opts| opts.associated_class.natural_join(:an).filter(Sequel[:an][:nodeid]=>pk)}, :order=> :a, :limit=>10, :select=>nil do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attributes_dataset.sql.must_equal 'SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 10'
n.attributes.must_equal [@c1.load({})]
DB.sqls.must_equal ['SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 10']
end
it "should support a :limit option" do
@c2.many_to_many :attributes, :class => @c1 , :limit=>10
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 10'
@c2.many_to_many :attributes, :class => @c1 , :limit=>[10, 10]
@c2.new(:id => 1234).attributes_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 10 OFFSET 10'
end
it "should have the :eager option affect the _dataset method" do
@c2.many_to_many :attributes, :class => @c2 , :eager=>:attributes
@c2.new(:id => 1234).attributes_dataset.opts[:eager].must_equal(:attributes=>nil)
end
it "should handle an aliased join table" do
@c2.many_to_many :attributes, :class => @c1, :join_table => Sequel[:attribute2node].as(:attributes_nodes)
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345)
n.attributes_dataset.sql.must_equal "SELECT attributes.* FROM attributes INNER JOIN attribute2node AS attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)"
a.must_equal n.add_attribute(a)
a.must_equal n.remove_attribute(a)
n.remove_all_attributes
DB.sqls.must_equal ["INSERT INTO attribute2node (node_id, attribute_id) VALUES (1234, 2345)",
"DELETE FROM attribute2node WHERE ((node_id = 1234) AND (attribute_id = 2345))",
"DELETE FROM attribute2node WHERE (node_id = 1234)"]
end
with_symbol_splitting "should handle an aliased symbol join table" do
@c2.many_to_many :attributes, :class => @c1, :join_table => :attribute2node___attributes_nodes
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345)
n.attributes_dataset.sql.must_equal "SELECT attributes.* FROM attributes INNER JOIN attribute2node AS attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)"
a.must_equal n.add_attribute(a)
a.must_equal n.remove_attribute(a)
n.remove_all_attributes
DB.sqls.must_equal ["INSERT INTO attribute2node (node_id, attribute_id) VALUES (1234, 2345)",
"DELETE FROM attribute2node WHERE ((node_id = 1234) AND (attribute_id = 2345))",
"DELETE FROM attribute2node WHERE (node_id = 1234)"]
end
it "should define an add_ method that works on existing records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345)
n.add_attribute(a).must_equal a
DB.sqls.must_equal ["INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2345)"]
end
it "should define an add_ method that works with a primary key" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345)
@c1.dataset = @c1.dataset.with_fetch(:id=>2345)
n.add_attribute(2345).must_equal a
DB.sqls.must_equal ["SELECT * FROM attributes WHERE id = 2345",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2345)"]
end
it "should raise an error if the primary key passed to the add_ method does not match an existing record" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch([])
proc{n.add_attribute(2345)}.must_raise(Sequel::NoMatchingRow)
DB.sqls.must_equal ["SELECT * FROM attributes WHERE id = 2345"]
end
it "should allow passing a hash to the add_ method which creates a new record" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch(:id=>1)
n.add_attribute(:id => 1).must_equal @c1.load(:id => 1)
DB.sqls.must_equal ['INSERT INTO attributes (id) VALUES (1)',
"SELECT * FROM attributes WHERE id = 1",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 1)"]
end
it "should define a remove_ method that works on existing records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a = @c1.new(:id => 2345)
n.remove_attribute(a).must_equal a
DB.sqls.must_equal ['DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 2345))']
end
it "should raise an error in the add_ method if the passed associated object is not of the correct type" do
@c2.many_to_many :attributes, :class => @c1
proc{@c2.new(:id => 1234).add_attribute(@c2.new)}.must_raise(Sequel::Error)
end
it "should accept a primary key for the remove_ method and remove an existing record" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch(:id=>234)
n.remove_attribute(234).must_equal @c1.load(:id => 234)
DB.sqls.must_equal ["SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (attributes.id = 234)) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 234))"]
end
it "should raise an error in the remove_ method if the passed associated object is not of the correct type" do
@c2.many_to_many :attributes, :class => @c1
proc{@c2.new(:id => 1234).remove_attribute(@c2.new)}.must_raise(Sequel::Error)
end
it "should have the add_ method respect the :left_primary_key and :right_primary_key options" do
@c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
n = @c2.load(:id => 1234).set(:xxx=>5)
a = @c1.load(:id => 2345).set(:yyy=>8)
n.add_attribute(a).must_equal a
DB.sqls.must_equal ["INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (5, 8)"]
end
it "should have add_ method not add the same object to the cached association array if the object is already in the array" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234).set(:xxx=>5)
a = @c1.load(:id => 2345).set(:yyy=>8)
n.associations[:attributes] = []
a.must_equal n.add_attribute(a)
a.must_equal n.add_attribute(a)
n.attributes.must_equal [a]
end
it "should have the add_ method respect composite keys" do
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :z]
n = @c2.load(:id => 1234, :x=>5)
a = @c1.load(:id => 2345, :z=>8)
a.must_equal n.add_attribute(a)
sqls = DB.sqls
m = /INSERT INTO attributes_nodes \((\w+), (\w+), (\w+), (\w+)\) VALUES \((\d+), (\d+), (\d+), (\d+)\)/.match(sqls.pop)
sqls.must_equal []
m.wont_equal nil
map = {'l1'=>1234, 'l2'=>5, 'r1'=>2345, 'r2'=>8}
%w[l1 l2 r1 r2].each do |x|
v = false
4.times do |i| i += 1
if m[i] == x
m[i+4].must_equal map[x].to_s
v = true
end
end
v.must_equal true
end
end
it "should have the add_ method respect composite keys" do
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :z]
@c1.dataset = @c1.dataset.with_fetch(:id=>2345, :z=>8)
@c1.set_primary_key [:id, :z]
n = @c2.load(:id => 1234, :x=>5)
a = @c1.load(:id => 2345, :z=>8)
n.add_attribute([2345, 8]).must_equal a
DB.sqls.must_equal ["SELECT * FROM attributes WHERE ((id = 2345) AND (z = 8)) LIMIT 1",
"INSERT INTO attributes_nodes (l1, l2, r1, r2) VALUES (1234, 5, 2345, 8)"]
end
it "should have the remove_ method respect the :left_primary_key and :right_primary_key options" do
@c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
n = @c2.new(:id => 1234, :xxx=>5)
a = @c1.new(:id => 2345, :yyy=>8)
n.remove_attribute(a).must_equal a
DB.sqls.must_equal ['DELETE FROM attributes_nodes WHERE ((node_id = 5) AND (attribute_id = 8))']
end
it "should have the remove_ method respect composite keys" do
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :z]
n = @c2.load(:id => 1234, :x=>5)
a = @c1.load(:id => 2345, :z=>8)
a.must_equal n.remove_attribute(a)
DB.sqls.must_equal ["DELETE FROM attributes_nodes WHERE ((l1 = 1234) AND (l2 = 5) AND (r1 = 2345) AND (r2 = 8))"]
end
it "should accept a array of composite primary key values for the remove_ method and remove an existing record" do
@c1.dataset = @c1.dataset.with_fetch(:id=>234, :y=>8)
@c1.set_primary_key [:id, :y]
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
@c1.load(:id => 234, :y=>8).must_equal n.remove_attribute([234, 8])
DB.sqls.must_equal ["SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (attributes.id = 234) AND (attributes.y = 8)) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 234))"]
end
it "should raise an error if the model object doesn't have a valid primary key" do
@c2.many_to_many :attributes, :class => @c1
a = @c2.new
n = @c1.load(:id=>123)
proc{a.attributes_dataset}.must_raise(Sequel::Error)
proc{a.add_attribute(n)}.must_raise(Sequel::Error)
proc{a.remove_attribute(n)}.must_raise(Sequel::Error)
proc{a.remove_all_attributes}.must_raise(Sequel::Error)
end
it "should save the associated object first in add_ if passed a new model object" do
@c2.many_to_many :attributes, :class => @c1
n = @c1.new
a = @c2.load(:id=>123)
n.new?.must_equal true
@c1.dataset = @c1.dataset.with_fetch(:id=>1)
a.add_attribute(n)
n.new?.must_equal false
end
it "should raise a ValidationFailed in add_ if the associated object is new and invalid" do
@c2.many_to_many :attributes, :class => @c1
n = @c1.new
a = @c2.load(:id=>123)
def n.validate() errors.add(:id, 'foo') end
proc{a.add_attribute(n)}.must_raise(Sequel::ValidationFailed)
end
it "should raise an Error in add_ if the associated object is new and invalid and raise_on_save_failure is false" do
@c2.many_to_many :attributes, :class => @c1
n = @c1.new
n.raise_on_save_failure = false
a = @c2.load(:id=>123)
def n.validate() errors.add(:id, 'foo') end
proc{a.add_attribute(n)}.must_raise(Sequel::Error)
end
it "should not attempt to validate the associated object in add_ if the :validate=>false option is used" do
@c2.many_to_many :attributes, :class => @c1, :validate=>false
n = @c1.new
a = @c2.load(:id=>123)
def n.validate() errors.add(:id, 'foo') end
@c1.dataset = @c1.dataset.with_fetch(:id=>1)
a.add_attribute(n)
n.new?.must_equal false
end
it "should raise an error if trying to remove a model object that doesn't have a valid primary key" do
@c2.many_to_many :attributes, :class => @c1
n = @c1.new
a = @c2.load(:id=>123)
proc{a.remove_attribute(n)}.must_raise(Sequel::Error)
end
it "should provide an array with all members of the association" do
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes.must_equal [@c1.load({})]
DB.sqls.must_equal ['SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)']
end
it "should populate cache when accessed" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
n.associations.include?(:attributes).must_equal false
atts = n.attributes
atts.must_equal n.associations[:attributes]
end
it "should use cache if available" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
n.associations[:attributes] = 42
n.attributes.must_equal 42
DB.sqls.must_equal []
end
it "should not use cache if asked to reload" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
n.associations[:attributes] = 42
n.attributes(:reload=>true).wont_equal 42
DB.sqls.must_equal ["SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234)"]
end
it "should add item to cache if it exists when calling add_" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
att = @c1.load(:id => 345)
a = []
n.associations[:attributes] = a
n.add_attribute(att)
a.must_equal [att]
end
it "should add item to reciprocal's cache if it exists when calling add_" do
@c2.many_to_many :attributes, :class => @c1
@c1.many_to_many :nodes, :class => @c2
n = @c2.new(:id => 1234)
att = @c1.load(:id => 345)
att.associations[:nodes] = []
n.add_attribute(att)
att.nodes.must_equal [n]
end
it "should remove item from cache if it exists when calling remove_" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
att = @c1.load(:id => 345)
a = [att]
n.associations[:attributes] = a
n.remove_attribute(att)
a.must_equal []
end
it "should remove item from reciprocal's if it exists when calling remove_" do
@c2.many_to_many :attributes, :class => @c1
@c1.many_to_many :nodes, :class => @c2
n = @c2.new(:id => 1234)
att = @c1.new(:id => 345)
att.associations[:nodes] = [n]
n.remove_attribute(att)
att.nodes.must_equal []
end
it "should not create the add_, remove_, or remove_all_ methods if :read_only option is used" do
@c2.many_to_many :attributes, :class => @c1, :read_only=>true
im = @c2.instance_methods
im.must_include(:attributes)
im.must_include(:attributes_dataset)
im.wont_include(:add_attribute)
im.wont_include(:remove_attribute)
im.wont_include(:remove_all_attributes)
end
it "should not add associations methods directly to class" do
@c2.many_to_many :attributes, :class => @c1
im = @c2.instance_methods
im.must_include(:attributes)
im.must_include(:attributes_dataset)
im.must_include(:add_attribute)
im.must_include(:remove_attribute)
im.must_include(:remove_all_attributes)
im2 = @c2.instance_methods(false)
im2.wont_include(:attributes)
im2.wont_include(:attributes_dataset)
im2.wont_include(:add_attribute)
im2.wont_include(:remove_attribute)
im2.wont_include(:remove_all_attributes)
end
it "should have an remove_all_ method that removes all associations" do
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).remove_all_attributes
DB.sqls.must_equal ['DELETE FROM attributes_nodes WHERE (node_id = 1234)']
end
it "should have the remove_all_ method respect the :left_primary_key option" do
@c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx
@c2.new(:id => 1234, :xxx=>5).remove_all_attributes
DB.sqls.must_equal ['DELETE FROM attributes_nodes WHERE (node_id = 5)']
end
it "should have the remove_all_ method respect composite keys" do
@c2.many_to_many :attributes, :class => @c1, :left_primary_key=>[:id, :x], :left_key=>[:l1, :l2]
@c2.load(:id => 1234, :x=>5).remove_all_attributes
DB.sqls.must_equal ['DELETE FROM attributes_nodes WHERE ((l1 = 1234) AND (l2 = 5))']
end
it "remove_all should set the cached instance variable to []" do
@c2.many_to_many :attributes, :class => @c1
node = @c2.new(:id => 1234)
node.remove_all_attributes
node.associations[:attributes].must_equal []
end
it "remove_all should return the array of previously associated items if the cached instance variable exists" do
@c2.many_to_many :attributes, :class => @c1
attrib = @c1.load(:id=>3)
node = @c2.load(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch([])
node.attributes.must_equal []
node.add_attribute(attrib)
node.associations[:attributes].must_equal [attrib]
node.remove_all_attributes.must_equal [attrib]
end
it "remove_all should return nil if the cached instance variable does not exist" do
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).remove_all_attributes.must_be_nil
end
it "remove_all should remove the current item from all reciprocal instance varaibles if it cached instance variable exists" do
@c2.many_to_many :attributes, :class => @c1
@c1.many_to_many :nodes, :class => @c2
@c1.dataset = @c1.dataset.with_fetch([])
@c2.dataset = @c2.dataset.with_fetch([])
attrib = @c1.load(:id=>3)
node = @c2.new(:id => 1234)
node.attributes.must_equal []
attrib.nodes.must_equal []
node.add_attribute(attrib)
attrib.associations[:nodes].must_equal [node]
node.remove_all_attributes
attrib.associations[:nodes].must_equal []
end
it "add, remove, and remove_all methods should respect :join_table_block option" do
@c2.many_to_many :attributes, :class => @c1, :join_table_block=>proc{|ds| ds.filter(:x=>123)}
o = @c2.load(:id => 1234)
o.add_attribute(@c1.load(:id=>44))
o.remove_attribute(@c1.load(:id=>45))
o.remove_all_attributes
sqls = DB.sqls
sqls.shift =~ /INSERT INTO attributes_nodes \((node_id|attribute_id), (node_id|attribute_id)\) VALUES \((1234|44), (1234|44)\)/
sqls.must_equal ["DELETE FROM attributes_nodes WHERE ((x = 123) AND (node_id = 1234) AND (attribute_id = 45))",
"DELETE FROM attributes_nodes WHERE ((x = 123) AND (node_id = 1234))"]
end
it "should call an _add_ method internally to add attributes" do
@c2.many_to_many :attributes, :class => @c1
@c2.private_instance_methods.must_include(:_add_attribute)
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._add_attribute(x)
@x = x
end
p.add_attribute(c)
p.instance_variable_get(:@x).must_equal c
DB.sqls.must_equal []
end
it "should support an :adder option for defining the _add_ method" do
@c2.many_to_many :attributes, :class => @c1, :adder=>proc{|x| @x = x}
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
p.add_attribute(c)
p.instance_variable_get(:@x).must_equal c
DB.sqls.must_equal []
end
it "should allow additional arguments given to the add_ method and pass them onwards to the _add_ method" do
@c2.many_to_many :attributes, :class => @c1
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._add_attribute(x,*y)
@x = x
@y = y
end
p.add_attribute(c,:foo,:bar=>:baz)
p.instance_variable_get(:@x).must_equal c
p.instance_variable_get(:@y).must_equal [:foo,{:bar=>:baz}]
end
it "should call a _remove_ method internally to remove attributes" do
@c2.many_to_many :attributes, :class => @c1
@c2.private_instance_methods.must_include(:_remove_attribute)
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._remove_attribute(x)
@x = x
end
p.remove_attribute(c)
p.instance_variable_get(:@x).must_equal c
DB.sqls.must_equal []
end
it "should support a :remover option for defining the _remove_ method" do
@c2.many_to_many :attributes, :class => @c1, :remover=>proc{|x| @x = x}
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
p.remove_attribute(c)
p.instance_variable_get(:@x).must_equal c
DB.sqls.must_equal []
end
it "should allow additional arguments given to the remove_ method and pass them onwards to the _remove_ method" do
@c2.many_to_many :attributes, :class => @c1
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
def p._remove_attribute(x,*y)
@x = x
@y = y
end
p.remove_attribute(c,:foo,:bar=>:baz)
p.instance_variable_get(:@x).must_equal c
p.instance_variable_get(:@y).must_equal [:foo,{:bar=>:baz}]
end
it "should allow additional arguments given to the remove_all_ method and pass them onwards to the _remove_all_ method" do
@c2.many_to_many :attributes, :class => @c1
p = @c2.load(:id=>10)
def p._remove_all_attributes(*y)
@y = y
end
p.remove_all_attributes(:foo,:bar=>:baz)
p.instance_variable_get(:@y).must_equal [:foo,{:bar=>:baz}]
end
it "should call a _remove_all_ method internally to remove attributes" do
@c2.many_to_many :attributes, :class => @c1
@c2.private_instance_methods.must_include(:_remove_all_attributes)
p = @c2.load(:id=>10)
def p._remove_all_attributes
@x = :foo
end
p.remove_all_attributes
p.instance_variable_get(:@x).must_equal :foo
DB.sqls.must_equal []
end
it "should support a :clearer option for defining the _remove_all_ method" do
@c2.many_to_many :attributes, :class => @c1, :clearer=>proc{@x = :foo}
p = @c2.load(:id=>10)
p.remove_all_attributes
p.instance_variable_get(:@x).must_equal :foo
DB.sqls.must_equal []
end
it "should support (before|after)_(add|remove) callbacks" do
h = []
@c2.many_to_many :attributes, :class => @c1, :before_add=>[proc{|x,y| h << x.pk; h << -y.pk}, :blah], :after_add=>proc{h << 3}, :before_remove=>:blah, :after_remove=>[:blahr]
@c2.class_eval do
self::Foo = h
def _add_attribute(v)
model::Foo << 4
end
def _remove_attribute(v)
model::Foo << 5
end
def blah(x)
model::Foo << x.pk
end
def blahr(x)
model::Foo << 6
end
end
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
h.must_equal []
p.add_attribute(c)
h.must_equal [10, -123, 123, 4, 3]
p.remove_attribute(c)
h.must_equal [10, -123, 123, 4, 3, 123, 5, 6]
end
it "should support after_load association callback" do
h = []
@c2.many_to_many :attributes, :class => @c1, :after_load=>[proc{|x,y| h << [x.pk, y.collect{|z|z.pk}]}, :al]
@c2.class_eval do
self::Foo = h
def al(v)
v.each{|x| model::Foo << x.pk}
end
end
@c1.dataset = @c1.dataset.with_fetch([{:id=>20}, {:id=>30}])
p = @c2.load(:id=>10, :parent_id=>20)
attributes = p.attributes
h.must_equal [[10, [20, 30]], 20, 30]
attributes.collect{|a| a.pk}.must_equal [20, 30]
end
it "should raise error and not call internal add or remove method if before callback calls cancel_action if raise_on_save_failure is true" do
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
@c2.many_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
def p.ba(o) cancel_action end
def p._add_attribute; raise; end
def p._remove_attribute; raise; end
p.associations[:attributes] = []
p.raise_on_save_failure = true
proc{p.add_attribute(c)}.must_raise(Sequel::HookFailed)
p.attributes.must_equal []
p.associations[:attributes] = [c]
def p.br(o) cancel_action end
proc{p.remove_attribute(c)}.must_raise(Sequel::HookFailed)
p.attributes.must_equal [c]
end
it "should return nil and not call internal add or remove method if before callback calls cancel_action if raise_on_save_failure is false" do
p = @c2.load(:id=>10)
c = @c1.load(:id=>123)
p.raise_on_save_failure = false
@c2.many_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
def p.ba(o) cancel_action end
def p._add_attribute; raise; end
def p._remove_attribute; raise; end
p.associations[:attributes] = []
p.add_attribute(c).must_be_nil
p.attributes.must_equal []
p.associations[:attributes] = [c]
def p.br(o) cancel_action end
p.remove_attribute(c).must_be_nil
p.attributes.must_equal [c]
end
it "should support a :uniq option that removes duplicates from the association" do
@c2.many_to_many :attributes, :class => @c1, :uniq=>true
@c1.dataset = @c1.dataset.with_fetch([{:id=>20}, {:id=>30}, {:id=>20}, {:id=>30}])
@c2.load(:id=>10, :parent_id=>20).attributes.must_equal [@c1.load(:id=>20), @c1.load(:id=>30)]
end
it "should support a :distinct option that uses the DISTINCT clause" do
@c2.many_to_many :attributes, :class => @c1, :distinct=>true
@c2.load(:id=>10).attributes_dataset.sql.must_equal "SELECT DISTINCT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 10)"
end
it "should not apply association options when removing all associated records" do
@c2.many_to_many :attributes, :class => @c1 do |ds|
ds.filter(:name=>'John')
end
@c2.load(:id=>1).remove_all_attributes
DB.sqls.must_equal ["DELETE FROM attributes_nodes WHERE (node_id = 1)"]
end
it "should use assocation's dataset when grabbing a record to remove from the assocation by primary key" do
@c2.many_to_many :attributes, :class => @c1 do |ds|
ds.filter(:join_table_att=>3)
end
@c1.dataset = @c1.dataset.with_fetch(:id=>2)
@c2.load(:id=>1).remove_attribute(2)
DB.sqls.must_equal ["SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1) AND (join_table_att = 3) AND (attributes.id = 2)) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 1) AND (attribute_id = 2))"]
end
it "should have association dataset use false condition if any key is nil" do
@c1.many_to_many :attributes, :class => @c1, :left_primary_key=>:y
@c1.load(:id=>1).attributes_dataset.sql.must_equal "SELECT attributes.* FROM attributes INNER JOIN attributes_attributes ON (attributes_attributes.attribute_id = attributes.id) WHERE 'f'"
end
end
describe Sequel::Model, "one_through_one" do
before do
@c1 = Class.new(Sequel::Model(:attributes)) do
unrestrict_primary_key
attr_accessor :yyy
def self.name; 'Attribute'; end
def self.to_s; 'Attribute'; end
columns :id, :y, :z
end
@c2 = Class.new(Sequel::Model(:nodes)) do
unrestrict_primary_key
attr_accessor :xxx
def self.name; 'Node'; end
def self.to_s; 'Node'; end
columns :id, :x
end
@dataset = @c2.dataset
@c1.dataset = @c1.dataset.with_autoid(1)
[@c1, @c2].each{|c| c.dataset = c.dataset.with_fetch({})}
DB.reset
end
after do
DB.fetch = {:id => 1, :x => 1}
end
it "should use implicit key values and join table if omitted" do
@c2.one_through_one :attribute, :class => @c1
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1'
end
it "should respect :predicate_key when lazily loading" do
@c2.one_through_one :attribute, :class => @c1, :predicate_key=>Sequel.subscript(Sequel[:attributes_nodes][:node_id], 0)
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id[0] = 1234) LIMIT 1'
end
it "should use explicit key values and join table if given" do
@c2.one_through_one :attribute, :class => @c1, :left_key => :nodeid, :right_key => :attributeid, :join_table => :attribute2node
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attribute2node ON (attribute2node.attributeid = attributes.id) WHERE (attribute2node.nodeid = 1234) LIMIT 1'
end
it "should support a conditions option" do
@c2.one_through_one :attribute, :class => @c1, :conditions => {:a=>32}
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((a = 32) AND (attributes_nodes.node_id = 1234)) LIMIT 1'
@c2.one_through_one :attribute, :class => @c1, :conditions => Sequel.lit('a = ?', 32)
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((a = 32) AND (attributes_nodes.node_id = 1234)) LIMIT 1'
@c2.new(:id => 1234).attribute.must_equal @c1.load({})
end
it "should support an order option" do
@c2.one_through_one :attribute, :class => @c1, :order => :blah
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) ORDER BY blah LIMIT 1'
end
it "should support an array for the order option" do
@c2.one_through_one :attribute, :class => @c1, :order => [:blah1, :blah2]
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) ORDER BY blah1, blah2 LIMIT 1'
end
it "should support :left_primary_key and :right_primary_key options" do
@c2.one_through_one :attribute, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
@c2.new(:id => 1234, :xxx=>5).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.yyy) WHERE (attributes_nodes.node_id = 5) LIMIT 1'
end
it "should support composite keys" do
@c2.one_through_one :attribute, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
@c2.load(:id => 1234, :x=>5).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.r1 = attributes.id) AND (attributes_nodes.r2 = attributes.y)) WHERE ((attributes_nodes.l1 = 1234) AND (attributes_nodes.l2 = 5)) LIMIT 1'
end
it "should not issue query if not all keys have values" do
@c2.one_through_one :attribute, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
@c2.load(:id => 1234, :x=>nil).attribute.must_be_nil
DB.sqls.must_equal []
end
it "should raise an Error unless same number of composite keys used" do
proc{@c2.one_through_one :attribute, :class => @c1, :left_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :left_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :left_key=>[:node_id, :id], :left_primary_key=>:id}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :left_key=>:id, :left_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :left_key=>[:node_id, :id, :x], :left_primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :right_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :right_key=>[:node_id, :id], :right_primary_key=>:id}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :right_key=>:id, :left_primary_key=>[:node_id, :id]}.must_raise(Sequel::Error)
proc{@c2.one_through_one :attribute, :class => @c1, :right_key=>[:node_id, :id, :x], :right_primary_key=>[:parent_id, :id]}.must_raise(Sequel::Error)
end
it "should support a select option" do
@c2.one_through_one :attribute, :class => @c1, :select => :blah
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT blah FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1'
end
it "should support an array for the select option" do
@c2.one_through_one :attribute, :class => @c1, :select => [Sequel::SQL::ColumnAll.new(:attributes), Sequel[:attribute_nodes][:blah2]]
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.*, attribute_nodes.blah2 FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1'
end
it "should accept a block" do
@c2.one_through_one :attribute, :class => @c1 do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (xxx = 555)) LIMIT 1'
end
it "should allow the :order option while accepting a block" do
@c2.one_through_one :attribute, :class => @c1, :order=>[:blah1, :blah2] do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (xxx = 555)) ORDER BY blah1, blah2 LIMIT 1'
end
it "should support a :dataset option that is used instead of the default" do
c1 = @c1
@c2.one_through_one :attribute, :class => @c1, :dataset=>proc{c1.join_table(:natural, :an).filter(Sequel[:an][:nodeid]=>pk)}, :order=> :a, :select=>nil do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attribute_dataset.sql.must_equal 'SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 1'
n.attribute.must_equal @c1.load({})
DB.sqls.must_equal ['SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 1']
end
it "should support a :dataset option that accepts the reflection as an argument" do
@c2.one_through_one :attribute, :class => @c1, :dataset=>lambda{|opts| opts.associated_class.natural_join(:an).filter(Sequel[:an][:nodeid]=>pk)}, :order=> :a, :select=>nil do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 555
n.attribute_dataset.sql.must_equal 'SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 1'
n.attribute.must_equal @c1.load({})
DB.sqls.must_equal ['SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 1']
end
it "should support a :limit option to specify an offset" do
@c2.one_through_one :attribute, :class => @c1 , :limit=>[nil, 10]
@c2.new(:id => 1234).attribute_dataset.sql.must_equal 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1 OFFSET 10'
end
it "should have the :eager option affect the _dataset method" do
@c2.one_through_one :attribute, :class => @c2 , :eager=>:attribute
@c2.new(:id => 1234).attribute_dataset.opts[:eager].must_equal(:attribute=>nil)
end
it "should handle an aliased join table" do
@c2.one_through_one :attribute, :class => @c1, :join_table => Sequel[:attribute2node].as(:attributes_nodes)
n = @c2.load(:id => 1234)
n.attribute_dataset.sql.must_equal "SELECT attributes.* FROM attributes INNER JOIN attribute2node AS attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1"
end
with_symbol_splitting "should handle an aliased join table with splittable symbol" do
@c2.one_through_one :attribute, :class => @c1, :join_table => :attribute2node___attributes_nodes
n = @c2.load(:id => 1234)
n.attribute_dataset.sql.must_equal "SELECT attributes.* FROM attributes INNER JOIN attribute2node AS attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1"
end
it "should raise an error if the model object doesn't have a valid primary key" do
@c2.one_through_one :attribute, :class => @c1
a = @c2.new
proc{a.attribute_dataset}.must_raise(Sequel::Error)
end
it "should provide an array with all members of the association" do
@c2.one_through_one :attribute, :class => @c1
@c2.new(:id => 1234).attribute.must_equal @c1.load({})
DB.sqls.must_equal ['SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1']
end
it "should populate cache when accessed" do
@c2.one_through_one :attribute, :class => @c1
n = @c2.new(:id => 1234)
n.associations.include?(:attribute).must_equal false
atts = n.attribute
atts.must_equal n.associations[:attribute]
end
it "should use cache if available" do
@c2.one_through_one :attribute, :class => @c1
n = @c2.new(:id => 1234)
n.associations[:attribute] = 42
n.attribute.must_equal 42
DB.sqls.must_equal []
end
it "should not use cache if asked to reload" do
@c2.one_through_one :attribute, :class => @c1
n = @c2.new(:id => 1234)
n.associations[:attribute] = 42
n.attribute(:reload=>true).wont_equal 42
DB.sqls.must_equal ["SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 1234) LIMIT 1"]
end
it "should not add associations methods directly to class" do
@c2.one_through_one :attribute, :class => @c1
im = @c2.instance_methods
im.must_include(:attribute)
im.must_include(:attribute_dataset)
im2 = @c2.instance_methods(false)
im2.wont_include(:attribute)
im2.wont_include(:attribute_dataset)
end
it "should support after_load association callback" do
h = []
@c2.one_through_one :attribute, :class => @c1, :after_load=>[proc{|x,y| h << [x.pk, y.pk]}, :al]
@c2.class_eval do
self::Foo = h
def al(v)
model::Foo << v.pk
end
end
@c1.dataset = @c1.dataset.with_fetch([{:id=>20}])
p = @c2.load(:id=>10, :parent_id=>20)
attribute = p.attribute
h.must_equal [[10, 20], 20]
attribute.pk.must_equal 20
end
it "should support a :distinct option that uses the DISTINCT clause" do
@c2.one_through_one :attribute, :class => @c1, :distinct=>true
@c2.load(:id=>10).attribute_dataset.sql.must_equal "SELECT DISTINCT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE (attributes_nodes.node_id = 10) LIMIT 1"
end
it "should not add a setter method if the :read_only option is true" do
@c2.one_through_one :attribute, :class => @c1, :read_only=>true
im = @c2.instance_methods
im.must_include(:attribute)
im.wont_include(:attribute=)
end
it "should add a setter method" do
@c2.one_through_one :attribute, :class => @c1
attrib = @c1.new(:id=>3)
DB.fetch = []
o = @c2.load(:id => 1234)
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 1234) LIMIT 1"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 1234) LIMIT 1",
"INSERT INTO attributes_nodes (attribute_id, node_id) VALUES (3, 1234)"]
DB.fetch = {:node_id=>1234, :attribute_id=>5}
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 1234) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 5))"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 1234) LIMIT 1",
"UPDATE attributes_nodes SET attribute_id = 3 WHERE ((node_id = 1234) AND (attribute_id = 5))"]
@c2.load(:id => 1234).attribute = @c1.new(:id=>5)
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 1234) LIMIT 1"]
end
it "should use a transaction in the setter method" do
@c2.one_through_one :attribute, :class => @c1
@c2.use_transactions = true
@c1.load(:id=>3)
DB.fetch = []
@c2.new(:id => 1234).attribute = nil
DB.sqls.must_equal ['BEGIN',
"SELECT * FROM attributes_nodes WHERE (node_id = 1234) LIMIT 1",
'COMMIT']
end
it "should have setter method respect :join_table_block option" do
@c2.one_through_one :attribute, :class => @c1, :join_table_block=>proc{|ds| ds.where(:a)}
attrib = @c1.new(:id=>3)
DB.fetch = []
o = @c2.new(:id => 1234)
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (a AND (node_id = 1234)) LIMIT 1"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (a AND (node_id = 1234)) LIMIT 1",
"INSERT INTO attributes_nodes (attribute_id, node_id) VALUES (3, 1234)"]
DB.fetch = {:node_id=>1234, :attribute_id=>5}
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (a AND (node_id = 1234)) LIMIT 1",
"DELETE FROM attributes_nodes WHERE (a AND (node_id = 1234) AND (attribute_id = 5))"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (a AND (node_id = 1234)) LIMIT 1",
"UPDATE attributes_nodes SET attribute_id = 3 WHERE (a AND (node_id = 1234) AND (attribute_id = 5))"]
end
it "should have the setter method respect the :left_primary_key and :right_primary_key option" do
@c2.one_through_one :attribute, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
attrib = @c1.new(:id=>3, :yyy=>7)
DB.fetch = []
o = @c2.new(:id => 1234, :xxx=>5)
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 5) LIMIT 1"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 5) LIMIT 1",
"INSERT INTO attributes_nodes (attribute_id, node_id) VALUES (7, 5)"]
DB.fetch = {:node_id=>1234, :attribute_id=>9}
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 5) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 5) AND (attribute_id = 9))"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE (node_id = 5) LIMIT 1",
"UPDATE attributes_nodes SET attribute_id = 7 WHERE ((node_id = 5) AND (attribute_id = 9))"]
end
it "should have the setter method respect composite keys" do
@c2.one_through_one :attribute, :class => @c1, :left_key=>[:node_id, :y], :left_primary_key=>[:id, :x], :right_key=>[:attribute_id, :z], :right_primary_key=>[:id, :w]
attrib = @c1.load(:id=>3, :w=>7)
@c1.def_column_alias :w, :w
DB.fetch = []
o = @c2.new(:id => 1234, :x=>5)
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE ((node_id = 1234) AND (y = 5)) LIMIT 1"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE ((node_id = 1234) AND (y = 5)) LIMIT 1",
"INSERT INTO attributes_nodes (attribute_id, z, node_id, y) VALUES (3, 7, 1234, 5)"]
DB.fetch = {:node_id=>1234, :attribute_id=>10, :y=>6, :z=>8}
o.attribute = nil
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE ((node_id = 1234) AND (y = 5)) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (y = 5) AND (attribute_id = 10) AND (z = 8))"]
o.attribute = attrib
DB.sqls.must_equal ["SELECT * FROM attributes_nodes WHERE ((node_id = 1234) AND (y = 5)) LIMIT 1",
"UPDATE attributes_nodes SET attribute_id = 3, z = 7 WHERE ((node_id = 1234) AND (y = 5) AND (attribute_id = 10) AND (z = 8))"]
end
it "should raise an error if the current model object that doesn't have a valid primary key" do
@c2.one_through_one :attribute, :class => @c1
p = @c2.new
c = @c2.load(:id=>123)
proc{c.attribute = p}.must_raise(Sequel::Error)
end
it "should raise an error if the associated object that doesn't have a valid primary key" do
@c2.one_through_one :attribute, :class => @c1
p = @c2.new
c = @c2.load(:id=>123)
proc{p.attribute = c}.must_raise(Sequel::Error)
end
it "should make the change to the foreign_key value inside a _association= method" do
@c2.one_through_one :attribute, :class => @c1
@c2.private_instance_methods.must_include(:_attribute=)
attrib = @c1.new(:id=>3)
o = @c2.new(:id => 1234)
def o._attribute=(x)
@x = x
end
o.attribute = attrib
o.instance_variable_get(:@x).must_equal attrib
end
it "should have a :setter option define the _association= method" do
@c2.one_through_one :attribute, :class => @c1, :setter=>proc{|x| @x = x}
attrib = @c1.new(:id=>3)
o = @c2.new(:id => 1234)
o.attribute = attrib
o.instance_variable_get(:@x).must_equal attrib
end
it "should support (before|after)_set callbacks" do
h = []
@c2.one_through_one :attribute, :class => @c1, :before_set=>[proc{|x,y| h << x.pk; h << (y ? -y.pk : :y)}, :blah], :after_set=>proc{h << :l}
@c2.class_eval do
self::Foo = h
def blah(x)
model::Foo << (x ? x.pk : :x)
end
end
attrib = @c1.new(:id=>3)
o = @c2.new(:id => 1234)
h.must_equal []
o.attribute = attrib
h.must_equal [1234, -3, 3, :l]
o.attribute = nil
h.must_equal [1234, -3, 3, :l, 1234, :y, :x, :l]
end
it "should have association dataset use false condition if any key is nil" do
@c1.one_through_one :attribute, :class => @c1, :left_primary_key=>:y
@c1.load(:id=>1).attribute_dataset.sql.must_equal "SELECT attributes.* FROM attributes INNER JOIN attributes_attributes ON (attributes_attributes.attribute_id = attributes.id) WHERE 'f' LIMIT 1"
end
end
describe "many_to_many/one_through_one associations with :join_table_db" do
before do
@db1, @db2, @db3 = @dbs = 3.times.map{Sequel.mock(:fetch=>{:id => 1, :x => 1}, :numrows=>1, :autoid=>proc{|sql| 10})}
@c1 = Class.new(Sequel::Model(@db1[:attributes])) do
unrestrict_primary_key
attr_accessor :yyy
def self.name; 'Attribute'; end
def self.to_s; 'Attribute'; end
columns :id, :y, :z
end
@c2 = Class.new(Sequel::Model(@db2[:nodes])) do
unrestrict_primary_key
attr_accessor :xxx
def self.name; 'Node'; end
def self.to_s; 'Node'; end
columns :id, :x
end
@c1.default_association_options[:join_table_db] = @db3
@c2.default_association_options[:join_table_db] = @db3
@db3.fetch = {:attribute_id=>555}
@db1.fetch = {:id=>555}
sqls
end
def sqls
@dbs.map(&:sqls)
end
it "should support dataset method" do
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes_dataset.sql.must_equal "SELECT attributes.* FROM attributes WHERE (id IN (555))"
sqls.must_equal [[], [], ["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
end
it "should support association method" do
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).attributes.must_equal [@c1.load(:id=>555)]
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE (id IN (555))"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
@c2.one_through_one :attribute, :class => @c1
@db1.fetch = [{:id=>555, :x=>1}, {:id=>555, :x=>2}]
@c2.new(:id => 1234).attribute.must_equal @c1.load(:id=>555, :x=>1)
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE (id IN (555)) LIMIT 1"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
end
it "should support an existing selection on the dataset" do
@c1.dataset = @c1.dataset.select(Sequel.qualify(:attributes, :id), Sequel.qualify(:attributes, :b))
@db1.sqls
@c2.many_to_many :attributes, :class => @c1
@db1.fetch = {:id=>555, :b=>10}
@c2.new(:id => 1234).attributes.must_equal [@c1.load(:id=>555, :b=>10)]
sqls.must_equal [["SELECT attributes.id, attributes.b FROM attributes WHERE (id IN (555))"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
end
it "should support a conditions option" do
@c2.many_to_many :attributes, :class => @c1, :conditions => {:a=>32}
@c2.new(:id => 1234).attributes.must_equal [@c1.load(:id=>555)]
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE ((a = 32) AND (id IN (555)))"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
end
it "should support an order option" do
@c2.many_to_many :attributes, :class => @c1, :order => :blah
@c2.new(:id => 1234).attributes.must_equal [@c1.load(:id=>555)]
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE (id IN (555)) ORDER BY blah"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
end
it "should support :left_primary_key and :right_primary_key options" do
@c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
@db3.fetch = {:attribute_id=>555, :node_id=>5}
@db1.fetch = {:id=>14, :yyy=>555}
@c2.new(:id => 1234, :xxx=>5).attributes.must_equal [@c1.load(:id=>14, :yyy=>555)]
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE (yyy IN (555))"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 5)"]]
end
it "should support composite keys" do
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
@db3.fetch = {:l1=>555, :l2=>5, :r1=>14, :r2=>555}
@db1.fetch = {:id=>14, :y=>555}
@c2.new(:id => 1234, :x=>5).attributes.must_equal [@c1.load(:id=>14, :y=>555)]
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE ((id, y) IN ((14, 555)))"], [],
["SELECT r1, r2 FROM attributes_nodes WHERE ((l1 = 1234) AND (l2 = 5))"]]
end
it "should handle case where join table query does not produce any rows" do
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
@db3.fetch = []
@db1.fetch = []
@c2.load(:id => 1234, :x=>5).attributes.must_equal []
sqls.must_equal [[], [], ["SELECT r1, r2 FROM attributes_nodes WHERE ((l1 = 1234) AND (l2 = 5))"]]
end
it "should handle case where join table query returns a NULL value" do
@db1.fetch = []
@c2.many_to_many :attributes, :class => @c1
@db3.fetch = {:attribute_id=>nil}
@c2.new(:id => 1234).attributes.must_equal []
sqls.must_equal [[], [], ["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
@db3.fetch = {:r1=>nil, :r2=>3}
@c2.load(:id => 1234, :x=>5).attributes.must_equal []
sqls.must_equal [[], [], ["SELECT r1, r2 FROM attributes_nodes WHERE ((l1 = 1234) AND (l2 = 5))"]]
end
it "should support a select option" do
@c2.many_to_many :attributes, :class => @c1, :select => :blah
@db1.fetch = {:blah=>19}
@c2.new(:id => 1234).attributes.must_equal [@c1.load(:blah=>19)]
sqls.must_equal [["SELECT blah FROM attributes WHERE (id IN (555))"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
end
it "should accept a block" do
@c2.many_to_many :attributes, :class => @c1 do |ds|
ds.filter(:xxx => @xxx)
end
n = @c2.new(:id => 1234)
n.xxx = 444
n.attributes.must_equal [@c1.load(:id=>555)]
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE ((id IN (555)) AND (xxx = 444))"], [],
["SELECT attribute_id FROM attributes_nodes WHERE (node_id = 1234)"]]
end
it "should handle an aliased join table" do
@c2.many_to_many :attributes, :class => @c1, :join_table => Sequel[:attribute2node].as(:attributes_nodes)
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345)
n.attributes.must_equal [@c1.load(:id=>555)]
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE (id IN (555))"], [],
["SELECT attribute_id FROM attribute2node AS attributes_nodes WHERE (node_id = 1234)"]]
a.must_equal n.add_attribute(a)
a.must_equal n.remove_attribute(a)
n.remove_all_attributes
sqls.must_equal [[], [],
["INSERT INTO attribute2node (node_id, attribute_id) VALUES (1234, 2345)",
"DELETE FROM attribute2node WHERE ((node_id = 1234) AND (attribute_id = 2345))",
"DELETE FROM attribute2node WHERE (node_id = 1234)"]]
end
it "should define an add_ method that works on existing records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a = @c1.load(:id => 2345)
n.add_attribute(a).must_equal a
sqls.must_equal [[], [], ["INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2345)"]]
end
it "should define a remove_ method that works on existing records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a = @c1.new(:id => 2345)
n.remove_attribute(a).must_equal a
sqls.must_equal [[], [], ['DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 2345))']]
end
it "should have an remove_all_ method that removes all associations" do
@c2.many_to_many :attributes, :class => @c1
@c2.new(:id => 1234).remove_all_attributes
sqls.must_equal [[], [], ['DELETE FROM attributes_nodes WHERE (node_id = 1234)']]
end
it "should support eager loading" do
@db2.fetch = [{:id=>1234}, {:id=>33}]
@db3.fetch = [{:attribute_id=>555, :node_id=>1234}]
@c2.many_to_many :attributes, :class => @c1
a = @c2.eager(:attributes).all
a.must_equal [@c2.load(:id=>1234), @c2.load(:id=>33)]
a[0].associations[:attributes].must_equal [@c1.load(:id=>555)]
a[1].associations[:attributes].must_equal []
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE (id IN (555))"],
["SELECT * FROM nodes"],
["SELECT attribute_id, node_id FROM attributes_nodes WHERE (node_id IN (1234, 33))"]]
@c2.one_through_one :attribute, :clone=>:attributes
@db1.fetch = [{:id=>555, :x=>1}, {:id=>555, :x=>2}]
a = @c2.eager(:attribute).all
a.must_equal [@c2.load(:id=>1234), @c2.load(:id=>33)]
a[0].associations[:attribute].must_equal @c1.load(:id=>555, :x=>1)
a[1].associations[:attribute].must_be_nil
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE (id IN (555))"],
["SELECT * FROM nodes"],
["SELECT attribute_id, node_id FROM attributes_nodes WHERE (node_id IN (1234, 33))"]]
end
it "should skip loading associated table when the join table has no results" do
@db2.fetch = [{:id=>1234}, {:id=>33}]
@db3.fetch = []
@c2.many_to_many :attributes, :class => @c1
a = @c2.eager(:attributes).all
a.must_equal [@c2.load(:id=>1234), @c2.load(:id=>33)]
a[0].associations[:attributes].must_equal []
a[1].associations[:attributes].must_equal []
sqls.must_equal [[], ["SELECT * FROM nodes"],
["SELECT attribute_id, node_id FROM attributes_nodes WHERE (node_id IN (1234, 33))"]]
@c2.one_through_one :attribute, :clone=>:attributes
@db1.fetch = [{:id=>555, :x=>1}, {:id=>555, :x=>2}]
a = @c2.eager(:attribute).all
a.must_equal [@c2.load(:id=>1234), @c2.load(:id=>33)]
a[0].associations[:attribute].must_be_nil
a[1].associations[:attribute].must_be_nil
sqls.must_equal [[], ["SELECT * FROM nodes"],
["SELECT attribute_id, node_id FROM attributes_nodes WHERE (node_id IN (1234, 33))"]]
end
it "should support eager loading when the join table includes NULL values" do
@db2.fetch = [{:id=>1234}, {:id=>33}]
@db3.fetch = [{:attribute_id=>nil, :node_id=>1234}]
@c2.many_to_many :attributes, :class => @c1
a = @c2.eager(:attributes).all
a.must_equal [@c2.load(:id=>1234), @c2.load(:id=>33)]
a[0].associations[:attributes].must_equal []
a[1].associations[:attributes].must_equal []
sqls.must_equal [[], ["SELECT * FROM nodes"],
["SELECT attribute_id, node_id FROM attributes_nodes WHERE (node_id IN (1234, 33))"]]
@c2.one_through_one :attribute, :clone=>:attributes
a = @c2.eager(:attribute).all
a.must_equal [@c2.load(:id=>1234), @c2.load(:id=>33)]
a[0].associations[:attribute].must_be_nil
a[1].associations[:attribute].must_be_nil
sqls.must_equal [[], ["SELECT * FROM nodes"],
["SELECT attribute_id, node_id FROM attributes_nodes WHERE (node_id IN (1234, 33))"]]
end
it "should support eager loading when using composite keys" do
@db1.fetch = {:id=>14, :y=>555}
@db2.fetch = [{:id=>1234, :x=>333}, {:id=>33, :x=>4}]
@db3.fetch = [{:l1=>1234, :l2=>333, :r1=>14, :r2=>555}]
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
a = @c2.eager(:attributes).all
a.must_equal [@c2.load(:id=>1234, :x=>333), @c2.load(:id=>33, :x=>4)]
a[0].associations[:attributes].must_equal [@c1.load(:id=>14, :y=>555)]
a[1].associations[:attributes].must_equal []
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE ((id, y) IN ((14, 555)))"],
["SELECT * FROM nodes"],
["SELECT r1, r2, l1, l2 FROM attributes_nodes WHERE ((l1, l2) IN ((1234, 333), (33, 4)))"]]
@c2.one_through_one :attribute, :clone=>:attributes
@db1.fetch = [{:id=>14, :y=>555, :z=>2}, {:id=>14, :y=>555, :z=>3}]
a = @c2.eager(:attribute).all
a.must_equal [@c2.load(:id=>1234, :x=>333), @c2.load(:id=>33, :x=>4)]
a[0].associations[:attribute].must_equal @c1.load(:id=>14, :y=>555, :z=>2)
a[1].associations[:attribute].must_be_nil
sqls.must_equal [["SELECT attributes.* FROM attributes WHERE ((id, y) IN ((14, 555)))"],
["SELECT * FROM nodes"],
["SELECT r1, r2, l1, l2 FROM attributes_nodes WHERE ((l1, l2) IN ((1234, 333), (33, 4)))"]]
end
it "should support eager loading when using composite keys when the join table includes NULL values" do
@db2.fetch = [{:id=>1234, :x=>333}, {:id=>33, :x=>4}]
@db3.fetch = [{:l1=>1234, :l2=>333, :r1=>nil, :r2=>555}, {:l1=>1234, :l2=>333, :r1=>14, :r2=>nil}, {:l1=>1234, :l2=>333, :r1=>nil, :r2=>nil}]
@c2.many_to_many :attributes, :class => @c1, :left_key=>[:l1, :l2], :right_key=>[:r1, :r2], :left_primary_key=>[:id, :x], :right_primary_key=>[:id, :y]
a = @c2.eager(:attributes).all
a.must_equal [@c2.load(:id=>1234, :x=>333), @c2.load(:id=>33, :x=>4)]
a[0].associations[:attributes].must_equal []
a[1].associations[:attributes].must_equal []
sqls.must_equal [[], ["SELECT * FROM nodes"],
["SELECT r1, r2, l1, l2 FROM attributes_nodes WHERE ((l1, l2) IN ((1234, 333), (33, 4)))"]]
@c2.one_through_one :attribute, :clone=>:attributes
a = @c2.eager(:attribute).all
a.must_equal [@c2.load(:id=>1234, :x=>333), @c2.load(:id=>33, :x=>4)]
a[0].associations[:attribute].must_be_nil
a[1].associations[:attribute].must_be_nil
sqls.must_equal [[], ["SELECT * FROM nodes"],
["SELECT r1, r2, l1, l2 FROM attributes_nodes WHERE ((l1, l2) IN ((1234, 333), (33, 4)))"]]
end
end
describe "Filtering by associations" do
before(:all) do
db = Sequel.mock
db.extend_datasets do
def supports_window_functions?; true; end
def supports_distinct_on?; true; end
end
@Album = Class.new(Sequel::Model(db[:albums]))
artist = @Artist = Class.new(Sequel::Model(db[:artists]))
tag = @Tag = Class.new(Sequel::Model(db[:tags]))
track = @Track = Class.new(Sequel::Model(db[:tracks]))
album_info = @AlbumInfo = Class.new(Sequel::Model(db[:album_infos]))
@Artist.columns :id, :id1, :id2
@Tag.columns :id, :tid1, :tid2
@Track.columns :id, :album_id, :album_id1, :album_id2
@AlbumInfo.columns :id, :album_id, :album_id1, :album_id2
@Album.class_eval do
columns :id, :id1, :id2, :artist_id, :artist_id1, :artist_id2
b = lambda{|ds| ds.where(:name=>'B')}
c = {:name=>'A'}
many_to_one :no_artist, :class=>artist, :key=>:artist_id, :allow_filtering_by=>false
many_to_one :artist, :class=>artist, :key=>:artist_id
one_to_many :tracks, :class=>track, :key=>:album_id
one_to_one :track, :class=>track, :key=>:album_id
one_to_one :album_info, :class=>album_info, :key=>:album_id
many_to_many :tags, :class=>tag, :left_key=>:album_id, :join_table=>:albums_tags, :right_key=>:tag_id
many_to_one :a_artist, :clone=>:artist, :conditions=>c
one_to_many :a_tracks, :clone=>:tracks, :conditions=>c
one_to_one :a_album_info, :clone=>:album_info, :conditions=>c
many_to_many :a_tags, :clone=>:tags, :conditions=>c
many_to_one :b_artist, :clone=>:artist, &b
one_to_many :b_tracks, :clone=>:tracks, &b
one_to_one :b_album_info, :clone=>:album_info, &b
many_to_many :b_tags, :clone=>:tags, &b
one_to_many :l_tracks, :clone=>:tracks, :limit=>10
one_to_one :l_track, :clone=>:tracks, :order=>:name
many_to_many :l_tags, :clone=>:tags, :limit=>10
one_through_one :l_tag, :clone=>:tags, :order=>:name
one_to_many :al_tracks, :clone=>:l_tracks, :conditions=>c
one_to_one :al_track, :clone=>:l_track, :conditions=>c
many_to_many :al_tags, :clone=>:l_tags, :conditions=>c
one_through_one :al_tag, :clone=>:l_tag, :conditions=>c
many_to_one :cartist, :class=>artist, :key=>[:artist_id1, :artist_id2], :primary_key=>[:id1, :id2]
one_to_many :ctracks, :class=>track, :key=>[:album_id1, :album_id2], :primary_key=>[:id1, :id2]
one_to_one :calbum_info, :class=>album_info, :key=>[:album_id1, :album_id2], :primary_key=>[:id1, :id2]
many_to_many :ctags, :class=>tag, :left_key=>[:album_id1, :album_id2], :left_primary_key=>[:id1, :id2], :right_key=>[:tag_id1, :tag_id2], :right_primary_key=>[:tid1, :tid2], :join_table=>:albums_tags
many_to_one :a_cartist, :clone=>:cartist, :conditions=>c
one_to_many :a_ctracks, :clone=>:ctracks, :conditions=>c
one_to_one :a_calbum_info, :clone=>:calbum_info, :conditions=>c
many_to_many :a_ctags, :clone=>:ctags, :conditions=>c
many_to_one :b_cartist, :clone=>:cartist, &b
one_to_many :b_ctracks, :clone=>:ctracks, &b
one_to_one :b_calbum_info, :clone=>:calbum_info, &b
many_to_many :b_ctags, :clone=>:ctags, &b
one_to_many :l_ctracks, :clone=>:ctracks, :limit=>10
one_to_one :l_ctrack, :clone=>:ctracks, :order=>:name
many_to_many :l_ctags, :clone=>:ctags, :limit=>10
one_through_one :l_ctag, :clone=>:ctags, :order=>:name
one_to_many :al_ctracks, :clone=>:l_ctracks, :conditions=>c
one_to_one :al_ctrack, :clone=>:l_ctrack, :conditions=>c
many_to_many :al_ctags, :clone=>:l_ctags, :conditions=>c
one_through_one :al_ctag, :clone=>:l_ctag, :conditions=>c
end
end
after do
@Album.default_eager_limit_strategy = true
end
it "should be able to filter on many_to_one associations" do
@Album.filter(:artist=>@Artist.load(:id=>3)).sql.must_equal 'SELECT * FROM albums WHERE (albums.artist_id = 3)'
end
it "should be able to filter on one_to_many associations" do
@Album.filter(:tracks=>@Track.load(:album_id=>3)).sql.must_equal 'SELECT * FROM albums WHERE (albums.id = 3)'
end
it "should be able to filter on one_to_one associations" do
@Album.filter(:album_info=>@AlbumInfo.load(:album_id=>3)).sql.must_equal 'SELECT * FROM albums WHERE (albums.id = 3)'
end
it "should be able to filter on many_to_many associations" do
@Album.filter(:tags=>@Tag.load(:id=>3)).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id = 3) AND (albums_tags.album_id IS NOT NULL))))'
end
it "should be able to filter on many_to_one associations with :conditions" do
@Album.filter(:a_artist=>@Artist.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.artist_id IN (SELECT artists.id FROM artists WHERE ((name = 'A') AND (artists.id IS NOT NULL) AND (artists.id = 3))))"
end
it "should be able to filter on one_to_many associations with :conditions" do
@Album.filter(:a_tracks=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :conditions" do
@Album.filter(:a_album_info=>@AlbumInfo.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id = 5))))"
end
it "should be able to filter on many_to_many associations with :conditions" do
@Album.filter(:a_tags=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND (tags.id = 3))))"
end
it "should be able to filter on many_to_one associations with block" do
@Album.filter(:b_artist=>@Artist.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.artist_id IN (SELECT artists.id FROM artists WHERE ((name = 'B') AND (artists.id IS NOT NULL) AND (artists.id = 3))))"
end
it "should be able to filter on one_to_many associations with block" do
@Album.filter(:b_tracks=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'B') AND (tracks.album_id IS NOT NULL) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with block" do
@Album.filter(:b_album_info=>@AlbumInfo.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id = 5))))"
end
it "should be able to filter on many_to_many associations with block" do
@Album.filter(:b_tags=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'B') AND (albums_tags.album_id IS NOT NULL) AND (tags.id = 3))))"
end
it "should not use an eager limit strategy if the strategy is not specified and the model defaults to not using one" do
@Album.default_eager_limit_strategy = nil
@Album.one_to_many :l_tracks2, :clone=>:l_tracks
@Album.filter(:l_tracks2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_many associations with :limit" do
@Album.filter(:l_tracks=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order" do
@Album.filter(:l_track=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT DISTINCT ON (tracks.album_id) tracks.id FROM tracks ORDER BY tracks.album_id, name)) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :filter_limit_strategy" do
@Album.one_to_one :l_track2, :clone=>:track, :filter_limit_strategy=>:window_function
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x = 1))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :eager_limit_strategy" do
@Album.one_to_one :l_track2, :clone=>:track, :eager_limit_strategy=>:window_function
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x = 1))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and :filter_limit_strategy" do
@Album.one_to_one :l_track2, :clone=>:l_track, :filter_limit_strategy=>:window_function
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id ORDER BY name) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x = 1))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and :eager_limit_strategy" do
@Album.one_to_one :l_track2, :clone=>:l_track, :eager_limit_strategy=>:window_function
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id ORDER BY name) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x = 1))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and Model.default_eager_limit_strategy" do
@Album.default_eager_limit_strategy = :window_function
@Album.one_to_one :l_track2, :clone=>:l_track
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id ORDER BY name) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x = 1))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and :eager_limit_strategy=>:union" do
@Album.one_to_one :l_track2, :clone=>:l_track, :eager_limit_strategy=>:union
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT DISTINCT ON (tracks.album_id) tracks.id FROM tracks ORDER BY tracks.album_id, name)) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and :eager_limit_strategy=>:ruby" do
@Album.one_to_one :l_track2, :clone=>:l_track, :eager_limit_strategy=>:ruby
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT DISTINCT ON (tracks.album_id) tracks.id FROM tracks ORDER BY tracks.album_id, name)) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :filter_limit_strategy :correlated_subquery" do
@Album.one_to_one :l_track2, :clone=>:track, :filter_limit_strategy=>:correlated_subquery
@Album.filter(:l_track2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT t1.id FROM tracks AS t1 WHERE (t1.album_id = tracks.album_id) LIMIT 1)) AND (tracks.id = 5))))"
end
it "should be able to filter on many_to_many associations with :limit" do
@Album.filter(:l_tags=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((albums_tags.album_id IS NOT NULL) AND ((albums_tags.album_id, tags.id) IN (SELECT b, c FROM (SELECT albums_tags.album_id AS b, tags.id AS c, row_number() OVER (PARTITION BY albums_tags.album_id) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id)) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 3))))"
end
it "should be able to filter on one_through_one associations with :order" do
@Album.filter(:l_tag=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((albums_tags.album_id IS NOT NULL) AND ((albums_tags.album_id, tags.id) IN (SELECT DISTINCT ON (albums_tags.album_id) albums_tags.album_id, tags.id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) ORDER BY albums_tags.album_id, name)) AND (tags.id = 3))))"
end
it "should be able to filter on one_to_many associations with :limit and :conditions" do
@Album.filter(:al_tracks=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id) AS x_sequel_row_number_x FROM tracks WHERE (name = 'A')) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and :conditions" do
@Album.filter(:al_track=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT DISTINCT ON (tracks.album_id) tracks.id FROM tracks WHERE (name = 'A') ORDER BY tracks.album_id, name)) AND (tracks.id = 5))))"
end
it "should be able to filter on many_to_many associations with :limit and :conditions" do
@Album.filter(:al_tags=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND ((albums_tags.album_id, tags.id) IN (SELECT b, c FROM (SELECT albums_tags.album_id AS b, tags.id AS c, row_number() OVER (PARTITION BY albums_tags.album_id) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE (name = 'A')) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 3))))"
end
it "should be able to filter on one_through_one associations with :order and :conditions" do
@Album.filter(:al_tag=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND ((albums_tags.album_id, tags.id) IN (SELECT DISTINCT ON (albums_tags.album_id) albums_tags.album_id, tags.id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE (name = 'A') ORDER BY albums_tags.album_id, name)) AND (tags.id = 3))))"
end
it "should be able to filter on many_to_one associations with composite keys" do
@Album.filter(:cartist=>@Artist.load(:id1=>3, :id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id1 = 3) AND (albums.artist_id2 = 4))'
end
it "should be able to filter on one_to_many associations with composite keys" do
@Album.filter(:ctracks=>@Track.load(:album_id1=>3, :album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1 = 3) AND (albums.id2 = 4))'
end
it "should be able to filter on one_to_one associations with composite keys" do
@Album.filter(:calbum_info=>@AlbumInfo.load(:album_id1=>3, :album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1 = 3) AND (albums.id2 = 4))'
end
it "should be able to filter on many_to_many associations with composite keys" do
@Album.filter(:ctags=>@Tag.load(:tid1=>3, :tid2=>4)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE ((albums_tags.tag_id1 = 3) AND (albums_tags.tag_id2 = 4) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL))))'
end
it "should be able to filter on many_to_one associations with :conditions and composite keys" do
@Album.filter(:a_cartist=>@Artist.load(:id=>5, :id1=>3, :id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'A') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id = 5))))"
end
it "should be able to filter on one_to_many associations with :conditions and composite keys" do
@Album.filter(:a_ctracks=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :conditions and composite keys" do
@Album.filter(:a_calbum_info=>@AlbumInfo.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id = 5))))"
end
it "should be able to filter on many_to_many associations with block and composite keys" do
@Album.filter(:a_ctags=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id = 5))))"
end
it "should be able to filter on many_to_one associations with block and composite keys" do
@Album.filter(:b_cartist=>@Artist.load(:id=>5, :id1=>3, :id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'B') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id = 5))))"
end
it "should be able to filter on one_to_many associations with block and composite keys" do
@Album.filter(:b_ctracks=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'B') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with block and composite keys" do
@Album.filter(:b_calbum_info=>@AlbumInfo.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id = 5))))"
end
it "should be able to filter on many_to_many associations with block and composite keys" do
@Album.filter(:b_ctags=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'B') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id = 5))))"
end
it "should be able to filter on one_to_many associations with :limit and composite keys" do
@Album.filter(:l_ctracks=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id1, tracks.album_id2) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_many associations with composite keys and :filter_limit_strategy :correlated_subquery" do
@Album.one_to_one :l_ctracks2, :clone=>:l_ctracks, :filter_limit_strategy=>:correlated_subquery
@Album.filter(:l_ctracks2=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT t1.id FROM tracks AS t1 WHERE ((t1.album_id1 = tracks.album_id1) AND (t1.album_id2 = tracks.album_id2)) LIMIT 1)) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and composite keys" do
@Album.filter(:l_ctrack=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT DISTINCT ON (tracks.album_id1, tracks.album_id2) tracks.id FROM tracks ORDER BY tracks.album_id1, tracks.album_id2, name)) AND (tracks.id = 5))))"
end
it "should be able to filter on many_to_many associations with :limit and composite keys" do
@Album.filter(:l_ctags=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND ((albums_tags.album_id1, albums_tags.album_id2, tags.id) IN (SELECT b, c, d FROM (SELECT albums_tags.album_id1 AS b, albums_tags.album_id2 AS c, tags.id AS d, row_number() OVER (PARTITION BY albums_tags.album_id1, albums_tags.album_id2) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2))) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 5))))"
end
it "should be able to filter on one_through_one associations with :order and composite keys" do
@Album.filter(:l_ctag=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND ((albums_tags.album_id1, albums_tags.album_id2, tags.id) IN (SELECT DISTINCT ON (albums_tags.album_id1, albums_tags.album_id2) albums_tags.album_id1, albums_tags.album_id2, tags.id FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) ORDER BY albums_tags.album_id1, albums_tags.album_id2, name)) AND (tags.id = 5))))"
end
it "should be able to filter on one_to_many associations with :limit and :conditions and composite keys" do
@Album.filter(:al_ctracks=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id1, tracks.album_id2) AS x_sequel_row_number_x FROM tracks WHERE (name = 'A')) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tracks.id = 5))))"
end
it "should be able to filter on one_to_one associations with :order and :conditions and composite keys" do
@Album.filter(:al_ctrack=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT DISTINCT ON (tracks.album_id1, tracks.album_id2) tracks.id FROM tracks WHERE (name = 'A') ORDER BY tracks.album_id1, tracks.album_id2, name)) AND (tracks.id = 5))))"
end
it "should be able to filter on many_to_many associations with :limit and :conditions and composite keys" do
@Album.filter(:al_ctags=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND ((albums_tags.album_id1, albums_tags.album_id2, tags.id) IN (SELECT b, c, d FROM (SELECT albums_tags.album_id1 AS b, albums_tags.album_id2 AS c, tags.id AS d, row_number() OVER (PARTITION BY albums_tags.album_id1, albums_tags.album_id2) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE (name = 'A')) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tags.id = 5))))"
end
it "should be able to filter on one_through_one associations with :order and :conditions and composite keys" do
@Album.filter(:al_ctag=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND ((albums_tags.album_id1, albums_tags.album_id2, tags.id) IN (SELECT DISTINCT ON (albums_tags.album_id1, albums_tags.album_id2) albums_tags.album_id1, albums_tags.album_id2, tags.id FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE (name = 'A') ORDER BY albums_tags.album_id1, albums_tags.album_id2, name)) AND (tags.id = 5))))"
end
it "should work inside a complex filter" do
artist = @Artist.load(:id=>3)
@Album.filter{foo & {:artist=>artist}}.sql.must_equal 'SELECT * FROM albums WHERE (foo AND (albums.artist_id = 3))'
track = @Track.load(:album_id=>4)
@Album.filter{foo & [[:artist, artist], [:tracks, track]]}.sql.must_equal 'SELECT * FROM albums WHERE (foo AND (albums.artist_id = 3) AND (albums.id = 4))'
end
it "should raise for an invalid association name" do
proc{@Album.filter(:foo=>@Artist.load(:id=>3)).sql}.must_raise(Sequel::Error)
end
it "should raise for an invalid association type" do
@Album.many_to_many :iatags, :clone=>:tags
@Album.association_reflection(:iatags)[:type] = :foo
proc{@Album.filter(:iatags=>@Tag.load(:id=>3)).sql}.must_raise(Sequel::Error)
end
it "should raise for an invalid associated object class " do
proc{@Album.filter(:tags=>@Artist.load(:id=>3)).sql}.must_raise(Sequel::Error)
end
it "should raise for an invalid associated object class when multiple objects are used" do
proc{@Album.filter(:tags=>[@Tag.load(:id=>3), @Artist.load(:id=>3)]).sql}.must_raise(Sequel::Error)
end
it "should correctly handle case when a multiple value association is used" do
proc{@Album.filter(:tags=>[@Tag.load(:id=>3), @Artist.load(:id=>3)]).sql}.must_raise(Sequel::Error)
end
it "should not affect non-association IN/NOT IN filtering with an empty array" do
@Album.filter(:tag_id=>[]).sql.must_equal 'SELECT * FROM albums WHERE (1 = 0)'
@Album.exclude(:tag_id=>[]).sql.must_equal 'SELECT * FROM albums WHERE (1 = 1)'
end
it "should work correctly in subclasses" do
c = Class.new(@Album)
c.many_to_one :sartist, :class=>@Artist
c.filter(:sartist=>@Artist.load(:id=>3)).sql.must_equal 'SELECT * FROM albums WHERE (albums.sartist_id = 3)'
end
it "should be able to exclude on many_to_one associations" do
@Album.exclude(:artist=>@Artist.load(:id=>3)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id != 3) OR (albums.artist_id IS NULL))'
end
it "should be able to exclude on one_to_many associations" do
@Album.exclude(:tracks=>@Track.load(:album_id=>3)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id != 3) OR (albums.id IS NULL))'
end
it "should be able to exclude on one_to_one associations" do
@Album.exclude(:album_info=>@AlbumInfo.load(:album_id=>3)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id != 3) OR (albums.id IS NULL))'
end
it "should be able to exclude on many_to_many associations" do
@Album.exclude(:tags=>@Tag.load(:id=>3)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id = 3) AND (albums_tags.album_id IS NOT NULL)))) OR (albums.id IS NULL))'
end
it "should be able to exclude on many_to_one associations with :conditions" do
@Album.exclude(:a_artist=>@Artist.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id NOT IN (SELECT artists.id FROM artists WHERE ((name = 'A') AND (artists.id IS NOT NULL) AND (artists.id = 3)))) OR (albums.artist_id IS NULL))"
end
it "should be able to exclude on one_to_many associations with :conditions" do
@Album.exclude(:a_tracks=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id = 5)))) OR (albums.id IS NULL))"
end
it "should be able to exclude on one_to_one associations with :conditions" do
@Album.exclude(:a_album_info=>@AlbumInfo.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id = 5)))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_many associations with :conditions" do
@Album.exclude(:a_tags=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND (tags.id = 3)))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_one associations with block" do
@Album.exclude(:b_artist=>@Artist.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id NOT IN (SELECT artists.id FROM artists WHERE ((name = 'B') AND (artists.id IS NOT NULL) AND (artists.id = 3)))) OR (albums.artist_id IS NULL))"
end
it "should be able to exclude on one_to_many associations with block" do
@Album.exclude(:b_tracks=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'B') AND (tracks.album_id IS NOT NULL) AND (tracks.id = 5)))) OR (albums.id IS NULL))"
end
it "should be able to exclude on one_to_one associations with block" do
@Album.exclude(:b_album_info=>@AlbumInfo.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id = 5)))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_many associations with block" do
@Album.exclude(:b_tags=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'B') AND (albums_tags.album_id IS NOT NULL) AND (tags.id = 3)))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_one associations with composite keys" do
@Album.exclude(:cartist=>@Artist.load(:id1=>3, :id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id1 != 3) OR (albums.artist_id2 != 4) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))'
end
it "should be able to exclude on one_to_many associations with composite keys" do
@Album.exclude(:ctracks=>@Track.load(:album_id1=>3, :album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1 != 3) OR (albums.id2 != 4) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on one_to_one associations with composite keys" do
@Album.exclude(:calbum_info=>@AlbumInfo.load(:album_id1=>3, :album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1 != 3) OR (albums.id2 != 4) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on many_to_many associations with composite keys" do
@Album.exclude(:ctags=>@Tag.load(:tid1=>3, :tid2=>4)).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE ((albums_tags.tag_id1 = 3) AND (albums_tags.tag_id2 = 4) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on many_to_one associations with :conditions and composite keys" do
@Album.exclude(:a_cartist=>@Artist.load(:id=>5, :id1=>3, :id2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'A') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id = 5)))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))"
end
it "should be able to exclude on one_to_many associations with :conditions and composite keys" do
@Album.exclude(:a_ctracks=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id = 5)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on one_to_one associations with :conditions and composite keys" do
@Album.exclude(:a_calbum_info=>@AlbumInfo.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id = 5)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on many_to_many associations with block and composite keys" do
@Album.exclude(:a_ctags=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id = 5)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on many_to_one associations with block and composite keys" do
@Album.exclude(:b_cartist=>@Artist.load(:id=>5, :id1=>3, :id2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'B') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id = 5)))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))"
end
it "should be able to exclude on one_to_many associations with block and composite keys" do
@Album.exclude(:b_ctracks=>@Track.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'B') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id = 5)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on one_to_one associations with block and composite keys" do
@Album.exclude(:b_calbum_info=>@AlbumInfo.load(:id=>5, :album_id1=>3, :album_id2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id = 5)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on many_to_many associations with block and composite keys" do
@Album.exclude(:b_ctags=>@Tag.load(:id=>5, :tid1=>3, :tid2=>4)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'B') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id = 5)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to filter on multiple many_to_one associations" do
@Album.filter(:artist=>[@Artist.load(:id=>3), @Artist.load(:id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE (albums.artist_id IN (3, 4))'
end
it "should be able to filter on multiple one_to_many associations" do
@Album.filter(:tracks=>[@Track.load(:album_id=>3), @Track.load(:album_id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (3, 4))'
end
it "should be able to filter on multiple one_to_one associations" do
@Album.filter(:album_info=>[@AlbumInfo.load(:album_id=>3), @AlbumInfo.load(:album_id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (3, 4))'
end
it "should be able to filter on multiple many_to_many associations" do
@Album.filter(:tags=>[@Tag.load(:id=>3), @Tag.load(:id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id IN (3, 4)) AND (albums_tags.album_id IS NOT NULL))))'
end
it "should be able to filter on multiple many_to_one associations with :conditions" do
@Album.filter(:a_artist=>[@Artist.load(:id=>3), @Artist.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.artist_id IN (SELECT artists.id FROM artists WHERE ((name = 'A') AND (artists.id IS NOT NULL) AND (artists.id IN (3, 4)))))"
end
it "should be able to filter on multiple one_to_many associations with :conditions" do
@Album.filter(:a_tracks=>[@Track.load(:id=>5, :album_id=>3), @Track.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (5, 6)))))"
end
it "should be able to filter on multiple one_to_one associations with :conditions" do
@Album.filter(:a_album_info=>[@AlbumInfo.load(:id=>5, :album_id=>3), @AlbumInfo.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (5, 6)))))"
end
it "should be able to filter on multiple many_to_many associations with :conditions" do
@Album.filter(:a_tags=>[@Tag.load(:id=>3), @Tag.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (3, 4)))))"
end
it "should be able to filter on multiple many_to_one associations with block" do
@Album.filter(:b_artist=>[@Artist.load(:id=>3), @Artist.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.artist_id IN (SELECT artists.id FROM artists WHERE ((name = 'B') AND (artists.id IS NOT NULL) AND (artists.id IN (3, 4)))))"
end
it "should be able to filter on multiple one_to_many associations with block" do
@Album.filter(:b_tracks=>[@Track.load(:id=>5, :album_id=>3), @Track.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'B') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (5, 6)))))"
end
it "should be able to filter on multiple one_to_one associations with block" do
@Album.filter(:b_album_info=>[@AlbumInfo.load(:id=>5, :album_id=>3), @AlbumInfo.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (5, 6)))))"
end
it "should be able to filter on multiple many_to_many associations with block" do
@Album.filter(:b_tags=>[@Tag.load(:id=>3), @Tag.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'B') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (3, 4)))))"
end
it "should be able to filter on multiple many_to_one associations with composite keys" do
@Album.filter(:cartist=>[@Artist.load(:id1=>3, :id2=>4), @Artist.load(:id1=>5, :id2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN ((3, 4), (5, 6)))'
end
it "should be able to filter on multiple one_to_many associations with composite keys" do
@Album.filter(:ctracks=>[@Track.load(:album_id1=>3, :album_id2=>4), @Track.load(:album_id1=>5, :album_id2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN ((3, 4), (5, 6)))'
end
it "should be able to filter on multiple one_to_one associations with composite keys" do
@Album.filter(:calbum_info=>[@AlbumInfo.load(:album_id1=>3, :album_id2=>4), @AlbumInfo.load(:album_id1=>5, :album_id2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN ((3, 4), (5, 6)))'
end
it "should be able to filter on multiple many_to_many associations with composite keys" do
@Album.filter(:ctags=>[@Tag.load(:tid1=>3, :tid2=>4), @Tag.load(:tid1=>5, :tid2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN ((3, 4), (5, 6))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL))))'
end
it "should be able to filter on multiple many_to_one associations with :conditions and composite keys" do
@Album.filter(:a_cartist=>[@Artist.load(:id=>7, :id1=>3, :id2=>4), @Artist.load(:id=>8, :id1=>5, :id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'A') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (7, 8)))))"
end
it "should be able to filter on multiple one_to_many associations with :conditions and composite keys" do
@Album.filter(:a_ctracks=>[@Track.load(:id=>7, :album_id1=>3, :album_id2=>4), @Track.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (7, 8)))))"
end
it "should be able to filter on multiple one_to_one associations with :conditions and composite keys" do
@Album.filter(:a_calbum_info=>[@AlbumInfo.load(:id=>7, :album_id1=>3, :album_id2=>4), @AlbumInfo.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (7, 8)))))"
end
it "should be able to filter on multiple many_to_many associations with block and composite keys" do
@Album.filter(:a_ctags=>[@Tag.load(:id=>7, :tid1=>3, :tid2=>4), @Tag.load(:id=>8, :tid1=>5, :tid2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (7, 8)))))"
end
it "should be able to filter on multiple many_to_one associations with block and composite keys" do
@Album.filter(:b_cartist=>[@Artist.load(:id=>7, :id1=>3, :id2=>4), @Artist.load(:id=>8, :id1=>5, :id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'B') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (7, 8)))))"
end
it "should be able to filter on multiple one_to_many associations with block and composite keys" do
@Album.filter(:b_ctracks=>[@Track.load(:id=>7, :album_id1=>3, :album_id2=>4), @Track.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'B') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (7, 8)))))"
end
it "should be able to filter on multiple one_to_one associations with block and composite keys" do
@Album.filter(:b_calbum_info=>[@AlbumInfo.load(:id=>7, :album_id1=>3, :album_id2=>4), @AlbumInfo.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (7, 8)))))"
end
it "should be able to filter on multiple many_to_many associations with block and composite keys" do
@Album.filter(:b_ctags=>[@Tag.load(:id=>7, :tid1=>3, :tid2=>4), @Tag.load(:id=>8, :tid1=>5, :tid2=>6)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'B') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (7, 8)))))"
end
it "should be able to exclude on multiple many_to_one associations" do
@Album.exclude(:artist=>[@Artist.load(:id=>3), @Artist.load(:id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id NOT IN (3, 4)) OR (albums.artist_id IS NULL))'
end
it "should be able to exclude on multiple one_to_many associations" do
@Album.exclude(:tracks=>[@Track.load(:album_id=>3), @Track.load(:album_id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (3, 4)) OR (albums.id IS NULL))'
end
it "should be able to exclude on multiple one_to_one associations" do
@Album.exclude(:album_info=>[@AlbumInfo.load(:album_id=>3), @AlbumInfo.load(:album_id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (3, 4)) OR (albums.id IS NULL))'
end
it "should be able to exclude on multiple many_to_many associations" do
@Album.exclude(:tags=>[@Tag.load(:id=>3), @Tag.load(:id=>4)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id IN (3, 4)) AND (albums_tags.album_id IS NOT NULL)))) OR (albums.id IS NULL))'
end
it "should be able to exclude on multiple many_to_one associations with :conditions" do
@Album.exclude(:a_artist=>[@Artist.load(:id=>3), @Artist.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id NOT IN (SELECT artists.id FROM artists WHERE ((name = 'A') AND (artists.id IS NOT NULL) AND (artists.id IN (3, 4))))) OR (albums.artist_id IS NULL))"
end
it "should be able to exclude on multiple one_to_many associations with :conditions" do
@Album.exclude(:a_tracks=>[@Track.load(:id=>5, :album_id=>3), @Track.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (5, 6))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on multiple one_to_one associations with :conditions" do
@Album.exclude(:a_album_info=>[@AlbumInfo.load(:id=>5, :album_id=>3), @AlbumInfo.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (5, 6))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on multiple many_to_many associations with :conditions" do
@Album.exclude(:a_tags=>[@Tag.load(:id=>3), @Tag.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (3, 4))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on multiple many_to_one associations with block" do
@Album.exclude(:b_artist=>[@Artist.load(:id=>3), @Artist.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id NOT IN (SELECT artists.id FROM artists WHERE ((name = 'B') AND (artists.id IS NOT NULL) AND (artists.id IN (3, 4))))) OR (albums.artist_id IS NULL))"
end
it "should be able to exclude on multiple one_to_many associations with block" do
@Album.exclude(:b_tracks=>[@Track.load(:id=>5, :album_id=>3), @Track.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'B') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (5, 6))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on multiple one_to_one associations with block" do
@Album.exclude(:b_album_info=>[@AlbumInfo.load(:id=>5, :album_id=>3), @AlbumInfo.load(:id=>6, :album_id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (5, 6))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on multiple many_to_many associations with block" do
@Album.exclude(:b_tags=>[@Tag.load(:id=>3), @Tag.load(:id=>4)]).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'B') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (3, 4))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on multiple many_to_one associations with composite keys" do
@Album.exclude(:cartist=>[@Artist.load(:id1=>3, :id2=>4), @Artist.load(:id1=>5, :id2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN ((3, 4), (5, 6))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))'
end
it "should be able to exclude on multiple one_to_many associations with composite keys" do
@Album.exclude(:ctracks=>[@Track.load(:album_id1=>3, :album_id2=>4), @Track.load(:album_id1=>5, :album_id2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN ((3, 4), (5, 6))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on multiple one_to_one associations with composite keys" do
@Album.exclude(:calbum_info=>[@AlbumInfo.load(:album_id1=>3, :album_id2=>4), @AlbumInfo.load(:album_id1=>5, :album_id2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN ((3, 4), (5, 6))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on multiple many_to_many associations with composite keys" do
@Album.exclude(:ctags=>[@Tag.load(:tid1=>3, :tid2=>4), @Tag.load(:tid1=>5, :tid2=>6)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN ((3, 4), (5, 6))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on multiple many_to_one associations with :conditions and composite keys" do
@Album.exclude(:a_cartist=>[@Artist.load(:id=>7, :id1=>3, :id2=>4), @Artist.load(:id=>8, :id1=>5, :id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'A') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (7, 8))))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))"
end
it "should be able to exclude on multiple one_to_many associations with :conditions and composite keys" do
@Album.exclude(:a_ctracks=>[@Track.load(:id=>7, :album_id1=>3, :album_id2=>4), @Track.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (7, 8))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on multiple one_to_one associations with :conditions and composite keys" do
@Album.exclude(:a_calbum_info=>[@AlbumInfo.load(:id=>7, :album_id1=>3, :album_id2=>4), @AlbumInfo.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (7, 8))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on multiple many_to_many associations with :conditions and composite keys" do
@Album.exclude(:a_ctags=>[@Tag.load(:id=>7, :tid1=>3, :tid2=>4), @Tag.load(:id=>8, :tid1=>5, :tid2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (7, 8))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on multiple many_to_one associations with block and composite keys" do
@Album.exclude(:b_cartist=>[@Artist.load(:id=>7, :id1=>3, :id2=>4), @Artist.load(:id=>8, :id1=>5, :id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'B') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (7, 8))))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))"
end
it "should be able to exclude on multiple one_to_many associations with block and composite keys" do
@Album.exclude(:b_ctracks=>[@Track.load(:id=>7, :album_id1=>3, :album_id2=>4), @Track.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'B') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (7, 8))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on multiple one_to_one associations with block and composite keys" do
@Album.exclude(:b_calbum_info=>[@AlbumInfo.load(:id=>7, :album_id1=>3, :album_id2=>4), @AlbumInfo.load(:id=>8, :album_id1=>5, :album_id2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (7, 8))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on multiple many_to_many associations with block and composite keys" do
@Album.exclude(:b_ctags=>[@Tag.load(:id=>7, :tid1=>3, :tid2=>4), @Tag.load(:id=>8, :tid1=>5, :tid2=>6)]).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'B') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (7, 8))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to handle NULL values when filtering many_to_one associations" do
@Album.filter(:artist=>@Artist.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when filtering one_to_many associations" do
@Album.filter(:tracks=>@Track.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when filtering one_to_one associations" do
@Album.filter(:album_info=>@AlbumInfo.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when filtering many_to_many associations" do
@Album.filter(:tags=>@Tag.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle filtering with NULL values for many_to_one associations with composite keys" do
@Album.filter(:cartist=>@Artist.load(:id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:cartist=>@Artist.load(:id1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:cartist=>@Artist.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to filter with NULL values for one_to_many associations with composite keys" do
@Album.filter(:ctracks=>@Track.load(:album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:ctracks=>@Track.load(:album_id1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:ctracks=>@Track.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to filter with NULL values for one_to_one associations with composite keys" do
@Album.filter(:calbum_info=>@AlbumInfo.load(:album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:calbum_info=>@AlbumInfo.load(:album_id1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:calbum_info=>@AlbumInfo.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to filter with NULL values for many_to_many associations with composite keys" do
@Album.filter(:ctags=>@Tag.load(:tid1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:ctags=>@Tag.load(:tid2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
@Album.filter(:ctags=>@Tag.new).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when excluding many_to_one associations" do
@Album.exclude(:artist=>@Artist.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when excluding one_to_many associations" do
@Album.exclude(:tracks=>@Track.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when excluding one_to_one associations" do
@Album.exclude(:album_info=>@AlbumInfo.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when excluding many_to_many associations" do
@Album.exclude(:tags=>@Tag.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle excluding with NULL values for many_to_one associations with composite keys" do
@Album.exclude(:cartist=>@Artist.load(:id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:cartist=>@Artist.load(:id1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:cartist=>@Artist.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to excluding with NULL values for one_to_many associations with composite keys" do
@Album.exclude(:ctracks=>@Track.load(:album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:ctracks=>@Track.load(:album_id1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:ctracks=>@Track.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to excluding with NULL values for one_to_one associations with composite keys" do
@Album.exclude(:calbum_info=>@AlbumInfo.load(:album_id2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:calbum_info=>@AlbumInfo.load(:album_id1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:calbum_info=>@AlbumInfo.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to excluding with NULL values for many_to_many associations with composite keys" do
@Album.exclude(:ctags=>@Tag.load(:tid1=>3)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:ctags=>@Tag.load(:tid2=>4)).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
@Album.exclude(:ctags=>@Tag.new).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when filtering multiple many_to_one associations" do
@Album.filter(:artist=>[@Artist.load(:id=>3), @Artist.new]).sql.must_equal 'SELECT * FROM albums WHERE (albums.artist_id IN (3))'
@Album.filter(:artist=>[@Artist.new, @Artist.new]).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when filtering multiple one_to_many associations" do
@Album.filter(:tracks=>[@Track.load(:album_id=>3), @Track.new]).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (3))'
@Album.filter(:tracks=>[@Track.new, @Track.new]).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when filtering multiple one_to_one associations" do
@Album.filter(:album_info=>[@AlbumInfo.load(:album_id=>3), @AlbumInfo.new]).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (3))'
@Album.filter(:album_info=>[@AlbumInfo.new, @AlbumInfo.new]).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when filtering multiple many_to_many associations" do
@Album.filter(:tags=>[@Tag.load(:id=>3), @Tag.new]).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id IN (3)) AND (albums_tags.album_id IS NOT NULL))))'
@Album.filter(:tags=>[@Tag.new, @Tag.new]).sql.must_equal 'SELECT * FROM albums WHERE \'f\''
end
it "should be able to handle NULL values when filtering multiple many_to_one associations with composite keys" do
@Album.filter(:cartist=>[@Artist.load(:id1=>3, :id2=>4), @Artist.load(:id1=>3)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN ((3, 4)))'
@Album.filter(:cartist=>[@Artist.load(:id1=>3, :id2=>4), @Artist.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN ((3, 4)))'
end
it "should be able handle NULL values when filtering multiple one_to_many associations with composite keys" do
@Album.filter(:ctracks=>[@Track.load(:album_id1=>3, :album_id2=>4), @Track.load(:album_id1=>3)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN ((3, 4)))'
@Album.filter(:ctracks=>[@Track.load(:album_id1=>3, :album_id2=>4), @Track.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN ((3, 4)))'
end
it "should be able to handle NULL values when filtering multiple one_to_one associations with composite keys" do
@Album.filter(:calbum_info=>[@AlbumInfo.load(:album_id1=>3, :album_id2=>4), @AlbumInfo.load(:album_id1=>5)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN ((3, 4)))'
@Album.filter(:calbum_info=>[@AlbumInfo.load(:album_id1=>3, :album_id2=>4), @AlbumInfo.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN ((3, 4)))'
end
it "should be able to handle NULL values when filtering multiple many_to_many associations with composite keys" do
@Album.filter(:ctags=>[@Tag.load(:tid1=>3, :tid2=>4), @Tag.load(:tid1=>5)]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN ((3, 4))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL))))'
@Album.filter(:ctags=>[@Tag.load(:tid1=>3, :tid2=>4), @Tag.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN ((3, 4))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL))))'
end
it "should be able to handle NULL values when excluding multiple many_to_one associations" do
@Album.exclude(:artist=>[@Artist.load(:id=>3), @Artist.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id NOT IN (3)) OR (albums.artist_id IS NULL))'
@Album.exclude(:artist=>[@Artist.new, @Artist.new]).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when excluding multiple one_to_many associations" do
@Album.exclude(:tracks=>[@Track.load(:album_id=>3), @Track.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (3)) OR (albums.id IS NULL))'
@Album.exclude(:tracks=>[@Track.new, @Track.new]).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when excluding multiple one_to_one associations" do
@Album.exclude(:album_info=>[@AlbumInfo.load(:album_id=>3), @AlbumInfo.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (3)) OR (albums.id IS NULL))'
@Album.exclude(:album_info=>[@AlbumInfo.new, @AlbumInfo.new]).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when excluding multiple many_to_many associations" do
@Album.exclude(:tags=>[@Tag.load(:id=>3), @Tag.new]).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id IN (3)) AND (albums_tags.album_id IS NOT NULL)))) OR (albums.id IS NULL))'
@Album.exclude(:tags=>[@Tag.new, @Tag.new]).sql.must_equal 'SELECT * FROM albums WHERE \'t\''
end
it "should be able to handle NULL values when excluding multiple many_to_one associations with composite keys" do
@Album.exclude(:cartist=>[@Artist.load(:id1=>3, :id2=>4), @Artist.load(:id1=>3)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN ((3, 4))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))'
@Album.exclude(:cartist=>[@Artist.load(:id1=>3, :id2=>4), @Artist.new]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN ((3, 4))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))'
end
it "should be able handle NULL values when excluding multiple one_to_many associations with composite keys" do
@Album.exclude(:ctracks=>[@Track.load(:album_id1=>3, :album_id2=>4), @Track.load(:album_id1=>3)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN ((3, 4))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
@Album.exclude(:ctracks=>[@Track.load(:album_id1=>3, :album_id2=>4), @Track.new]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN ((3, 4))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to handle NULL values when excluding multiple one_to_one associations with composite keys" do
@Album.exclude(:calbum_info=>[@AlbumInfo.load(:album_id1=>3, :album_id2=>4), @AlbumInfo.load(:album_id1=>5)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN ((3, 4))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
@Album.exclude(:calbum_info=>[@AlbumInfo.load(:album_id1=>3, :album_id2=>4), @AlbumInfo.new]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN ((3, 4))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to handle NULL values when excluding multiple many_to_many associations with composite keys" do
@Album.exclude(:ctags=>[@Tag.load(:tid1=>3, :tid2=>4), @Tag.load(:tid1=>5)]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN ((3, 4))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
@Album.exclude(:ctags=>[@Tag.load(:tid1=>3, :tid2=>4), @Tag.new]).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN ((3, 4))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should not allow filtering on associations with allow_filtering_by: false" do
proc{@Album.filter(:no_artist=>@Artist.filter(:x=>1)).sql}.must_raise Sequel::Error
end
it "should be able to filter on many_to_one association datasets" do
@Album.filter(:artist=>@Artist.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (albums.artist_id IN (SELECT artists.id FROM artists WHERE ((x = 1) AND (artists.id IS NOT NULL))))'
end
it "should be able to filter on one_to_many association datasets" do
@Album.filter(:tracks=>@Track.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((x = 1) AND (tracks.album_id IS NOT NULL))))'
end
it "should be able to filter on one_to_one association datasets" do
@Album.filter(:album_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (SELECT album_infos.album_id FROM album_infos WHERE ((x = 1) AND (album_infos.album_id IS NOT NULL))))'
end
it "should be able to filter on many_to_many association datasets" do
@Album.filter(:tags=>@Tag.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id IN (SELECT tags.id FROM tags WHERE ((x = 1) AND (tags.id IS NOT NULL)))) AND (albums_tags.album_id IS NOT NULL))))'
end
it "should be able to filter on many_to_one association datasets with :conditions" do
@Album.filter(:a_artist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.artist_id IN (SELECT artists.id FROM artists WHERE ((name = 'A') AND (artists.id IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1))))))"
end
it "should be able to filter on one_to_many association datasets with :conditions" do
@Album.filter(:a_tracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1))))))"
end
it "should be able to filter on one_to_one association datasets with :conditions" do
@Album.filter(:a_album_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1))))))"
end
it "should be able to filter on many_to_many association datasets with :conditions" do
@Album.filter(:a_tags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
end
it "should be able to filter on many_to_one association datasets with block" do
@Album.filter(:b_artist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.artist_id IN (SELECT artists.id FROM artists WHERE ((name = 'B') AND (artists.id IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1))))))"
end
it "should be able to filter on one_to_many association datasets with block" do
@Album.filter(:b_tracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'B') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1))))))"
end
it "should be able to filter on one_to_one association datasets with block" do
@Album.filter(:b_album_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1))))))"
end
it "should be able to filter on many_to_many association datasets with block" do
@Album.filter(:b_tags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'B') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
end
it "should be able to filter on many_to_one association datasets with composite keys" do
@Album.filter(:cartist=>@Artist.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((x = 1) AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL))))'
end
it "should be able to filter on one_to_many association datasets with composite keys" do
@Album.filter(:ctracks=>@Track.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((x = 1) AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL))))'
end
it "should be able to filter on one_to_one association datasets with composite keys" do
@Album.filter(:calbum_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((x = 1) AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL))))'
end
it "should be able to filter on many_to_many association datasets with composite keys" do
@Album.filter(:ctags=>@Tag.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN (SELECT tags.tid1, tags.tid2 FROM tags WHERE ((x = 1) AND (tags.tid1 IS NOT NULL) AND (tags.tid2 IS NOT NULL)))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL))))'
end
it "should be able to filter on many_to_one association datasets with :conditions and composite keys" do
@Album.filter(:a_cartist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'A') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1))))))"
end
it "should be able to filter on one_to_many association datasets with :conditions and composite keys" do
@Album.filter(:a_ctracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1))))))"
end
it "should be able to filter on one_to_one association datasets with :conditions and composite keys" do
@Album.filter(:a_calbum_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1))))))"
end
it "should be able to filter on many_to_many association datasets with :conditions and composite keys" do
@Album.filter(:a_ctags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
end
it "should be able to filter on many_to_one association datasets with block and composite keys" do
@Album.filter(:b_cartist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id1, albums.artist_id2) IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'B') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1))))))"
end
it "should be able to filter on one_to_many association datasets with block and composite keys" do
@Album.filter(:b_ctracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'B') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1))))))"
end
it "should be able to filter on one_to_one association datasets with block and composite keys" do
@Album.filter(:b_calbum_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1))))))"
end
it "should be able to filter on many_to_many association datasets with block and composite keys" do
@Album.filter(:b_ctags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id1, albums.id2) IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'B') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1))))))"
end
it "should be able to exclude on many_to_one association datasets" do
@Album.exclude(:artist=>@Artist.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.artist_id NOT IN (SELECT artists.id FROM artists WHERE ((x = 1) AND (artists.id IS NOT NULL)))) OR (albums.artist_id IS NULL))'
end
it "should be able to exclude on one_to_many association datasets" do
@Album.exclude(:tracks=>@Track.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT tracks.album_id FROM tracks WHERE ((x = 1) AND (tracks.album_id IS NOT NULL)))) OR (albums.id IS NULL))'
end
it "should be able to exclude on one_to_one association datasets" do
@Album.exclude(:album_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT album_infos.album_id FROM album_infos WHERE ((x = 1) AND (album_infos.album_id IS NOT NULL)))) OR (albums.id IS NULL))'
end
it "should be able to exclude on many_to_many association datasets" do
@Album.exclude(:tags=>@Tag.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM albums_tags WHERE ((albums_tags.tag_id IN (SELECT tags.id FROM tags WHERE ((x = 1) AND (tags.id IS NOT NULL)))) AND (albums_tags.album_id IS NOT NULL)))) OR (albums.id IS NULL))'
end
it "should be able to exclude on many_to_one association datasets with :conditions" do
@Album.exclude(:a_artist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id NOT IN (SELECT artists.id FROM artists WHERE ((name = 'A') AND (artists.id IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1)))))) OR (albums.artist_id IS NULL))"
end
it "should be able to exclude on one_to_many association datasets with :conditions" do
@Album.exclude(:a_tracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'A') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1)))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on one_to_one association datasets with :conditions" do
@Album.exclude(:a_album_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1)))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_many association datasets with :conditions" do
@Album.exclude(:a_tags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'A') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_one association datasets with block" do
@Album.exclude(:b_artist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.artist_id NOT IN (SELECT artists.id FROM artists WHERE ((name = 'B') AND (artists.id IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1)))))) OR (albums.artist_id IS NULL))"
end
it "should be able to exclude on one_to_many association datasets with block" do
@Album.exclude(:b_tracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT tracks.album_id FROM tracks WHERE ((name = 'B') AND (tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1)))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on one_to_one association datasets with block" do
@Album.exclude(:b_album_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT album_infos.album_id FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1)))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_many association datasets with block" do
@Album.exclude(:b_tags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE ((albums.id NOT IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'B') AND (albums_tags.album_id IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (albums.id IS NULL))"
end
it "should be able to exclude on many_to_one association datasets with composite keys" do
@Album.exclude(:cartist=>@Artist.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((x = 1) AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL)))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))'
end
it "should be able to exclude on one_to_many association datasets with composite keys" do
@Album.exclude(:ctracks=>@Track.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((x = 1) AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on one_to_one association datasets with composite keys" do
@Album.exclude(:calbum_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((x = 1) AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on many_to_many association datasets with composite keys" do
@Album.exclude(:ctags=>@Tag.filter(:x=>1)).sql.must_equal 'SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM albums_tags WHERE (((albums_tags.tag_id1, albums_tags.tag_id2) IN (SELECT tags.tid1, tags.tid2 FROM tags WHERE ((x = 1) AND (tags.tid1 IS NOT NULL) AND (tags.tid2 IS NOT NULL)))) AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL)))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))'
end
it "should be able to exclude on many_to_one association datasets with :conditions and composite keys" do
@Album.exclude(:a_cartist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'A') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1)))))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))"
end
it "should be able to exclude on one_to_many association datasets with :conditions and composite keys" do
@Album.exclude(:a_ctracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'A') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1)))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on one_to_one association datasets with :conditions and composite keys" do
@Album.exclude(:a_calbum_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'A') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1)))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on many_to_many association datasets with :conditions and composite keys" do
@Album.exclude(:a_ctags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'A') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on many_to_one association datasets with block and composite keys" do
@Album.exclude(:b_cartist=>@Artist.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.artist_id1, albums.artist_id2) NOT IN (SELECT artists.id1, artists.id2 FROM artists WHERE ((name = 'B') AND (artists.id1 IS NOT NULL) AND (artists.id2 IS NOT NULL) AND (artists.id IN (SELECT artists.id FROM artists WHERE (x = 1)))))) OR (albums.artist_id1 IS NULL) OR (albums.artist_id2 IS NULL))"
end
it "should be able to exclude on one_to_many association datasets with block and composite keys" do
@Album.exclude(:b_ctracks=>@Track.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT tracks.album_id1, tracks.album_id2 FROM tracks WHERE ((name = 'B') AND (tracks.album_id1 IS NOT NULL) AND (tracks.album_id2 IS NOT NULL) AND (tracks.id IN (SELECT tracks.id FROM tracks WHERE (x = 1)))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on one_to_one association datasets with block and composite keys" do
@Album.exclude(:b_calbum_info=>@AlbumInfo.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT album_infos.album_id1, album_infos.album_id2 FROM album_infos WHERE ((name = 'B') AND (album_infos.album_id1 IS NOT NULL) AND (album_infos.album_id2 IS NOT NULL) AND (album_infos.id IN (SELECT album_infos.id FROM album_infos WHERE (x = 1)))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should be able to exclude on many_to_many association datasets with block and composite keys" do
@Album.exclude(:b_ctags=>@Tag.filter(:x=>1)).sql.must_equal "SELECT * FROM albums WHERE (((albums.id1, albums.id2) NOT IN (SELECT albums_tags.album_id1, albums_tags.album_id2 FROM tags INNER JOIN albums_tags ON ((albums_tags.tag_id1 = tags.tid1) AND (albums_tags.tag_id2 = tags.tid2)) WHERE ((name = 'B') AND (albums_tags.album_id1 IS NOT NULL) AND (albums_tags.album_id2 IS NOT NULL) AND (tags.id IN (SELECT tags.id FROM tags WHERE (x = 1)))))) OR (albums.id1 IS NULL) OR (albums.id2 IS NULL))"
end
it "should do a regular IN query if the dataset for a different model is used" do
@Album.filter(:artist=>@Album.select(:x)).sql.must_equal 'SELECT * FROM albums WHERE (artist IN (SELECT x FROM albums))'
end
it "should do a regular IN query if a non-model dataset is used" do
@Album.filter(:artist=>@Album.db.from(:albums).select(:x)).sql.must_equal 'SELECT * FROM albums WHERE (artist IN (SELECT x FROM albums))'
end
end
describe "Sequel::Model Associations with clashing column names" do
before do
@db = Sequel.mock(:fetch=>{:id=>1, :object_id=>2})
@Foo = Class.new(Sequel::Model(@db[:foos]))
@Bar = Class.new(Sequel::Model(@db[:bars]))
@Foo.columns :id, :object_id
@Bar.columns :id, :object_id
@Foo.def_column_alias(:obj_id, :object_id)
@Bar.def_column_alias(:obj_id, :object_id)
@Foo.one_to_many :bars, :primary_key=>:obj_id, :primary_key_column=>:object_id, :key=>:object_id, :key_method=>:obj_id, :class=>@Bar
@Foo.one_to_one :bar, :primary_key=>:obj_id, :primary_key_column=>:object_id, :key=>:object_id, :key_method=>:obj_id, :class=>@Bar
@Bar.many_to_one :foo, :key=>:obj_id, :key_column=>:object_id, :primary_key=>:object_id, :primary_key_method=>:obj_id, :class=>@Foo
@Foo.many_to_many :mtmbars, :join_table=>:bars_foos, :left_primary_key=>:obj_id, :left_primary_key_column=>:object_id, :right_primary_key=>:object_id, :right_primary_key_method=>:obj_id, :left_key=>:foo_id, :right_key=>:object_id, :class=>@Bar
@Bar.many_to_many :mtmfoos, :join_table=>:bars_foos, :left_primary_key=>:obj_id, :left_primary_key_column=>:object_id, :right_primary_key=>:object_id, :right_primary_key_method=>:obj_id, :left_key=>:object_id, :right_key=>:foo_id, :class=>@Foo
@foo = @Foo.load(:id=>1, :object_id=>2)
@bar = @Bar.load(:id=>1, :object_id=>2)
@db.sqls
end
it "should have working regular association methods" do
@Bar.first.foo.must_equal @foo
@db.sqls.must_equal ["SELECT * FROM bars LIMIT 1", "SELECT * FROM foos WHERE (foos.object_id = 2) LIMIT 1"]
@Foo.first.bars.must_equal [@bar]
@db.sqls.must_equal ["SELECT * FROM foos LIMIT 1", "SELECT * FROM bars WHERE (bars.object_id = 2)"]
@Foo.first.bar.must_equal @bar
@db.sqls.must_equal ["SELECT * FROM foos LIMIT 1", "SELECT * FROM bars WHERE (bars.object_id = 2) LIMIT 1"]
@Foo.first.mtmbars.must_equal [@bar]
@db.sqls.must_equal ["SELECT * FROM foos LIMIT 1", "SELECT bars.* FROM bars INNER JOIN bars_foos ON (bars_foos.object_id = bars.object_id) WHERE (bars_foos.foo_id = 2)"]
@Bar.first.mtmfoos.must_equal [@foo]
@db.sqls.must_equal ["SELECT * FROM bars LIMIT 1", "SELECT foos.* FROM foos INNER JOIN bars_foos ON (bars_foos.foo_id = foos.object_id) WHERE (bars_foos.object_id = 2)"]
end
it "should have working eager loading methods" do
@Bar.eager(:foo).all.map{|o| [o, o.foo]}.must_equal [[@bar, @foo]]
@db.sqls.must_equal ["SELECT * FROM bars", "SELECT * FROM foos WHERE (foos.object_id IN (2))"]
@Foo.eager(:bars).all.map{|o| [o, o.bars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT * FROM foos", "SELECT * FROM bars WHERE (bars.object_id IN (2))"]
@Foo.eager(:bar).all.map{|o| [o, o.bar]}.must_equal [[@foo, @bar]]
@db.sqls.must_equal ["SELECT * FROM foos", "SELECT * FROM bars WHERE (bars.object_id IN (2))"]
@db.fetch = [[{:id=>1, :object_id=>2}], [{:id=>1, :object_id=>2, :x_foreign_key_x=>2}]]
@Foo.eager(:mtmbars).all.map{|o| [o, o.mtmbars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT * FROM foos", "SELECT bars.*, bars_foos.foo_id AS x_foreign_key_x FROM bars INNER JOIN bars_foos ON (bars_foos.object_id = bars.object_id) WHERE (bars_foos.foo_id IN (2))"]
@db.fetch = [[{:id=>1, :object_id=>2}], [{:id=>1, :object_id=>2, :x_foreign_key_x=>2}]]
@Bar.eager(:mtmfoos).all.map{|o| [o, o.mtmfoos]}.must_equal [[@bar, [@foo]]]
@db.sqls.must_equal ["SELECT * FROM bars", "SELECT foos.*, bars_foos.object_id AS x_foreign_key_x FROM foos INNER JOIN bars_foos ON (bars_foos.foo_id = foos.object_id) WHERE (bars_foos.object_id IN (2))"]
end
it "should have working eager graphing methods" do
@db.fetch = {:id=>1, :object_id=>2, :foo_id=>1, :foo_object_id=>2}
@Bar.eager_graph(:foo).all.map{|o| [o, o.foo]}.must_equal [[@bar, @foo]]
@db.sqls.must_equal ["SELECT bars.id, bars.object_id, foo.id AS foo_id, foo.object_id AS foo_object_id FROM bars LEFT OUTER JOIN foos AS foo ON (foo.object_id = bars.object_id)"]
@db.fetch = {:id=>1, :object_id=>2, :bars_id=>1, :bars_object_id=>2}
@Foo.eager_graph(:bars).all.map{|o| [o, o.bars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT foos.id, foos.object_id, bars.id AS bars_id, bars.object_id AS bars_object_id FROM foos LEFT OUTER JOIN bars ON (bars.object_id = foos.object_id)"]
@db.fetch = {:id=>1, :object_id=>2, :bar_id=>1, :bar_object_id=>2}
@Foo.eager_graph(:bar).all.map{|o| [o, o.bar]}.must_equal [[@foo, @bar]]
@db.sqls.must_equal ["SELECT foos.id, foos.object_id, bar.id AS bar_id, bar.object_id AS bar_object_id FROM foos LEFT OUTER JOIN bars AS bar ON (bar.object_id = foos.object_id)"]
@db.fetch = {:id=>1, :object_id=>2, :mtmfoos_id=>1, :mtmfoos_object_id=>2}
@Bar.eager_graph(:mtmfoos).all.map{|o| [o, o.mtmfoos]}.must_equal [[@bar, [@foo]]]
@db.sqls.must_equal ["SELECT bars.id, bars.object_id, mtmfoos.id AS mtmfoos_id, mtmfoos.object_id AS mtmfoos_object_id FROM bars LEFT OUTER JOIN bars_foos ON (bars_foos.object_id = bars.object_id) LEFT OUTER JOIN foos AS mtmfoos ON (mtmfoos.object_id = bars_foos.foo_id)"]
@db.fetch = {:id=>1, :object_id=>2, :mtmbars_id=>1, :mtmbars_object_id=>2}
@Foo.eager_graph(:mtmbars).all.map{|o| [o, o.mtmbars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT foos.id, foos.object_id, mtmbars.id AS mtmbars_id, mtmbars.object_id AS mtmbars_object_id FROM foos LEFT OUTER JOIN bars_foos ON (bars_foos.foo_id = foos.object_id) LEFT OUTER JOIN bars AS mtmbars ON (mtmbars.object_id = bars_foos.object_id)"]
end
it "should not have filter by associations code break if using IN/NOT in with a set-returning function" do
@Bar.where(Sequel::SQL::BooleanExpression.new(:IN, :foo, Sequel.function(:srf))).sql.must_equal 'SELECT * FROM bars WHERE (foo IN srf())'
@Bar.exclude(Sequel::SQL::BooleanExpression.new(:IN, :foo, Sequel.function(:srf))).sql.must_equal 'SELECT * FROM bars WHERE (foo NOT IN srf())'
end
it "should have working eager graphing methods when using SQL::Identifier inside SQL::AliasedExpression" do
@db.fetch = {:id=>1, :object_id=>2, :f_id=>1, :f_object_id=>2}
@Bar.eager_graph(Sequel[:foo].as(:f)).all.map{|o| [o, o.foo]}.must_equal [[@bar, @foo]]
@db.sqls.must_equal ["SELECT bars.id, bars.object_id, f.id AS f_id, f.object_id AS f_object_id FROM bars LEFT OUTER JOIN foos AS f ON (f.object_id = bars.object_id)"]
end
it "should have working filter by associations with model instances" do
@Bar.first(:foo=>@foo).must_equal @bar
@db.sqls.must_equal ["SELECT * FROM bars WHERE (bars.object_id = 2) LIMIT 1"]
@Foo.first(:bars=>@bar).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_id = 2) LIMIT 1"]
@Foo.first(:bar=>@bar).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_id = 2) LIMIT 1"]
@Foo.first(:mtmbars=>@bar).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_id IN (SELECT bars_foos.foo_id FROM bars_foos WHERE ((bars_foos.object_id = 2) AND (bars_foos.foo_id IS NOT NULL)))) LIMIT 1"]
@Bar.first(:mtmfoos=>@foo).must_equal @bar
@db.sqls.must_equal ["SELECT * FROM bars WHERE (bars.object_id IN (SELECT bars_foos.object_id FROM bars_foos WHERE ((bars_foos.foo_id = 2) AND (bars_foos.object_id IS NOT NULL)))) LIMIT 1"]
end
it "should have working filter by associations for associations with :conditions with model instances" do
@Bar.many_to_one :foo, :clone=>:foo, :conditions=>{:name=>'A'}
@Foo.one_to_many :bars, :clone=>:bars, :conditions=>{:name=>'A'}
@Foo.one_to_one :bar, :clone=>:bars
@Foo.many_to_many :mtmbars, :clone=>:mtmbars, :conditions=>{:name=>'A'}
@Bar.many_to_many :mtmfoos, :clone=>:mtmfoos, :conditions=>{:name=>'A'}
@Bar.where(:foo=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_id IN (SELECT foos.object_id FROM foos WHERE ((name = 'A') AND (foos.object_id IS NOT NULL) AND (foos.id = 1))))"
@Foo.where(:bars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_id IN (SELECT bars.object_id FROM bars WHERE ((name = 'A') AND (bars.object_id IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:bar=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_id IN (SELECT bars.object_id FROM bars WHERE ((name = 'A') AND (bars.object_id IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:mtmbars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_id IN (SELECT bars_foos.foo_id FROM bars INNER JOIN bars_foos ON (bars_foos.object_id = bars.object_id) WHERE ((name = 'A') AND (bars_foos.foo_id IS NOT NULL) AND (bars.id = 1))))"
@Bar.where(:mtmfoos=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_id IN (SELECT bars_foos.object_id FROM foos INNER JOIN bars_foos ON (bars_foos.foo_id = foos.object_id) WHERE ((name = 'A') AND (bars_foos.object_id IS NOT NULL) AND (foos.id = 1))))"
end
it "should have working filter by associations for associations with block with model instances" do
b = lambda{|ds| ds.where(:name=>'A')}
@Bar.many_to_one :foo, :clone=>:foo, &b
@Foo.one_to_many :bars, :clone=>:bars, &b
@Foo.one_to_one :bar, :clone=>:bars
@Foo.many_to_many :mtmbars, :clone=>:mtmbars, &b
@Bar.many_to_many :mtmfoos, :clone=>:mtmfoos, &b
@Bar.where(:foo=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_id IN (SELECT foos.object_id FROM foos WHERE ((name = 'A') AND (foos.object_id IS NOT NULL) AND (foos.id = 1))))"
@Foo.where(:bars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_id IN (SELECT bars.object_id FROM bars WHERE ((name = 'A') AND (bars.object_id IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:bar=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_id IN (SELECT bars.object_id FROM bars WHERE ((name = 'A') AND (bars.object_id IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:mtmbars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_id IN (SELECT bars_foos.foo_id FROM bars INNER JOIN bars_foos ON (bars_foos.object_id = bars.object_id) WHERE ((name = 'A') AND (bars_foos.foo_id IS NOT NULL) AND (bars.id = 1))))"
@Bar.where(:mtmfoos=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_id IN (SELECT bars_foos.object_id FROM foos INNER JOIN bars_foos ON (bars_foos.foo_id = foos.object_id) WHERE ((name = 'A') AND (bars_foos.object_id IS NOT NULL) AND (foos.id = 1))))"
end
it "should have working modification methods" do
b = @Bar.load(:id=>2, :object_id=>3)
f = @Foo.load(:id=>2, :object_id=>3)
@db.numrows = 1
@bar.foo = f
@bar.obj_id.must_equal 3
@foo.bar = @bar
@bar.obj_id.must_equal 2
@foo.add_bar(b)
@db.fetch = [[{:id=>1, :object_id=>2}, {:id=>2, :object_id=>2}], [{:id=>1, :object_id=>2}]]
@foo.bars.must_equal [@bar, b]
@foo.remove_bar(b)
@foo.bars.must_equal [@bar]
@foo.remove_all_bars
@foo.bars.must_equal []
@db.fetch = [[{:id=>1, :object_id=>2}], [], [{:id=>2, :object_id=>2}]]
@bar = @Bar.load(:id=>1, :object_id=>2)
@foo.mtmbars.must_equal [@bar]
@foo.remove_all_mtmbars
@foo.mtmbars.must_equal []
@foo.add_mtmbar(b)
@foo.mtmbars.must_equal [b]
@foo.remove_mtmbar(b)
@foo.mtmbars.must_equal []
@db.fetch = [[{:id=>2, :object_id=>3}], [], [{:id=>2, :object_id=>3}]]
@bar.add_mtmfoo(f)
@bar.mtmfoos.must_equal [f]
@bar.remove_all_mtmfoos
@bar.mtmfoos.must_equal []
@bar.add_mtmfoo(f)
@bar.mtmfoos.must_equal [f]
@bar.remove_mtmfoo(f)
@bar.mtmfoos.must_equal []
end
end
describe "Sequel::Model Associations with non-column expression keys" do
before do
@db = Sequel.mock(:fetch=>{:id=>1, :object_ids=>[2]})
@Foo = Class.new(Sequel::Model(@db[:foos]))
@Bar = Class.new(Sequel::Model(@db[:bars]))
@Foo.columns :id, :object_ids
@Bar.columns :id, :object_ids
m = Module.new{def obj_id; object_ids[0]; end}
@Foo.include m
@Bar.include m
@Foo.one_to_many :bars, :primary_key=>:obj_id, :primary_key_column=>Sequel.subscript(:object_ids, 0), :key=>Sequel.subscript(:object_ids, 0), :key_method=>:obj_id, :class=>@Bar
@Foo.one_to_one :bar, :primary_key=>:obj_id, :primary_key_column=>Sequel.subscript(:object_ids, 0), :key=>Sequel.subscript(:object_ids, 0), :key_method=>:obj_id, :class=>@Bar
@Bar.many_to_one :foo, :key=>:obj_id, :key_column=>Sequel.subscript(:object_ids, 0), :primary_key=>Sequel.subscript(:object_ids, 0), :primary_key_method=>:obj_id, :class=>@Foo
@Foo.many_to_many :mtmbars, :join_table=>:bars_foos, :left_primary_key=>:obj_id, :left_primary_key_column=>Sequel.subscript(:object_ids, 0), :right_primary_key=>Sequel.subscript(:object_ids, 0), :right_primary_key_method=>:obj_id, :left_key=>Sequel.subscript(:foo_ids, 0), :right_key=>Sequel.subscript(:bar_ids, 0), :class=>@Bar
@Bar.many_to_many :mtmfoos, :join_table=>:bars_foos, :left_primary_key=>:obj_id, :left_primary_key_column=>Sequel.subscript(:object_ids, 0), :right_primary_key=>Sequel.subscript(:object_ids, 0), :right_primary_key_method=>:obj_id, :left_key=>Sequel.subscript(:bar_ids, 0), :right_key=>Sequel.subscript(:foo_ids, 0), :class=>@Foo, :reciprocal=>nil
@foo = @Foo.load(:id=>1, :object_ids=>[2])
@bar = @Bar.load(:id=>1, :object_ids=>[2])
@db.sqls
end
it "should have working regular association methods" do
@Bar.first.foo.must_equal @foo
@db.sqls.must_equal ["SELECT * FROM bars LIMIT 1", "SELECT * FROM foos WHERE (foos.object_ids[0] = 2) LIMIT 1"]
@Foo.first.bars.must_equal [@bar]
@db.sqls.must_equal ["SELECT * FROM foos LIMIT 1", "SELECT * FROM bars WHERE (bars.object_ids[0] = 2)"]
@Foo.first.bar.must_equal @bar
@db.sqls.must_equal ["SELECT * FROM foos LIMIT 1", "SELECT * FROM bars WHERE (bars.object_ids[0] = 2) LIMIT 1"]
@Foo.first.mtmbars.must_equal [@bar]
@db.sqls.must_equal ["SELECT * FROM foos LIMIT 1", "SELECT bars.* FROM bars INNER JOIN bars_foos ON (bars_foos.bar_ids[0] = bars.object_ids[0]) WHERE (bars_foos.foo_ids[0] = 2)"]
@Bar.first.mtmfoos.must_equal [@foo]
@db.sqls.must_equal ["SELECT * FROM bars LIMIT 1", "SELECT foos.* FROM foos INNER JOIN bars_foos ON (bars_foos.foo_ids[0] = foos.object_ids[0]) WHERE (bars_foos.bar_ids[0] = 2)"]
end
it "should have working eager loading methods" do
@Bar.eager(:foo).all.map{|o| [o, o.foo]}.must_equal [[@bar, @foo]]
@db.sqls.must_equal ["SELECT * FROM bars", "SELECT * FROM foos WHERE (foos.object_ids[0] IN (2))"]
@Foo.eager(:bars).all.map{|o| [o, o.bars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT * FROM foos", "SELECT * FROM bars WHERE (bars.object_ids[0] IN (2))"]
@Foo.eager(:bar).all.map{|o| [o, o.bar]}.must_equal [[@foo, @bar]]
@db.sqls.must_equal ["SELECT * FROM foos", "SELECT * FROM bars WHERE (bars.object_ids[0] IN (2))"]
@db.fetch = [[{:id=>1, :object_ids=>[2]}], [{:id=>1, :object_ids=>[2], :x_foreign_key_x=>2}]]
@Foo.eager(:mtmbars).all.map{|o| [o, o.mtmbars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT * FROM foos", "SELECT bars.*, bars_foos.foo_ids[0] AS x_foreign_key_x FROM bars INNER JOIN bars_foos ON (bars_foos.bar_ids[0] = bars.object_ids[0]) WHERE (bars_foos.foo_ids[0] IN (2))"]
@db.fetch = [[{:id=>1, :object_ids=>[2]}], [{:id=>1, :object_ids=>[2], :x_foreign_key_x=>2}]]
@Bar.eager(:mtmfoos).all.map{|o| [o, o.mtmfoos]}.must_equal [[@bar, [@foo]]]
@db.sqls.must_equal ["SELECT * FROM bars", "SELECT foos.*, bars_foos.bar_ids[0] AS x_foreign_key_x FROM foos INNER JOIN bars_foos ON (bars_foos.foo_ids[0] = foos.object_ids[0]) WHERE (bars_foos.bar_ids[0] IN (2))"]
end
it "should have working eager graphing methods" do
@db.fetch = {:id=>1, :object_ids=>[2], :foo_id=>1, :foo_object_ids=>[2]}
@Bar.eager_graph(:foo).all.map{|o| [o, o.foo]}.must_equal [[@bar, @foo]]
@db.sqls.must_equal ["SELECT bars.id, bars.object_ids, foo.id AS foo_id, foo.object_ids AS foo_object_ids FROM bars LEFT OUTER JOIN foos AS foo ON (foo.object_ids[0] = bars.object_ids[0])"]
@db.fetch = {:id=>1, :object_ids=>[2], :bars_id=>1, :bars_object_ids=>[2]}
@Foo.eager_graph(:bars).all.map{|o| [o, o.bars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT foos.id, foos.object_ids, bars.id AS bars_id, bars.object_ids AS bars_object_ids FROM foos LEFT OUTER JOIN bars ON (bars.object_ids[0] = foos.object_ids[0])"]
@db.fetch = {:id=>1, :object_ids=>[2], :bar_id=>1, :bar_object_ids=>[2]}
@Foo.eager_graph(:bar).all.map{|o| [o, o.bar]}.must_equal [[@foo, @bar]]
@db.sqls.must_equal ["SELECT foos.id, foos.object_ids, bar.id AS bar_id, bar.object_ids AS bar_object_ids FROM foos LEFT OUTER JOIN bars AS bar ON (bar.object_ids[0] = foos.object_ids[0])"]
@db.fetch = {:id=>1, :object_ids=>[2], :mtmfoos_id=>1, :mtmfoos_object_ids=>[2]}
@Bar.eager_graph(:mtmfoos).all.map{|o| [o, o.mtmfoos]}.must_equal [[@bar, [@foo]]]
@db.sqls.must_equal ["SELECT bars.id, bars.object_ids, mtmfoos.id AS mtmfoos_id, mtmfoos.object_ids AS mtmfoos_object_ids FROM bars LEFT OUTER JOIN bars_foos ON (bars_foos.bar_ids[0] = bars.object_ids[0]) LEFT OUTER JOIN foos AS mtmfoos ON (mtmfoos.object_ids[0] = bars_foos.foo_ids[0])"]
@db.fetch = {:id=>1, :object_ids=>[2], :mtmbars_id=>1, :mtmbars_object_ids=>[2]}
@Foo.eager_graph(:mtmbars).all.map{|o| [o, o.mtmbars]}.must_equal [[@foo, [@bar]]]
@db.sqls.must_equal ["SELECT foos.id, foos.object_ids, mtmbars.id AS mtmbars_id, mtmbars.object_ids AS mtmbars_object_ids FROM foos LEFT OUTER JOIN bars_foos ON (bars_foos.foo_ids[0] = foos.object_ids[0]) LEFT OUTER JOIN bars AS mtmbars ON (mtmbars.object_ids[0] = bars_foos.bar_ids[0])"]
end
it "should have working filter by associations with model instances" do
@Bar.first(:foo=>@foo).must_equal @bar
@db.sqls.must_equal ["SELECT * FROM bars WHERE (bars.object_ids[0] = 2) LIMIT 1"]
@Foo.first(:bars=>@bar).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] = 2) LIMIT 1"]
@Foo.first(:bar=>@bar).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] = 2) LIMIT 1"]
@Foo.first(:mtmbars=>@bar).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars_foos.foo_ids[0] FROM bars_foos WHERE ((bars_foos.bar_ids[0] = 2) AND (bars_foos.foo_ids[0] IS NOT NULL)))) LIMIT 1"]
@Bar.first(:mtmfoos=>@foo).must_equal @bar
@db.sqls.must_equal ["SELECT * FROM bars WHERE (bars.object_ids[0] IN (SELECT bars_foos.bar_ids[0] FROM bars_foos WHERE ((bars_foos.foo_ids[0] = 2) AND (bars_foos.bar_ids[0] IS NOT NULL)))) LIMIT 1"]
end
it "should have working filter by associations for associations with :conditions with model instances" do
@Bar.many_to_one :foo, :clone=>:foo, :conditions=>{:name=>'A'}
@Foo.one_to_many :bars, :clone=>:bars, :conditions=>{:name=>'A'}
@Foo.one_to_one :bar, :clone=>:bars
@Foo.many_to_many :mtmbars, :clone=>:mtmbars, :conditions=>{:name=>'A'}
@Bar.many_to_many :mtmfoos, :clone=>:mtmfoos, :conditions=>{:name=>'A'}
@Bar.where(:foo=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_ids[0] IN (SELECT foos.object_ids[0] FROM foos WHERE ((name = 'A') AND (foos.object_ids[0] IS NOT NULL) AND (foos.id = 1))))"
@Foo.where(:bars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars.object_ids[0] FROM bars WHERE ((name = 'A') AND (bars.object_ids[0] IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:bar=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars.object_ids[0] FROM bars WHERE ((name = 'A') AND (bars.object_ids[0] IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:mtmbars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars_foos.foo_ids[0] FROM bars INNER JOIN bars_foos ON (bars_foos.bar_ids[0] = bars.object_ids[0]) WHERE ((name = 'A') AND (bars_foos.foo_ids[0] IS NOT NULL) AND (bars.id = 1))))"
@Bar.where(:mtmfoos=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_ids[0] IN (SELECT bars_foos.bar_ids[0] FROM foos INNER JOIN bars_foos ON (bars_foos.foo_ids[0] = foos.object_ids[0]) WHERE ((name = 'A') AND (bars_foos.bar_ids[0] IS NOT NULL) AND (foos.id = 1))))"
end
it "should have working filter by associations for associations with block with model instances" do
b = lambda{|ds| ds.where(:name=>'A')}
@Bar.many_to_one :foo, :clone=>:foo, &b
@Foo.one_to_many :bars, :clone=>:bars, &b
@Foo.one_to_one :bar, :clone=>:bars
@Foo.many_to_many :mtmbars, :clone=>:mtmbars, &b
@Bar.many_to_many :mtmfoos, :clone=>:mtmfoos, &b
@Bar.where(:foo=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_ids[0] IN (SELECT foos.object_ids[0] FROM foos WHERE ((name = 'A') AND (foos.object_ids[0] IS NOT NULL) AND (foos.id = 1))))"
@Foo.where(:bars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars.object_ids[0] FROM bars WHERE ((name = 'A') AND (bars.object_ids[0] IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:bar=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars.object_ids[0] FROM bars WHERE ((name = 'A') AND (bars.object_ids[0] IS NOT NULL) AND (bars.id = 1))))"
@Foo.where(:mtmbars=>@bar).sql.must_equal "SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars_foos.foo_ids[0] FROM bars INNER JOIN bars_foos ON (bars_foos.bar_ids[0] = bars.object_ids[0]) WHERE ((name = 'A') AND (bars_foos.foo_ids[0] IS NOT NULL) AND (bars.id = 1))))"
@Bar.where(:mtmfoos=>@foo).sql.must_equal "SELECT * FROM bars WHERE (bars.object_ids[0] IN (SELECT bars_foos.bar_ids[0] FROM foos INNER JOIN bars_foos ON (bars_foos.foo_ids[0] = foos.object_ids[0]) WHERE ((name = 'A') AND (bars_foos.bar_ids[0] IS NOT NULL) AND (foos.id = 1))))"
end
it "should have working filter by associations with model datasets" do
@Bar.first(:foo=>@Foo.where(:id=>@foo.id)).must_equal @bar
@db.sqls.must_equal ["SELECT * FROM bars WHERE (bars.object_ids[0] IN (SELECT foos.object_ids[0] FROM foos WHERE ((id = 1) AND (foos.object_ids[0] IS NOT NULL)))) LIMIT 1"]
@Foo.first(:bars=>@Bar.where(:id=>@bar.id)).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars.object_ids[0] FROM bars WHERE ((id = 1) AND (bars.object_ids[0] IS NOT NULL)))) LIMIT 1"]
@Foo.first(:bar=>@Bar.where(:id=>@bar.id)).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars.object_ids[0] FROM bars WHERE ((id = 1) AND (bars.object_ids[0] IS NOT NULL)))) LIMIT 1"]
@Foo.first(:mtmbars=>@Bar.where(:id=>@bar.id)).must_equal @foo
@db.sqls.must_equal ["SELECT * FROM foos WHERE (foos.object_ids[0] IN (SELECT bars_foos.foo_ids[0] FROM bars_foos WHERE ((bars_foos.bar_ids[0] IN (SELECT bars.object_ids[0] FROM bars WHERE ((id = 1) AND (bars.object_ids[0] IS NOT NULL)))) AND (bars_foos.foo_ids[0] IS NOT NULL)))) LIMIT 1"]
@Bar.first(:mtmfoos=>@Foo.where(:id=>@foo.id)).must_equal @bar
@db.sqls.must_equal ["SELECT * FROM bars WHERE (bars.object_ids[0] IN (SELECT bars_foos.bar_ids[0] FROM bars_foos WHERE ((bars_foos.foo_ids[0] IN (SELECT foos.object_ids[0] FROM foos WHERE ((id = 1) AND (foos.object_ids[0] IS NOT NULL)))) AND (bars_foos.bar_ids[0] IS NOT NULL)))) LIMIT 1"]
end
end
describe Sequel::Model, "#refresh" do
before do
@c = Class.new(Sequel::Model(:items)) do
unrestrict_primary_key
columns :id, :x
end
DB.reset
end
it "should remove cached associations" do
@c.many_to_one :node, :class=>@c
@m = @c.new(:id => 555)
@m.associations[:node] = 15
@m.reload
@m.associations.must_equal({})
end
end
describe "Model#freeze" do
before do
class ::Album < Sequel::Model
columns :id
class B < Sequel::Model
columns :id, :album_id
many_to_one :album, :class=>Album
end
one_to_one :b, :key=>:album_id, :class=>B
end
@o = Album.load(:id=>1).freeze
DB.sqls
end
after do
Object.send(:remove_const, :Album)
end
it "should freeze the object's associations" do
@o.associations.frozen?.must_equal true
end
it "should freeze associations after validating" do
Album.send(:define_method, :validate){super(); b}
@o = Album.load(:id=>1)
@o.freeze
@o.associations.fetch(:b).id.must_equal 1
end
it "should not break associations getters" do
Album::B.dataset = Album::B.dataset.with_fetch(:album_id=>1, :id=>2)
@o.b.must_equal Album::B.load(:id=>2, :album_id=>1)
@o.associations[:b].must_be_nil
@o = @o.dup
@o.b.must_equal Album::B.load(:id=>2, :album_id=>1)
@o.associations[:b].must_equal Album::B.load(:id=>2, :album_id=>1)
end
it "should not break reciprocal associations" do
b = Album::B.load(:id=>2, :album_id=>nil)
b.album = @o
@o.associations[:b].must_be_nil
@o = @o.dup
b = Album::B.load(:id=>2, :album_id=>nil)
b.album = @o
@o.associations[:b].must_equal Album::B.load(:id=>2, :album_id=>1)
end
end
describe "association autoreloading" do
before do
@c = Class.new(Sequel::Model)
@Artist = Class.new(@c).set_dataset(:artists)
@Artist.dataset = @Artist.dataset.with_fetch(:id=>2, :name=>'Ar')
@Album = Class.new(@c).set_dataset(:albums)
@Artist.columns :id, :name
@Album.columns :id, :name, :artist_id
@Album.db_schema[:artist_id][:type] = :integer
@Album.many_to_one :artist, :class=>@Artist
DB.reset
end
it "should not reload many_to_one association when foreign key is not modified" do
album = @Album.load(:id => 1, :name=>'Al', :artist_id=>1)
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 1']
album.artist_id = 1
album.artist
DB.sqls.must_equal []
album = @Album.new(:name=>'Al', :artist_id=>1)
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 1']
album.artist_id = 1
album.artist
DB.sqls.must_equal []
end
it "should reload many_to_one association when foreign key is modified" do
album = @Album.load(:id => 1, :name=>'Al', :artist_id=>2)
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 2']
album.artist_id = 1
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 1']
end
it "should handle multiple many_to_one association with the same foreign key" do
@Album.many_to_one :artist2, :key=>:artist_id, :class=>@Artist
album = @Album.load(:id => 1, :name=>'Al', :artist_id=>2)
album.artist
album.artist2
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 2'] * 2
album.artist
album.artist2
DB.sqls.must_equal []
album.artist_id = 1
album.artist
album.artist2
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 1'] * 2
end
it "should not reload when value has not changed" do
album = @Album.load(:id => 1, :name=>'Al', :artist_id=>2)
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 2']
album.artist_id = 2
album.artist
DB.sqls.must_equal []
album.artist_id = "2"
album.artist
DB.sqls.must_equal []
end
it "should reload all associations which use the foreign key" do
@Album.many_to_one :other_artist, :key => :artist_id, :foreign_key => :id, :class => @Artist
album = @Album.load(:id => 1, :name=>'Al', :artist_id=>2)
album.artist
album.other_artist
DB.reset
album.artist_id = 1
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 1']
album.other_artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 1']
end
it "should work with composite keys" do
@Album.many_to_one :composite_artist, :key => [:artist_id, :name], :primary_key => [:id, :name], :class => @Artist
album = @Album.load(:id => 1, :name=>'Al', :artist_id=>2)
album.composite_artist
DB.reset
album.artist_id = 1
album.composite_artist
DB.sqls.must_equal ["SELECT * FROM artists WHERE ((artists.id = 1) AND (artists.name = 'Al')) LIMIT 1"]
album.name = 'Al2'
album.composite_artist
DB.sqls.must_equal ["SELECT * FROM artists WHERE ((artists.id = 1) AND (artists.name = 'Al2')) LIMIT 1"]
end
it "should work with subclasses" do
salbum = Class.new(@Album)
oartist = Class.new(@c).set_dataset(:oartist)
oartist.columns :id, :name
salbum.many_to_one :artist2, :class=>oartist, :key=>:artist_id
album = salbum.load(:id => 1, :name=>'Al', :artist_id=>2)
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 2']
album.artist_id = 1
album.artist
DB.sqls.must_equal ['SELECT * FROM artists WHERE id = 1']
end
end
describe Sequel::Model, ".dataset_module" do
before do
@c = Class.new(Sequel::Model(:items))
end
it "should have dataset_module support an eager method" do
@c.many_to_one :foo, :class=>@c
@c.many_to_one :bar, :class=>@c
@c.many_to_one :baz, :class=>@c
@c.many_to_one :quux, :class=>@c
@c.dataset_module{eager(:foo, {:foo=>{:bar=>:baz}}, :quux)}
@c.foo.opts[:eager].must_equal(:foo=>{:bar=>:baz}, :quux=>nil)
@c.where(:bar).foo.opts[:eager].must_equal(:foo=>{:bar=>:baz}, :quux=>nil)
end
end
|