Adding tests for getting notes by id
This commit is contained in:
@@ -113,6 +113,10 @@ func (s *SQLiteStore) SaveNote(ctx context.Context, note models.Note) (models.No
|
||||
return note, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetNoteByID(ctx context.Context, id int) (models.Note, error) {
|
||||
return models.Note{}, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) validateSchema(ctx context.Context) error {
|
||||
_, err := s.write.ExecContext(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS notes (
|
||||
|
||||
@@ -151,3 +151,64 @@ func TestSaveNote(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNoteByID(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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
store.SaveNote(context.Background(), models.Note{
|
||||
Content: "Test note",
|
||||
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
})
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
id int
|
||||
expectedNote models.Note
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "get existing note",
|
||||
id: 1,
|
||||
expectedNote: models.Note{
|
||||
ID: 1,
|
||||
Content: "Test note",
|
||||
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "get non-existent note",
|
||||
id: 999,
|
||||
expectedNote: models.Note{},
|
||||
expectedError: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
note, err := store.GetNoteByID(context.Background(), tc.id)
|
||||
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 note.ID != tc.expectedNote.ID || note.Content != tc.expectedNote.Content || !note.LastUpdate.Equal(tc.expectedNote.LastUpdate) {
|
||||
t.Errorf("expected note %+v but got %+v", tc.expectedNote, note)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user