Merge branch 'develop' into global-status-expiration
[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 ExUnit.CaptureLog
8 import Pleroma.Factory
9 import Mock
10
11 alias Pleroma.User
12
13 describe "find_resource/1" do
14 test "returns user" do
15 user = insert(:user)
16 query = from(t in User, where: t.id == ^user.id)
17 assert Repo.find_resource(query) == {:ok, user}
18 end
19
20 test "returns not_found" do
21 query = from(t in User, where: t.id == ^"9gBuXNpD2NyDmmxxdw")
22 assert Repo.find_resource(query) == {:error, :not_found}
23 end
24 end
25
26 describe "get_assoc/2" do
27 test "get assoc from preloaded data" do
28 user = %User{name: "Agent Smith"}
29 token = %Pleroma.Web.OAuth.Token{insert(:oauth_token) | user: user}
30 assert Repo.get_assoc(token, :user) == {:ok, user}
31 end
32
33 test "get one-to-one assoc from repo" do
34 user = insert(:user, name: "Jimi Hendrix")
35 token = refresh_record(insert(:oauth_token, user: user))
36
37 assert Repo.get_assoc(token, :user) == {:ok, user}
38 end
39
40 test "get one-to-many assoc from repo" do
41 user = insert(:user)
42 notification = refresh_record(insert(:notification, user: user))
43
44 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
45 end
46
47 test "return error if has not assoc " do
48 token = insert(:oauth_token, user: nil)
49 assert Repo.get_assoc(token, :user) == {:error, :not_found}
50 end
51 end
52
53 describe "check_migrations_applied!" do
54 setup_with_mocks([
55 {Ecto.Migrator, [],
56 [
57 with_repo: fn repo, fun -> passthrough([repo, fun]) end,
58 migrations: fn Pleroma.Repo ->
59 [
60 {:up, 20_191_128_153_944, "fix_missing_following_count"},
61 {:up, 20_191_203_043_610, "create_report_notes"},
62 {:down, 20_191_220_174_645, "add_scopes_to_pleroma_feo_auth_records"}
63 ]
64 end
65 ]}
66 ]) do
67 :ok
68 end
69
70 setup do: clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check])
71
72 test "raises if it detects unapplied migrations" do
73 assert_raise Pleroma.Repo.UnappliedMigrationsError, fn ->
74 capture_log(&Repo.check_migrations_applied!/0)
75 end
76 end
77
78 test "doesn't do anything if disabled" do
79 Pleroma.Config.put([:i_am_aware_this_may_cause_data_loss, :disable_migration_check], true)
80
81 assert :ok == Repo.check_migrations_applied!()
82 end
83 end
84 end