-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrepo_test.exs
More file actions
83 lines (65 loc) · 2.83 KB
/
repo_test.exs
File metadata and controls
83 lines (65 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
defmodule ElixirBench.RepoTest do
alias ElixirBench.Repo
alias ElixirBench.Benchmarks
use ElixirBench.DataCase
defmodule RepoMeasurement do
use Ecto.Schema
import Ecto.Changeset
schema "repo_measurements" do
field :map_float_percentiles, {:map, :float}
field :map_percentiles, :map
end
@fields [:map_float_percentiles, :map_percentiles]
def changeset(%RepoMeasurement{} = measurement, attrs) do
measurement
|> cast(attrs, @fields)
end
end
defmodule CreateMeasurementMigration do
use Ecto.Migration
def change do
create table(:repo_measurements) do
add(:map_float_percentiles, {:map, :float})
add(:map_percentiles, :map)
end
end
end
# A bug in ecto with {:map, :float} fields
# See discussions in https://github.com/elixir-ecto/ecto/issues/2637
# This test will let us know when the bug is fixed in future updates of ecto,
# after that we can remove this
test "raise when exponential notation is given to field of type {:map, :float}" do
:ok = Ecto.Migrator.up(Repo, 0, CreateMeasurementMigration, log: false)
{value, _} = Float.parse("416500")
assert ^value = 4.165e5
percentiles = %{"50" => value, "99" => value}
changeset =
RepoMeasurement.changeset(%RepoMeasurement{}, %{"map_float_percentiles" => percentiles})
assert {:ok, _} = Repo.insert(changeset)
assert_raise ArgumentError,
~r/cannot load `%{"50" => 416500, "99" => 416500}` as type {:map, :float} for field `map_float_percentiles`/,
fn ->
Repo.all(RepoMeasurement)
end
:ok = Ecto.Migrator.down(Repo, 0, CreateMeasurementMigration, log: false)
end
test "not raise when float notation is given for field of type {:map, :float}" do
:ok = Ecto.Migrator.up(Repo, 0, CreateMeasurementMigration, log: false)
percentiles = %{"50" => 4101.0, "99" => 4160.0}
changeset =
RepoMeasurement.changeset(%RepoMeasurement{}, %{"map_float_percentiles" => percentiles})
assert {:ok, measurement} = Repo.insert(changeset)
^measurement = Repo.one(RepoMeasurement)
assert %{"50" => 4101.0, "99" => 4160.0} = measurement.map_float_percentiles
:ok = Ecto.Migrator.down(Repo, 0, CreateMeasurementMigration, log: false)
end
test "not raise when exponential notation is given to field of type :map" do
:ok = Ecto.Migrator.up(Repo, 0, CreateMeasurementMigration, log: false)
percentiles = %{"50" => 4.165e5, "99" => 4.165e5}
changeset = RepoMeasurement.changeset(%RepoMeasurement{}, %{"map_percentiles" => percentiles})
assert {:ok, _} = Repo.insert(changeset)
measurement = Repo.one(RepoMeasurement)
assert %{"50" => 416_500, "99" => 416_500} = measurement.map_percentiles
:ok = Ecto.Migrator.down(Repo, 0, CreateMeasurementMigration, log: false)
end
end