@@ -5,6 +5,16 @@ defmodule Ecto.Query.Builder.FromTest do
55
66 import Ecto.Query
77
8+ defmodule Schema do
9+ use Ecto.Schema
10+
11+ schema "schema" do
12+ field :num , :integer
13+ field :text , :string
14+ end
15+ end
16+
17+
818 defmacro from_macro ( left , right ) do
919 quote do
1020 fragment ( "? <> ?" , unquote ( left ) , unquote ( right ) )
@@ -19,31 +29,52 @@ defmodule Ecto.Query.Builder.FromTest do
1929 end
2030
2131 test "values list source" do
22- # Valid input
2332 values = [ % { num: 1 , text: "one" } , % { num: 2 , text: "two" } ]
2433 types = % { num: :integer , text: :string }
2534 query = from v in values ( values , types )
2635
2736 types_kw = Enum . map ( types , & & 1 )
2837 assert query . from . source == { :values , [ ] , [ types_kw , length ( values ) ] }
38+ end
39+
40+ test "values list source with types defined by schema" do
41+ values = [ % { num: 1 , text: "one" } , % { num: 2 , text: "two" } ]
42+ type_schema = Schema
43+ types_kw = Enum . map ( % { num: :integer , text: :string } , & & 1 )
44+ query = from v in values ( values , type_schema )
45+
46+ assert query . from . source == { :values , [ ] , [ types_kw , length ( values ) ] }
47+ end
2948
30- # Empty values
49+ test "values list source with empty values" do
3150 msg = "must provide a non-empty list to values/2"
3251
3352 assert_raise ArgumentError , msg , fn ->
3453 from v in values ( [ ] , % { } )
3554 end
55+ end
3656
37-
38- # Missing type
57+ test "values list source with missing types" do
3958 msg = "values/2 must declare the type for every field. The type was not given for field `text`"
4059
4160 assert_raise ArgumentError , msg , fn ->
4261 values = [ % { num: 1 , text: "one" } , % { num: 2 , text: "two" } ]
4362 types = % { num: :integer }
4463 from v in values ( values , types )
4564 end
65+ end
66+
67+ test "values list source with missing schema types" do
68+ msg = "values/2 must declare the type for every field. The type was not given for field `not_a_field`"
69+
70+ assert_raise ArgumentError , msg , fn ->
71+ values = [ % { not_a_field: 1 } ]
72+ types = Schema
73+ from v in values ( values , types )
74+ end
75+ end
4676
77+ test "values list source with inconsistent fields across entries" do
4778 # Missing field
4879 msg = "each member of a values list must have the same fields. Missing field `text` in %{num: 2}"
4980
0 commit comments