logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

repo_test.exs (2239B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.RepoTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.User
  8. describe "find_resource/1" do
  9. test "returns user" do
  10. user = insert(:user)
  11. query = from(t in User, where: t.id == ^user.id)
  12. assert Repo.find_resource(query) == {:ok, user}
  13. end
  14. test "returns not_found" do
  15. query = from(t in User, where: t.id == ^"9gBuXNpD2NyDmmxxdw")
  16. assert Repo.find_resource(query) == {:error, :not_found}
  17. end
  18. end
  19. describe "get_assoc/2" do
  20. test "get assoc from preloaded data" do
  21. user = %User{name: "Agent Smith"}
  22. token = %Pleroma.Web.OAuth.Token{insert(:oauth_token) | user: user}
  23. assert Repo.get_assoc(token, :user) == {:ok, user}
  24. end
  25. test "get one-to-one assoc from repo" do
  26. user = insert(:user, name: "Jimi Hendrix")
  27. token = refresh_record(insert(:oauth_token, user: user))
  28. assert Repo.get_assoc(token, :user) == {:ok, user}
  29. end
  30. test "get one-to-many assoc from repo" do
  31. user = insert(:user)
  32. notification =
  33. refresh_record(insert(:notification, user: user, activity: insert(:note_activity)))
  34. assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
  35. end
  36. test "return error if has not assoc " do
  37. token = insert(:oauth_token, user: nil)
  38. assert Repo.get_assoc(token, :user) == {:error, :not_found}
  39. end
  40. end
  41. describe "chunk_stream/3" do
  42. test "fetch records one-by-one" do
  43. users = insert_list(50, :user)
  44. {fetch_users, 50} =
  45. from(t in User)
  46. |> Repo.chunk_stream(5)
  47. |> Enum.reduce({[], 0}, fn %User{} = user, {acc, count} ->
  48. {acc ++ [user], count + 1}
  49. end)
  50. assert users == fetch_users
  51. end
  52. test "fetch records in bulk" do
  53. users = insert_list(50, :user)
  54. {fetch_users, 10} =
  55. from(t in User)
  56. |> Repo.chunk_stream(5, :batches)
  57. |> Enum.reduce({[], 0}, fn users, {acc, count} ->
  58. {acc ++ users, count + 1}
  59. end)
  60. assert users == fetch_users
  61. end
  62. end
  63. end