1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.RepoTest do
6 use Pleroma.DataCase, async: true
11 describe "find_resource/1" do
12 test "returns user" do
14 query = from(t in User, where: t.id == ^user.id)
15 assert Repo.find_resource(query) == {:ok, user}
18 test "returns not_found" do
19 query = from(t in User, where: t.id == ^"9gBuXNpD2NyDmmxxdw")
20 assert Repo.find_resource(query) == {:error, :not_found}
24 describe "get_assoc/2" do
25 test "get assoc from preloaded data" do
26 user = %User{name: "Agent Smith"}
27 token = %Pleroma.Web.OAuth.Token{insert(:oauth_token) | user: user}
28 assert Repo.get_assoc(token, :user) == {:ok, user}
31 test "get one-to-one assoc from repo" do
32 user = insert(:user, name: "Jimi Hendrix")
33 token = refresh_record(insert(:oauth_token, user: user))
35 assert Repo.get_assoc(token, :user) == {:ok, user}
38 test "get one-to-many assoc from repo" do
42 refresh_record(insert(:notification, user: user, activity: insert(:note_activity)))
44 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
47 test "return error if has not assoc " do
48 token = insert(:oauth_token, user: nil)
49 assert Repo.get_assoc(token, :user) == {:error, :not_found}
53 describe "chunk_stream/3" do
54 test "fetch records one-by-one" do
55 users = insert_list(50, :user)
59 |> Repo.chunk_stream(5)
60 |> Enum.reduce({[], 0}, fn %User{} = user, {acc, count} ->
61 {acc ++ [user], count + 1}
64 assert users == fetch_users
67 test "fetch records in bulk" do
68 users = insert_list(50, :user)
72 |> Repo.chunk_stream(5, :batches)
73 |> Enum.reduce({[], 0}, fn users, {acc, count} ->
74 {acc ++ users, count + 1}
77 assert users == fetch_users