elbow-jason/dgraph_ex

add functions for Repo to accept DgraphEx.Changesets

elbow-jason opened this issue · 3 comments

It would be nice to be able to pipe a changeset directly into repo and decide in the repo whether to send or reject the changes.

This would also remove the need for Changeset.uncast to be included in the changeset function of a model.

also this would keep confusing down about which models have been through changesets and saved already vs models that have simple been uncast.

added to Repo.insert and Repo.update by commit c0bafe8

  test "Repo.insert returns error tuple given an invalid changeset" do
    changes = %{}
    {:error, %Changeset{} = changeset} =
      %Company{}
      |> Company.changeset(changes)
      |> Repo.insert
    assert changeset.errors == [name: :invalid_string, name: :cannot_be_nil]
  end

  test "Repo.insert returns an inserted model if everything is ok" do
    changes = %{name: "Wot"}
    company =
      %Company{}
      |> Company.changeset(changes)
      |> Repo.insert
    assert company.__struct__ == Company
    assert company.name == "Wot"
    assert company._uid_ |> is_binary
  end

  test "Repo.update returns an error tuple for invalid changes" do
    company1 = Repo.insert(%Company{
      name: "Flim",
    })
    changes = %{name: 1}
    {:error, %Changeset{} = changeset} =
      company1
      |> Company.changeset(changes)
      |> Repo.update
    assert changeset.errors == [name: :invalid_string]
  end

  test "Repo.update returns an inserted model for valid changes" do
    company1 = Repo.insert(%Company{
      name: "Flim",
    })
    changes = %{name: "Beefer"}
    company2 =
      company1
      |> Company.changeset(changes)
      |> Repo.update

    assert company2._uid_ == company1._uid_
    assert company2.name == "Beefer"
    company3 = Repo.get(Company, company1._uid_)
    assert company2 == company3
  end