stream.exs (1306B)
1 defmodule Ecto.Integration.StreamTest do 2 use Ecto.Integration.Case, async: Application.compile_env(:ecto, :async_integration_tests, true) 3 4 alias Ecto.Integration.TestRepo 5 alias Ecto.Integration.Post 6 alias Ecto.Integration.Comment 7 import Ecto.Query 8 9 test "stream empty" do 10 assert {:ok, []} = TestRepo.transaction(fn() -> 11 TestRepo.stream(Post) 12 |> Enum.to_list() 13 end) 14 15 assert {:ok, []} = TestRepo.transaction(fn() -> 16 TestRepo.stream(from p in Post) 17 |> Enum.to_list() 18 end) 19 end 20 21 test "stream without schema" do 22 %Post{} = TestRepo.insert!(%Post{title: "title1"}) 23 %Post{} = TestRepo.insert!(%Post{title: "title2"}) 24 25 assert {:ok, ["title1", "title2"]} = TestRepo.transaction(fn() -> 26 TestRepo.stream(from(p in "posts", order_by: p.title, select: p.title)) 27 |> Enum.to_list() 28 end) 29 end 30 31 test "stream with assoc" do 32 p1 = TestRepo.insert!(%Post{title: "1"}) 33 34 %Comment{id: cid1} = TestRepo.insert!(%Comment{text: "1", post_id: p1.id}) 35 %Comment{id: cid2} = TestRepo.insert!(%Comment{text: "2", post_id: p1.id}) 36 37 stream = TestRepo.stream(Ecto.assoc(p1, :comments)) 38 assert {:ok, [c1, c2]} = TestRepo.transaction(fn() -> 39 Enum.to_list(stream) 40 end) 41 assert c1.id == cid1 42 assert c2.id == cid2 43 end 44 end