Skip to content

Commit

Permalink
chore(books): add get_book!/1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gladear committed Jan 17, 2025
1 parent 08a005c commit 80b64e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/app/lib/app/books.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ defmodule App.Books do

## Database getters

@doc """
Gets a single book.
Raises `Ecto.NoResultsError` if the book does not exist.
"""
@spec get_book!(Book.id()) :: Book.t()
def get_book!(id), do: Repo.get!(Book, id)

@doc """
Gets a single book if it belongs to the user.
Expand Down
13 changes: 13 additions & 0 deletions apps/app/test/app/books_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ defmodule App.BooksTest do

## Database getters

describe "get_book!/1" do
test "returns the book" do
%{id: id} = book_fixture()
assert %{id: ^id} = Books.get_book!(id)
end

test "raises if the book doesn't exist" do
assert_raise Ecto.NoResultsError, fn ->
Books.get_book!(0)
end
end
end

describe "get_book_of_user/2" do
setup :book_with_creator_context

Expand Down

0 comments on commit 80b64e0

Please sign in to comment.