From d3fb9e02cc0ce7dc462e587e639e117aaef5fbc5 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Wed, 23 Oct 2019 22:48:04 +0300 Subject: [PATCH] add tests --- lib/pleroma/marker.ex | 9 +++------ .../20191021113356_add_unread_to_marker.exs | 3 ++- test/marker_test.exs | 15 +++++++++++++++ test/notification_test.exs | 3 +++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/marker.ex b/lib/pleroma/marker.ex index 098fe3bbd..5f6a47f38 100644 --- a/lib/pleroma/marker.ex +++ b/lib/pleroma/marker.ex @@ -58,7 +58,8 @@ def multi_set_unread_count(multi, %User{} = user, "notifications") do timeline: "notifications", user_id: type(^user.id, :string), unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"), - last_read_id: type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string) + last_read_id: + type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string) } ) @@ -77,11 +78,7 @@ def multi_set_unread_count(multi, %User{} = user, "notifications") do ) end - def set_unread_count(%User{} = user, timeline) do - Multi.new() - |> multi_set_unread_count(user, timeline) - |> Repo.transaction() - end + def multi_set_unread_count(multi, _, _), do: multi defp get_marker(user, timeline) do case Repo.find_resource(get_query(user, timeline)) do diff --git a/priv/repo/migrations/20191021113356_add_unread_to_marker.exs b/priv/repo/migrations/20191021113356_add_unread_to_marker.exs index 964c7fb98..c15e2ff13 100644 --- a/priv/repo/migrations/20191021113356_add_unread_to_marker.exs +++ b/priv/repo/migrations/20191021113356_add_unread_to_marker.exs @@ -26,7 +26,8 @@ def update_markers do timeline: "notifications", user_id: q.user_id, unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"), - last_read_id: type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string) + last_read_id: + type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string) }, group_by: [q.user_id] ) diff --git a/test/marker_test.exs b/test/marker_test.exs index 04bd67fe6..1900ed08b 100644 --- a/test/marker_test.exs +++ b/test/marker_test.exs @@ -8,6 +8,21 @@ defmodule Pleroma.MarkerTest do import Pleroma.Factory + describe "multi_set_unread_count/3" do + test "returns multi" do + user = insert(:user) + + assert %Ecto.Multi{ + operations: [marker: {:run, _}, counters: {:run, _}] + } = + Marker.multi_set_unread_count( + Ecto.Multi.new(), + user, + "notifications" + ) + end + end + describe "get_markers/2" do test "returns user markers" do user = insert(:user) diff --git a/test/notification_test.exs b/test/notification_test.exs index 558ac358c..1e8a9ca98 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -31,6 +31,9 @@ test "notifies someone when they are directly addressed" do assert notified_ids == [other_user.id, third_user.id] assert notification.activity_id == activity.id assert other_notification.activity_id == activity.id + + assert [%Pleroma.Marker{unread_count: 2}] = + Pleroma.Marker.get_markers(other_user, ["notifications"]) end test "it creates a notification for subscribed users" do