Merge remote-tracking branch 'pleroma/develop' into feature/addressable-lists
[akkoma] / test / repo_test.exs
1 defmodule Pleroma.RepoTest do
2 use Pleroma.DataCase
3 import Pleroma.Factory
4 alias Pleroma.User
5
6 describe "find_resource/1" do
7 test "returns user" do
8 user = insert(:user)
9 query = from(t in User, where: t.id == ^user.id)
10 assert Repo.find_resource(query) == {:ok, user}
11 end
12
13 test "returns not_found" do
14 query = from(t in User, where: t.id == ^"9gBuXNpD2NyDmmxxdw")
15 assert Repo.find_resource(query) == {:error, :not_found}
16 end
17 end
18
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
26 test "get one-to-one assoc from repo" do
27 user = insert(:user, name: "Jimi Hendrix")
28 token = refresh_record(insert(:oauth_token, user: user))
29
30 assert Repo.get_assoc(token, :user) == {:ok, user}
31 end
32
33 test "get one-to-many assoc from repo" do
34 user = insert(:user)
35 notification = refresh_record(insert(:notification, user: user))
36
37 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
38 end
39
40 test "return error if has not assoc " do
41 token = insert(:oauth_token, user: nil)
42 assert Repo.get_assoc(token, :user) == {:error, :not_found}
43 end
44 end
45 end