Current section

Files

Jump to
fsdb test fsdb_test.exs
Raw

test/fsdb_test.exs

defmodule FsdbTest do
use ExUnit.Case
doctest Fsdb
@path "_build/test/.fsdb2"
test "direct crud" do
path = Path.expand(@path)
File.rm_rf!(path)
{:ok, pid} = Fsdb.Server.start_link([path: path])
assert File.exists?(path)
assert :ok == Fsdb.create(pid, "table1")
assert File.exists?(Path.join(path, "table1"))
assert File.exists?(Path.join([path, "table1", "row.id"]))
assert [] == Fsdb.list(pid, "table1")
assert {:not_found, "table1", 1} == Fsdb.fetch(pid, "table1", 1)
assert {1, "row1"} == Fsdb.insert(pid, "table1", "row1")
assert File.exists?(Path.join([path, "table1", "row.000001"]))
assert {1, "row1"} == Fsdb.fetch(pid, "table1", 1)
assert [{1, "row1"}] == Fsdb.list(pid, "table1")
assert {1, "row1+"} == Fsdb.update(pid, "table1", 1, "row1+")
assert {1, "row1+"} == Fsdb.fetch(pid, "table1", 1)
assert {1, "row1+"} == Fsdb.delete(pid, "table1", 1)
assert !File.exists?(Path.join([path, "table1", "row.000001"]))
assert [] == Fsdb.list(pid, "table1")
assert {:not_found, "table1", 1} == Fsdb.fetch(pid, "table1", 1)
#should not reset id file
assert :ok == Fsdb.create(pid, "table1")
assert {2, "row2"} == Fsdb.insert(pid, "table1", "row2")
#bypass the id generator
assert {3, "row3"} == Fsdb.save(pid, "table1", 3, "row3")
#FIXME tuples may not be ID ordered
assert [{2, "row2"}, {3, "row3"}] == Fsdb.list(pid, "table1")
assert :ok == Fsdb.drop(pid, "table1")
assert !File.exists?(Path.join(path, "table1"))
:ok = Fsdb.Server.stop(pid)
#FIXME delay required
#assert !Process.alive?(pid)
end
test "app crud" do
path = Application.fetch_env!(:fsdb, :path)
File.rm_rf!(path)
:ok = Application.start(:fsdb)
#FIXME delay required
assert File.exists?(path)
assert :ok == Fsdb.create("table1")
assert File.exists?(Path.join(path, "table1"))
assert File.exists?(Path.join([path, "table1", "row.id"]))
assert [] == Fsdb.list("table1")
assert {:not_found, "table1", 1} == Fsdb.fetch("table1", 1)
assert {1, "row1"} == Fsdb.insert("table1", "row1")
assert File.exists?(Path.join([path, "table1", "row.000001"]))
assert {1, "row1"} == Fsdb.fetch("table1", 1)
assert [{1, "row1"}] == Fsdb.list("table1")
assert {1, "row1+"} == Fsdb.update("table1", 1, "row1+")
assert {1, "row1+"} == Fsdb.fetch("table1", 1)
assert {1, "row1+"} == Fsdb.delete("table1", 1)
assert !File.exists?(Path.join([path, "table1", "row.000001"]))
assert [] == Fsdb.list("table1")
assert {:not_found, "table1", 1} == Fsdb.fetch("table1", 1)
#bypass the id generator
assert {2, "row2"} == Fsdb.save("table1", 2, "row2")
assert {3, "row3"} == Fsdb.save("table1", 3, "row3")
#FIXME tuples may not be ID ordered
assert [{2, "row2"}, {3, "row3"}] == Fsdb.list("table1")
assert :ok == Fsdb.drop("table1")
assert !File.exists?(Path.join(path, "table1"))
:ok = Application.stop(:fsdb)
end
end