Test getting all notes
This commit is contained in:
@@ -128,6 +128,10 @@ func (s *SQLiteStore) GetNoteByID(ctx context.Context, id int) (models.Note, err
|
|||||||
return note, nil
|
return note, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SQLiteStore) GetAllNotes(ctx context.Context) ([]models.Note, error) {
|
||||||
|
return nil, fmt.Errorf("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SQLiteStore) validateSchema(ctx context.Context) error {
|
func (s *SQLiteStore) validateSchema(ctx context.Context) error {
|
||||||
_, err := s.write.ExecContext(ctx, `
|
_, err := s.write.ExecContext(ctx, `
|
||||||
CREATE TABLE IF NOT EXISTS notes (
|
CREATE TABLE IF NOT EXISTS notes (
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ func TestGetNoteByID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
store.SaveNote(context.Background(), models.Note{
|
store.SaveNote(context.Background(), models.Note{
|
||||||
Content: "Test note",
|
Content: "Test note",
|
||||||
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
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