mirror of
https://github.com/ash-project/ash_sqlite.git
synced 2024-09-19 12:52:50 +12:00
chore: update tests
This commit is contained in:
parent
2229285757
commit
d8a9328921
4 changed files with 2 additions and 157 deletions
|
@ -1,89 +0,0 @@
|
|||
defprotocol SqliteMigrationDefault do
|
||||
@moduledoc """
|
||||
Allows configuring how values are translated to default values in migrations.
|
||||
|
||||
Still a work in progress, but covers most standard values aside from maps.
|
||||
"""
|
||||
@fallback_to_any true
|
||||
@doc "Returns the text (elixir code) that will be placed into a migration as the default value"
|
||||
def to_default(value)
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: Any do
|
||||
require Logger
|
||||
|
||||
def to_default(value) do
|
||||
Logger.warning("""
|
||||
You have specified a default value for a type that cannot be explicitly
|
||||
converted to an Ecto default:
|
||||
|
||||
`#{inspect(value)}`
|
||||
|
||||
The default value in the migration will be set to `nil` and you can edit
|
||||
your migration accordingly.
|
||||
|
||||
To prevent this warning, implement the `SqliteMigrationDefault` protocol
|
||||
for the appropriate Elixir type in your Ash project, or configure its
|
||||
default value in `migration_defaults` in the sqlite section. Use `\\\"nil\\\"`
|
||||
for no default.
|
||||
""")
|
||||
|
||||
"nil"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: Integer do
|
||||
def to_default(value) do
|
||||
to_string(value)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: Float do
|
||||
def to_default(value) do
|
||||
to_string(value)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: Decimal do
|
||||
def to_default(value) do
|
||||
~s["#{value}"]
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: BitString do
|
||||
def to_default(value) do
|
||||
inspect(value)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: DateTime do
|
||||
def to_default(value) do
|
||||
~s[fragment("'#{to_string(value)}'")]
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: NaiveDateTime do
|
||||
def to_default(value) do
|
||||
~s[fragment("'#{to_string(value)}'")]
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: Date do
|
||||
def to_default(value) do
|
||||
~s[fragment("'#{to_string(value)}'")]
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: Time do
|
||||
def to_default(value) do
|
||||
~s[fragment("'#{to_string(value)}'")]
|
||||
end
|
||||
end
|
||||
|
||||
defimpl SqliteMigrationDefault, for: Atom do
|
||||
def to_default(value) when value in [nil, true, false], do: inspect(value)
|
||||
|
||||
def to_default(value) do
|
||||
inspect(to_string(value))
|
||||
end
|
||||
end
|
|
@ -2301,7 +2301,7 @@ defmodule AshSqlite.MigrationGenerator do
|
|||
default
|
||||
|
||||
:error ->
|
||||
SqliteMigrationDefault.to_default(value)
|
||||
"nil"
|
||||
end
|
||||
|
||||
default ->
|
||||
|
|
3
mix.exs
3
mix.exs
|
@ -156,9 +156,6 @@ defmodule AshSqlite.MixProject do
|
|||
Types: [
|
||||
AshSqlite.Type
|
||||
],
|
||||
"Sqlite Migrations": [
|
||||
SqliteMigrationDefault
|
||||
],
|
||||
Expressions: [
|
||||
AshSqlite.Functions.Fragment,
|
||||
AshSqlite.Functions.Like
|
||||
|
|
|
@ -690,68 +690,7 @@ defmodule AshSqlite.MigrationGeneratorTest do
|
|||
end)
|
||||
end
|
||||
|
||||
test "when default value is specified that implements SqliteMigrationDefault" do
|
||||
defposts do
|
||||
attributes do
|
||||
uuid_primary_key(:id)
|
||||
attribute(:start_date, :date, default: ~D[2022-04-19])
|
||||
attribute(:start_time, :time, default: ~T[08:30:45])
|
||||
attribute(:timestamp, :utc_datetime, default: ~U[2022-02-02 08:30:30Z])
|
||||
attribute(:timestamp_naive, :naive_datetime, default: ~N[2022-02-02 08:30:30])
|
||||
attribute(:number, :integer, default: 5)
|
||||
attribute(:fraction, :float, default: 0.25)
|
||||
attribute(:decimal, :decimal, default: Decimal.new("123.4567890987654321987"))
|
||||
attribute(:name, :string, default: "Fred")
|
||||
attribute(:tag, :atom, default: :value)
|
||||
attribute(:enabled, :boolean, default: false)
|
||||
end
|
||||
end
|
||||
|
||||
defapi([Post])
|
||||
|
||||
AshSqlite.MigrationGenerator.generate(Api,
|
||||
snapshot_path: "test_snapshots_path",
|
||||
migration_path: "test_migration_path",
|
||||
quiet: true,
|
||||
format: false
|
||||
)
|
||||
|
||||
assert [file1] = Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
|
||||
|
||||
file = File.read!(file1)
|
||||
|
||||
assert file =~
|
||||
~S[add :start_date, :date, default: fragment("'2022-04-19'")]
|
||||
|
||||
assert file =~
|
||||
~S[add :start_time, :time, default: fragment("'08:30:45'")]
|
||||
|
||||
assert file =~
|
||||
~S[add :timestamp, :utc_datetime, default: fragment("'2022-02-02 08:30:30Z'")]
|
||||
|
||||
assert file =~
|
||||
~S[add :timestamp_naive, :naive_datetime, default: fragment("'2022-02-02 08:30:30'")]
|
||||
|
||||
assert file =~
|
||||
~S[add :number, :bigint, default: 5]
|
||||
|
||||
assert file =~
|
||||
~S[add :fraction, :float, default: 0.25]
|
||||
|
||||
assert file =~
|
||||
~S[add :decimal, :decimal, default: "123.4567890987654321987"]
|
||||
|
||||
assert file =~
|
||||
~S[add :name, :text, default: "Fred"]
|
||||
|
||||
assert file =~
|
||||
~S[add :tag, :text, default: "value"]
|
||||
|
||||
assert file =~
|
||||
~S[add :enabled, :boolean, default: false]
|
||||
end
|
||||
|
||||
test "when default value is specified that does not implement SqliteMigrationDefault" do
|
||||
test "when default value is specified that has no impl" do
|
||||
defposts do
|
||||
attributes do
|
||||
uuid_primary_key(:id)
|
||||
|
@ -771,8 +710,6 @@ defmodule AshSqlite.MigrationGeneratorTest do
|
|||
)
|
||||
end)
|
||||
|
||||
assert log =~ "`{\"xyz\"}`"
|
||||
|
||||
assert [file1] = Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
|
||||
|
||||
file = File.read!(file1)
|
||||
|
|
Loading…
Reference in a new issue