defmodule FsdbTest do use ExUnit.Case doctest Fsdb @path "_build/test/.fsdb" test "direct crud" do path = Path.expand(@path) File.rm_rf!(path) {:ok, pid} = Fsdb.start_link(path) assert File.exists?(path) assert !File.exists?(Path.join(path, "table1")) 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 :nf == Fsdb.fetch(pid, "table1", 1) assert :nf == Fsdb.update(pid, "table1", 1, "row1") assert :nf == Fsdb.delete(pid, "table1", 1) assert {:ok, 1} == Fsdb.insert(pid, "table1", "row1") assert File.exists?(Path.join([path, "table1", "row.000001"])) assert {:ok, "row1"} == Fsdb.fetch(pid, "table1", 1) assert [{1, "row1"}] == Fsdb.list(pid, "table1") assert {:ok, "row1"} == Fsdb.update(pid, "table1", 1, "row1+") assert {:ok, "row1+"} == Fsdb.fetch(pid, "table1", 1) assert [{1, "row1+"}] == Fsdb.list(pid, "table1") assert {:ok, "row1+"} == Fsdb.delete(pid, "table1", 1) assert !File.exists?(Path.join([path, "table1", "row.000001"])) assert [] == Fsdb.list(pid, "table1") assert :nf == Fsdb.fetch(pid, "table1", 1) assert :nf == Fsdb.delete(pid, "table1", 1) assert :nf == Fsdb.update(pid, "table1", 1, "row1++") #create should not reset id file assert :ok == Fsdb.create(pid, "table1") assert {:ok, 2} == Fsdb.insert(pid, "table1", "row2") #save bypasses the id generator assert :ok == Fsdb.save(pid, "table1", 5, "row5") assert {:ok, "row2"} == Fsdb.save(pid, "table1", 2, "row2+") #save updates current id to maximum assert {:ok, 6} == Fsdb.insert(pid, "table1", "row6") #tuples may not be ID ordered assert [{2, "row2+"}, {5, "row5"}, {6, "row6"}] == Fsdb.list(pid, "table1") assert :ok == Fsdb.drop(pid, "table1") assert !File.exists?(Path.join(path, "table1")) :ok = Fsdb.stop(pid) end end