Watch 1 Star 0 Fork 0

linuxr / phoenix-apiElixirMIT

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
api sample by phoenix framework spread retract

Clone or download
user_test.exs 1.75 KB
Copy Edit Web IDE Raw Blame History
汪玉斌 authored 2016-09-14 10:22 . add test for ecto
defmodule PhoenixApi.UserTest do
use PhoenixApi.ModelCase
alias PhoenixApi.User
alias PhoenixApi.Repo
@valid_attrs %{age: 42, email: "some content", name: "some content"}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
end
test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end
test "insert data" do
changeset = User.changeset(%User{}, @valid_attrs)
assert {:ok, _user} = Repo.insert(changeset)
end
test "delete data" do
# insert for delete
changeset = User.changeset(%User{}, @valid_attrs)
{:ok, user} = Repo.insert(changeset)
# delete
assert {:ok, _user} = Repo.delete(user)
end
test "found data" do
# insert for search
changeset = User.changeset(%User{}, @valid_attrs)
{:ok, user} = Repo.insert(changeset)
# found data
search_user = Repo.get(User, user.id)
assert search_user.name === @valid_attrs[:name]
assert search_user.email === @valid_attrs[:email]
assert search_user.age === @valid_attrs[:age]
end
test "not found data" do
# not found data
not_found_user = Repo.get(User, -1)
refute not_found_user
end
test "change data" do
# insert for search
changeset = User.changeset(%User{}, @valid_attrs)
{:ok, user} = Repo.insert(changeset)
# before change
assert user.name === @valid_attrs[:name]
assert user.email === @valid_attrs[:email]
assert user.age === @valid_attrs[:age]
# change
changeset = User.changeset(user, %{name: "change name"})
assert {:ok, new_user} = Repo.update(changeset)
# after change
assert new_user.name === "change name"
end
end

Comment ( 0 )

Sign in for post a comment

Elixir
1
https://gitee.com/wangyubin/phoenix-api.git
git@gitee.com:wangyubin/phoenix-api.git
wangyubin
phoenix-api
phoenix-api
master

Help Search