AP C2S: Remove restrictions and make it go through pipeline
[akkoma] / test / pleroma / repo_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.RepoTest do
6 use Pleroma.DataCase, async: true
7 import Pleroma.Factory
8
9 alias Pleroma.User
10
11 describe "find_resource/1" do
12 test "returns user" do
13 user = insert(:user)
14 query = from(t in User, where: t.id == ^user.id)
15 assert Repo.find_resource(query) == {:ok, user}
16 end
17
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}
21 end
22 end
23
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}
29 end
30
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))
34
35 assert Repo.get_assoc(token, :user) == {:ok, user}
36 end
37
38 test "get one-to-many assoc from repo" do
39 user = insert(:user)
40
41 notification =
42 refresh_record(insert(:notification, user: user, activity: insert(:note_activity)))
43
44 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
45 end
46
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}
50 end
51 end
52
53 describe "chunk_stream/3" do
54 test "fetch records one-by-one" do
55 users = insert_list(50, :user)
56
57 {fetch_users, 50} =
58 from(t in User)
59 |> Repo.chunk_stream(5)
60 |> Enum.reduce({[], 0}, fn %User{} = user, {acc, count} ->
61 {acc ++ [user], count + 1}
62 end)
63
64 assert users == fetch_users
65 end
66
67 test "fetch records in bulk" do
68 users = insert_list(50, :user)
69
70 {fetch_users, 10} =
71 from(t in User)
72 |> Repo.chunk_stream(5, :batches)
73 |> Enum.reduce({[], 0}, fn users, {acc, count} ->
74 {acc ++ users, count + 1}
75 end)
76
77 assert users == fetch_users
78 end
79 end
80 end