1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.RepoTest do
10 describe "find_resource/1" do
11 test "returns user" do
13 query = from(t in User, where: t.id == ^user.id)
14 assert Repo.find_resource(query) == {:ok, user}
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}
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}
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))
34 assert Repo.get_assoc(token, :user) == {:ok, user}
37 test "get one-to-many assoc from repo" do
39 notification = refresh_record(insert(:notification, user: user))
41 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
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}