diff --git a/lib/radiator/event_store.ex b/lib/radiator/event_store.ex index d9baae21..157c35f2 100644 --- a/lib/radiator/event_store.ex +++ b/lib/radiator/event_store.ex @@ -12,7 +12,8 @@ defmodule Radiator.EventStore do create_event_data(%{ data: AbstractEvent.payload(event), event_type: AbstractEvent.event_type(event), - uuid: event.event_id + uuid: event.event_id, + user_id: event.user_id }) event diff --git a/lib/radiator/event_store/event_data.ex b/lib/radiator/event_store/event_data.ex index e85ae4b7..90286a5d 100644 --- a/lib/radiator/event_store/event_data.ex +++ b/lib/radiator/event_store/event_data.ex @@ -19,7 +19,7 @@ defmodule Radiator.EventStore.EventData do @doc false def changeset(event, attrs) do event - |> cast(attrs, [:uuid, :event_type, :data]) - |> validate_required([:uuid, :event_type]) + |> cast(attrs, [:uuid, :event_type, :data, :user_id]) + |> validate_required([:uuid, :event_type, :user_id]) end end diff --git a/lib/radiator/outline/event/node_content_changed_event.ex b/lib/radiator/outline/event/node_content_changed_event.ex index 30e5c435..5c3b027b 100644 --- a/lib/radiator/outline/event/node_content_changed_event.ex +++ b/lib/radiator/outline/event/node_content_changed_event.ex @@ -1,5 +1,5 @@ defmodule Radiator.Outline.Event.NodeContentChangedEvent do @moduledoc false - defstruct [:event_id, :node_id, :content] + defstruct [:event_id, :node_id, :content, :user_id] end diff --git a/lib/radiator/outline/event/node_deleted_event.ex b/lib/radiator/outline/event/node_deleted_event.ex index 2bd1012e..78e4ed8c 100644 --- a/lib/radiator/outline/event/node_deleted_event.ex +++ b/lib/radiator/outline/event/node_deleted_event.ex @@ -1,4 +1,4 @@ defmodule Radiator.Outline.Event.NodeDeletedEvent do @moduledoc false - defstruct [:event_id, :node_id] + defstruct [:event_id, :node_id, :user_id] end diff --git a/lib/radiator/outline/event/node_inserted_event.ex b/lib/radiator/outline/event/node_inserted_event.ex index b8569087..78e2a5a4 100644 --- a/lib/radiator/outline/event/node_inserted_event.ex +++ b/lib/radiator/outline/event/node_inserted_event.ex @@ -1,5 +1,5 @@ defmodule Radiator.Outline.Event.NodeInsertedEvent do @moduledoc false - defstruct [:event_id, :node] + defstruct [:event_id, :node, :user_id] end diff --git a/lib/radiator/outline/event/node_moved_event.ex b/lib/radiator/outline/event/node_moved_event.ex index 01636cac..41c6abe8 100644 --- a/lib/radiator/outline/event/node_moved_event.ex +++ b/lib/radiator/outline/event/node_moved_event.ex @@ -1,4 +1,4 @@ defmodule Radiator.Outline.Event.NodeMovedEvent do @moduledoc false - defstruct [:event_id, :node_id, :parent_id, :prev_id] + defstruct [:event_id, :node_id, :parent_id, :prev_id, :user_id] end diff --git a/lib/radiator/outline/event_consumer.ex b/lib/radiator/outline/event_consumer.ex index 8ea8a6f5..8b2c345e 100644 --- a/lib/radiator/outline/event_consumer.ex +++ b/lib/radiator/outline/event_consumer.ex @@ -24,8 +24,9 @@ defmodule Radiator.Outline.EventConsumer do {:noreply, [], state} end - defp process_command(%InsertNodeCommand{payload: payload} = command) do + defp process_command(%InsertNodeCommand{payload: payload, user_id: user_id} = command) do payload + |> Map.merge(%{"user_id" => user_id}) |> Outline.insert_node() |> handle_insert_node_result(command) end @@ -37,7 +38,7 @@ defmodule Radiator.Outline.EventConsumer do end defp handle_insert_node_result({:ok, node}, command) do - %NodeInsertedEvent{node: node, event_id: command.event_id} + %NodeInsertedEvent{node: node, event_id: command.event_id, user_id: command.user_id} |> EventStore.persist_event() |> Dispatch.broadcast() @@ -49,8 +50,13 @@ defmodule Radiator.Outline.EventConsumer do :error end - def handle_change_node_content_result({:ok, node}, command) do - %NodeContentChangedEvent{node_id: node.id, event_id: command.event_id} + def handle_change_node_content_result({:ok, node}, %ChangeNodeContentCommand{} = command) do + %NodeContentChangedEvent{ + node_id: node.id, + content: node.content, + user_id: command.user_id, + event_id: command.event_id + } |> EventStore.persist_event() |> Dispatch.broadcast() diff --git a/test/radiator/event_store_test.exs b/test/radiator/event_store_test.exs index 3128255b..bfc28f64 100644 --- a/test/radiator/event_store_test.exs +++ b/test/radiator/event_store_test.exs @@ -6,6 +6,7 @@ defmodule Radiator.EventStoreTest do describe "event_data" do alias Radiator.EventStore.EventData + alias Radiator.AccountsFixtures import Radiator.EventStoreFixtures @invalid_attrs %{data: nil, uuid: nil, event_type: nil} @@ -21,16 +22,20 @@ defmodule Radiator.EventStoreTest do end test "create_event/1 with valid data creates a event" do + user = AccountsFixtures.user_fixture() + valid_attrs = %{ data: %{}, - uuid: "7488a646-e31f-11e4-aace-600308960662", - event_type: "some event_type" + uuid: Ecto.UUID.generate(), + event_type: "some event_type", + user_id: user.id } assert {:ok, %EventData{} = event} = EventStore.create_event_data(valid_attrs) assert event.data == %{} - assert event.uuid == "7488a646-e31f-11e4-aace-600308960662" - assert event.event_type == "some event_type" + assert event.uuid == valid_attrs.uuid + assert event.event_type == valid_attrs.event_type + assert event.user_id == valid_attrs.user_id end test "create_event/1 with invalid data returns error changeset" do diff --git a/test/radiator/outline/event_consumer_test.exs b/test/radiator/outline/event_consumer_test.exs index 6269cdb7..84b8500f 100644 --- a/test/radiator/outline/event_consumer_test.exs +++ b/test/radiator/outline/event_consumer_test.exs @@ -1,40 +1,56 @@ defmodule Radiator.Outline.EventConsumerTest do + alias Radiator.Outline.NodeRepository use Radiator.DataCase alias Radiator.AccountsFixtures - alias Radiator.Outline.Command + alias Radiator.EventStore + alias Radiator.Outline.{Command, Dispatch, EventConsumer, EventProducer, NodeRepository} alias Radiator.Outline.Command.InsertNodeCommand - alias Radiator.Outline.Dispatch alias Radiator.Outline.Event.NodeInsertedEvent - alias Radiator.Outline.EventConsumer - alias Radiator.Outline.EventProducer alias Radiator.PodcastFixtures describe "handle_events/2" do - test "insert_node" do - episode = PodcastFixtures.episode_fixture() + setup :prepare_outline + test "insert_node stores a node", %{episode: episode, user: user, event_id: event_id} do attributes = %{ "title" => "Node Title", "content" => "Node Content", "episode_id" => episode.id } - user_id = "user_id" - event_id = Ecto.UUID.generate() - - command = Command.build("insert_node", attributes, user_id, event_id) + num_nodes = NodeRepository.count_nodes_by_episode(episode.id) + command = Command.build("insert_node", attributes, user.id, event_id) EventConsumer.handle_events([command], 0, nil) + # assert a node has been created - # assert an event has been created (and be stored) + assert num_nodes + 1 == NodeRepository.count_nodes_by_episode(episode.id) end - test "handles previously enqueued events" do - producer = start_supervised!({EventProducer, name: TestEventProducer}) + test "insert_node creates and stores an event", %{ + episode: episode, + user: user, + event_id: event_id + } do + new_content = "Node Content" + + attributes = %{ + "title" => "Node Title", + "content" => new_content, + "episode_id" => episode.id + } - episode = PodcastFixtures.episode_fixture() - user = AccountsFixtures.user_fixture() - event_id = Ecto.UUID.generate() + command = Command.build("insert_node", attributes, user.id, event_id) + EventConsumer.handle_events([command], 0, nil) + event = EventStore.list_event_data() |> hd() + + assert event.event_type == "NodeInsertedEvent" + assert event.user_id == user.id + assert event.data["content"] == new_content + end + + test "handles previously enqueued events", %{episode: episode, user: user, event_id: event_id} do + producer = start_supervised!({EventProducer, name: TestEventProducer}) command = %InsertNodeCommand{ event_id: event_id, @@ -47,7 +63,6 @@ defmodule Radiator.Outline.EventConsumerTest do } Dispatch.subscribe(episode.id) - EventProducer.enqueue(producer, command) start_supervised!( @@ -57,4 +72,11 @@ defmodule Radiator.Outline.EventConsumerTest do assert_receive(%NodeInsertedEvent{}, 1000) end end + + def prepare_outline(_) do + episode = PodcastFixtures.episode_fixture() + user = AccountsFixtures.user_fixture() + event_id = Ecto.UUID.generate() + %{episode: episode, user: user, event_id: event_id} + end end diff --git a/test/support/fixtures/event_store_fixtures.ex b/test/support/fixtures/event_store_fixtures.ex index 8b8d9da5..eac8d4d9 100644 --- a/test/support/fixtures/event_store_fixtures.ex +++ b/test/support/fixtures/event_store_fixtures.ex @@ -3,17 +3,21 @@ defmodule Radiator.EventStoreFixtures do This module defines test helpers for creating entities via the `Radiator.EventStore` context. """ + alias Radiator.AccountsFixtures @doc """ Generate a event data. """ def event_data_fixture(attrs \\ %{}) do + user = AccountsFixtures.user_fixture() + {:ok, event} = attrs |> Enum.into(%{ data: %{}, event_type: "some event_type", - uuid: "7488a646-e31f-11e4-aace-600308960662" + uuid: Ecto.UUID.generate(), + user_id: user.id }) |> Radiator.EventStore.create_event_data()