defmodule Microwaveprop.Commercial.LinkTest do
  use Microwaveprop.DataCase, async: true

  alias Microwaveprop.Commercial.Link

  @valid_attrs %{
    label: "test-link-abc",
    host: "10.250.1.26",
    community: "public",
    radio_type: "af11x",
    endpoint_a: %{lat: 33.246171, lon: -96.426516, alt_m: 205.4},
    endpoint_b: %{lat: 33.187294, lon: -96.448128, alt_m: 191.0},
    weather_station: "KTKI",
    enabled: true
  }

  describe "changeset/2" do
    test "valid attributes" do
      changeset = Link.changeset(%Link{}, @valid_attrs)
      assert changeset.valid?
    end

    test "requires label, host, community" do
      changeset = Link.changeset(%Link{}, %{})

      assert %{
               label: ["can't be blank"],
               host: ["can't be blank"],
               community: ["can't be blank"]
             } = errors_on(changeset)
    end

    test "validates radio_type inclusion" do
      changeset = Link.changeset(%Link{}, %{@valid_attrs | radio_type: "unknown"})
      assert %{radio_type: ["is invalid"]} = errors_on(changeset)
    end

    test "defaults enabled to true" do
      changeset = Link.changeset(%Link{}, @valid_attrs)
      assert Ecto.Changeset.get_field(changeset, :enabled) == true
    end

    test "persists to the database" do
      changeset = Link.changeset(%Link{}, @valid_attrs)
      assert {:ok, link} = Repo.insert(changeset)
      assert link.label == "test-link-abc"
      assert link.host == "10.250.1.26"
      assert link.radio_type == "af11x"
      assert link.endpoint_a[:lat] == 33.246171
    end

    test "enforces unique label" do
      assert {:ok, _} = Repo.insert(Link.changeset(%Link{}, @valid_attrs))
      assert {:error, changeset} = Repo.insert(Link.changeset(%Link{}, @valid_attrs))
      assert %{label: ["has already been taken"]} = errors_on(changeset)
    end
  end
end
