Skip to content

Commit

Permalink
test(journal/ioutils): add simple test to SaveEntries function
Browse files Browse the repository at this point in the history
The test validates that the contenst we save match what we are passing to the function
  • Loading branch information
deadpyxel committed Nov 10, 2023
1 parent e26c914 commit 0fc875d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/ioutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package journal
import (
"encoding/json"
"os"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -94,3 +95,51 @@ func TestLoadEntries(t *testing.T) {
}
})
}

func TestSaveEntries(t *testing.T) {
// Create static time value for testing
staticTime := time.Date(2023, time.November, 5, 12, 0, 0, 0, time.UTC)
// Create some journal entries.
entries := []JournalEntry{
{
ID: "1",
StartTime: staticTime,
Notes: []string{"Note 1", "Note 2"},
},
{
ID: "2",
StartTime: staticTime,
Notes: []string{"Note 3", "Note 4"},
},
}

t.Run("When saving entries file contents match specification", func(t *testing.T) {
// Create a temporary file.
tmpfile, err := os.CreateTemp("", "t.json")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up

// Use the SaveEntries function to write the entries to the temporary file.
if err := SaveEntries(entries, tmpfile.Name()); err != nil {
t.Fatal(err)
}

data, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatal(err)
}

// Load the entries from the file.
var loadedEntries []JournalEntry
if err := json.Unmarshal(data, &loadedEntries); err != nil {
t.Fatal(err)
}

// Check if the loaded entries match the original entries.
if !reflect.DeepEqual(loadedEntries, entries) {
t.Errorf("Expected: %+v, but got: %+v", entries, loadedEntries)
}
})
}

0 comments on commit 0fc875d

Please sign in to comment.