Update: Finished persistable todo server
This commit is contained in:
parent
09665c3b88
commit
55589a963f
80 changed files with 809 additions and 0 deletions
1
chapter7/todo_cache/test/test_helper.exs
Normal file
1
chapter7/todo_cache/test/test_helper.exs
Normal file
|
|
@ -0,0 +1 @@
|
|||
ExUnit.start()
|
||||
21
chapter7/todo_cache/test/todo_cache_test.exs
Normal file
21
chapter7/todo_cache/test/todo_cache_test.exs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
defmodule TodoCacheTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "server_process" do
|
||||
{:ok, cache} = Todo.Cache.start()
|
||||
bob_pid = Todo.Cache.server_process(cache, "bob")
|
||||
|
||||
assert bob_pid != Todo.Cache.server_process(cache, "alice")
|
||||
assert bob_pid == Todo.Cache.server_process(cache, "bob")
|
||||
end
|
||||
|
||||
test "to-do operations" do
|
||||
{:ok, cache} = Todo.Cache.start()
|
||||
alice = Todo.Cache.server_process(cache, "alice")
|
||||
|
||||
Todo.Server.add_entry(alice, %{date: ~D[2020-12-12], title: "Hello"})
|
||||
entries = Todo.Server.entries(alice, ~D[2020-12-12])
|
||||
|
||||
assert [%{date: ~D[2020-12-12], title: "Hello"}] = entries
|
||||
end
|
||||
end
|
||||
64
chapter7/todo_cache/test/todo_list_test.exs
Normal file
64
chapter7/todo_cache/test/todo_list_test.exs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
defmodule TodoListTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
test "empty list" do
|
||||
assert Todo.List.size(Todo.List.new()) == 0
|
||||
end
|
||||
|
||||
test "entries" do
|
||||
todo_list =
|
||||
Todo.List.new([
|
||||
%{date: ~D[2018-12-19], title: "Dentist"},
|
||||
%{date: ~D[2018-12-20], title: "Shopping"},
|
||||
%{date: ~D[2018-12-19], title: "Movies"}
|
||||
])
|
||||
|
||||
assert Todo.List.size(todo_list) == 3
|
||||
assert todo_list |> Todo.List.entries(~D[2018-12-19]) |> length() == 2
|
||||
assert todo_list |> Todo.List.entries(~D[2018-12-20]) |> length() == 1
|
||||
assert todo_list |> Todo.List.entries(~D[2018-12-21]) |> length() == 0
|
||||
|
||||
titles = todo_list |> Todo.List.entries(~D[2018-12-19]) |> Enum.map(& &1.title)
|
||||
assert ["Dentist", "Movies"] = titles
|
||||
end
|
||||
|
||||
test "add_entry" do
|
||||
todo_list =
|
||||
Todo.List.new()
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-19], title: "Dentist"})
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-20], title: "Shopping"})
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-19], title: "Movies"})
|
||||
|
||||
assert Todo.List.size(todo_list) == 3
|
||||
assert todo_list |> Todo.List.entries(~D[2018-12-19]) |> length() == 2
|
||||
assert todo_list |> Todo.List.entries(~D[2018-12-20]) |> length() == 1
|
||||
assert todo_list |> Todo.List.entries(~D[2018-12-21]) |> length() == 0
|
||||
|
||||
titles = todo_list |> Todo.List.entries(~D[2018-12-19]) |> Enum.map(& &1.title)
|
||||
assert ["Dentist", "Movies"] = titles
|
||||
end
|
||||
|
||||
test "update_entry" do
|
||||
todo_list =
|
||||
Todo.List.new()
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-19], title: "Dentist"})
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-20], title: "Shopping"})
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-19], title: "Movies"})
|
||||
|> Todo.List.update_entry(2, &Map.put(&1, :title, "Updated shopping"))
|
||||
|
||||
assert Todo.List.size(todo_list) == 3
|
||||
assert [%{title: "Updated shopping"}] = Todo.List.entries(todo_list, ~D[2018-12-20])
|
||||
end
|
||||
|
||||
test "delete_entry" do
|
||||
todo_list =
|
||||
Todo.List.new()
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-19], title: "Dentist"})
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-20], title: "Shopping"})
|
||||
|> Todo.List.add_entry(%{date: ~D[2018-12-19], title: "Movies"})
|
||||
|> Todo.List.delete_entry(2)
|
||||
|
||||
assert Todo.List.size(todo_list) == 2
|
||||
assert Todo.List.entries(todo_list, ~D[2018-12-20]) == []
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue