Adding save tests
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"modernc.org/sqlite"
|
||||
|
||||
"git.hrafn.xyz/aether/notes/internal/models"
|
||||
)
|
||||
|
||||
const sqlSet = `
|
||||
@@ -71,6 +73,10 @@ func (s *SQLiteStore) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) SaveNote(ctx context.Context, note models.Note) (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 (
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.hrafn.xyz/aether/notes/internal/models"
|
||||
"git.hrafn.xyz/aether/notes/internal/store/sqlite"
|
||||
)
|
||||
|
||||
@@ -58,3 +60,94 @@ func TestCreateSQLiteStore(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestSaveNote(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
|
||||
note models.Note
|
||||
expectedNote models.Note
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "new note",
|
||||
note: models.Note{
|
||||
Content: "Test note",
|
||||
LastUpdate: time.Now(),
|
||||
},
|
||||
expectedNote: models.Note{
|
||||
ID: 1,
|
||||
Content: "Test note",
|
||||
LastUpdate: time.Now(),
|
||||
},
|
||||
expectedError: false,
|
||||
}, {
|
||||
name: "new 2 note",
|
||||
note: models.Note{
|
||||
Content: "Test note 2!",
|
||||
LastUpdate: time.Now(),
|
||||
},
|
||||
expectedNote: models.Note{
|
||||
ID: 2,
|
||||
Content: "Test note 2!",
|
||||
LastUpdate: time.Now(),
|
||||
},
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "update note",
|
||||
note: models.Note{
|
||||
ID: 1,
|
||||
Content: "Updated note",
|
||||
LastUpdate: time.Now(),
|
||||
},
|
||||
expectedNote: models.Note{
|
||||
ID: 1,
|
||||
Content: "Updated note",
|
||||
LastUpdate: time.Now(),
|
||||
},
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "update non-existent note",
|
||||
note: models.Note{
|
||||
ID: 999,
|
||||
Content: "This note does not exist",
|
||||
LastUpdate: time.Now(),
|
||||
},
|
||||
expectedNote: models.Note{},
|
||||
expectedError: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
savedNote, err := store.SaveNote(context.Background(), tc.note)
|
||||
if tc.expectedError {
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but got none")
|
||||
}
|
||||
}
|
||||
if !tc.expectedError {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if savedNote.Content != tc.note.Content {
|
||||
t.Errorf("expected content %q but got %q", tc.note.Content, savedNote.Content)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user