Skip to content

Commit 2045eb8

Browse files
committed
Push normalized query tests a bit
1 parent d574181 commit 2045eb8

2 files changed

Lines changed: 47 additions & 117 deletions

File tree

test/mongo_ecto/normalized_query_new_test.exs

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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 =

test/mongo_ecto/normalized_query_test.exs

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -293,45 +293,6 @@
293293
# assert_query(query, query: %{x: 123.0})
294294
# end
295295

296-
# test "nested expressions" do
297-
# z = 123
298-
# query = from(r in Schema, [])
299-
# |> where([r], r.x > 0 and (r.y > ^(-z)) or true) |> normalize
300-
# assert_query(query, query:
301-
# %{"$or": [["$and": [[x: ["$gt": 0]], [y: ["$gt": -123]]]], true]})
302-
# end
303-
304-
# test "binary ops" do
305-
# query = Schema |> where([r], r.x == 2) |> normalize
306-
# assert_query(query, query: %{x: 2})
307-
308-
# query = Schema |> where([r], r.x != 2) |> normalize
309-
# assert_query(query, query: %{x: ["$ne": 2]})
310-
311-
# query = Schema |> where([r], r.x <= 2) |> normalize
312-
# assert_query(query, query: %{x: ["$lte": 2]})
313-
314-
# query = Schema |> where([r], r.x >= 2) |> normalize
315-
# assert_query(query, query: %{x: ["$gte": 2]})
316-
317-
# query = Schema |> where([r], r.x < 2) |> normalize
318-
# assert_query(query, query: %{x: ["$lt": 2]})
319-
320-
# query = Schema |> where([r], r.x > 2) |> normalize
321-
# assert_query(query, query: %{x: ["$gt": 2]})
322-
# end
323-
324-
# test "bool ops" do
325-
# query = Schema |> where([], true and false) |> normalize
326-
# assert_query(query, query: %{"$and": [true, false]})
327-
328-
# query = Schema |> where([], true or false) |> normalize
329-
# assert_query(query, query: %{"$or": [true, false]})
330-
331-
# query = Schema |> where([r], not (r.x > 0) and not (r.x < 5)) |> normalize
332-
# assert_query(query, query:
333-
# %{"$and": [["$not": [x: ["$gt": 0]]], ["$not": [x: ["$lt": 5]]]]})
334-
# end
335296

336297
# test "in expression" do
337298
# query = Schema |> where([e], e.x in []) |> normalize
@@ -355,32 +316,7 @@
355316
# end
356317
# end
357318

358-
# test "having" do
359-
# assert_raise Ecto.QueryError, fn ->
360-
# Schema |> having([p], p.x == p.x) |> normalize
361-
# end
362-
# end
363-
364-
# test "group by" do
365-
# assert_raise Ecto.QueryError, fn ->
366-
# Schema |> group_by([r], r.x) |> select([r], r.x) |> normalize
367-
# end
368-
# end
369319

370-
# test "interpolated values" do
371-
# query = Schema
372-
# |> where([], ^true)
373-
# |> where([], ^false)
374-
# |> order_by([], ^:x)
375-
# |> limit([], ^4)
376-
# |> offset([], ^5)
377-
# |> normalize
378-
379-
# assert_query(query,
380-
# opts: [limit: 4, skip: 5],
381-
# order: [x: 1],
382-
# query: %{_id: ["$exists": true, "$exists": false]})
383-
# end
384320

385321
# # *_all
386322

0 commit comments

Comments
 (0)