Skip to content

Commit

Permalink
Increase coverage (mercadona#32)
Browse files Browse the repository at this point in the history
* Remove unused Messaging methods

* Remove unused alias

* Add test for messaging counter methods
  • Loading branch information
lonamiaec authored Feb 1, 2020
1 parent 36d2809 commit a8c180b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 145 deletions.
51 changes: 0 additions & 51 deletions lib/postoffice/messaging.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,53 +65,6 @@ defmodule Postoffice.Messaging do
|> Repo.insert()
end

@doc """
Updates a message.
## Examples
iex> update_message(message, %{field: new_value})
{:ok, %Message{}}
iex> update_message(message, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_message(%Message{} = message, attrs) do
message
|> Message.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a Message.
## Examples
iex> delete_message(message)
{:ok, %Message{}}
iex> delete_message(message)
{:error, %Ecto.Changeset{}}
"""
def delete_message(%Message{} = message) do
Repo.delete(message)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking message changes.
## Examples
iex> change_message(message)
%Ecto.Changeset{source: %Message{}}
"""
def change_message(%Message{} = message) do
Message.changeset(message, %{})
end

@doc """
Returns the list of pending messages to be consumed for a topic for a consumer.
Expand Down Expand Up @@ -181,10 +134,6 @@ defmodule Postoffice.Messaging do
Repo.all(query)
end

@spec create_publisher_failure(
:invalid
| %{optional(:__struct__) => none, optional(atom | binary) => any}
) :: any
def create_publisher_failure(attrs \\ %{}) do
%PublisherFailures{}
|> PublisherFailures.changeset(attrs)
Expand Down
1 change: 0 additions & 1 deletion test/postoffice/handlers/http_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Postoffice.Handlers.HttpTest do
alias Postoffice.Adapters.HttpMock
alias Postoffice.Handlers.Http
alias Postoffice.Messaging
alias Postoffice.Messaging.Message

@valid_message_attrs %{
attributes: %{},
Expand Down
215 changes: 122 additions & 93 deletions test/postoffice/messaging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,71 @@ defmodule Postoffice.MessagingTest do
use Postoffice.DataCase

alias Postoffice.Messaging
alias Postoffice.Messaging.Message

@topic_attrs %{
name: "test"
}

@second_topic_attrs %{
name: "test2"
}

@publisher_attrs %{
active: true,
endpoint: "http://fake.endpoint",
initial_message: 0,
type: "http"
}

@disabled_publisher_attrs %{
active: false,
endpoint: "http://fake.endpoint/disabled",
initial_message: 0,
type: "http"
}

@second_publisher_attrs %{
active: true,
endpoint: "http://fake.endpoint2",
initial_message: 0,
type: "http"
}

@valid_attrs %{
attributes: %{},
payload: %{},
public_id: "7488a646-e31f-11e4-aace-600308960662"
}
@invalid_attrs %{attributes: nil, payload: nil, public_id: nil, topic: nil}

def message_fixture(topic, attrs \\ @valid_attrs) do
{:ok, message} = Messaging.create_message(topic, attrs)

message
end

describe "messages" do
alias Postoffice.Messaging.Message

@topic_attrs %{
name: "test"
}

@second_topic_attrs %{
name: "test2"
}

@publisher_attrs %{
active: true,
endpoint: "http://fake.endpoint",
initial_message: 0,
type: "http"
}

@disabled_publisher_attrs %{
active: false,
endpoint: "http://fake.endpoint/disabled",
initial_message: 0,
type: "http"
}

@second_publisher_attrs %{
active: true,
endpoint: "http://fake.endpoint2",
initial_message: 0,
type: "http"
}

@valid_attrs %{
attributes: %{},
payload: %{},
public_id: "7488a646-e31f-11e4-aace-600308960662"
}
@update_attrs %{
attributes: %{},
payload: %{},
public_id: "7488a646-e31f-11e4-aace-600308960668"
}
@invalid_attrs %{attributes: nil, payload: nil, public_id: nil, topic: nil}

def message_fixture(topic, attrs \\ @valid_attrs) do
{:ok, message} = Messaging.create_message(topic, attrs)

message
end
def topic_fixture(attrs \\ @topic_attrs) do
{:ok, topic} = Messaging.create_topic(attrs)
topic
end

def topic_fixture(attrs \\ @topic_attrs) do
{:ok, topic} = Messaging.create_topic(attrs)
topic
end
def publisher_fixture(topic, attrs \\ @publisher_attrs) do
{:ok, publisher} = Messaging.create_publisher(Map.put(attrs, :topic_id, topic.id))
publisher
end

def publisher_fixture(topic, attrs \\ @publisher_attrs) do
{:ok, publisher} = Messaging.create_publisher(Map.put(attrs, :topic_id, topic.id))
publisher
end
def publisher_success_fixture(message, publisher) do
{:ok, _publisher_success} =
Messaging.create_publisher_success(%{message_id: message.id, publisher_id: publisher.id})
end

def publisher_success_fixture(message, publisher) do
{:ok, _publisher_success} =
Messaging.create_publisher_success(%{message_id: message.id, publisher_id: publisher.id})
end
def publishers_failure_fixture(message, publisher) do
{:ok, _publisher_success} =
Messaging.create_publisher_failure(%{message_id: message.id, publisher_id: publisher.id})
end

describe "messages" do
test "list_messages/0 returns all messages" do
topic = topic_fixture()
message = message_fixture(topic)
Expand Down Expand Up @@ -102,38 +101,6 @@ defmodule Postoffice.MessagingTest do
Messaging.create_message(topic_fixture(), @invalid_attrs)
end

test "update_message/2 with valid data updates the message" do
topic = topic_fixture()
message = message_fixture(topic)

assert {:ok, %Message{} = message} = Messaging.update_message(message, @update_attrs)
assert message.attributes == %{}
assert message.payload == %{}
assert message.public_id == "7488a646-e31f-11e4-aace-600308960668"
end

test "update_message/2 with invalid data returns error changeset" do
topic = topic_fixture()
message = message_fixture(topic)

assert {:error, %Ecto.Changeset{}} = Messaging.update_message(message, @invalid_attrs)
end

test "delete_message/1 deletes the message" do
topic = topic_fixture()
message = message_fixture(topic)

assert {:ok, %Message{}} = Messaging.delete_message(message)
assert Messaging.get_message!(message.id) == nil
end

test "change_message/1 returns a message changeset" do
topic = topic_fixture()
message = message_fixture(topic)

assert %Ecto.Changeset{} = Messaging.change_message(message)
end

test "list_topics/0 returns all topics" do
topic = topic_fixture()

Expand Down Expand Up @@ -248,4 +215,66 @@ defmodule Postoffice.MessagingTest do
assert pending_message.topic_id == topic.id
end
end

describe "counters" do
test "count_topics returns 0 if no topic exists" do
assert Messaging.count_topics() == 0
end

test "count_topics returns number of created topics" do
_topic = topic_fixture()

assert Messaging.count_topics() == 1
end

test "count_messages returns 0 if no message exists" do
assert Messaging.count_messages() == 0
end

test "count_messages returns number of created messages" do
topic = topic_fixture()
_message = message_fixture(topic)

assert Messaging.count_messages() == 1
end

test "count_publishers returns 0 if no publisher exists" do
assert Messaging.count_publishers() == 0
end

test "count_publishers returns number of created publishers" do
topic = topic_fixture()
_publisher = publisher_fixture(topic)
_publisher = publisher_fixture(topic, @second_publisher_attrs)

assert Messaging.count_publishers() == 2
end

test "count_published_messages returns 0 if no published message exists" do
assert Messaging.count_published_messages() == 0
end

test "count_published_messages returns number of published messages" do
topic = topic_fixture()
publisher = publisher_fixture(topic)
message = message_fixture(topic)
publisher_success_fixture(message, publisher)

assert Messaging.count_published_messages() == 1
end

test "count_publishers_failures returns 0 if no failed message exists" do
assert Messaging.count_publishers_failures() == 0
end

test "count_publishers_failures returns number of failed messages" do
topic = topic_fixture()
publisher = publisher_fixture(topic)
message = message_fixture(topic)
publishers_failure_fixture(message, publisher)

assert Messaging.count_publishers_failures() == 1
end

end
end

0 comments on commit a8c180b

Please sign in to comment.