|
| 1 | +defmodule BrasilEmDados.BlogTest do |
| 2 | + use BrasilEmDados.DataCase |
| 3 | + |
| 4 | + alias BrasilEmDados.Blog |
| 5 | + |
| 6 | + describe "posts" do |
| 7 | + alias BrasilEmDados.Blog.Post |
| 8 | + |
| 9 | + @valid_attrs %{body: "some body", title: "some title"} |
| 10 | + @update_attrs %{body: "some updated body", title: "some updated title"} |
| 11 | + @invalid_attrs %{body: nil, title: nil} |
| 12 | + |
| 13 | + def post_fixture(attrs \\ %{}) do |
| 14 | + {:ok, post} = |
| 15 | + attrs |
| 16 | + |> Enum.into(@valid_attrs) |
| 17 | + |> Blog.create_post() |
| 18 | + |
| 19 | + post |
| 20 | + end |
| 21 | + |
| 22 | + test "list_posts/0 returns all posts" do |
| 23 | + post = post_fixture() |
| 24 | + assert Blog.list_posts() == [post] |
| 25 | + end |
| 26 | + |
| 27 | + test "get_post!/1 returns the post with given id" do |
| 28 | + post = post_fixture() |
| 29 | + assert Blog.get_post!(post.id) == post |
| 30 | + end |
| 31 | + |
| 32 | + test "create_post/1 with valid data creates a post" do |
| 33 | + assert {:ok, %Post{} = post} = Blog.create_post(@valid_attrs) |
| 34 | + assert post.body == "some body" |
| 35 | + assert post.title == "some title" |
| 36 | + end |
| 37 | + |
| 38 | + test "create_post/1 with invalid data returns error changeset" do |
| 39 | + assert {:error, %Ecto.Changeset{}} = Blog.create_post(@invalid_attrs) |
| 40 | + end |
| 41 | + |
| 42 | + test "update_post/2 with valid data updates the post" do |
| 43 | + post = post_fixture() |
| 44 | + assert {:ok, %Post{} = post} = Blog.update_post(post, @update_attrs) |
| 45 | + assert post.body == "some updated body" |
| 46 | + assert post.title == "some updated title" |
| 47 | + end |
| 48 | + |
| 49 | + test "update_post/2 with invalid data returns error changeset" do |
| 50 | + post = post_fixture() |
| 51 | + assert {:error, %Ecto.Changeset{}} = Blog.update_post(post, @invalid_attrs) |
| 52 | + assert post == Blog.get_post!(post.id) |
| 53 | + end |
| 54 | + |
| 55 | + test "delete_post/1 deletes the post" do |
| 56 | + post = post_fixture() |
| 57 | + assert {:ok, %Post{}} = Blog.delete_post(post) |
| 58 | + assert_raise Ecto.NoResultsError, fn -> Blog.get_post!(post.id) end |
| 59 | + end |
| 60 | + |
| 61 | + test "change_post/1 returns a post changeset" do |
| 62 | + post = post_fixture() |
| 63 | + assert %Ecto.Changeset{} = Blog.change_post(post) |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments