X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Ftasks%2Fdatabase_test.exs;h=883828d7731cb132268edf2d0128f471a6762f8f;hb=814c3e51714b2a7de30ed751a6aef361fc712807;hp=579130b055a37821d51fa788ede3e2dc165a5f05;hpb=5ece901af3e887664653c79c5e61618cc5cf0ecf;p=akkoma
diff --git a/test/tasks/database_test.exs b/test/tasks/database_test.exs
index 579130b05..883828d77 100644
--- a/test/tasks/database_test.exs
+++ b/test/tasks/database_test.exs
@@ -1,10 +1,14 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mix.Tasks.Pleroma.DatabaseTest do
+ alias Pleroma.Activity
+ alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.User
+ alias Pleroma.Web.CommonAPI
+
use Pleroma.DataCase
import Pleroma.Factory
@@ -19,31 +23,108 @@ defmodule Mix.Tasks.Pleroma.DatabaseTest do
:ok
end
+ describe "running remove_embedded_objects" do
+ test "it replaces objects with references" do
+ user = insert(:user)
+ {:ok, activity} = CommonAPI.post(user, %{status: "test"})
+ new_data = Map.put(activity.data, "object", activity.object.data)
+
+ {:ok, activity} =
+ activity
+ |> Activity.change(%{data: new_data})
+ |> Repo.update()
+
+ assert is_map(activity.data["object"])
+
+ Mix.Tasks.Pleroma.Database.run(["remove_embedded_objects"])
+
+ activity = Activity.get_by_id_with_object(activity.id)
+ assert is_binary(activity.data["object"])
+ end
+ end
+
+ describe "prune_objects" do
+ test "it prunes old objects from the database" do
+ insert(:note)
+ deadline = Pleroma.Config.get([:instance, :remote_post_retention_days]) + 1
+
+ date =
+ Timex.now()
+ |> Timex.shift(days: -deadline)
+ |> Timex.to_naive_datetime()
+ |> NaiveDateTime.truncate(:second)
+
+ %{id: id} =
+ :note
+ |> insert()
+ |> Ecto.Changeset.change(%{inserted_at: date})
+ |> Repo.update!()
+
+ assert length(Repo.all(Object)) == 2
+
+ Mix.Tasks.Pleroma.Database.run(["prune_objects"])
+
+ assert length(Repo.all(Object)) == 1
+ refute Object.get_by_id(id)
+ end
+ end
+
describe "running update_users_following_followers_counts" do
test "following and followers count are updated" do
[user, user2] = insert_pair(:user)
- {:ok, %User{following: following, info: info} = user} = User.follow(user, user2)
+ {:ok, %User{} = user} = User.follow(user, user2)
- assert length(following) == 2
- assert info.follower_count == 0
+ following = User.following(user)
- info_cng = Ecto.Changeset.change(info, %{follower_count: 3})
+ assert length(following) == 2
+ assert user.follower_count == 0
{:ok, user} =
user
- |> Ecto.Changeset.change(%{following: following ++ following})
- |> Ecto.Changeset.put_embed(:info, info_cng)
+ |> Ecto.Changeset.change(%{follower_count: 3})
|> Repo.update()
- assert length(user.following) == 4
- assert user.info.follower_count == 3
+ assert user.follower_count == 3
assert :ok == Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
user = User.get_by_id(user.id)
- assert length(user.following) == 2
- assert user.info.follower_count == 0
+ assert length(User.following(user)) == 2
+ assert user.follower_count == 0
+ end
+ end
+
+ describe "running fix_likes_collections" do
+ test "it turns OrderedCollection likes into empty arrays" do
+ [user, user2] = insert_pair(:user)
+
+ {:ok, %{id: id, object: object}} = CommonAPI.post(user, %{status: "test"})
+ {:ok, %{object: object2}} = CommonAPI.post(user, %{status: "test test"})
+
+ CommonAPI.favorite(user2, id)
+
+ likes = %{
+ "first" =>
+ "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes?page=1",
+ "id" => "http://mastodon.example.org/objects/dbdbc507-52c8-490d-9b7c-1e1d52e5c132/likes",
+ "totalItems" => 3,
+ "type" => "OrderedCollection"
+ }
+
+ new_data = Map.put(object2.data, "likes", likes)
+
+ object2
+ |> Ecto.Changeset.change(%{data: new_data})
+ |> Repo.update()
+
+ assert length(Object.get_by_id(object.id).data["likes"]) == 1
+ assert is_map(Object.get_by_id(object2.id).data["likes"])
+
+ assert :ok == Mix.Tasks.Pleroma.Database.run(["fix_likes_collections"])
+
+ assert length(Object.get_by_id(object.id).data["likes"]) == 1
+ assert Enum.empty?(Object.get_by_id(object2.id).data["likes"])
end
end
end