|
55 | 55 | end |
56 | 56 |
|
57 | 57 | describe ".all" do |
58 | | - it "chains through active query" do |
59 | | - expect(base_class).to receive(:find).with(:all, {}).and_return([]) |
| 58 | + it "comes from QueryRelation" do |
| 59 | + expect(base_class).to receive(:search).with(:all, {}).and_return([]) |
60 | 60 | expect(base_class.all.to_a).to eq([]) |
61 | 61 | end |
62 | 62 |
|
63 | | - it "supports where (as an example)" do |
64 | | - expect(base_class).to receive(:find).with(:all, {:conditions => {:id => 5}}).and_return([]) |
| 63 | + it "supports where from QueryRelation (as an example)" do |
| 64 | + expect(base_class).to receive(:search).with(:all, {:where => {:id => 5}}).and_return([]) |
65 | 65 | expect(base_class.all.where(:id => 5).to_a).to eq([]) |
66 | 66 | end |
67 | 67 | end |
68 | 68 |
|
69 | 69 | describe ".first" do |
70 | | - it "chains through active query" do |
71 | | - expect(base_class).to receive(:find).with(:first).and_return(nil) |
| 70 | + it "comes from QueryRelation" do |
| 71 | + expect(base_class).to receive(:search).with(:first, {}).and_return(nil) |
72 | 72 | expect(base_class.first).to eq(nil) |
73 | 73 | end |
74 | 74 | end |
75 | 75 |
|
76 | 76 | describe ".last" do |
77 | | - it "chains through active query" do |
78 | | - expect(base_class).to receive(:find).with(:last).and_return(nil) |
| 77 | + it "comes from QueryRelation" do |
| 78 | + expect(base_class).to receive(:search).with(:last, {}).and_return(nil) |
79 | 79 | expect(base_class.last).to eq(nil) |
80 | 80 | end |
81 | 81 | end |
82 | 82 |
|
83 | 83 | describe ".count" do |
84 | | - it "chains through active query" do |
85 | | - expect(base_class).to receive(:find).with(:all, {}).and_return([]) |
| 84 | + it "comes from QueryRelation" do |
| 85 | + expect(base_class).to receive(:search).with(:all, {}).and_return([]) |
86 | 86 | expect(base_class.count).to eq(0) |
87 | 87 | end |
88 | 88 | end |
| 89 | + |
| 90 | + describe ".find (deprecated)" do |
| 91 | + around do |example| |
| 92 | + Vmdb::Deprecation.silence do |
| 93 | + example.run |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe "find(:all)" do |
| 98 | + it "chains through QueryRelation" do |
| 99 | + expect(base_class).to receive(:search).with(:all, {}).and_return([]) |
| 100 | + expect(base_class.find(:all).to_a).to eq([]) |
| 101 | + end |
| 102 | + |
| 103 | + it "supports :conditions legacy option (as an example)" do |
| 104 | + expect(base_class).to receive(:search).with(:all, {:where => {:id => 5}}).and_return([]) |
| 105 | + expect(base_class.find(:all, :conditions => {:id => 5}).to_a).to eq([]) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + describe "find(:first)" do |
| 110 | + it "chains through QueryRelation" do |
| 111 | + expect(base_class).to receive(:search).with(:first, {}).and_return(nil) |
| 112 | + expect(base_class.find(:first)).to eq(nil) |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + describe ".find(:last)" do |
| 117 | + it "chains through QueryRelation" do |
| 118 | + expect(base_class).to receive(:search).with(:last, {}).and_return(nil) |
| 119 | + expect(base_class.find(:last)).to eq(nil) |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe ".find" do |
| 125 | + it "finds a record by id" do |
| 126 | + expected = Object.new |
| 127 | + expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(expected) |
| 128 | + expect(base_class.find(5)).to eq(expected) |
| 129 | + end |
| 130 | + |
| 131 | + it "raises when not found" do |
| 132 | + expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(nil) |
| 133 | + expect { base_class.find(5) }.to raise_error(ActiveRecord::RecordNotFound) |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + describe ".lookup_by_id" do |
| 138 | + it "finds a record by id" do |
| 139 | + expected = Object.new |
| 140 | + expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(expected) |
| 141 | + expect(base_class.lookup_by_id(5)).to eq(expected) |
| 142 | + end |
| 143 | + |
| 144 | + it "returns nil when not found" do |
| 145 | + expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(nil) |
| 146 | + expect(base_class.lookup_by_id(5)).to be_nil |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + describe ".find_by_id (deprecated)" do |
| 151 | + around do |example| |
| 152 | + Vmdb::Deprecation.silence do |
| 153 | + example.run |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + it "finds a record by id" do |
| 158 | + expected = Object.new |
| 159 | + expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(expected) |
| 160 | + expect(base_class.find_by_id(5)).to eq(expected) |
| 161 | + end |
| 162 | + |
| 163 | + it "returns nil when not found" do |
| 164 | + expect(base_class).to receive(:search).with(:first, {:where => {:id => 5}}).and_return(nil) |
| 165 | + expect(base_class.find_by_id(5)).to be_nil |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + describe ".find_all_by_id (deprecated)" do |
| 170 | + around do |example| |
| 171 | + Vmdb::Deprecation.silence do |
| 172 | + example.run |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + it "finds record by ids" do |
| 177 | + expected = [Object.new, Object.new] |
| 178 | + expect(base_class).to receive(:search).with(:all, {:where => {:id => [5, 6]}}).and_return(expected) |
| 179 | + expect(base_class.find_all_by_id(5, 6)).to eq(expected) |
| 180 | + end |
| 181 | + |
| 182 | + it "returns empty Array when none found" do |
| 183 | + expect(base_class).to receive(:search).with(:all, {:where => {:id => [5, 6]}}).and_return([]) |
| 184 | + expect(base_class.find_all_by_id(5, 6)).to eq([]) |
| 185 | + end |
| 186 | + end |
89 | 187 | end |
0 commit comments