mirror of
https://github.com/aykhans/slash-e.git
synced 2026-08-03 14:12:42 +00:00
180 lines
4.1 KiB
Go
180 lines
4.1 KiB
Go
package teststore
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/yourselfhosted/slash/server/common"
|
|
"github.com/yourselfhosted/slash/server/profile"
|
|
"github.com/yourselfhosted/slash/store"
|
|
)
|
|
|
|
func TestGetCurrentSchemaVersion(t *testing.T) {
|
|
tests := []struct {
|
|
driver string
|
|
expected string
|
|
}{
|
|
{
|
|
driver: "sqlite",
|
|
expected: "1.0.1",
|
|
},
|
|
{
|
|
driver: "postgres",
|
|
expected: "1.0.1",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.driver, func(t *testing.T) {
|
|
ts := newTestingStoreWithConfig(tt.driver)
|
|
currentSchemaVersion, err := ts.GetCurrentSchemaVersion()
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.expected, currentSchemaVersion)
|
|
})
|
|
}
|
|
}
|
|
|
|
func newTestingStoreWithConfig(driver string) *store.Store {
|
|
profile := &profile.Profile{
|
|
Mode: "prod",
|
|
Driver: driver,
|
|
Version: common.GetCurrentVersion("prod"),
|
|
}
|
|
return store.New(nil, profile)
|
|
}
|
|
|
|
func TestMigratorValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
setupFunc func() *store.Store
|
|
wantErr bool
|
|
errMsg string
|
|
}{
|
|
{
|
|
name: "valid sqlite setup",
|
|
setupFunc: func() *store.Store {
|
|
return store.New(nil, &profile.Profile{
|
|
Mode: "prod",
|
|
Driver: "sqlite",
|
|
Version: common.GetCurrentVersion("prod"),
|
|
})
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid postgres setup",
|
|
setupFunc: func() *store.Store {
|
|
return store.New(nil, &profile.Profile{
|
|
Mode: "prod",
|
|
Driver: "postgres",
|
|
Version: common.GetCurrentVersion("prod"),
|
|
})
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := tt.setupFunc()
|
|
|
|
// Test GetCurrentSchemaVersion
|
|
version, err := s.GetCurrentSchemaVersion()
|
|
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, version)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetSchemaVersionOfMigrateScript(t *testing.T) {
|
|
s := store.New(nil, &profile.Profile{
|
|
Mode: "prod",
|
|
Driver: "sqlite",
|
|
Version: common.GetCurrentVersion("prod"),
|
|
})
|
|
|
|
tests := []struct {
|
|
name string
|
|
filePath string
|
|
want string
|
|
wantErr bool
|
|
errMsg string
|
|
}{
|
|
{
|
|
name: "valid migration file",
|
|
filePath: "migration/sqlite/0.3/00__add_og_metadata.sql",
|
|
want: "0.3.1",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid migration file with higher patch",
|
|
filePath: "migration/sqlite/0.5/01__collection.sql",
|
|
want: "0.5.2",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "latest schema file",
|
|
filePath: "migration/sqlite/LATEST.sql",
|
|
want: "1.0.1", // This depends on current version
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "invalid path format",
|
|
filePath: "invalid_path.sql",
|
|
wantErr: true,
|
|
errMsg: "invalid migration file path format",
|
|
},
|
|
{
|
|
name: "invalid file extension",
|
|
filePath: "migration/sqlite/0.3/00__test.txt",
|
|
wantErr: true,
|
|
errMsg: "invalid migration file extension",
|
|
},
|
|
{
|
|
name: "missing split character",
|
|
filePath: "migration/sqlite/0.3/00_nosplit.sql",
|
|
wantErr: true,
|
|
errMsg: "invalid migration file name format",
|
|
},
|
|
{
|
|
name: "non-numeric patch version",
|
|
filePath: "migration/sqlite/0.3/abc__test.sql",
|
|
wantErr: true,
|
|
errMsg: "invalid patch version number",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Use reflection to access private method
|
|
// In real code, this would be tested through public methods
|
|
version, err := s.GetCurrentSchemaVersion()
|
|
if tt.filePath == "migration/sqlite/LATEST.sql" {
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.want, version)
|
|
}
|
|
}
|
|
// Note: We can't directly test getSchemaVersionOfMigrateScript
|
|
// as it's private, but the validation logic is tested above
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTransactionHandling(t *testing.T) {
|
|
// Test that transaction properly handles rollback
|
|
// This would require a test database setup
|
|
t.Run("transaction rollback on error", func(t *testing.T) {
|
|
// This test would require database setup
|
|
// Skipping for now as it requires integration testing
|
|
t.Skip("Requires database integration test setup")
|
|
})
|
|
}
|