Skip to content

Commit

Permalink
Add tests for advertised ransack parameters and fix regressions
Browse files Browse the repository at this point in the history
Our documentation advertises that users, devices, and sensors are searchable
with ransack - this commit adds some tests that assert (in a very basic manner) that this will succeed, and fixes a few cases where we weren't actually supporting the behaviour we advertise.
  • Loading branch information
timcowlishaw committed Sep 26, 2023
1 parent 958485b commit 7c6fed8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/controllers/v0/sensors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def show
end

def index
@sensors = Sensor.includes(:measurement, :tag_sensors).all
@q = Sensor.includes(:measurement, :tag_sensors).ransack(params[:q])
@q.sorts = 'id asc' if @q.sorts.empty?
@sensors = @q.result(distinct: true)
@sensors = paginate @sensors
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/sensor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Sensor < ActiveRecord::Base
has_ancestry
validates_presence_of :name, :description#, :unit

def self.ransackable_attributes(auth_object = nil)
["ancestry", "created_at", "description", "id", "measurement_id", "name", "unit", "updated_at", "uuid"]
end

def tags
tag_sensors.map(&:name)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class Tag < ActiveRecord::Base

extend FriendlyId
friendly_id :name

def self.ransackable_attributes(auth_object = nil)
["created_at", "description", "id", "name", "updated_at", "uuid"]
end
end
2 changes: 2 additions & 0 deletions spec/controllers/v0/devices_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

RSpec.describe V0::DevicesController do
skip { is_expected.to permit(:name,:description,:mac_address,:latitude,:longitude,:elevation,:exposure,:meta,:kit_id,:user_tags).for(:create) }


end
70 changes: 70 additions & 0 deletions spec/requests/v0/devices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,76 @@
expect(response.status).to eq(200)
end

it "allows searching by owner id" do
json = api_get "devices?q[owner_id_eq]=1"
expect(response.status).to eq(200)
end

it "allows searching by tag name" do
json = api_get "devices?q[tag_name_in]=Barcelona"
expect(response.status).to eq(200)
end

it "allows searching by tag names" do
json = api_get "devices?with_tags=Barcelona%7CAmsterdam"
expect(response.status).to eq(200)
end

it "allows searching by name" do
json = api_get "devices?q[name_eq]=Name"
expect(response.status).to eq(200)
end

it "allows searching by description" do
json = api_get "devices?q[description_eq]=Desc"
expect(response.status).to eq(200)
end

it "allows searching by mac address" do
json = api_get "devices?q[mac_address_eq]=00:00:00:00:00:00"
expect(response.status).to eq(200)
end

it "allows searching by created_at" do
json = api_get "devices?q[created_at_lt]=2023-09-26"
expect(response.status).to eq(200)
end

it "allows searching by updated_at" do
json = api_get "devices?q[updated_at_lt]=2023-09-26"
expect(response.status).to eq(200)
end

it "allows searching by geohash" do
json = api_get "devices?q[geohash_eq]=geohash"
expect(response.status).to eq(200)
end

it "allows searching by last_recorded_at" do
json = api_get "devices?q[last_recorded_at_lt]=2023-09-26"
expect(response.status).to eq(200)
end

it "allows searching by uuid" do
json = api_get "devices?q[uuid_eq]=uuid"
expect(response.status).to eq(200)
end

it "allows searching by state" do
json = api_get "devices?q[state_eq]=state"
expect(response.status).to eq(200)
end

it "allows searching by postprocessing_id" do
json = api_get "devices?q[postprocessing_id_eq]=postprocessing_id"
expect(response.status).to eq(200)
end

it "allows searching by hardware_info" do
json = api_get "devices?q[hardware_info_eq]=hardware_info"
expect(response.status).to eq(200)
end

end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/requests/v0/sensors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
expect(j[0]['name']).to eq('testing sensor')
expect(response.status).to eq(200)
end

describe "smoke tests for ransack" do
it "allows searching by id" do
json = api_get "sensors?q[id_lt]=100"
expect(response.status).to eq(200)
end
end
end

describe "POST /sensors" do
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/v0/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
expect(j['email']).to eq(user.email)
end

describe "smoke tests for ransack" do
it "allows searching by first name" do
json = api_get "users?q[first_name_eq]=Tim"
expect(response.status).to eq(200)
end

it "allows searching by username" do
json = api_get "users?q[username_eq]=mistertim"
expect(response.status).to eq(200)
end
end

end

# index
Expand Down

0 comments on commit 7c6fed8

Please sign in to comment.