Merge branch 'feature/native-captcha' into 'develop'
[akkoma] / test / repo_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 alias Pleroma.User
9
10 describe "find_resource/1" do
11 test "returns user" do
12 user = insert(:user)
13 query = from(t in User, where: t.id == ^user.id)
14 assert Repo.find_resource(query) == {:ok, user}
15 end
16
17 test "returns not_found" do
18 query = from(t in User, where: t.id == ^"9gBuXNpD2NyDmmxxdw")
19 assert Repo.find_resource(query) == {:error, :not_found}
20 end
21 end
22
23 describe "get_assoc/2" do
24 test "get assoc from preloaded data" do
25 user = %User{name: "Agent Smith"}
26 token = %Pleroma.Web.OAuth.Token{insert(:oauth_token) | user: user}
27 assert Repo.get_assoc(token, :user) == {:ok, user}
28 end
29
30 test "get one-to-one assoc from repo" do
31 user = insert(:user, name: "Jimi Hendrix")
32 token = refresh_record(insert(:oauth_token, user: user))
33
34 assert Repo.get_assoc(token, :user) == {:ok, user}
35 end
36
37 test "get one-to-many assoc from repo" do
38 user = insert(:user)
39 notification = refresh_record(insert(:notification, user: user))
40
41 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
42 end
43
44 test "return error if has not assoc " do
45 token = insert(:oauth_token, user: nil)
46 assert Repo.get_assoc(token, :user) == {:error, :not_found}
47 end
48 end
49 end