1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.RepoTest do
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
40 notification = refresh_record(insert(:notification, user: user))
42 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
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}