Merge branch 'issue/1878' into 'develop'
[akkoma] / test / repo_test.exs
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
5 defmodule Pleroma.RepoTest do
6 use Pleroma.DataCase
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 notification = refresh_record(insert(:notification, user: user))
41
42 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
43 end
44
45 test "return error if has not assoc " do
46 token = insert(:oauth_token, user: nil)
47 assert Repo.get_assoc(token, :user) == {:error, :not_found}
48 end
49 end
50 end