From 888c6e3fd7955d0c7bac72c0c5d938182fc1fae6 Mon Sep 17 00:00:00 2001 From: Milan Malfait <38256462+milanmlft@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:51:22 +0100 Subject: [PATCH] Add tests for db initialisation --- todo-go/database/database_test.go | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 todo-go/database/database_test.go diff --git a/todo-go/database/database_test.go b/todo-go/database/database_test.go new file mode 100644 index 0000000..ce045f8 --- /dev/null +++ b/todo-go/database/database_test.go @@ -0,0 +1,32 @@ +package database + +import ( + "os" + "testing" +) + +func TestInitialiseDB(t *testing.T) { + path, err := os.CreateTemp("", "tmp.json") + if err != nil { + panic(err) + } + defer os.Remove(path.Name()) + + InitialiseDB(path.Name()) + result, err := os.ReadFile(path.Name()) + if err != nil { + t.Fatalf("Failed to read from %s; %v", path.Name(), err) + } + resultString := string(result) + want := "[]" + if resultString != want { + t.Fatalf("InitialiseDB() wrote %s; want %s", resultString, want) + } +} + +func TestInitialiseDBErrors(t *testing.T) { + err := InitialiseDB("~/fail.json") + if err == nil { + t.Fatalf("InitialiseDB(\"~/fail.json\") gave nil error; want non-nill error") + } +}