Test getting all notes
This commit is contained in:
@@ -167,7 +167,7 @@ func TestGetNoteByID(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
store.SaveNote(context.Background(), models.Note{
|
||||
Content: "Test note",
|
||||
Content: "Test note",
|
||||
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
})
|
||||
|
||||
@@ -212,3 +212,97 @@ func TestGetNoteByID(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllNotes(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "test.db")
|
||||
store, err := sqlite.NewSQLiteStore(context.Background(), dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create SQLite store: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
store.Close()
|
||||
if _, err := os.Stat(dbPath); !errors.Is(err, os.ErrNotExist) {
|
||||
if err := os.Remove(dbPath); err != nil {
|
||||
t.Errorf("failed to clean up database file: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
expectedNotes []models.Note
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "get all notes when empty",
|
||||
expectedNotes: []models.Note{},
|
||||
expectedError: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
notes, err := store.GetAllNotes(context.Background())
|
||||
if tc.expectedError {
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but got none")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if len(notes) != len(tc.expectedNotes) {
|
||||
t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
store.SaveNote(context.Background(), models.Note{
|
||||
Content: "Test note 1",
|
||||
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
})
|
||||
store.SaveNote(context.Background(), models.Note{
|
||||
Content: "Test note 2",
|
||||
LastUpdate: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
|
||||
})
|
||||
|
||||
testcases = []struct {
|
||||
name string
|
||||
expectedNotes []models.Note
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "get all notes when not empty",
|
||||
expectedNotes: []models.Note{
|
||||
{
|
||||
ID: 1,
|
||||
Content: "Test note 1",
|
||||
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Content: "Test note 2",
|
||||
LastUpdate: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
},
|
||||
expectedError: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
notes, err := store.GetAllNotes(context.Background())
|
||||
if tc.expectedError {
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but got none")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if len(notes) != len(tc.expectedNotes) {
|
||||
t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user