Skip to content

Commit dbc5f31

Browse files
committed
Iterate specs for error cases and association options
1 parent ad3baf7 commit dbc5f31

File tree

1 file changed

+83
-14
lines changed

1 file changed

+83
-14
lines changed

spec/closure_tree/has_closure_tree_roots_spec.rb

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@
105105
expect(empty_post.comments_including_tree).to eq([])
106106
end
107107
end
108+
109+
it "works if eager load association map is not given" do
110+
expect do
111+
roots = post_reloaded.comments_including_tree
112+
expect(roots.size).to eq 2
113+
expect(roots[0].body).to eq "Top comment 1"
114+
expect(roots[0].children[1].children[0].body).to eq "Reply 1-2-1"
115+
end.to_not exceed_query_limit(2)
116+
end
108117
end
109118

110119
context "when comment is destroyed" do
@@ -132,23 +141,83 @@
132141
end
133142
end
134143

135-
context "with nested comment creation" do
136-
it "properly builds the hierarchy" do
137-
# Create a new root comment with nested children
138-
new_comment = Comment.new(body: "New root", post: post)
139-
reply1 = Comment.new(body: "New reply 1", post: post)
140-
reply2 = Comment.new(body: "New reply 2", post: post)
141-
142-
new_comment.children << reply1
143-
new_comment.children << reply2
144+
context "with no tree root" do
145+
let(:empty_post) { Post.create!(title: "Empty Post") }
146+
147+
it "should return []" do
148+
expect(empty_post.comments_including_tree).to eq([])
149+
end
150+
end
151+
152+
context "with explicit class_name and foreign_key" do
153+
before do
154+
# Create a model similar to Grouping in the models.rb file
155+
class ForumPost < ApplicationRecord
156+
self.table_name = "posts"
157+
has_closure_tree_roots :thread_comments, class_name: 'Comment', foreign_key: 'post_id'
158+
end
144159

145-
new_comment.save!
160+
# Create the post and comments - reusing the same ones from above for simplicity
161+
@post_collection = ForumPost.find(post.id)
162+
@post_collection_reloaded = @post_collection.class.find(@post_collection.id)
163+
end
164+
165+
after do
166+
# Clean up our dynamically created class after the test
167+
Object.send(:remove_const, :ForumPost) if Object.const_defined?(:ForumPost)
168+
end
169+
170+
it "should still work" do
171+
roots = @post_collection_reloaded.thread_comments_including_tree
172+
expect(roots.size).to eq 2
173+
expect(roots[0].body).to eq "Top comment 1"
174+
expect(roots[0].children[1].body).to eq "Reply 1-2"
175+
end
176+
end
177+
178+
context "with bad class_name" do
179+
before do
180+
# Create a model with an invalid class_name
181+
class BadClassPost < ApplicationRecord
182+
self.table_name = "posts"
183+
has_closure_tree_roots :invalid_comments, class_name: 'NonExistentComment'
184+
end
146185

147-
roots = post_reloaded.comments_including_tree(true)
148-
new_root = roots.find { |r| r.body == "New root" }
186+
@bad_class_post = BadClassPost.find(post.id)
187+
@bad_class_post_reloaded = @bad_class_post.class.find(@bad_class_post.id)
188+
end
189+
190+
after do
191+
Object.send(:remove_const, :BadClassPost) if Object.const_defined?(:BadClassPost)
192+
end
193+
194+
it "should error" do
195+
expect do
196+
@bad_class_post_reloaded.invalid_comments_including_tree
197+
end.to raise_error(NameError)
198+
end
199+
end
200+
201+
context "with bad foreign_key" do
202+
before do
203+
# Create a model with an invalid foreign_key
204+
class BadKeyPost < ApplicationRecord
205+
self.table_name = "posts"
206+
has_closure_tree_roots :broken_comments, class_name: 'Comment', foreign_key: 'nonexistent_id'
207+
end
149208

150-
expect(new_root.children.size).to eq 2
151-
expect(new_root.children.map(&:body)).to include("New reply 1", "New reply 2")
209+
@bad_key_post = BadKeyPost.find(post.id)
210+
@bad_key_post_reloaded = @bad_key_post.class.find(@bad_key_post.id)
211+
end
212+
213+
after do
214+
Object.send(:remove_const, :BadKeyPost) if Object.const_defined?(:BadKeyPost)
215+
end
216+
217+
it "should error" do
218+
expect do
219+
@bad_key_post_reloaded.broken_comments_including_tree
220+
end.to raise_error(ActiveRecord::StatementInvalid)
152221
end
153222
end
154223
end

0 commit comments

Comments
 (0)