OLD | NEW |
(Empty) | |
| 1 package db |
| 2 |
| 3 import ( |
| 4 "fmt" |
| 5 "os" |
| 6 "testing" |
| 7 ) |
| 8 |
| 9 import ( |
| 10 // Using 'require' which is like using 'assert' but causes tests to fail
. |
| 11 assert "github.com/stretchr/testify/require" |
| 12 ) |
| 13 |
| 14 // Connection string to the local MySQL database for testing. |
| 15 const MYSQL_DB_OPEN = "root:%s@tcp(localhost:3306)/skia?parseTime=true" |
| 16 |
| 17 func TestSQLiteVersioning(t *testing.T) { |
| 18 // Initialize without argument to test against SQLite3 |
| 19 Init("") |
| 20 assert.False(t, isMySQL) |
| 21 testDBVersioning(t) |
| 22 } |
| 23 |
| 24 func TestMySQLVersioning(t *testing.T) { |
| 25 // Skip this test unless there is an environment variable with the root |
| 26 // password for the local MySQL instance. |
| 27 password := os.Getenv("MYSQL_TESTING_ROOTPW") |
| 28 if password == "" { |
| 29 t.Skip("Skipping versioning tests against MySQL. Set 'MYSQL_TEST
ING_ROOTPW' to enable tests.") |
| 30 } |
| 31 |
| 32 // Initialize to test against local MySQL db |
| 33 Init(fmt.Sprintf(MYSQL_DB_OPEN, password)) |
| 34 assert.True(t, isMySQL) |
| 35 testDBVersioning(t) |
| 36 } |
| 37 |
| 38 // Test wether the migration scripts execute. |
| 39 func testDBVersioning(t *testing.T) { |
| 40 // get the DB version |
| 41 dbVersion, err := DBVersion() |
| 42 assert.Nil(t, err) |
| 43 maxVersion := MaxDBVersion() |
| 44 |
| 45 // downgrade to 0 |
| 46 err = Migrate(0) |
| 47 assert.Nil(t, err) |
| 48 dbVersion, err = DBVersion() |
| 49 assert.Nil(t, err) |
| 50 assert.Equal(t, 0, dbVersion) |
| 51 |
| 52 // upgrade the the latest version |
| 53 err = Migrate(maxVersion) |
| 54 assert.Nil(t, err) |
| 55 dbVersion, err = DBVersion() |
| 56 assert.Nil(t, err) |
| 57 assert.Equal(t, maxVersion, dbVersion) |
| 58 } |
OLD | NEW |