Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
treagod committed Jan 27, 2025
1 parent 0cd9889 commit 1c3f350
Showing 1 changed file with 88 additions and 15 deletions.
103 changes: 88 additions & 15 deletions spec/marten/db/query/set_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3189,20 +3189,34 @@ describe Marten::DB::Query::Set do
qset[1].get_related_object_variable(:user).should eq user_2
end

it "allows to prefetch a single reverse many-to-one relation with a custom query" do
it "allows to prefetch a single one-to-one relation with a custom query" do
user_1 = TestUser.create!(username: "jd1", email: "[email protected]", first_name: "John", last_name: "Doe")
user_2 = TestUser.create!(username: "jd2", email: "[email protected]", first_name: "John", last_name: "Doe")

user_profile_1 = TestUserProfile.create!(user: user_1, bio: "Test 1")
user_profile_2 = TestUserProfile.create!(user: user_2, bio: "Test 2")

qset = Marten::DB::Query::Set(TestUserProfile)
.new.order(:bio).prefetch(:user, TestUser.filter(username: "jd1"))

qset.to_a.should eq [user_profile_1, user_profile_2]
qset[0].get_related_object_variable(:user).should eq user_1
qset[1].get_related_object_variable(:user).should be_nil
end

it "allows to prefetch a single many-to-one relation with a custom query" do
user_1 = TestUser.create!(username: "jd1", email: "[email protected]", first_name: "John", last_name: "Doe")
user_2 = TestUser.create!(username: "jd2", email: "[email protected]", first_name: "John", last_name: "Doe")

post_1 = Post.create!(author: user_1, title: "Post 1")
post_2 = Post.create!(author: user_2, title: "Post 2")
post_3 = Post.create!(author: user_1, title: "Post 3")
post_4 = Post.create!(author: user_2, title: "Post 4")

qset = Marten::DB::Query::Set(TestUser).new.order(:username).prefetch(:posts, Post.order("-pk"))
qset = Marten::DB::Query::Set(Post)
.new.order(:title).prefetch(:author, TestUser.filter(username: "jd1"))

qset.to_a.should eq [user_1, user_2]
qset[0].posts.result_cache.should eq [post_3, post_1]
qset[1].posts.result_cache.should eq [post_4, post_2]
qset.to_a.should eq [post_1, post_2]
qset[0].get_related_object_variable(:author).should eq user_1
qset[1].get_related_object_variable(:author).should be_nil
end

it "allows to prefetch a single many-to-many relation with a custom query" do
Expand All @@ -3228,15 +3242,74 @@ describe Marten::DB::Query::Set do
qset[0].tags.result_cache.try(&.sort_by(&.pk!)).should eq [tag_1]
qset[1].tags.result_cache.try(&.sort_by(&.pk!)).should eq [tag_2]
qset[2].tags.result_cache.not_nil!.empty?.should be_true
end

# The other way around
qset = Marten::DB::Query::Set(Tag).new.order(:pk).prefetch(:test_users, TestUser.filter(first_name: "John"))
qset[0].test_users.result_cache.try(&.sort_by(&.pk!)).should eq [user_1]
qset[1].test_users.result_cache.not_nil!.empty?.should be_true
qset[2].test_users.result_cache.try(&.sort_by(&.pk!)).should eq [user_1]
qset[3].test_users.result_cache.try(&.sort_by(&.pk!)).should eq [user_3]
qset[4].test_users.result_cache.try(&.sort_by(&.pk!)).should eq [user_3]
qset[5].test_users.result_cache.not_nil!.empty?.should be_true
it "allows to prefetch a single reverse one-to-one relation with a custom query" do
user_1 = TestUser.create!(username: "jd1", email: "[email protected]", first_name: "John", last_name: "Doe")
user_2 = TestUser.create!(username: "jd2", email: "[email protected]", first_name: "John", last_name: "Doe")

user_profile_1 = TestUserProfile.create!(user: user_1, bio: "Test 1")
user_profile_2 = TestUserProfile.create!(user: user_2, bio: "Test 2")

qset = Marten::DB::Query::Set(TestUser)
.new.order(:username).prefetch(:profile, TestUserProfile.filter(bio: "Test 1"))

qset.to_a.should eq [user_1, user_2]
qset[0].get_reverse_related_object_variable(:profile).should eq user_profile_1
qset[1].get_reverse_related_object_variable(:profile).should be_nil
end

it "allows to prefetch a single reverse many-to-one relation with a custom query" do
user_1 = TestUser.create!(username: "jd1", email: "[email protected]", first_name: "John", last_name: "Doe")
user_2 = TestUser.create!(username: "jd2", email: "[email protected]", first_name: "John", last_name: "Doe")

post_1 = Post.create!(author: user_1, title: "Post 1")
post_2 = Post.create!(author: user_2, title: "Post 2")
post_3 = Post.create!(author: user_1, title: "Post 3")
post_4 = Post.create!(author: user_2, title: "Post 4")

qset = Marten::DB::Query::Set(TestUser).new.order(:username).prefetch(:posts, Post.order("-pk"))

qset.to_a.should eq [user_1, user_2]
qset[0].posts.result_cache.should eq [post_3, post_1]
qset[1].posts.result_cache.should eq [post_4, post_2]
end

it "can prefetch many relations with a custom query" do
tag_1 = Tag.create!(name: "ruby", is_active: true)
tag_2 = Tag.create!(name: "crystal", is_active: true)
tag_3 = Tag.create!(name: "coding", is_active: true)
tag_4 = Tag.create!(name: "programming", is_active: true)
tag_5 = Tag.create!(name: "typing", is_active: true)
Tag.create!(name: "debugging", is_active: true)

user_1 = TestUser.create!(username: "jd1", email: "[email protected]", first_name: "John", last_name: "Doe")
user_2 = TestUser.create!(username: "jd2", email: "[email protected]", first_name: "John", last_name: "Doe")
user_3 = TestUser.create!(username: "jd3", email: "[email protected]", first_name: "John", last_name: "Doe")

user_1.tags.add(tag_1, tag_3)
user_2.tags.add(tag_2, tag_3)
user_3.tags.add(tag_4, tag_5)

post_1 = Post.create!(author: user_1, title: "Post 1")
post_2 = Post.create!(author: user_2, title: "Post 2")
post_3 = Post.create!(author: user_1, title: "Post 3")
post_4 = Post.create!(author: user_2, title: "Post 4")

qset = Marten::DB::Query::Set(TestUser)
.new.order(:username)
.prefetch(:tags, Tag.order("pk"))
.prefetch(:posts, Post.order("pk"))

qset.to_a.should eq [user_1, user_2, user_3]

qset[0].tags.result_cache.should eq [tag_1, tag_3]
qset[1].tags.result_cache.should eq [tag_2, tag_3]
qset[2].tags.result_cache.should eq [tag_4, tag_5]

qset[0].posts.result_cache.should eq [post_1, post_3]
qset[1].posts.result_cache.should eq [post_2, post_4]
qset[2].posts.result_cache.try(&.empty?).should be_true
end

it "raises if an incompatible query set is used for the custom query" do
Expand Down

0 comments on commit 1c3f350

Please sign in to comment.