Skip to content

Commit

Permalink
Allow wildcard prefix and suffix searching for device id (#1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
lawik authored Sep 5, 2024
1 parent 232a55c commit b5cc843
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ defmodule NervesHub.Devices do
where(query, [d], d.updates_enabled == false)

{:device_id, value} ->
where(query, [d], ilike(d.identifier, ^"#{value}%"))
where(query, [d], ilike(d.identifier, ^"%#{value}%"))

{:tag, value} ->
case NervesHub.Types.Tag.cast(value) do
Expand Down
53 changes: 50 additions & 3 deletions test/nerves_hub_web/live/devices/index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,63 @@ defmodule NervesHubWeb.Live.Devices.IndexTest do
end

describe "handle_event" do
test "filters devices by field", %{conn: conn, fixture: fixture} do
test "filters devices by exact identifier", %{conn: conn, fixture: fixture} do
%{device: device, firmware: firmware, org: org, product: product} = fixture

device2 = Fixtures.device_fixture(org, product, firmware)

{:ok, view, html} = live(conn, device_index_path(fixture))
assert html =~ device.identifier
assert html =~ device2.identifier

refute render_change(view, "update-filters", %{"device_id" => device.identifier}) =~
device2.identifier
change = render_change(view, "update-filters", %{"device_id" => device.identifier})
assert change =~ device.identifier
refute change =~ device2.identifier
end

test "filters devices by wrong identifier", %{conn: conn, fixture: fixture} do
%{device: device, firmware: firmware, org: org, product: product} = fixture

device2 = Fixtures.device_fixture(org, product, firmware)

{:ok, view, html} = live(conn, device_index_path(fixture))
assert html =~ device.identifier
assert html =~ device2.identifier

change = render_change(view, "update-filters", %{"device_id" => "foo"})
refute change =~ device.identifier
refute change =~ device2.identifier
end

test "filters devices by prefix identifier", %{conn: conn, fixture: fixture} do
%{device: device, firmware: firmware, org: org, product: product} = fixture

{:ok, view, html} = live(conn, device_index_path(fixture))
assert html =~ device.identifier

assert render_change(view, "update-filters", %{"device_id" => "device-"}) =~
device.identifier
end

test "filters devices by suffix identifier", %{conn: conn, fixture: fixture} do
%{device: device, firmware: firmware, org: org, product: product} = fixture

{:ok, view, html} = live(conn, device_index_path(fixture))
assert html =~ device.identifier
"device-" <> tail = device.identifier

assert render_change(view, "update-filters", %{"device_id" => tail}) =~
device.identifier
end

test "filters devices by middle identifier", %{conn: conn, fixture: fixture} do
%{device: device, firmware: firmware, org: org, product: product} = fixture

{:ok, view, html} = live(conn, device_index_path(fixture))
assert html =~ device.identifier

assert render_change(view, "update-filters", %{"device_id" => "ice-"}) =~
device.identifier
end

test "filters devices by tag", %{conn: conn, fixture: fixture} do
Expand Down

0 comments on commit b5cc843

Please sign in to comment.