Skip to content

Commit

Permalink
Implemented Table.delete!/2
Browse files Browse the repository at this point in the history
  • Loading branch information
sheharyarn committed Aug 26, 2018
1 parent ea90757 commit ba677f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/memento/errors/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule Memento.Error do

defp do_normalize(reason) do
case reason do
# Mnesia Error Codes
:no_transaction ->
%NoTransactionError{message: "Not inside a Memento Transaction"}

Expand All @@ -59,9 +60,12 @@ defmodule Memento.Error do
{:already_exists, resource} ->
%AlreadyExistsError{message: "#{inspect(resource)} already exists"}


# Custom Error Code - Not Part of Mnesia
{:autoincrement, message} ->
%InvalidOperationError{message: "Autoincrement #{message}"}


# Don't need custom errors for the rest, fallback to MnesiaException
# and raise with Mnesia's description of the error
error ->
Expand Down
12 changes: 12 additions & 0 deletions lib/memento/table/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ defmodule Memento.Table do




@doc "Same as `delete/1`, but raises error on failure."
@spec delete!(name) :: :ok | no_return
def delete!(table) do
table
|> delete
|> handle_for_bang!
end




@doc """
Returns all table information.
Expand Down
22 changes: 21 additions & 1 deletion test/memento/table/table_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,31 @@ defmodule Memento.Tests.Table do

test "deletes an mnesia table from memento definition" do
assert {:atomic, :ok} = :mnesia.create_table(@table, [])
assert :ok = Memento.Table.delete(@table)
assert :ok = Memento.Table.delete!(@table)
end


test "returns :error if the table does not exists" do
assert_raise(Memento.DoesNotExistError, ~r/does not exist/i, fn ->
Memento.Table.delete!(@table)
end)
end
end




describe "#delete!" do
@table Tables.User


test "deletes an mnesia table from memento definition" do
assert {:atomic, :ok} = :mnesia.create_table(@table, [])
assert :ok = Memento.Table.delete!(@table)
end


test "raises errors if the table does not exists" do
assert {:error, {:no_exists, _}} = Memento.Table.delete(@table)
end
end
Expand Down

0 comments on commit ba677f9

Please sign in to comment.