Merge remote-tracking branch 'pleroma/develop' into feature/addressable-lists
[akkoma] / test / tasks / database_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.DatabaseTest do
6 alias Pleroma.Repo
7 alias Pleroma.User
8 use Pleroma.DataCase
9
10 import Pleroma.Factory
11
12 setup_all do
13 Mix.shell(Mix.Shell.Process)
14
15 on_exit(fn ->
16 Mix.shell(Mix.Shell.IO)
17 end)
18
19 :ok
20 end
21
22 describe "running update_users_following_followers_counts" do
23 test "following and followers count are updated" do
24 [user, user2] = insert_pair(:user)
25 {:ok, %User{following: following, info: info} = user} = User.follow(user, user2)
26
27 assert length(following) == 2
28 assert info.follower_count == 0
29
30 info_cng = Ecto.Changeset.change(info, %{follower_count: 3})
31
32 {:ok, user} =
33 user
34 |> Ecto.Changeset.change(%{following: following ++ following})
35 |> Ecto.Changeset.put_embed(:info, info_cng)
36 |> Repo.update()
37
38 assert length(user.following) == 4
39 assert user.info.follower_count == 3
40
41 assert :ok == Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
42
43 user = User.get_by_id(user.id)
44
45 assert length(user.following) == 2
46 assert user.info.follower_count == 0
47 end
48 end
49 end