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
7 import ExUnit.CaptureLog
13 describe "find_resource/1" do
14 test "returns user" do
16 query = from(t in User, where: t.id == ^user.id)
17 assert Repo.find_resource(query) == {:ok, user}
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}
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}
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))
37 assert Repo.get_assoc(token, :user) == {:ok, user}
40 test "get one-to-many assoc from repo" do
42 notification = refresh_record(insert(:notification, user: user))
44 assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
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}
53 describe "check_migrations_applied!" do
57 with_repo: fn repo, fun -> passthrough([repo, fun]) end,
58 migrations: fn Pleroma.Repo ->
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"}
70 setup do: clear_config([:i_am_aware_this_may_cause_data_loss, :disable_migration_check])
72 test "raises if it detects unapplied migrations" do
73 assert_raise Pleroma.Repo.UnappliedMigrationsError, fn ->
74 capture_log(&Repo.check_migrations_applied!/0)
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)
81 assert :ok == Repo.check_migrations_applied!()