Skip to content

Commit

Permalink
Refactor to use helper functions to keep the code DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
claudealdric committed Aug 30, 2024
1 parent f22bb23 commit 4d49303
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
26 changes: 15 additions & 11 deletions datastore/file_system_data_store_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datastore_test

import (
"slices"
"testing"

"github.com/claudealdric/go-todolist-restful-api-server/datastore"
Expand All @@ -13,13 +12,15 @@ import (
func TestFileSystemDataStore(t *testing.T) {
initialTasks := []models.Task{{1, "Buy groceries"}}
jsonTasks, err := utils.ConvertToJSON(initialTasks)

testutils.AssertNoError(t, err)

t.Run("works with an empty file", func(t *testing.T) {
database, cleanDatabase := testutils.CreateTempFile(t, "")
defer cleanDatabase()

_, err := datastore.NewFileSystemDataStore(database)

testutils.AssertNoError(t, err)
})

Expand All @@ -28,8 +29,11 @@ func TestFileSystemDataStore(t *testing.T) {
defer cleanDatabase()

store, err := datastore.NewFileSystemDataStore(database)

testutils.AssertNoError(t, err)

tasks, err := store.GetTasks()

testutils.AssertNoError(t, err)
testutils.AssertEquals(t, tasks, initialTasks)
})
Expand All @@ -39,31 +43,31 @@ func TestFileSystemDataStore(t *testing.T) {
defer cleanDatabase()

store, err := datastore.NewFileSystemDataStore(database)

testutils.AssertNoError(t, err)

newTask := models.Task{2, "Launder clothes"}
store.CreateTask(newTask)
tasks, err := store.GetTasks()

testutils.AssertNoError(t, err)
if !slices.Contains(tasks, newTask) {
t.Errorf("missing task '%v' from tasks '%v'", newTask, tasks)
}
testutils.AssertContains(t, tasks, newTask)
})

t.Run("DeleteTaskById deletes the selected task", func(t *testing.T) {
database, cleanDatabase := testutils.CreateTempFile(t, string(jsonTasks))
defer cleanDatabase()

store, err := datastore.NewFileSystemDataStore(database)

testutils.AssertNoError(t, err)
store.DeleteTaskById(initialTasks[0].Id)

taskToDelete := initialTasks[0]
store.DeleteTaskById(taskToDelete.Id)
tasks, err := store.GetTasks()

testutils.AssertNoError(t, err)
if slices.Contains(tasks, initialTasks[0]) {
t.Errorf(
"expected task '%+v' to be deleted but isn't",
initialTasks[0],
)
}
testutils.AssertDoesNotContain(t, tasks, taskToDelete)
})

}
7 changes: 7 additions & 0 deletions testutils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ func AssertCalls(t testing.TB, got, want int) {
}
}

func AssertContains[T comparable](t testing.TB, slice []T, element T) {
t.Helper()
if !slices.Contains(slice, element) {
t.Errorf("slice should contain %v but doesn't", element)
}
}

func AssertContentType(t testing.TB, got, want string) {
t.Helper()
if got != want {
Expand Down

0 comments on commit 4d49303

Please sign in to comment.