- Rename beacons.height_m to height_ft; migration converts existing values (m * 3.28084). Form/list/show labels updated. - Beacon changeset now fills lat/lon from the grid when they're blank (previously only the reverse worked). Lat/lon and grid inputs are no longer marked required in the form — enter one side and the other populates on blur via phx-change.
145 lines
4.3 KiB
Elixir
145 lines
4.3 KiB
Elixir
defmodule Microwaveprop.BeaconsTest do
|
|
use Microwaveprop.DataCase
|
|
|
|
import Microwaveprop.AccountsFixtures
|
|
import Microwaveprop.BeaconsFixtures
|
|
|
|
alias Microwaveprop.Beacons
|
|
alias Microwaveprop.Beacons.Beacon
|
|
|
|
@invalid_attrs %{
|
|
frequency_mhz: nil,
|
|
callsign: nil,
|
|
grid: nil,
|
|
lat: nil,
|
|
lon: nil,
|
|
power_mw: nil,
|
|
height_ft: nil
|
|
}
|
|
|
|
describe "list_beacons/0" do
|
|
test "returns all beacons regardless of creator" do
|
|
u1 = user_fixture()
|
|
u2 = user_fixture()
|
|
b1 = beacon_fixture(u1)
|
|
b2 = beacon_fixture(u2, callsign: "W5TX")
|
|
assert [^b1, ^b2] = Enum.sort_by(Beacons.list_beacons(), & &1.callsign)
|
|
end
|
|
end
|
|
|
|
describe "get_beacon!/1" do
|
|
test "returns the beacon with given id" do
|
|
user = user_fixture()
|
|
beacon = beacon_fixture(user)
|
|
assert Beacons.get_beacon!(beacon.id).id == beacon.id
|
|
end
|
|
end
|
|
|
|
describe "create_beacon/2" do
|
|
test "with valid data creates a beacon and records the creator" do
|
|
user = user_fixture()
|
|
attrs = valid_beacon_attrs()
|
|
|
|
assert {:ok, %Beacon{} = beacon} = Beacons.create_beacon(user, attrs)
|
|
assert beacon.frequency_mhz == attrs.frequency_mhz
|
|
assert beacon.callsign == "W5HN"
|
|
assert beacon.lat == attrs.lat
|
|
assert beacon.lon == attrs.lon
|
|
assert beacon.power_mw == attrs.power_mw
|
|
assert beacon.height_ft == attrs.height_ft
|
|
assert beacon.user_id == user.id
|
|
end
|
|
|
|
test "derives grid from lat/lon when grid is omitted" do
|
|
user = user_fixture()
|
|
# DFW is EM12
|
|
{:ok, beacon} = Beacons.create_beacon(user, valid_beacon_attrs(lat: 32.897, lon: -97.038))
|
|
assert String.starts_with?(beacon.grid, "EM12")
|
|
end
|
|
|
|
test "derives lat/lon from grid when lat/lon are omitted" do
|
|
user = user_fixture()
|
|
|
|
attrs =
|
|
valid_beacon_attrs(grid: "EM12kp")
|
|
|> Map.drop([:lat, :lon])
|
|
|
|
assert {:ok, beacon} = Beacons.create_beacon(user, attrs)
|
|
assert beacon.grid == "EM12kp"
|
|
assert_in_delta beacon.lat, 32.6458, 0.01
|
|
assert_in_delta beacon.lon, -97.125, 0.01
|
|
end
|
|
|
|
test "keeps an explicitly provided valid grid" do
|
|
user = user_fixture()
|
|
{:ok, beacon} = Beacons.create_beacon(user, valid_beacon_attrs(grid: "EM13"))
|
|
assert beacon.grid == "EM13"
|
|
end
|
|
|
|
test "rejects an invalid grid" do
|
|
user = user_fixture()
|
|
|
|
assert {:error, changeset} =
|
|
Beacons.create_beacon(user, valid_beacon_attrs(grid: "ZZ99"))
|
|
|
|
assert "is not a valid Maidenhead grid" in errors_on(changeset).grid
|
|
end
|
|
|
|
test "upcases the callsign" do
|
|
user = user_fixture()
|
|
{:ok, beacon} = Beacons.create_beacon(user, valid_beacon_attrs(callsign: "w5hn"))
|
|
assert beacon.callsign == "W5HN"
|
|
end
|
|
|
|
test "rejects non-positive frequency" do
|
|
user = user_fixture()
|
|
|
|
assert {:error, changeset} =
|
|
Beacons.create_beacon(user, valid_beacon_attrs(frequency_mhz: 0))
|
|
|
|
assert "must be greater than 0" in errors_on(changeset).frequency_mhz
|
|
end
|
|
|
|
test "with invalid data returns error changeset" do
|
|
user = user_fixture()
|
|
assert {:error, %Ecto.Changeset{}} = Beacons.create_beacon(user, @invalid_attrs)
|
|
end
|
|
end
|
|
|
|
describe "update_beacon/2" do
|
|
test "with valid data updates the beacon" do
|
|
user = user_fixture()
|
|
beacon = beacon_fixture(user)
|
|
|
|
update_attrs = %{frequency_mhz: 24_192.1, power_mw: 5.0}
|
|
|
|
assert {:ok, %Beacon{} = beacon} = Beacons.update_beacon(beacon, update_attrs)
|
|
assert beacon.frequency_mhz == 24_192.1
|
|
assert beacon.power_mw == 5.0
|
|
end
|
|
|
|
test "with invalid data returns error changeset" do
|
|
user = user_fixture()
|
|
beacon = beacon_fixture(user)
|
|
assert {:error, %Ecto.Changeset{}} = Beacons.update_beacon(beacon, @invalid_attrs)
|
|
assert Beacons.get_beacon!(beacon.id).callsign == beacon.callsign
|
|
end
|
|
end
|
|
|
|
describe "delete_beacon/1" do
|
|
test "deletes the beacon" do
|
|
user = user_fixture()
|
|
beacon = beacon_fixture(user)
|
|
assert {:ok, %Beacon{}} = Beacons.delete_beacon(beacon)
|
|
assert_raise Ecto.NoResultsError, fn -> Beacons.get_beacon!(beacon.id) end
|
|
end
|
|
end
|
|
|
|
describe "change_beacon/1" do
|
|
test "returns a beacon changeset" do
|
|
user = user_fixture()
|
|
beacon = beacon_fixture(user)
|
|
assert %Ecto.Changeset{} = Beacons.change_beacon(beacon)
|
|
end
|
|
end
|
|
end
|