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 Mix.Tasks.Pleroma.DatabaseTest do
10 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
17 Mix.shell(Mix.Shell.Process)
20 Mix.shell(Mix.Shell.IO)
26 describe "running remove_embedded_objects" do
27 test "it replaces objects with references" do
29 {:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
30 new_data = Map.put(activity.data, "object", activity.object.data)
34 |> Activity.change(%{data: new_data})
37 assert is_map(activity.data["object"])
39 Mix.Tasks.Pleroma.Database.run(["remove_embedded_objects"])
41 activity = Activity.get_by_id_with_object(activity.id)
42 assert is_binary(activity.data["object"])
46 describe "prune_objects" do
47 test "it prunes old objects from the database" do
49 deadline = Pleroma.Config.get([:instance, :remote_post_retention_days]) + 1
53 |> Timex.shift(days: -deadline)
54 |> Timex.to_naive_datetime()
55 |> NaiveDateTime.truncate(:second)
60 |> Ecto.Changeset.change(%{inserted_at: date})
63 assert length(Repo.all(Object)) == 2
65 Mix.Tasks.Pleroma.Database.run(["prune_objects"])
67 assert length(Repo.all(Object)) == 1
68 refute Object.get_by_id(id)
72 describe "running update_users_following_followers_counts" do
73 test "following and followers count are updated" do
74 [user, user2] = insert_pair(:user)
75 {:ok, %User{following: following, info: info} = user} = User.follow(user, user2)
77 assert length(following) == 2
78 assert info.follower_count == 0
82 |> Ecto.Changeset.change(%{following: following ++ following})
83 |> User.change_info(&Ecto.Changeset.change(&1, %{follower_count: 3}))
86 assert length(user.following) == 4
87 assert user.info.follower_count == 3
89 assert :ok == Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
91 user = User.get_by_id(user.id)
93 assert length(user.following) == 2
94 assert user.info.follower_count == 0
98 describe "running fix_likes_collections" do
99 test "it turns OrderedCollection likes into empty arrays" do
100 [user, user2] = insert_pair(:user)
102 {:ok, %{id: id, object: object}} = CommonAPI.post(user, %{"status" => "test"})
103 {:ok, %{object: object2}} = CommonAPI.post(user, %{"status" => "test test"})
105 CommonAPI.favorite(id, user2)
109 "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
110 "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
112 "type" => "OrderedCollection"
115 new_data = Map.put(object2.data, "likes", likes)
118 |> Ecto.Changeset.change(%{data: new_data})
121 assert length(Object.get_by_id(object.id).data["likes"]) == 1
122 assert is_map(Object.get_by_id(object2.id).data["likes"])
124 assert :ok == Mix.Tasks.Pleroma.Database.run(["fix_likes_collections"])
126 assert length(Object.get_by_id(object.id).data["likes"]) == 1
127 assert Enum.empty?(Object.get_by_id(object2.id).data["likes"])