- Actually use preloaded bookmarks in views
[akkoma] / test / activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ActivityTest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Bookmark
9 alias Pleroma.Object
10 import Pleroma.Factory
11
12 test "returns an activity by it's AP id" do
13 activity = insert(:note_activity)
14 found_activity = Activity.get_by_ap_id(activity.data["id"])
15
16 assert activity == found_activity
17 end
18
19 test "returns activities by it's objects AP ids" do
20 activity = insert(:note_activity)
21 [found_activity] = Activity.get_all_create_by_object_ap_id(activity.data["object"]["id"])
22
23 assert activity == found_activity
24 end
25
26 test "returns the activity that created an object" do
27 activity = insert(:note_activity)
28
29 found_activity = Activity.get_create_by_object_ap_id(activity.data["object"]["id"])
30
31 assert activity == found_activity
32 end
33
34 describe "preloading bookmarks" do
35 setup do
36 user1 = insert(:user)
37 user2 = insert(:user)
38 activity = insert(:note_activity)
39 {:ok, bookmark1} = Bookmark.create(user1.id, activity.id)
40 {:ok, bookmark2} = Bookmark.create(user2.id, activity.id)
41 [activity: activity, bookmarks: Enum.sort([bookmark1, bookmark2])]
42 end
43
44 test "using with_preloaded_bookmarks", %{activity: activity, bookmarks: bookmarks} do
45 queried_activity =
46 Ecto.Query.from(a in Activity, where: a.id == ^activity.id)
47 |> Activity.with_preloaded_bookmarks()
48 |> Repo.one()
49
50 assert Enum.sort(queried_activity.bookmarks) == bookmarks
51 end
52
53 test "using with_preloaded_object", %{activity: activity, bookmarks: bookmarks} do
54 queried_activity =
55 Ecto.Query.from(a in Activity, where: a.id == ^activity.id)
56 |> Activity.with_preloaded_object()
57 |> Repo.one()
58
59 assert Enum.sort(queried_activity.bookmarks) == bookmarks
60 end
61
62 test "using get_by_ap_id_with_object", %{activity: activity, bookmarks: bookmarks} do
63 queried_activity = Activity.get_by_ap_id_with_object(activity.data["id"])
64 assert Enum.sort(queried_activity.bookmarks) == bookmarks
65 end
66
67 test "using get_by_id_with_object", %{activity: activity, bookmarks: bookmarks} do
68 queried_activity = Activity.get_by_id_with_object(activity.id)
69 assert Enum.sort(queried_activity.bookmarks) == bookmarks
70 end
71
72 test "using get_create_by_object_ap_id_with_object", %{
73 activity: activity,
74 bookmarks: bookmarks
75 } do
76 queried_activity =
77 Activity.get_create_by_object_ap_id_with_object(Object.normalize(activity).data["id"])
78
79 assert Enum.sort(queried_activity.bookmarks) == bookmarks
80 end
81 end
82 end