diff --git a/lib/memento/table/table.ex b/lib/memento/table/table.ex index 43b5641..09e17ad 100644 --- a/lib/memento/table/table.ex +++ b/lib/memento/table/table.ex @@ -1,5 +1,7 @@ defmodule Memento.Table do alias Memento.Table.Definition + + require Memento.Error require Memento.Mnesia @@ -188,6 +190,18 @@ defmodule Memento.Table do + + @doc "Same as `create/2`, but raises error on failure." + @spec create!(name, Keyword.t) :: :ok | no_return + def create!(table, opts \\ []) do + table + |> create(opts) + |> handle_for_bang! + end + + + + @doc """ Deletes a Memento Table for Mnesia. @@ -214,12 +228,12 @@ defmodule Memento.Table do @spec info(name, atom) :: any def info(table, key \\ :all) do Definition.validate_table!(table) - Memento.Mnesia.call(:table_info, [table, key]) end + @doc """ Deletes all entries in the given Memento Table. @@ -235,5 +249,18 @@ defmodule Memento.Table do end + + + + # Private Helpers + # --------------- + + + # Handle Result for Bang Methods + defp handle_for_bang!(:ok), do: :ok + defp handle_for_bang!(error) do + Memento.Error.raise_from_code(error) + end + end diff --git a/test/memento/table/table_test.exs b/test/memento/table/table_test.exs index 91e1826..b799146 100644 --- a/test/memento/table/table_test.exs +++ b/test/memento/table/table_test.exs @@ -109,6 +109,38 @@ defmodule Memento.Tests.Table do + describe "#create!" do + @table Tables.User + + test "returns :ok when everything goes as expected" do + assert :ok = Memento.Table.create!(@table) + end + + + test "raises AlreadyExistsError if table already exists" do + assert {:atomic, :ok} = :mnesia.create_table(@table, []) + assert_raise(Memento.AlreadyExistsError, ~r/already exists/i, fn -> + Memento.Table.create!(@table) + end) + end + + + test "raises InvalidOperationError when autoincrement is used with a type other than :ordered_set" do + defmodule MetaApp.AutoincrementBagBang do + use Memento.Table, + attributes: [:id, :name], + autoincrement: true + end + + assert_raise(Memento.InvalidOperationError, ~r/can only be used with.*ordered.set/i, fn -> + Memento.Table.create!(MetaApp.AutoincrementBagBang) + end) + end + end + + + + describe "#delete" do @table Tables.User