|
31 | 31 | require "models/discount"
|
32 | 32 | require "models/line_item"
|
33 | 33 | require "models/shipping_line"
|
| 34 | +require "models/essay" |
34 | 35 |
|
35 | 36 | class AssociationsTest < ActiveRecord::TestCase
|
36 | 37 | fixtures :accounts, :companies, :developers, :projects, :developers_projects,
|
@@ -384,7 +385,7 @@ def test_associations_raise_with_name_error_if_associated_to_classes_that_do_not
|
384 | 385 | end
|
385 | 386 |
|
386 | 387 | class PreloaderTest < ActiveRecord::TestCase
|
387 |
| - fixtures :posts, :comments, :books, :authors, :tags, :taggings |
| 388 | + fixtures :posts, :comments, :books, :authors, :tags, :taggings, :essays, :categories |
388 | 389 |
|
389 | 390 | def test_preload_with_scope
|
390 | 391 | post = posts(:welcome)
|
@@ -760,6 +761,85 @@ def test_preload_does_not_group_same_scope_different_key_name
|
760 | 761 | postesque.author
|
761 | 762 | end
|
762 | 763 | end
|
| 764 | + |
| 765 | + def test_preload_with_available_records |
| 766 | + post = posts(:welcome) |
| 767 | + david = authors(:david) |
| 768 | + |
| 769 | + assert_no_queries do |
| 770 | + ActiveRecord::Associations::Preloader.new(records: [post], associations: :author, available_records: [[david]]).call |
| 771 | + |
| 772 | + assert_predicate post.association(:author), :loaded? |
| 773 | + assert_same david, post.author |
| 774 | + end |
| 775 | + end |
| 776 | + |
| 777 | + def test_preload_with_available_records_with_through_association |
| 778 | + author = authors(:david) |
| 779 | + categories = Category.all.to_a |
| 780 | + |
| 781 | + assert_queries(1) do |
| 782 | + # One query to get the middle records (i.e. essays) |
| 783 | + ActiveRecord::Associations::Preloader.new(records: [author], associations: :essay_category, available_records: categories).call |
| 784 | + end |
| 785 | + |
| 786 | + assert_predicate author.association(:essay_category), :loaded? |
| 787 | + assert categories.map(&:object_id).include?(author.essay_category.object_id) |
| 788 | + end |
| 789 | + |
| 790 | + def test_preload_with_available_records_with_multiple_classes |
| 791 | + essay = essays(:david_modest_proposal) |
| 792 | + general = categories(:general) |
| 793 | + david = authors(:david) |
| 794 | + |
| 795 | + assert_no_queries do |
| 796 | + ActiveRecord::Associations::Preloader.new(records: [essay], associations: [:category, :author], available_records: [general, david]).call |
| 797 | + |
| 798 | + assert_predicate essay.association(:category), :loaded? |
| 799 | + assert_predicate essay.association(:author), :loaded? |
| 800 | + assert_same general, essay.category |
| 801 | + assert_same david, essay.author |
| 802 | + end |
| 803 | + end |
| 804 | + |
| 805 | + def test_preload_with_available_records_queries_when_scoped |
| 806 | + post = posts(:welcome) |
| 807 | + david = authors(:david) |
| 808 | + |
| 809 | + assert_queries(1) do |
| 810 | + ActiveRecord::Associations::Preloader.new(records: [post], associations: :author, scope: Author.where(name: "David"), available_records: [david]).call |
| 811 | + end |
| 812 | + |
| 813 | + assert_predicate post.association(:author), :loaded? |
| 814 | + assert_not_equal david.object_id, post.author.object_id |
| 815 | + end |
| 816 | + |
| 817 | + def test_preload_with_available_records_queries_when_collection |
| 818 | + post = posts(:welcome) |
| 819 | + comments = Comment.all.to_a |
| 820 | + |
| 821 | + assert_queries(1) do |
| 822 | + ActiveRecord::Associations::Preloader.new(records: [post], associations: :comments, available_records: comments).call |
| 823 | + end |
| 824 | + |
| 825 | + assert_predicate post.association(:comments), :loaded? |
| 826 | + assert_empty post.comments.map(&:object_id) & comments.map(&:object_id) |
| 827 | + end |
| 828 | + |
| 829 | + def test_preload_with_available_records_queries_when_incomplete |
| 830 | + post = posts(:welcome) |
| 831 | + bob = authors(:bob) |
| 832 | + david = authors(:david) |
| 833 | + |
| 834 | + assert_queries(1) do |
| 835 | + ActiveRecord::Associations::Preloader.new(records: [post], associations: :author, available_records: [bob]).call |
| 836 | + end |
| 837 | + |
| 838 | + assert_no_queries do |
| 839 | + assert_predicate post.association(:author), :loaded? |
| 840 | + assert_equal david, post.author |
| 841 | + end |
| 842 | + end |
763 | 843 | end
|
764 | 844 |
|
765 | 845 | class GeneratedMethodsTest < ActiveRecord::TestCase
|
|
0 commit comments