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
|
require_relative "spec_helper"
describe "association_multi_add_remove plugin - 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
plugin :association_multi_add_remove
unrestrict_primary_key
attr_accessor :xxx
def self.name; 'Node'; end
def self.to_s; 'Node'; end
columns :id, :x
private
def _refresh(ds); end
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 define an add_*s method that works on existing records" do
@c2.one_to_many :attributes, class: @c1
n = @c2.load(id: 1234)
a1 = @c1.load(id: 2345)
a2 = @c1.load(id: 3456)
[a1, a2].must_equal n.add_attributes([a1, a2])
a1.values.must_equal(:node_id => 1234, id: 2345)
a2.values.must_equal(:node_id => 1234, id: 3456)
DB.sqls.must_equal [
'BEGIN',
'UPDATE attributes SET node_id = 1234 WHERE (id = 2345)',
'UPDATE attributes SET node_id = 1234 WHERE (id = 3456)',
'COMMIT'
]
end
it "should not define add/remove methods with the same name as the ones defined by default " do
@c2.one_to_many :sheep, class: @c1, :key=>:node_id
n = @c2.load(id: 1234)
a1 = @c1.load(id: 2345)
a1.must_be_same_as n.add_sheep(a1)
a1.values.must_equal(:node_id => 1234, id: 2345)
DB.sqls.must_equal ['UPDATE attributes SET node_id = 1234 WHERE (id = 2345)']
a1.must_be_same_as n.remove_sheep(a1)
a1.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)',
]
n.respond_to?(:sheep=).must_equal false
end
it "should support :multi_add_method" do
@c2.one_to_many :attributes, class: @c1, :multi_add_method=>:add_multiple_attributes
n = @c2.load(id: 1234)
a1 = @c1.load(id: 2345)
a2 = @c1.load(id: 3456)
[a1, a2].must_equal n.add_multiple_attributes([a1, a2])
a1.values.must_equal(:node_id => 1234, id: 2345)
a2.values.must_equal(:node_id => 1234, id: 3456)
DB.sqls.must_equal [
'BEGIN',
'UPDATE attributes SET node_id = 1234 WHERE (id = 2345)',
'UPDATE attributes SET node_id = 1234 WHERE (id = 3456)',
'COMMIT'
]
end
it "should define an add_*s method that works on new records" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a1 = @c1.new(:id => 234)
a2 = @c1.new(:id => 345)
@c1.dataset = @c1.dataset.with_fetch([
[{ :id=>234, :node_id=>1234 }], [{ :id=>345, :node_id=>1234 }]
])
[a1, a2].must_equal n.add_attributes([a1, a2])
DB.sqls.must_equal [
'BEGIN',
"INSERT INTO attributes (id, node_id) VALUES (234, 1234)",
"SELECT * FROM attributes WHERE id = 234",
"INSERT INTO attributes (id, node_id) VALUES (345, 1234)",
"SELECT * FROM attributes WHERE id = 345",
'COMMIT'
]
a1.values.must_equal(:node_id => 1234, :id => 234)
a2.values.must_equal(:node_id => 1234, :id => 345)
end
it "should define a remove_*s method that works on existing records" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a1 = @c1.load(:id => 2345, :node_id => 1234)
a2 = @c1.load(:id => 3456, :node_id => 1234)
[a1, a2].must_equal n.remove_attributes([a1, a2])
a1.values.must_equal(:node_id => nil, :id => 2345)
a2.values.must_equal(:node_id => nil, :id => 3456)
DB.sqls.must_equal [
'BEGIN',
"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)',
"SELECT 1 AS one FROM attributes WHERE ((attributes.node_id = 1234) AND (id = 3456)) LIMIT 1",
'UPDATE attributes SET node_id = NULL WHERE (id = 3456)',
'COMMIT'
]
end
it "should support :multi_remove_method" do
@c2.one_to_many :attributes, :class => @c1, :multi_remove_method=>:remove_multiple_attributes
n = @c2.load(:id => 1234)
a1 = @c1.load(:id => 2345, :node_id => 1234)
a2 = @c1.load(:id => 3456, :node_id => 1234)
[a1, a2].must_equal n.remove_multiple_attributes([a1, a2])
a1.values.must_equal(:node_id => nil, :id => 2345)
a2.values.must_equal(:node_id => nil, :id => 3456)
DB.sqls.must_equal [
'BEGIN',
"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)',
"SELECT 1 AS one FROM attributes WHERE ((attributes.node_id = 1234) AND (id = 3456)) LIMIT 1",
'UPDATE attributes SET node_id = NULL WHERE (id = 3456)',
'COMMIT'
]
end
it "should have the remove_*s method raise an error if the passed objects are not already associated" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a1 = @c1.load(:id => 2345, :node_id => 1234)
a2 = @c1.load(:id => 3456, :node_id => 1234)
@c1.dataset = @c1.dataset.with_fetch([])
proc{n.remove_attributes([a1, a2])}.must_raise(Sequel::Error)
DB.sqls.must_equal [
'BEGIN',
"SELECT 1 AS one FROM attributes WHERE ((attributes.node_id = 1234) AND (id = 2345)) LIMIT 1",
'ROLLBACK'
]
end
it "should accept hashes for the add_*s method and create a new records" 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 }], [{ :node_id => 1234, :id => 345 }]
])
n.add_attributes([{ :id => 234 }, { :id => 345 }]).must_equal [
@c1.load(:node_id => 1234, :id => 234),
@c1.load(:node_id => 1234, :id => 345)
]
DB.sqls.must_equal [
'BEGIN',
"INSERT INTO attributes (id, node_id) VALUES (234, 1234)",
"SELECT * FROM attributes WHERE id = 234",
"INSERT INTO attributes (id, node_id) VALUES (345, 1234)",
"SELECT * FROM attributes WHERE id = 345",
'COMMIT'
]
end
it "should accept primary keys for the add_*s 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 }], [{ :node_id => nil, :id => 345 }]
])
n.add_attributes([234, 345]).must_equal [
@c1.load(:node_id => 1234, :id => 234),
@c1.load(:node_id => 1234, :id => 345)
]
DB.sqls.must_equal [
'BEGIN',
"SELECT * FROM attributes WHERE id = 234",
"UPDATE attributes SET node_id = 1234 WHERE (id = 234)",
"SELECT * FROM attributes WHERE id = 345",
"UPDATE attributes SET node_id = 1234 WHERE (id = 345)",
'COMMIT'
]
end
it "should raise an error if the primary key passed to the add_*s 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_attributes([234, 345])}.must_raise(Sequel::NoMatchingRow)
DB.sqls.must_equal [
'BEGIN',
"SELECT * FROM attributes WHERE id = 234",
'ROLLBACK'
]
end
it "should raise an error in the add_*s method if the passed associated objects are not of the correct type" do
@c2.one_to_many :attributes, :class => @c1
proc{@c2.new(:id => 1234).add_attributes([@c2.new, @c2.new])}.must_raise(Sequel::Error)
end
it "should accept primary keys for the remove_*s method and remove existing records" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch([
[{ :id=>234, :node_id=>1234 }], [{ :id=>345, :node_id=>1234 }]
])
n.remove_attributes([234, 345]).must_equal [
@c1.load(:node_id => nil, :id => 234),
@c1.load(:node_id => nil, :id => 345)
]
DB.sqls.must_equal [
'BEGIN',
'SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (attributes.id = 234)) LIMIT 1',
'UPDATE attributes SET node_id = NULL WHERE (id = 234)',
'SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (attributes.id = 345)) LIMIT 1',
'UPDATE attributes SET node_id = NULL WHERE (id = 345)',
'COMMIT'
]
end
it "should raise an error in the remove_*s method if the passed associated objects are not of the correct type" do
@c2.one_to_many :attributes, :class => @c1
proc{@c2.new(:id => 1234).remove_attributes([@c2.new, @c2.new])}.must_raise(Sequel::Error)
end
it "should have add_*s method respect the :primary_key option" do
@c2.one_to_many :attributes, :class => @c1, :primary_key=>:xxx
n = @c2.new(:id => 1234, :xxx=>5)
a1 = @c1.load(:id => 2345)
a2 = @c1.load(:id => 3456)
n.add_attributes([a1, a2]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
'UPDATE attributes SET node_id = 5 WHERE (id = 2345)',
'UPDATE attributes SET node_id = 5 WHERE (id = 3456)',
'COMMIT'
]
end
it "should have add_*s method not add the same objects to the cached association array if the objects are already in the array" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a1 = @c1.load(:id => 2345)
a2 = @c1.load(:id => 3456)
n.associations[:attributes] = []
[a1, a2].must_equal n.add_attributes([a1, a2])
[a1, a2].must_equal n.add_attributes([a1, a2])
a1.values.must_equal(:node_id => 1234, :id => 2345)
a2.values.must_equal(:node_id => 1234, :id => 3456)
n.attributes.must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
'UPDATE attributes SET node_id = 1234 WHERE (id = 2345)',
'UPDATE attributes SET node_id = 1234 WHERE (id = 3456)',
'COMMIT'
] * 2
end
it "should have add_*s 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)
a1 = @c1.load(:id => 2345)
a2 = @c1.load(:id => 3456)
n.add_attributes([a1, a2]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
"UPDATE attributes SET node_id = 1234, y = 5 WHERE (id = 2345)",
"UPDATE attributes SET node_id = 1234, y = 5 WHERE (id = 3456)",
'COMMIT'
]
end
it "should have add_*s method accept composite keys" do
@c1.dataset = @c1.dataset.with_fetch([
[{ :id=>2345, :node_id=>1234, :z=>8, :y=>5 }],
[{ :id=>3456, :node_id=>1234, :z=>9, :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)
a1 = @c1.load(:id => 2345, :z => 8, :node_id => 1234, :y=>5)
a2 = @c1.load(:id => 3456, :z => 9, :node_id => 1234, :y=>5)
n.add_attributes([[2345, 8], [3456, 9]]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
"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))",
"SELECT * FROM attributes WHERE ((id = 3456) AND (z = 9)) LIMIT 1",
"UPDATE attributes SET node_id = 1234, y = 5 WHERE ((id = 3456) AND (z = 9))",
'COMMIT'
]
end
it "should have remove_*s 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)
a1 = @c1.load(:id => 2345, :node_id=>1234, :y=>5)
a2 = @c1.load(:id => 3456, :node_id=>1234, :y=>5)
n.remove_attributes([a1, a2]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
"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)",
"SELECT 1 AS one FROM attributes WHERE ((attributes.node_id = 1234) AND (attributes.y = 5) AND (id = 3456)) LIMIT 1",
"UPDATE attributes SET node_id = NULL, y = NULL WHERE (id = 3456)",
'COMMIT'
]
end
it "should accept a array of composite primary keys values for the remove_*s method and remove existing records" do
@c1.dataset = @c1.dataset.with_fetch([
[{ :id=>234, :node_id=>123, :y=>5 }], [{ :id=>345, :node_id=>123, :y=>6 }]
])
@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_attributes([[234, 5], [345, 6]]).must_equal [
@c1.load(:node_id => nil, :y => 5, :id => 234),
@c1.load(:node_id => nil, :y => 6, :id => 345)
]
DB.sqls.must_equal [
'BEGIN',
"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))",
"SELECT * FROM attributes WHERE ((attributes.node_id = 123) AND (attributes.id = 345) AND (attributes.y = 6)) LIMIT 1",
"UPDATE attributes SET node_id = NULL WHERE ((id = 345) AND (y = 6))",
'COMMIT'
]
end
it "should raise an error in add_*s and remove_*s if the passed objects return false to save (are not valid)" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a1 = @c1.new(:id => 2345)
a2 = @c1.new(:id => 3456)
def a1.validate() errors.add(:id, 'foo') end
def a2.validate() errors.add(:id, 'bar') end
proc{n.add_attributes([a1, a2])}.must_raise(Sequel::ValidationFailed)
proc{n.remove_attributes([a1, a2])}.must_raise(Sequel::ValidationFailed)
end
it "should not validate the associated objects in add_*s and remove_*s if the :validate=>false option is used" do
@c2.one_to_many :attributes, :class => @c1, :validate=>false
n = @c2.new(:id => 1234)
a1 = @c1.new(:id => 2345)
a2 = @c1.new(:id => 3456)
def a1.validate() errors.add(:id, 'foo') end
def a2.validate() errors.add(:id, 'bar') end
n.add_attributes([a1, a2]).must_equal [a1, a2]
n.remove_attributes([a1, a2]).must_equal [a1, a2]
end
it "should not raise exception in add_*s and remove_*s 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)
a1 = @c1.new(:id => 2345)
a2 = @c1.new(:id => 3456)
def a1.validate() errors.add(:id, 'foo') end
def a2.validate() errors.add(:id, 'bar') end
n.associations[:attributes] = []
n.add_attributes([a1, a2]).must_equal []
n.associations[:attributes].must_equal []
n.remove_attributes([a1, a2]).must_equal []
n.associations[:attributes].must_equal []
end
it "should add item to cache if it exists when calling add_*s" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.new(:id => 123)
a1 = @c1.load(:id => 234)
a2 = @c1.load(:id => 345)
arr = []
n.associations[:attributes] = arr
n.add_attributes([a1, a2])
arr.must_equal [a1, a2]
end
it "should set object to item's reciprocal cache when calling add_*s" do
@c2.one_to_many :attributes, :class => @c1
@c1.many_to_one :node, :class => @c2
n = @c2.new(:id => 123)
a1 = @c1.new(:id => 234)
a2 = @c1.new(:id => 345)
n.add_attributes([a1, a2])
a1.node.must_equal n
a2.node.must_equal n
end
it "should remove item from cache if it exists when calling remove_*s" do
@c2.one_to_many :attributes, :class => @c1
n = @c2.load(:id => 123)
a1 = @c1.load(:id => 234)
a2 = @c1.load(:id => 345)
arr = [a1, a2]
n.associations[:attributes] = arr
n.remove_attributes([a1, a2])
arr.must_equal []
end
it "should remove item's reciprocal cache calling remove_*s" do
@c2.one_to_many :attributes, :class => @c1
@c1.many_to_one :node, :class => @c2
n = @c2.new(:id => 123)
a1 = @c1.new(:id => 234)
a2 = @c1.new(:id => 345)
a1.associations[:node] = n
a2.associations[:node] = n
a1.node.must_equal n
a2.node.must_equal n
n.remove_attributes([a1, a2])
a1.node.must_be_nil
a2.node.must_be_nil
end
it "should not create the add_*s or remove_*s methods if :read_only option is used" do
@c2.one_to_many :attributes, :class => @c1, :read_only=>true
im = @c2.instance_methods
im.wont_include(:add_attributes)
im.wont_include(:remove_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(:add_attributes)
im.must_include(:remove_attributes)
im2 = @c2.instance_methods(false)
im2.wont_include(:add_attributes)
im2.wont_include(:remove_attributes)
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)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
def p._add_attribute(x)
(@x ||= []) << x
end
def c1._node_id=; raise; end
def c2._node_id=; raise; end
p.add_attributes([c1, c2])
p.instance_variable_get(:@x).must_equal [c1, c2]
end
it "should allow additional arguments given to the add_*s method and pass them onwards to the _add_ method" do
@c2.one_to_many :attributes, :class => @c1
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
def p._add_attribute(x,*y)
(@x ||= []) << x
(@y ||= []) << y
end
def c1._node_id=; raise; end
def c2._node_id=; raise; end
p.add_attributes([c1, c2], :foo, :bar=>:baz)
p.instance_variable_get(:@x).must_equal [c1, c2]
p.instance_variable_get(:@y).must_equal [
[:foo,{:bar=>:baz}], [: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)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
def p._remove_attribute(x)
(@x ||= []) << x
end
def c1._node_id=; raise; end
def c2._node_id=; raise; end
p.remove_attributes([c1, c2])
p.instance_variable_get(:@x).must_equal [c1, c2]
end
it "should allow additional arguments given to the remove_*s method and pass them onwards to the _remove_ method" do
@c2.one_to_many :attributes, :class => @c1, :reciprocal=>nil
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
def p._remove_attribute(x,*y)
(@x ||= []) << x
(@y ||= []) << y
end
def c1._node_id=; raise; end
def c2._node_id=; raise; end
p.remove_attributes([c1, c2], :foo, :bar=>:baz)
p.instance_variable_get(:@x).must_equal [c1, c2]
p.instance_variable_get(:@y).must_equal [
[:foo,{:bar=>:baz}], [:foo,{:bar=>:baz}]
]
end
it "should support (before|after)_(add|remove) callbacks for (add|remove)_*s methods" 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 blah(x)
model::Foo << x.pk
end
def blahr(x)
model::Foo << 6
end
private
def _add_attribute(v)
model::Foo << 4
end
def _remove_attribute(v)
model::Foo << 5
end
end
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
h.must_equal []
p.add_attributes([c1, c2])
h.must_equal [
10, -123, 123, 4, 3,
10, -234, 234, 4, 3
]
p.remove_attributes([c1, c2])
h.must_equal [
10, -123, 123, 4, 3,
10, -234, 234, 4, 3,
123, 5, 6,
234, 5, 6
]
end
it "should raise error and not call internal add_*s or remove_*s method if before callback calls cancel_action if raise_on_save_failure is true" do
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
@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_attributes([c1, c2])}.must_raise(Sequel::HookFailed)
p.attributes.must_equal []
p.associations[:attributes] = [c1, c2]
def p.br(o); cancel_action; end
proc{p.remove_attributes([c1, c2])}.must_raise(Sequel::HookFailed)
p.attributes.must_equal [c1, c2]
end
it "should return nil and not call internal add_*s or remove_*s method if before callback calls cancel_action if raise_on_save_failure is false" do
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
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_attributes([c1, c2]).must_equal []
p.attributes.must_equal []
p.associations[:attributes] = [c1, c2]
def p.br(o); cancel_action; end
p.remove_attributes([c1, c2]).must_equal []
p.attributes.must_equal [c1, c2]
end
it "should define a setter that works on existing records" do
@c2.one_to_many :attributes, class: @c1
n = @c2.load(id: 1234)
a1 = @c1.load(id: 2345, node_id: 1234)
a2 = @c1.load(id: 3456, node_id: 1234)
a3 = @c1.load(id: 4567)
n.associations[:attributes] = [a1, a2]
[a2, a3].must_equal(n.attributes = [a2, a3])
a1.values.must_equal(node_id: nil, id: 2345)
a2.values.must_equal(node_id: 1234, id: 3456)
a3.values.must_equal(node_id: 1234, id: 4567)
DB.sqls.must_equal [
'BEGIN',
'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)',
'UPDATE attributes SET node_id = 1234 WHERE (id = 4567)',
'COMMIT'
]
end
end
describe "association_multi_add_remove plugin - 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
plugin :association_multi_add_remove
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 define an add_*s method that works on existing records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a1 = @c1.load(:id => 2345)
a2 = @c1.load(:id => 3456)
n.add_attributes([a1, a2]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2345)",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 3456)",
'COMMIT'
]
end
it "should define an add_*s method that works with a primary key" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
a1 = @c1.load(:id => 2345)
a2 = @c1.load(:id => 3456)
@c1.dataset = @c1.dataset.with_fetch([[{ :id=>2345 }], [{ :id=>3456 }]])
n.add_attributes([2345, 3456]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
"SELECT * FROM attributes WHERE id = 2345",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2345)",
"SELECT * FROM attributes WHERE id = 3456",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 3456)",
'COMMIT'
]
end
it "should allow passing hashes to the add_*s method which creates new records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.load(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch([[{ :id=>1 }], [{ :id=>2 }]])
n.add_attributes([{ :id => 1 }, { :id => 2 }]).must_equal [
@c1.load(:id => 1), @c1.load(:id => 2)
]
DB.sqls.must_equal [
'BEGIN',
'INSERT INTO attributes (id) VALUES (1)',
"SELECT * FROM attributes WHERE id = 1",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 1)",
'INSERT INTO attributes (id) VALUES (2)',
"SELECT * FROM attributes WHERE id = 2",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2)",
'COMMIT'
]
end
it "should define a remove_*s method that works on existing records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a1 = @c1.new(:id => 2345)
a2 = @c1.new(:id => 3456)
n.remove_attributes([a1, a2]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
'DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 2345))',
'DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 3456))',
'COMMIT'
]
end
it "should accept primary keys for the remove_*s method and remove existing records" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
@c1.dataset = @c1.dataset.with_fetch([[{ :id=>234 }], [{ :id=>345 }]])
n.remove_attributes([234, 345]).must_equal [
@c1.load(:id => 234), @c1.load(:id => 345)
]
DB.sqls.must_equal [
'BEGIN',
"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))",
"SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (attributes.id = 345)) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 345))",
'COMMIT'
]
end
it "should have the add_*s 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)
a1 = @c1.load(:id => 2345).set(:yyy=>8)
a2 = @c1.load(:id => 3456).set(:yyy=>9)
n.add_attributes([a1, a2]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (5, 8)",
"INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (5, 9)",
'COMMIT'
]
end
it "should have the add_*s 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 }], [{ :id=>3456, :z=>9 }]
])
@c1.set_primary_key [:id, :z]
n = @c2.load(:id => 1234, :x=>5)
a1 = @c1.load(:id => 2345, :z=>8)
a2 = @c1.load(:id => 3456, :z=>9)
n.add_attributes([[2345, 8], [3456, 9]]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
"SELECT * FROM attributes WHERE ((id = 2345) AND (z = 8)) LIMIT 1",
"INSERT INTO attributes_nodes (l1, l2, r1, r2) VALUES (1234, 5, 2345, 8)",
"SELECT * FROM attributes WHERE ((id = 3456) AND (z = 9)) LIMIT 1",
"INSERT INTO attributes_nodes (l1, l2, r1, r2) VALUES (1234, 5, 3456, 9)",
'COMMIT'
]
end
it "should have the remove_*s 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)
a1 = @c1.new(:id => 2345, :yyy=>8)
a2 = @c1.new(:id => 3456, :yyy=>9)
n.remove_attributes([a1, a2]).must_equal [a1, a2]
DB.sqls.must_equal [
'BEGIN',
'DELETE FROM attributes_nodes WHERE ((node_id = 5) AND (attribute_id = 8))',
'DELETE FROM attributes_nodes WHERE ((node_id = 5) AND (attribute_id = 9))',
'COMMIT'
]
end
it "should have the remove_*s 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)
a1 = @c1.load(:id => 2345, :z=>8)
a2 = @c1.load(:id => 3456, :z=>9)
[a1, a2].must_equal n.remove_attributes([a1, a2])
DB.sqls.must_equal [
'BEGIN',
"DELETE FROM attributes_nodes WHERE ((l1 = 1234) AND (l2 = 5) AND (r1 = 2345) AND (r2 = 8))",
"DELETE FROM attributes_nodes WHERE ((l1 = 1234) AND (l2 = 5) AND (r1 = 3456) AND (r2 = 9))",
'COMMIT'
]
end
it "should accept an array of arrays of composite primary key values for the remove_*s method and remove existing records" do
@c1.dataset = @c1.dataset.with_fetch([
[{ :id=>234, :y=>8 }], [{ :id=>345, :y=>9 }]
])
@c1.set_primary_key [:id, :y]
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
n.remove_attributes([[234, 8], [345, 9]]).must_equal [
@c1.load(:id => 234, :y=>8), @c1.load(:id => 345, :y=>9)
]
DB.sqls.must_equal [
'BEGIN',
"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))",
"SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) WHERE ((attributes_nodes.node_id = 1234) AND (attributes.id = 345) AND (attributes.y = 9)) LIMIT 1",
"DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 345))",
'COMMIT'
]
end
it "should raise an error if trying to remove model objects that don't have valid primary keys" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new
a1 = @c1.load(:id=>123)
a2 = @c1.load(:id=>234)
proc { n.remove_attributes([a1, a2]) }.must_raise(Sequel::Error)
end
it "should remove items from cache if they exist when calling remove_*s" do
@c2.many_to_many :attributes, :class => @c1
n = @c2.new(:id => 1234)
a1 = @c1.load(:id => 345)
a2 = @c1.load(:id => 456)
arr = [a1, a2]
n.associations[:attributes] = arr
n.remove_attributes([a1, a2])
arr.must_equal []
end
it "should remove items from reciprocal's if they exist when calling remove_*s" do
@c2.many_to_many :attributes, :class => @c1
@c1.many_to_many :nodes, :class => @c2
n = @c2.new(:id => 1234)
a1 = @c1.new(:id => 345)
a2 = @c1.new(:id => 456)
a1.associations[:nodes] = [n]
a2.associations[:nodes] = [n]
n.remove_attributes([a1, a2])
a1.nodes.must_equal []
a2.nodes.must_equal []
end
it "should not create the add_*s or remove_*s methods if :read_only option is used" do
@c2.many_to_many :attributes, :class => @c1, :read_only=>true
im = @c2.instance_methods
im.wont_include(:add_attributes)
im.wont_include(:remove_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(:add_attributes)
im.must_include(:remove_attributes)
im2 = @c2.instance_methods(false)
im2.wont_include(:add_attributes)
im2.wont_include(:remove_attributes)
end
it "should call a _remove_*s 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)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
def p._remove_attribute(x)
(@x ||= []) << x
end
p.remove_attributes([c1, c2])
p.instance_variable_get(:@x).must_equal [c1, c2]
DB.sqls.must_equal ['BEGIN', 'COMMIT']
end
it "should support a :remover option for defining the _remove_*s method" do
@c2.many_to_many :attributes, :class => @c1,
:remover=>proc { |x| (@x ||= []) << x }
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
p.remove_attributes([c1, c2])
p.instance_variable_get(:@x).must_equal [c1, c2]
DB.sqls.must_equal ['BEGIN', 'COMMIT']
end
it "should allow additional arguments given to the remove_*s method and pass them onwards to the _remove_ method" do
@c2.many_to_many :attributes, :class => @c1
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
def p._remove_attribute(x,*y)
(@x ||= []) << x
(@y ||= []) << y
end
p.remove_attributes([c1, c2], :foo, :bar=>:baz)
p.instance_variable_get(:@x).must_equal [c1, c2]
p.instance_variable_get(:@y).must_equal [
[:foo, { :bar=>:baz }], [:foo, { :bar=>:baz }]
]
end
it "should raise an error in the remove_*s method if the passed associated objects are not of the correct type" do
@c2.many_to_many :attributes, :class => @c1
proc do
@c2.new(:id => 1234).remove_attributes([@c2.new, @c2.new])
end
.must_raise(Sequel::Error)
end
it "should support (before|after)_(add|remove) callbacks for (add|remove)_* methods" 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 blah(x)
model::Foo << x.pk
end
def blahr(x)
model::Foo << 6
end
private
def _add_attribute(v)
model::Foo << 4
end
def _remove_attribute(v)
model::Foo << 5
end
end
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
h.must_equal []
p.add_attributes([c1, c2])
h.must_equal [
10, -123, 123, 4, 3,
10, -234, 234, 4, 3
]
p.remove_attributes([c1, c2])
h.must_equal [
10, -123, 123, 4, 3,
10, -234, 234, 4, 3,
123, 5, 6,
234, 5, 6
]
end
it "should raise error and not call internal add_*s or remove_*s method if before callback calls cancel_action if raise_on_save_failure is true" do
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
@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_attributes([c1, c2])}.must_raise(Sequel::HookFailed)
p.attributes.must_equal []
p.associations[:attributes] = [c1, c2]
def p.br(o) cancel_action end
proc { p.remove_attributes([c1, c2]) }.must_raise(Sequel::HookFailed)
p.attributes.must_equal [c1, c2]
end
it "should return nil and not call internal add_*s or remove_*s method if before callback calls cancel_action if raise_on_save_failure is false" do
p = @c2.load(:id=>10)
c1 = @c1.load(:id=>123)
c2 = @c1.load(:id=>234)
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_attributes([c1, c2]).must_equal []
p.attributes.must_equal []
p.associations[:attributes] = [c1, c2]
def p.br(o) cancel_action end
p.remove_attributes([c1, c2]).must_equal []
p.attributes.must_equal [c1, c2]
end
it "should define a setter that works on existing records" do
@c2.many_to_many :attributes, class: @c1
n = @c2.load(id: 1234)
a1 = @c1.load(id: 2345)
a2 = @c1.load(id: 3456)
a3 = @c1.load(id: 4567)
n.associations[:attributes] = [a1, a2]
[a2, a3].must_equal(n.attributes = [a2, a3])
DB.sqls.must_equal [
'BEGIN',
'DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 2345))',
'INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 4567)',
'COMMIT'
]
end
end
describe "association_multi_add_remove plugin - sharding" do
before do
@db = Sequel.mock(:servers=>{:a=>{}}, :numrows=>1)
@c1 = Class.new(Sequel::Model(@db[:attributes])) do
unrestrict_primary_key
columns :id, :node_id, :y, :z
end
@c2 = Class.new(Sequel::Model(@db[:nodes])) do
plugin :association_multi_add_remove
unrestrict_primary_key
attr_accessor :xxx
def self.name; 'Node'; end
def self.to_s; 'Node'; end
columns :id, :x
private
def _refresh(ds); end
end
@dataset = @c2.dataset = @c2.dataset.with_fetch({})
@c1.dataset = @c1.dataset.with_fetch(proc { |sql| sql =~ /SELECT 1/ ? { a: 1 } : {} })
@db.sqls
end
it "should handle servers correctly" do
@c2.one_to_many :attributes, class: @c1
n = @c2.load(id: 1234).set_server(:a)
a1 = @c1.load(id: 2345).set_server(:a)
a2 = @c1.load(id: 3456).set_server(:a)
[a1, a2].must_equal n.add_attributes([a1, a2])
a1.values.must_equal(:node_id => 1234, id: 2345)
a2.values.must_equal(:node_id => 1234, id: 3456)
@db.sqls.must_equal [
'BEGIN -- a',
'UPDATE attributes SET node_id = 1234 WHERE (id = 2345) -- a',
'UPDATE attributes SET node_id = 1234 WHERE (id = 3456) -- a',
'COMMIT -- a'
]
end
end
|