Backend: Initialize test db with fixtures #84 #129

Fixtures can be found in assets/resources/examples/fixtures.sql and must be maintained manually for now

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-01-06 02:14:17 +01:00
parent fb82749447
commit 01ca94d536
13 changed files with 97 additions and 41 deletions

View file

@ -0,0 +1 @@
INSERT INTO cameras (camera_slug, camera_model, camera_make, camera_notes, created_at, updated_at) VALUES('iphone_5', 'iPhone 5', 'Apple', 'TEST FIXTURE', '2001-01-01', '2001-01-02')

View file

@ -334,12 +334,6 @@ func TestConfig_HttpStaticBuildPath(t *testing.T) {
assert.Equal(t, "/go/src/github.com/photoprism/photoprism/assets/resources/static/build", path)
}
func TestConfig_Db(t *testing.T) {
c := NewTestConfig()
assert.NotNil(t, c.Db())
}
func TestConfig_CloseDb(t *testing.T) {
c := NewTestConfig()
@ -350,7 +344,7 @@ func TestConfig_CloseDb(t *testing.T) {
}
func TestConfig_ClientConfig(t *testing.T) {
c := NewTestConfig()
c := TestConfig()
cc := c.ClientConfig()
assert.NotEmpty(t, cc)

View file

@ -3,6 +3,8 @@ package config
import (
"context"
"errors"
"io/ioutil"
"strings"
"time"
"github.com/jinzhu/gorm"
@ -33,7 +35,7 @@ func (c *Config) DatabaseDsn() string {
// Db returns the db connection.
func (c *Config) Db() *gorm.DB {
if c.db == nil {
log.Fatal("database not initialised.")
log.Fatal("config: database not initialised")
}
return c.db
@ -56,8 +58,6 @@ func (c *Config) CloseDb() error {
func (c *Config) MigrateDb() {
db := c.Db()
// db.LogMode(true)
db.AutoMigrate(
&entity.File{},
&entity.Photo{},
@ -90,11 +90,11 @@ func (c *Config) connectToDatabase(ctx context.Context) error {
dbDsn := c.DatabaseDsn()
if dbDriver == "" {
return errors.New("can't connect: database driver not specified")
return errors.New("config: database driver not specified")
}
if dbDsn == "" {
return errors.New("can't connect: database DSN not specified")
return errors.New("config: database DSN not specified")
}
isTiDB := false
@ -141,3 +141,51 @@ func (c *Config) connectToDatabase(ctx context.Context) error {
c.db = db
return err
}
// DropTables drops all tables in the currently configured database (be careful!).
func (c *Config) DropTables() {
db := c.Db()
db.DropTableIfExists(
&entity.File{},
&entity.Photo{},
&entity.Event{},
&entity.Place{},
&entity.Location{},
&entity.Camera{},
&entity.Lens{},
&entity.Country{},
&entity.Share{},
&entity.Album{},
&entity.PhotoAlbum{},
&entity.Label{},
&entity.Category{},
&entity.PhotoLabel{},
&entity.Keyword{},
&entity.PhotoKeyword{},
)
}
// ImportSQL imports a file to the currently configured database.
func (c *Config) ImportSQL(filename string) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
log.Error(err)
return
}
statements := strings.Split(string(contents), ";\n")
for _, stmt := range statements {
// Skip empty lines and comments
if len(stmt) < 3 || stmt[0] == '#' || stmt[0] == ';' {
continue
}
if _, err := c.Db().CommonDB().Query(stmt); err != nil {
log.Error(err)
}
}
}

View file

@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"sync"
"testing"
_ "github.com/jinzhu/gorm/dialects/mysql"
@ -21,6 +22,7 @@ const (
)
var testConfig *Config
var once sync.Once
func testDataPath(assetsPath string) string {
return assetsPath + "/testdata"
@ -69,9 +71,9 @@ func NewTestParamsError() *Params {
}
func TestConfig() *Config {
if testConfig == nil {
once.Do(func() {
testConfig = NewTestConfig()
}
})
return testConfig
}
@ -85,7 +87,12 @@ func NewTestConfig() *Config {
log.Fatalf("failed init config: %v", err)
}
c.DropTables()
c.MigrateDb()
c.ImportSQL(c.ExamplesPath() + "/fixtures.sql")
return c
}

View file

@ -13,7 +13,7 @@ type Album struct {
ID uint `gorm:"primary_key"`
CoverUUID string `gorm:"type:varbinary(36);"`
AlbumUUID string `gorm:"type:varbinary(36);unique_index;"`
AlbumSlug string `gorm:"index;"`
AlbumSlug string `gorm:"type:varbinary(128);index;"`
AlbumName string `gorm:"type:varchar(128);"`
AlbumDescription string `gorm:"type:text;"`
AlbumNotes string `gorm:"type:text;"`

View file

@ -11,8 +11,8 @@ import (
// Camera model and make (as extracted from UpdateExif metadata)
type Camera struct {
ID uint `gorm:"primary_key"`
CameraSlug string
ID uint `gorm:"primary_key"`
CameraSlug string `gorm:"type:varbinary(128);unique_index;"`
CameraModel string
CameraMake string
CameraType string

View file

@ -59,19 +59,6 @@ func TestNewCamera(t *testing.T) {
})
}
/* TODO find way to initialize db independently from config
func TestCamera_FirstOrCreate(t *testing.T) {
t.Run("model random make Nikon", func(t *testing.T) {
currentTime := time.Now()
modelName := currentTime.String()
camera := NewCamera(modelName, "Nikon")
assert.Equal(t, uint(0x0), camera.ID)
c := config.NewTestConfig()
camera.FirstOrCreate(c.Db())
t.Log(camera.ID)
})
}*/
func TestCamera_String(t *testing.T) {
t.Run("model XXX make Nikon", func(t *testing.T) {
camera := NewCamera("XXX", "Nikon")

View file

@ -14,7 +14,7 @@ var altCountryNames = map[string]string{
type Country struct {
ID string `gorm:"primary_key"`
CountrySlug string
CountrySlug string `gorm:"type:varbinary(128);unique_index;"`
CountryName string
CountryDescription string `gorm:"type:text;"`
CountryNotes string `gorm:"type:text;"`

View file

@ -9,7 +9,7 @@ import (
// Events
type Event struct {
EventUUID string `gorm:"type:varbinary(36);unique_index;"`
EventSlug string
EventSlug string `gorm:"type:varbinary(128);unique_index;"`
EventName string
EventType string
EventDescription string `gorm:"type:text;"`

View file

@ -12,7 +12,7 @@ import (
type Label struct {
ID uint `gorm:"primary_key"`
LabelUUID string `gorm:"type:varbinary(36);unique_index;"`
LabelSlug string `gorm:"type:varchar(128);index;"`
LabelSlug string `gorm:"type:varbinary(128);index;"`
LabelName string `gorm:"type:varchar(128);"`
LabelPriority int
LabelFavorite bool

View file

@ -9,8 +9,8 @@ import (
// Camera lens (as extracted from UpdateExif metadata)
type Lens struct {
ID uint `gorm:"primary_key"`
LensSlug string
ID uint `gorm:"primary_key"`
LensSlug string `gorm:"type:varbinary(128);unique_index;"`
LensModel string
LensMake string
LensType string

View file

@ -12,7 +12,7 @@ import (
func TestTensorFlow_LoadLabelRules(t *testing.T) {
t.Run("labels.txt exists", func(t *testing.T) {
conf := config.NewTestConfig()
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
@ -146,7 +146,7 @@ func TestTensorFlow_Labels(t *testing.T) {
func TestTensorFlow_LoadLabels(t *testing.T) {
t.Run("labels.txt exists", func(t *testing.T) {
conf := config.NewTestConfig()
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
path := conf.TensorFlowModelPath()
@ -167,7 +167,7 @@ func TestTensorFlow_LoadLabels(t *testing.T) {
func TestTensorFlow_LoadModel(t *testing.T) {
t.Run("model path exists", func(t *testing.T) {
conf := config.NewTestConfig()
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
@ -186,7 +186,7 @@ func TestTensorFlow_LoadModel(t *testing.T) {
func TestTensorFlow_LabelRule(t *testing.T) {
t.Run("label.txt exists", func(t *testing.T) {
conf := config.NewTestConfig()
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
@ -208,7 +208,7 @@ func TestTensorFlow_LabelRule(t *testing.T) {
func TestTensorFlow_BestLabels(t *testing.T) {
t.Run("labels not loaded", func(t *testing.T) {
conf := config.NewTestConfig()
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
@ -220,7 +220,7 @@ func TestTensorFlow_BestLabels(t *testing.T) {
assert.Empty(t, result)
})
t.Run("labels loaded", func(t *testing.T) {
conf := config.NewTestConfig()
conf := config.TestConfig()
path := conf.TensorFlowModelPath()
tensorFlow := NewTensorFlow(conf)
tensorFlow.loadLabels(path)

View file

@ -0,0 +1,19 @@
package query
import (
"testing"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/stretchr/testify/assert"
)
// Example for using database fixtures defined in assets/resources/examples/fixtures.sql
func TestCamera_FirstOrCreate(t *testing.T) {
t.Run("iphone_5", func(t *testing.T) {
camera := entity.NewCamera("iPhone 5", "Apple")
c := config.TestConfig()
camera.FirstOrCreate(c.Db())
assert.Equal(t, "TEST FIXTURE", camera.CameraNotes)
})
}