Skip to content

Commit

Permalink
Fix relationships, changesets, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Oct 24, 2016
1 parent 7e7f246 commit 2ac9079
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
5 changes: 3 additions & 2 deletions priv/repo/migrations/20161020144622_create_stripe_account.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ defmodule CodeCorps.Repo.Migrations.CreateStripeAccount do
add :details_submitted, :boolean
add :display_name, :string
add :email, :string
add :id_from_stripe, :string
add :id_from_stripe, :string, null: false
add :managed, :boolean
add :support_email, :string
add :support_phone, :string
add :support_url, :string
add :transfers_enabled, :boolean

add :organization_id, references(:organizations)
add :organization_id, references(:organizations), null: false

timestamps()
end

create unique_index(:stripe_accounts, [:id_from_stripe])
create unique_index(:stripe_accounts, [:organization_id])
end
end
24 changes: 18 additions & 6 deletions test/models/stripe_account_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ defmodule CodeCorps.StripeAccountTest do

alias CodeCorps.StripeAccount

@valid_attrs %{}
@valid_attrs %{
id_from_stripe: "abc123"
}

@invalid_attrs %{}

describe "changeset" do
test "with valid attributes" do
changeset = StripeAccount.changeset(%StripeAccount{}, @valid_attrs)
assert changeset.valid?
end
test "changeset with valid attributes" do
organization_id = insert(:organization).id

changes = Map.merge(@valid_attrs, %{organization_id: organization_id})
changeset = StripeAccount.create_changeset(%StripeAccount{}, changes)
assert changeset.valid?
end

test "changeset with invalid attributes" do
changeset = StripeAccount.create_changeset(%StripeAccount{}, @invalid_attrs)
refute changeset.valid?

assert changeset.errors[:id_from_stripe] == {"can't be blank", []}
assert changeset.errors[:organization_id] == {"can't be blank", []}
end
end
11 changes: 5 additions & 6 deletions web/models/stripe_account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule CodeCorps.StripeAccount do
field :details_submitted, :boolean
field :display_name, :string
field :email, :string
field :id_from_stripe, :string
field :id_from_stripe, :string, null: false
field :managed, :boolean
field :support_email, :string
field :support_phone, :string
Expand All @@ -26,11 +26,10 @@ defmodule CodeCorps.StripeAccount do
timestamps()
end

@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
def create_changeset(struct, params \\ %{}) do
struct
|> cast(params, [])
|> cast(params, [:id_from_stripe, :organization_id])
|> validate_required([:id_from_stripe, :organization_id])
|> assoc_constraint(:organization)
end
end

0 comments on commit 2ac9079

Please sign in to comment.