authelia/internal/storage/sql_provider_schema_test.go
James Elliott 3695aa8140
feat(storage): primary key for all tables and general qol refactoring (#2431)
This is a massive overhaul to the SQL Storage for Authelia. It facilitates a whole heap of utility commands to help manage the database, primary keys, ensures all database requests use a context for cancellations, and paves the way for a few other PR's which improve the database.

Fixes #1337
2021-11-23 20:45:38 +11:00

135 lines
4.8 KiB
Go

package storage
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldReturnErrOnTargetSameAsCurrent(t *testing.T) {
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, true, 1, 1),
fmt.Sprintf(ErrFmtMigrateAlreadyOnTargetVersion, 1, 1))
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, false, 1, 1),
fmt.Sprintf(ErrFmtMigrateAlreadyOnTargetVersion, 1, 1))
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, false, 2, 2),
fmt.Sprintf(ErrFmtMigrateAlreadyOnTargetVersion, 2, 2))
assert.EqualError(t,
schemaMigrateChecks(providerMySQL, false, 1, 1),
fmt.Sprintf(ErrFmtMigrateAlreadyOnTargetVersion, 1, 1))
assert.EqualError(t,
schemaMigrateChecks(providerPostgres, false, 1, 1),
fmt.Sprintf(ErrFmtMigrateAlreadyOnTargetVersion, 1, 1))
}
func TestShouldReturnErrOnUpMigrationTargetVersionLessTHanCurrent(t *testing.T) {
assert.EqualError(t,
schemaMigrateChecks(providerPostgres, true, 0, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateUpTargetLessThanCurrent, 0, testLatestVersion))
assert.NoError(t,
schemaMigrateChecks(providerPostgres, true, testLatestVersion, 0))
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, true, 0, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateUpTargetLessThanCurrent, 0, testLatestVersion))
assert.NoError(t,
schemaMigrateChecks(providerSQLite, true, testLatestVersion, 0))
assert.EqualError(t,
schemaMigrateChecks(providerMySQL, true, 0, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateUpTargetLessThanCurrent, 0, testLatestVersion))
assert.NoError(t,
schemaMigrateChecks(providerMySQL, true, testLatestVersion, 0))
}
func TestMigrationUpShouldReturnErrOnAlreadyLatest(t *testing.T) {
assert.Equal(t,
ErrSchemaAlreadyUpToDate,
schemaMigrateChecks(providerPostgres, true, SchemaLatest, testLatestVersion))
assert.Equal(t,
ErrSchemaAlreadyUpToDate,
schemaMigrateChecks(providerMySQL, true, SchemaLatest, testLatestVersion))
assert.Equal(t,
ErrSchemaAlreadyUpToDate,
schemaMigrateChecks(providerSQLite, true, SchemaLatest, testLatestVersion))
}
func TestShouldReturnErrOnVersionDoesntExits(t *testing.T) {
assert.EqualError(t,
schemaMigrateChecks(providerPostgres, true, SchemaLatest-1, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateUpTargetGreaterThanLatest, SchemaLatest-1, testLatestVersion))
assert.EqualError(t,
schemaMigrateChecks(providerMySQL, true, SchemaLatest-1, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateUpTargetGreaterThanLatest, SchemaLatest-1, testLatestVersion))
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, true, SchemaLatest-1, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateUpTargetGreaterThanLatest, SchemaLatest-1, testLatestVersion))
}
func TestMigrationDownShouldReturnErrOnTargetLessThanPre1(t *testing.T) {
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, false, -4, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateDownTargetLessThanMinimum, -4))
assert.EqualError(t,
schemaMigrateChecks(providerMySQL, false, -2, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateDownTargetLessThanMinimum, -2))
assert.EqualError(t,
schemaMigrateChecks(providerPostgres, false, -2, testLatestVersion),
fmt.Sprintf(ErrFmtMigrateDownTargetLessThanMinimum, -2))
assert.NoError(t,
schemaMigrateChecks(providerPostgres, false, -1, testLatestVersion))
}
func TestMigrationDownShouldReturnErrOnTargetVersionGreaterThanCurrent(t *testing.T) {
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, false, testLatestVersion, 0),
fmt.Sprintf(ErrFmtMigrateDownTargetGreaterThanCurrent, testLatestVersion, 0))
assert.EqualError(t,
schemaMigrateChecks(providerMySQL, false, testLatestVersion, 0),
fmt.Sprintf(ErrFmtMigrateDownTargetGreaterThanCurrent, testLatestVersion, 0))
assert.EqualError(t,
schemaMigrateChecks(providerPostgres, false, testLatestVersion, 0),
fmt.Sprintf(ErrFmtMigrateDownTargetGreaterThanCurrent, testLatestVersion, 0))
}
func TestShouldReturnErrWhenCurrentIsGreaterThanLatest(t *testing.T) {
assert.EqualError(t,
schemaMigrateChecks(providerPostgres, true, SchemaLatest-4, SchemaLatest-5),
fmt.Sprintf(errFmtSchemaCurrentGreaterThanLatestKnown, testLatestVersion))
assert.EqualError(t,
schemaMigrateChecks(providerMySQL, true, SchemaLatest-4, SchemaLatest-5),
fmt.Sprintf(errFmtSchemaCurrentGreaterThanLatestKnown, testLatestVersion))
assert.EqualError(t,
schemaMigrateChecks(providerSQLite, true, SchemaLatest-4, SchemaLatest-5),
fmt.Sprintf(errFmtSchemaCurrentGreaterThanLatestKnown, testLatestVersion))
}
func TestSchemaVersionToString(t *testing.T) {
assert.Equal(t, "unknown", SchemaVersionToString(-2))
assert.Equal(t, "pre1", SchemaVersionToString(-1))
assert.Equal(t, "N/A", SchemaVersionToString(0))
assert.Equal(t, "1", SchemaVersionToString(1))
assert.Equal(t, "2", SchemaVersionToString(2))
}