|
117 | 117 | device.send_upgrade_request |
118 | 118 | end |
119 | 119 |
|
| 120 | + it "builds a DB-only subquery for excess sensor readings" do |
| 121 | + relation = device.excess_sensor_readings |
| 122 | + |
| 123 | + expect(relation.to_sql).to include("OFFSET #{Device::DEFAULT_MAX_SENSOR_READINGS}") |
| 124 | + expect(relation.to_sql).to include("\"sensor_readings\".\"id\" IN") |
| 125 | + end |
| 126 | + |
| 127 | + it "returns limited sensor readings in reverse chronological order" do |
| 128 | + const_reassign(Device, :DEFAULT_MAX_SENSOR_READINGS, 2) do |
| 129 | + oldest = FactoryBot.create(:sensor_reading, |
| 130 | + device: device, |
| 131 | + created_at: 2.seconds.ago, |
| 132 | + updated_at: 2.seconds.ago) |
| 133 | + tied_time = 1.second.ago |
| 134 | + older_tied = FactoryBot.create(:sensor_reading, |
| 135 | + device: device, |
| 136 | + created_at: tied_time, |
| 137 | + updated_at: tied_time) |
| 138 | + newer_tied = FactoryBot.create(:sensor_reading, |
| 139 | + device: device, |
| 140 | + created_at: tied_time, |
| 141 | + updated_at: tied_time) |
| 142 | + |
| 143 | + expect(device.limited_sensor_readings_list.pluck(:id)) |
| 144 | + .to eq([newer_tied.id, older_tied.id]) |
| 145 | + expect(device.limited_sensor_readings_list.pluck(:id)) |
| 146 | + .not_to include(oldest.id) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + it "trims older sensor readings beyond the device limit" do |
| 151 | + const_reassign(Device, :DEFAULT_MAX_SENSOR_READINGS, 2) do |
| 152 | + oldest = FactoryBot.create(:sensor_reading, |
| 153 | + device: device, |
| 154 | + created_at: 3.seconds.ago, |
| 155 | + updated_at: 3.seconds.ago) |
| 156 | + middle = FactoryBot.create(:sensor_reading, |
| 157 | + device: device, |
| 158 | + created_at: 2.seconds.ago, |
| 159 | + updated_at: 2.seconds.ago) |
| 160 | + newest = FactoryBot.create(:sensor_reading, |
| 161 | + device: device, |
| 162 | + created_at: 1.second.ago, |
| 163 | + updated_at: 1.second.ago) |
| 164 | + |
| 165 | + device.trim_excess_sensor_readings |
| 166 | + |
| 167 | + expect(device.sensor_readings.pluck(:id)).to match_array([middle.id, newest.id]) |
| 168 | + expect(SensorReading.exists?(oldest.id)).to be(false) |
| 169 | + end |
| 170 | + end |
| 171 | + |
120 | 172 | it "reports unknown location in feedback payload when coordinates are missing" do |
121 | 173 | expect(Faraday).to receive(:post) do |_url, payload, _headers| |
122 | 174 | text = JSON.parse(payload)["text"] |
|
0 commit comments