pinchflat/test/pinchflat/settings_test.exs
Kieran 24875eaeac
[Housekeeping] Refactor settings model (#165)
* [WIP] renamed current settings module and tables to have backup suffix

* Created new settings table, schema, and context

* Migrated from old settings module to new one

* Removed settings backup modules

* Added some tests and docs
2024-04-04 12:43:17 -07:00

63 lines
1.6 KiB
Elixir

defmodule Pinchflat.SettingsTest do
use Pinchflat.DataCase
alias Pinchflat.Settings
alias Pinchflat.Settings.Setting
# NOTE: We're treating some of these tests differently
# than in other modules because certain settings
# are always created on app boot (including in the test env),
# so we can't treat these like a clean slate.
setup do
# Ensure we have a clean slate
Settings.set(onboarding: false)
Settings.set(pro_enabled: false)
Settings.set(yt_dlp_version: nil)
:ok
end
describe "record/0" do
test "returns the only setting" do
assert %Setting{} = Settings.record()
end
end
describe "set/1" do
test "updates the setting" do
assert {:ok, true} = Settings.set(onboarding: true)
assert {:ok, true} = Settings.get(:onboarding)
end
test "returns an error if the setting key doesn't exist" do
assert {:error, :invalid_key} = Settings.set(foo: "bar")
end
test "returns an error if the setting value is invalid" do
assert {:error, %Ecto.Changeset{}} = Settings.set(onboarding: "bar")
end
end
describe "get/1" do
test "returns the setting value" do
assert {:ok, false} = Settings.get(:onboarding)
end
test "returns an error if the setting key doesn't exist" do
assert {:error, :invalid_key} = Settings.get(:foo)
end
end
describe "get!/1" do
test "returns the setting value" do
assert Settings.get!(:onboarding) == false
end
test "raises an error if the setting key doesn't exist" do
assert_raise RuntimeError, "Setting `foo` not found", fn ->
Settings.get!(:foo)
end
end
end
end