shiori/internal/database/mysql_test.go
Felipe Martin Garcia 05fee53bd0
fix: saving bookmarks inconsistencies (#500)
* chore: updated go-migrate dependencies

* fix: specify if we're saving bookmarks expecting a creation

up until now the SaveBookmarks method was doing some "magic" to do
"upserts" on the databases, but consistency between engines was scarce
and not knowing if we were expecting saving a new bookmark or updating
an existing one was leading to errors and inconsistencies in logic all
around the place. Now we need to specify a creation boolean when
saving and a differnt query will be make (INSERT vs UPDATE).

* fix(api): using incorrect bookmark for content downlaod

* test(db): added test pipeline for databases

Added functions that will share logic among the engines and will be
called on fresh databases on each test run

* dev: added basic docker-compose for development

* chore: uncommented tests

* ci(test): added mysql service

* typo

* test(mysql): select database after reset

* fix(mysql): ignore empty row errors when parsing tags

* fix(mysql): handle insert errors

* chore: added mysql variables to compose

* ci: explicit mysql service port exposed
2022-10-11 23:47:38 +02:00

60 lines
1.2 KiB
Go

package database
import (
"context"
"log"
"os"
"testing"
"github.com/golang-migrate/migrate/v4"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
func init() {
connString := os.Getenv("SHIORI_TEST_MYSQL_URL")
if connString == "" {
log.Fatal("mysql tests can't run without a MysQL database, set SHIORI_TEST_MYSQL_URL environment variable")
}
}
func mysqlTestDatabaseFactory(ctx context.Context) (DB, error) {
connString := os.Getenv("SHIORI_TEST_MYSQL_URL")
db, err := OpenMySQLDatabase(ctx, connString)
if err != nil {
return nil, err
}
var dbname string
err = db.withTx(ctx, func(tx *sqlx.Tx) error {
err := tx.QueryRow("SELECT DATABASE()").Scan(&dbname)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "DROP DATABASE IF EXISTS "+dbname)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "CREATE DATABASE "+dbname)
return err
})
if err != nil {
return nil, err
}
if _, err := db.Exec("USE " + dbname); err != nil {
return nil, err
}
if err = db.Migrate(); err != nil && !errors.Is(migrate.ErrNoChange, err) {
return nil, err
}
return db, err
}
func TestMysqlsDatabase(t *testing.T) {
testDatabase(t, mysqlTestDatabaseFactory)
}