@@ -351,19 +351,34 @@ defmodule Mongo.Ecto.NormalizedQueryNewTest do
351351 assert_fields query , query: % { foo: 123.0 }
352352 end
353353
354- test "tagged type" do
355- # query = Schema |> select([], type(^"601d74e4-a8d3-4b6e-8365-eddb4c893327", Ecto.UUID)) |> normalize
354+ # test "tagged type" do
355+ # # query = Schema |> select([], type(^"601d74e4-a8d3-4b6e-8365-eddb4c893327", Ecto.UUID)) |> normalize
356356
357- # query = Schema |> select([], type(^1, Custom.Permalink)) |> normalize
357+ # # query = Schema |> select([], type(^1, Custom.Permalink)) |> normalize
358358
359- # query = Schema |> select([], type(^[1,2,3], {:array, Custom.Permalink})) |> normalize
359+ # # query = Schema |> select([], type(^[1,2,3], {:array, Custom.Permalink})) |> normalize
360+ # end
361+
362+ test "nested expressions" do
363+ z = 123
364+ query = from ( r in Schema , [ ] )
365+ |> where ( [ r ] , r . x > 0 and ( r . y > ^ ( - z ) ) or true ) |> normalize
366+ assert_fields query , query:
367+ % { "$or": [ [ "$and": [ [ x: [ "$gt": 0 ] ] , [ y: [ "$gt": - 123 ] ] ] ] , true ] }
368+ end
369+
370+ test "bool ops" do
371+ query = Schema |> where ( [ ] , true and false ) |> normalize
372+ assert_fields query , query: % { "$and": [ true , false ] }
373+
374+ query = Schema |> where ( [ ] , true or false ) |> normalize
375+ assert_fields query , query: % { "$or": [ true , false ] }
376+
377+ query = Schema |> where ( [ r ] , not ( r . x > 0 ) and not ( r . x < 5 ) ) |> normalize
378+ assert_fields query , query:
379+ % { "$and": [ [ "$not": [ x: [ "$gt": 0 ] ] ] , [ "$not": [ x: [ "$lt": 5 ] ] ] ] }
360380 end
361381
362- # test "nested expressions" do
363- # z = 123
364- # query = from(r in Schema, []) |> select([r], r.x > 0 and (r.y > ^(-z)) or true) |> normalize
365- # assert SQL.all(query) == ~s{SELECT ((m0."x" > 0) AND (m0."y" > $1)) OR TRUE FROM "schema" AS m0}
366- # end
367382
368383 # test "in expression" do
369384 # query = Schema |> select([e], 1 in []) |> normalize
@@ -394,27 +409,17 @@ defmodule Mongo.Ecto.NormalizedQueryNewTest do
394409 # assert SQL.all(query) == ~s{SELECT 1 = ANY(foo) FROM "schema" AS m0}
395410 # end
396411
397- # test "having" do
398- # query = Schema |> having([p], p.x == p.x) |> select([], true) |> normalize
399- # assert SQL.all(query) == ~s{SELECT TRUE FROM "schema" AS m0 HAVING (m0."x" = m0."x")}
400-
401- # query = Schema |> having([p], p.x == p.x) |> having([p], p.y == p.y) |> select([], true) |> normalize
402- # assert SQL.all(query) == ~s{SELECT TRUE FROM "schema" AS m0 HAVING (m0."x" = m0."x") AND (m0."y" = m0."y")}
403- # end
404-
405- # test "group by" do
406- # query = Schema |> group_by([r], r.x) |> select([r], r.x) |> normalize
407- # assert SQL.all(query) == ~s{SELECT m0."x" FROM "schema" AS m0 GROUP BY m0."x"}
408-
409- # query = Schema |> group_by([r], 2) |> select([r], r.x) |> normalize
410- # assert SQL.all(query) == ~s{SELECT m0."x" FROM "schema" AS m0 GROUP BY 2}
411-
412- # query = Schema |> group_by([r], [r.x, r.y]) |> select([r], r.x) |> normalize
413- # assert SQL.all(query) == ~s{SELECT m0."x" FROM "schema" AS m0 GROUP BY m0."x", m0."y"}
412+ test "having" do
413+ assert_raise Ecto.QueryError , fn ->
414+ Schema |> having ( [ p ] , p . x == p . x ) |> normalize
415+ end
416+ end
414417
415- # query = Schema |> group_by([r], []) |> select([r], r.x) |> normalize
416- # assert SQL.all(query) == ~s{SELECT m0."x" FROM "schema" AS m0}
417- # end
418+ test "group by" do
419+ assert_raise Ecto.QueryError , fn ->
420+ Schema |> group_by ( [ r ] , r . x ) |> select ( [ r ] , r . x ) |> normalize
421+ end
422+ end
418423
419424 # test "arrays and sigils" do
420425 # query = Schema |> select([], fragment("?", [1, 2, 3])) |> normalize
@@ -424,31 +429,20 @@ defmodule Mongo.Ecto.NormalizedQueryNewTest do
424429 # assert SQL.all(query) == ~s{SELECT ARRAY['abc','def'] FROM "schema" AS m0}
425430 # end
426431
427- # test "interpolated values" do
428- # query = "schema"
429- # |> select([m], {m.id, ^true})
430- # |> join(:inner, [], Schema2, ^true)
431- # |> join(:inner, [], Schema2, ^false)
432- # |> where([], fragment("?", ^true))
433- # |> where([], fragment("?", ^false))
434- # |> having([], fragment("?", ^true))
435- # |> having([], fragment("?", ^false))
436- # |> group_by([], fragment("?", ^1))
437- # |> group_by([], fragment("?", ^2))
438- # |> order_by([], fragment("?", ^3))
439- # |> order_by([], ^:x)
440- # |> limit([], ^4)
441- # |> offset([], ^5)
442- # |> normalize
432+ test "interpolated values" do
433+ query = Schema
434+ |> where ( [ ] , ^ true )
435+ |> where ( [ ] , ^ false )
436+ |> order_by ( [ ] , ^ :x )
437+ |> limit ( [ ] , ^ 4 )
438+ |> offset ( [ ] , ^ 5 )
439+ |> normalize
443440
444- # result =
445- # "SELECT m0.\"id\", $1 FROM \"schema\" AS m0 INNER JOIN \"schema2\" AS m1 ON $2 " <>
446- # "INNER JOIN \"schema2\" AS m2 ON $3 WHERE ($4) AND ($5) " <>
447- # "GROUP BY $6, $7 HAVING ($8) AND ($9) " <>
448- # "ORDER BY $10, m0.\"x\" LIMIT $11 OFFSET $12"
449-
450- # assert SQL.all(query) == String.rstrip(result)
451- # end
441+ assert_fields query ,
442+ opts: [ limit: 4 , skip: 5 ] ,
443+ order: [ x: 1 ] ,
444+ query: % { _id: [ "$exists": true , "$exists": false ] }
445+ end
452446
453447 # test "fragments and types" do
454448 # query =
0 commit comments