Skip to content

Commit

Permalink
Fix list messages (#42)
Browse files Browse the repository at this point in the history
* Update limt_messages to works with query limit passed as argument

* Add missing tests

* Remove unused Messaging method

* Do not call latest messages on index
  • Loading branch information
lonamiaec authored Feb 10, 2020
1 parent 3311cae commit 318966f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
14 changes: 4 additions & 10 deletions lib/postoffice/messaging.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ defmodule Postoffice.Messaging do
[%Message{}, ...]
"""
def list_messages(limit \\ 100) do
Repo.all(Message, limit: limit)
def list_messages(messages_limit \\ 100) do
Message
|> limit(^messages_limit)
|> Repo.all()
end

@doc """
Expand Down Expand Up @@ -212,12 +214,4 @@ defmodule Postoffice.Messaging do
from(p in PublisherFailures, where: p.message_id == ^message_id)
|> Repo.all()
end

def message_already_processed(message_id, publisher_id) do
query =
from ps in PublisherSuccess,
where: ps.publisher_id == ^publisher_id and ps.message_id == ^message_id

Repo.exists?(query)
end
end
3 changes: 1 addition & 2 deletions lib/postoffice_web/controllers/index_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ defmodule PostofficeWeb.IndexController do
messages_received: Postoffice.count_received_messages(),
messages_published: Postoffice.count_published_messages(),
publishers_failures: Postoffice.count_publishers_failures(),
publishers: Postoffice.count_publishers(),
last_messages: Postoffice.get_last_messages()
publishers: Postoffice.count_publishers()
)
end
end
78 changes: 61 additions & 17 deletions test/postoffice/messaging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ defmodule Postoffice.MessagingTest do
assert Messaging.list_messages() == []
end

test "list_messages/1 returns limited messages list" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)
_second_message = Fixtures.create_message(topic, %{@message_attrs | public_id: "7488a646-e31f-11e4-aace-600308960661"})

assert Messaging.list_messages(1) == [message]
end

test "get_message!/1 returns the message with given id" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)
Expand Down Expand Up @@ -111,23 +119,6 @@ defmodule Postoffice.MessagingTest do
assert message.id == searched_message.id
end

test "message_already_processed returns false if it hasnt been processed for a publisher" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)
publisher = Fixtures.create_publisher(topic)

assert Messaging.message_already_processed(message.id, publisher.id) == false
end

test "message_already_processed returns true if it has been processed for a publisher" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)
publisher = Fixtures.create_publisher(topic)
Fixtures.create_publisher_success(message, publisher)

assert Messaging.message_already_processed(message.id, publisher.id)
end

test "list_pending_messages_for_publisher/2 returns empty if no pending messages for a given publisher" do
topic = Fixtures.create_topic()
publisher = Fixtures.create_publisher(topic)
Expand Down Expand Up @@ -241,5 +232,58 @@ defmodule Postoffice.MessagingTest do

assert Messaging.count_publishers_failures() == 1
end

test "get_publisher! returns asked publisher data" do
topic = Fixtures.create_topic()
fixture_publisher = Fixtures.create_publisher(topic)

publisher = Messaging.get_publisher!(fixture_publisher.id)
assert publisher.id == fixture_publisher.id
assert publisher.target == fixture_publisher.target
assert publisher.active == fixture_publisher.active
assert publisher.type == fixture_publisher.type
end

test "no publisher_success is returned for a non existing message" do
assert Messaging.get_publisher_success_for_message(1) == []
end

test "no publisher_success is returned for a non processed message" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)

assert Messaging.get_publisher_success_for_message(message.id) == []
end

test "publisher_success for a processed messaged is returned" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)
publisher = Fixtures.create_publisher(topic)
_publisher_success = Fixtures.create_publisher_success(message, publisher)

loaded_publisher_success = Messaging.get_publisher_success_for_message(message.id)
assert Kernel.length(loaded_publisher_success) == 1
end

test "no publisher_failures is returned for a non existing message" do
assert Messaging.get_publisher_failures_for_message(1) == []
end

test "no publisher_failures is returned for a non processed message" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)

assert Messaging.get_publisher_failures_for_message(message.id) == []
end

test "publisher_failures for a processed messaged is returned" do
topic = Fixtures.create_topic()
message = Fixtures.create_message(topic)
publisher = Fixtures.create_publisher(topic)
_publisher_failures = Fixtures.create_publishers_failure(message, publisher)

loaded_publisher_failures = Messaging.get_publisher_failures_for_message(message.id)
assert Kernel.length(loaded_publisher_failures) == 1
end
end
end

0 comments on commit 318966f

Please sign in to comment.