Skip to content

Commit

Permalink
Actually create a Source for every “person”
Browse files Browse the repository at this point in the history
  • Loading branch information
LeakyBucket committed Dec 14, 2017
1 parent 1a35461 commit 3322bad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/edges/events/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ defmodule Edges.Events.Action do
"""

use Ecto.Schema

import Ecto.Changeset
import Ecto.Query

alias __MODULE__
alias Edges.Events.Source
Expand Down Expand Up @@ -66,7 +68,9 @@ defmodule Edges.Events.Action do
attrs
|> Map.get(:person)

Repo.one(Source, person: person) ||
source_query = from(s in Source, where: s.person == ^person)

Repo.one(source_query) ||
Repo.insert!(%Source{person: person})
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Edges.Mixfile do
def project do
[
app: :edges,
version: "0.1.0",
version: "0.2.0",
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
elixirc_paths: elixirc_paths(Mix.env),
Expand Down
43 changes: 43 additions & 0 deletions test/edges/events/action_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Edges.Events.ActionTest do
use ExUnit.Case

alias Edges.Events.Action
alias Edges.Events.Source
alias Edges.Repo

setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Edges.Repo)

{:ok, source} =
%Source{}
|> Source.changeset(%{person: "test source"})
|> Repo.insert()

{:ok, existing_source: source}
end

describe "Action Creation" do
test "creating for an existing person uses that source", %{existing_source: source} do
person = source.person()
source_id = source.id()

{:ok, action} =
%Action{}
|> Action.changeset(%{action: "Favorited", resource_type: "Listing", resource_id: "42", person: person})
|> Repo.insert()

assert ^source_id = action.source_id()
end

test "creating for a new person creates a new source", %{existing_source: source} do
existing_id = source.id()

{:ok, action} =
%Action{}
|> Action.changeset(%{action: "Favorites", resource_type: "Listing", resource_id: "42", person: "new guy"})
|> Repo.insert()

refute existing_id == action.source_id()
end
end
end

0 comments on commit 3322bad

Please sign in to comment.