optimise notifications query
[akkoma] / test / pleroma / object / updater_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Object.UpdaterTest do
6 use Pleroma.DataCase
7 use Oban.Testing, repo: Pleroma.Repo
8
9 import Pleroma.Factory
10
11 alias Pleroma.Object.Updater
12
13 describe "make_update_object_data/3" do
14 setup do
15 note = insert(:note)
16 %{original_data: note.data}
17 end
18
19 test "it makes an updated field", %{original_data: original_data} do
20 new_data = Map.put(original_data, "content", "new content")
21
22 date = Pleroma.Web.ActivityPub.Utils.make_date()
23 update_object_data = Updater.make_update_object_data(original_data, new_data, date)
24 assert %{"updated" => ^date} = update_object_data
25 end
26
27 test "it creates formerRepresentations", %{original_data: original_data} do
28 new_data = Map.put(original_data, "content", "new content")
29
30 date = Pleroma.Web.ActivityPub.Utils.make_date()
31 update_object_data = Updater.make_update_object_data(original_data, new_data, date)
32
33 history_item = original_data |> Map.drop(["id", "formerRepresentations"])
34
35 assert %{
36 "formerRepresentations" => %{
37 "totalItems" => 1,
38 "orderedItems" => [^history_item]
39 }
40 } = update_object_data
41 end
42 end
43
44 describe "make_new_object_data_from_update_object/2" do
45 test "it reuses formerRepresentations if it exists" do
46 %{data: original_data} = insert(:note)
47
48 new_data =
49 original_data
50 |> Map.put("content", "edited")
51
52 date = Pleroma.Web.ActivityPub.Utils.make_date()
53 update_object_data = Updater.make_update_object_data(original_data, new_data, date)
54
55 history = update_object_data["formerRepresentations"]["orderedItems"]
56
57 update_object_data =
58 update_object_data
59 |> put_in(
60 ["formerRepresentations", "orderedItems"],
61 history ++ [Map.put(original_data, "summary", "additional summary")]
62 )
63 |> put_in(["formerRepresentations", "totalItems"], length(history) + 1)
64
65 %{
66 updated_data: updated_data,
67 updated: updated,
68 used_history_in_new_object?: used_history_in_new_object?
69 } = Updater.make_new_object_data_from_update_object(original_data, update_object_data)
70
71 assert updated
72 assert used_history_in_new_object?
73 assert updated_data["formerRepresentations"] == update_object_data["formerRepresentations"]
74 end
75 end
76 end