|
9 | 9 | let!(:user4) { User.create!(email: "[email protected]", group_id: group.id) } |
10 | 10 | let!(:user5) { User.create!(email: "[email protected]", group_id: group.id) } |
11 | 11 | let!(:user6) { User.create!(email: "[email protected]", group_id: group.id) } |
| 12 | + let!(:group_reloaded) { group.class.find(group.id) } # Ensures were starting fresh. |
12 | 13 |
|
13 | 14 | before do |
14 | 15 | # The tree (contract types in parens) |
|
37 | 38 | context "with basic config" do |
38 | 39 | let!(:group) { Group.create!(name: "TheGroup") } |
39 | 40 |
|
40 | | - before do |
41 | | - group = Group.first # Ensure we're starting fresh |
42 | | - end |
43 | | - |
44 | 41 | it "loads all nodes and associations in a constant number of queries" do |
45 | 42 | expect do |
46 | | - root = group.root_user_including_tree(contracts: :contract_type) |
| 43 | + root = group_reloaded.root_user_including_tree(contracts: :contract_type) |
47 | 44 | expect(root.children[0].email).to eq "[email protected]" |
48 | 45 | expect(root.children[0].parent.children[1].email).to eq "[email protected]" |
49 | 46 | expect(root.children[1].contracts.map(&:contract_type).map(&:name)).to eq %w(Type1 Type2) |
|
53 | 50 | end.to_not exceed_query_limit(4) # Without this feature, this is 15, and scales with number of nodes. |
54 | 51 | end |
55 | 52 |
|
| 53 | + it "eager loads inverse association to group" do |
| 54 | + expect do |
| 55 | + root = group_reloaded.root_user_including_tree |
| 56 | + expect(root.group).to eq group |
| 57 | + expect(root.children[0].group).to eq group |
| 58 | + end.to_not exceed_query_limit(2) |
| 59 | + end |
| 60 | + |
56 | 61 | it "works if eager load association map is not given" do |
57 | 62 | expect do |
58 | | - root = group.root_user_including_tree |
| 63 | + root = group_reloaded.root_user_including_tree |
59 | 64 | expect(root.children[0].email).to eq "[email protected]" |
60 | 65 | expect(root.children[0].parent.children[1].children[0].email).to eq "[email protected]" |
61 | 66 | end.to_not exceed_query_limit(2) |
|
74 | 79 |
|
75 | 80 | it "should error" do |
76 | 81 | expect do |
77 | | - root = group.root_user_including_tree(contracts: :contract_type) |
| 82 | + root = group_reloaded.root_user_including_tree(contracts: :contract_type) |
78 | 83 | end.to raise_error(ClosureTree::MultipleRootError) |
79 | 84 | end |
80 | 85 | end |
|
84 | 89 | let(:group) { Grouping.create!(name: "TheGrouping") } |
85 | 90 |
|
86 | 91 | it "should still work" do |
87 | | - root = group.root_person_including_tree(contracts: :contract_type) |
| 92 | + root = group_reloaded.root_person_including_tree(contracts: :contract_type) |
88 | 93 | expect(root.children[0].email).to eq "[email protected]" |
89 | 94 | end |
90 | 95 | end |
|
94 | 99 |
|
95 | 100 | it "should error" do |
96 | 101 | expect do |
97 | | - root = group.root_user_including_tree(contracts: :contract_type) |
| 102 | + root = group_reloaded.root_user_including_tree(contracts: :contract_type) |
98 | 103 | end.to raise_error(NameError) |
99 | 104 | end |
100 | 105 | end |
|
104 | 109 |
|
105 | 110 | it "should error" do |
106 | 111 | expect do |
107 | | - root = group.root_user_including_tree(contracts: :contract_type) |
| 112 | + root = group_reloaded.root_user_including_tree(contracts: :contract_type) |
108 | 113 | end.to raise_error(ActiveRecord::StatementInvalid) |
109 | 114 | end |
110 | 115 | end |
|
0 commit comments