Skip to content

Commit

Permalink
Implemented Table.create!/2
Browse files Browse the repository at this point in the history
  • Loading branch information
sheharyarn committed Aug 26, 2018
1 parent 3bd8d9e commit ea90757
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/memento/table/table.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Memento.Table do
alias Memento.Table.Definition

require Memento.Error
require Memento.Mnesia


Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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

32 changes: 32 additions & 0 deletions test/memento/table/table_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ea90757

Please sign in to comment.