Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMPSITE: To-Do Tests Teardown #33

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/http_server_fixture/data/test-data.json

This file was deleted.

1 change: 0 additions & 1 deletion lib/http_server_test_fixture/mock_data/test-data.json

This file was deleted.

7 changes: 6 additions & 1 deletion lib/to_do/db.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
defmodule ToDo.DB do
@file_path Application.compile_env(:http_server, :file_path, "lib/to_do/data/to_dos.json")

def all, do: File.read!(@file_path) |> JSON.decode!()
def all do
case File.exists?(@file_path) do
true -> File.read!(@file_path) |> JSON.decode!()
false -> %{}
end
end

def save(data), do: File.write(@file_path, JSON.encode!(data), [:read, :write])

Expand Down
7 changes: 7 additions & 0 deletions test/http_server_spec/features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
HOSTNAME = yaml["server"]["hostname"]
PORT = yaml["server"]["port"]
PROTOCOL = yaml["server"]["protocol"]

Spinach.hooks.after_run do |status|
pn = File.expand_path('../../../../lib/http_server_fixture/data/test-data.json', __dir__)
File.delete(pn) if File.exist?(pn)
file_deleted = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ntest-data file deleted\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
puts file_deleted unless File.exist?(pn)
end
6 changes: 4 additions & 2 deletions test/http_to_do_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule HTTPServerTest.ToDo do
alias ToDo.API
use ExUnit.Case, async: true
use ExUnit.Case, async: false
doctest HTTPServer

@file_path Application.compile_env(
Expand All @@ -11,7 +11,9 @@ defmodule HTTPServerTest.ToDo do

setup do
act_arrange_test_todo = JSON.encode!(%{"1" => %{"todo1" => "Act"}, "2" => %{"todo2" => "Arrange"}})
File.write!(@file_path, act_arrange_test_todo)
File.write(@file_path, act_arrange_test_todo)
on_exit(fn -> if File.exists?(@file_path), do: File.rm!(@file_path) end)
:ok
end

test "Can write \"{\"todo3\":\"Assert\"}\" to data file without overwriting previous data" do
Expand Down