From 2ac907969ce5421d8d58810369ee422663c69e1a Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Sun, 23 Oct 2016 17:47:31 -0700 Subject: [PATCH] Fix relationships, changesets, tests --- .../20161020144622_create_stripe_account.exs | 5 ++-- test/models/stripe_account_test.exs | 24 ++++++++++++++----- web/models/stripe_account.ex | 11 ++++----- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/priv/repo/migrations/20161020144622_create_stripe_account.exs b/priv/repo/migrations/20161020144622_create_stripe_account.exs index 6564a67ad..a02e98409 100644 --- a/priv/repo/migrations/20161020144622_create_stripe_account.exs +++ b/priv/repo/migrations/20161020144622_create_stripe_account.exs @@ -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 diff --git a/test/models/stripe_account_test.exs b/test/models/stripe_account_test.exs index b26d98a13..098d9bc9e 100644 --- a/test/models/stripe_account_test.exs +++ b/test/models/stripe_account_test.exs @@ -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 diff --git a/web/models/stripe_account.ex b/web/models/stripe_account.ex index ff5bfad2b..1e7f48841 100644 --- a/web/models/stripe_account.ex +++ b/web/models/stripe_account.ex @@ -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 @@ -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