Skip to content

Commit

Permalink
Reorganise code and update description
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksy committed Jun 19, 2023
1 parent ca02609 commit 71ca1f9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
24 changes: 12 additions & 12 deletions lib/trento/application/projectors/host_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@ defmodule Trento.HostProjector do
end
)

project(
%HostChecksSelected{
host_id: id,
checks: checks
},
fn multi ->
changeset = HostReadModel.changeset(%HostReadModel{id: id}, %{selected_checks: checks})

Ecto.Multi.update(multi, :host, changeset)
end
)

project(
%HostAddedToCluster{
host_id: id,
Expand Down Expand Up @@ -97,6 +85,18 @@ defmodule Trento.HostProjector do
end
)

project(
%HostChecksSelected{
host_id: id,
checks: checks
},
fn multi ->
changeset = HostReadModel.changeset(%HostReadModel{id: id}, %{selected_checks: checks})

Ecto.Multi.update(multi, :host, changeset)
end
)

project(
%HeartbeatSucceded{host_id: id},
fn multi ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule YourApp.Repo.Migrations.AddSelectedChecksToHosts do
defmodule Trento.Repo.Migrations.AddSelectedChecksToHosts do
use Ecto.Migration

def change do
Expand Down
50 changes: 25 additions & 25 deletions test/trento/application/projectors/host_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,6 @@ defmodule Trento.HostProjectorTest do
%{host_id: host_id}
end

test "should project checks selection to host" do
insert(
:host,
id: host_id = UUID.uuid4(),
selected_checks: []
)

event = %HostChecksSelected{
host_id: host_id,
checks: selected_checks = [Faker.StarWars.character(), Faker.StarWars.character()]
}

ProjectorTestHelper.project(HostProjector, event, "host_projector")
host_projection = Repo.get!(HostReadModel, event.host_id)

assert event.checks == host_projection.selected_checks

assert_broadcast "host_checks_updated",
%{
id: ^host_id,
selected_checks: ^selected_checks
},
1000
end

test "should project a new host when HostRegistered event is received" do
event = build(:host_registered_event)

Expand Down Expand Up @@ -192,6 +167,31 @@ defmodule Trento.HostProjectorTest do
1000
end

test "should update the selected_checks field with a received list of checks when event is received" do
insert(
:host,
id: host_id = UUID.uuid4(),
selected_checks: []
)

event = %HostChecksSelected{
host_id: host_id,
checks: selected_checks = [Faker.StarWars.character(), Faker.StarWars.character()]
}

ProjectorTestHelper.project(HostProjector, event, "host_projector")
host_projection = Repo.get!(HostReadModel, event.host_id)

assert event.checks == host_projection.selected_checks

assert_broadcast "host_checks_updated",
%{
id: ^host_id,
selected_checks: ^selected_checks
},
1000
end

test "should update the heartbeat field to passing status when HeartbeatSucceded is received",
%{host_id: host_id} do
event = %HeartbeatSucceded{
Expand Down

0 comments on commit 71ca1f9

Please sign in to comment.