Backend: Add tests to internal/entity

This commit is contained in:
Theresa Gresch 2020-05-08 15:46:16 +02:00
parent 842da9f09b
commit aaf1ff64ad
3 changed files with 165 additions and 0 deletions

View file

@ -17,4 +17,5 @@ func CreateTestFixtures() {
CreatePhotoLabelFixtures()
CreateLocationFixtures()
CreatePlaceFixtures()
CreateDescriptionFixtures()
}

View file

@ -399,6 +399,55 @@ var PhotoFixtures = map[string]Photo{
EditedAt: &editTime,
DeletedAt: nil,
},
"Photo08": {
ID: 1000008,
PhotoUUID: "pt9jtdre2lvl0y15",
TakenAt: time.Date(2016, 11, 11, 9, 7, 18, 0, time.UTC),
TakenAtLocal: time.Date(2016, 11, 11, 9, 7, 18, 0, time.UTC),
TakenSrc: "",
PhotoTitle: "Black beach",
TitleSrc: "exif",
PhotoPath: "2016/11",
PhotoName: "",
PhotoQuality: 0,
PhotoResolution: 0,
PhotoFavorite: false,
PhotoPrivate: false,
PhotoStory: false,
PhotoLat: -21.342636,
PhotoLng: 55.466944,
PhotoAltitude: 0,
PhotoIso: 0,
PhotoFocalLength: 0,
PhotoFNumber: 0,
PhotoExposure: "",
CameraID: 0,
CameraSerial: "",
CameraSrc: "",
LensID: 0,
PlaceID: "zz",
LocationID: "",
LocationSrc: "",
TimeZone: "",
PhotoCountry: "zz",
PhotoYear: 2014,
PhotoMonth: 7,
Description: Description{},
DescriptionSrc: "",
Camera: nil,
Lens: nil,
Location: nil,
Place: nil,
Links: []Link{},
Keywords: []Keyword{},
Albums: []Album{},
Files: []File{},
Labels: []PhotoLabel{},
CreatedAt: time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC),
UpdatedAt: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
EditedAt: nil,
DeletedAt: nil,
},
}
var PhotoFixture19800101_000002_D640C559 = PhotoFixtures["19800101_000002_D640C559"]
@ -408,6 +457,7 @@ var PhotoFixturePhoto05 = PhotoFixtures["Photo05"]
var PhotoFixturePhoto03 = PhotoFixtures["Photo03"]
var PhotoFixturePhoto06 = PhotoFixtures["Photo06"]
var PhotoFixturePhoto07 = PhotoFixtures["Photo07"]
var PhotoFixturePhoto08 = PhotoFixtures["Photo08"]
// CreatePhotoFixtures inserts known entities into the database for testing.
func CreatePhotoFixtures() {

View file

@ -0,0 +1,114 @@
package entity
import (
"github.com/photoprism/photoprism/internal/form"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestSavePhotoForm(t *testing.T) {
t.Run("success", func(t *testing.T) {
f := form.Photo{
TakenAt: time.Date(2008, 1, 1, 2, 0, 0, 0, time.UTC),
TakenAtLocal: time.Date(2008, 1, 1, 2, 0, 0, 0, time.UTC),
TakenSrc: "manual",
TimeZone: "test",
PhotoTitle: "Pink beach",
TitleSrc: "manual",
PhotoFavorite: true,
PhotoPrivate: true,
PhotoStory: false,
PhotoLat: 7.9999,
PhotoLng: 8.8888,
PhotoAltitude: 2,
PhotoIso: 5,
PhotoFocalLength: 10,
PhotoFNumber: 3.3,
PhotoExposure: "exposure",
CameraID: uint(3),
CameraSrc: "exif",
LensID: uint(6),
LocationID: "1234",
LocationSrc: "geo",
PlaceID: "765",
PhotoCountry: "de",
}
m := PhotoFixtures["Photo08"]
err := SavePhotoForm(m, f, "places")
if err != nil {
t.Fatal(err)
}
Db().First(&m)
assert.Equal(t, "manual", m.TakenSrc)
assert.Equal(t, "test", m.TimeZone)
assert.Equal(t, "Pink beach", m.PhotoTitle)
assert.Equal(t, "manual", m.TitleSrc)
assert.Equal(t, true, m.PhotoFavorite)
assert.Equal(t, true, m.PhotoPrivate)
assert.Equal(t, false, m.PhotoStory)
assert.Equal(t, float32(7.9999), m.PhotoLat)
assert.NotNil(t, m.EditedAt)
})
}
func TestPhoto_Save(t *testing.T) {
t.Run("new photo", func(t *testing.T) {
photo := Photo{
TakenAt: time.Date(2008, 1, 1, 2, 0, 0, 0, time.UTC),
TakenAtLocal: time.Date(2008, 1, 1, 2, 0, 0, 0, time.UTC),
TakenSrc: "exif",
TimeZone: "UTC",
PhotoTitle: "Black beach",
TitleSrc: "manual",
PhotoFavorite: false,
PhotoPrivate: false,
PhotoStory: true,
PhotoLat: 9.9999,
PhotoLng: 8.8888,
PhotoAltitude: 2,
PhotoIso: 5,
PhotoFocalLength: 10,
PhotoFNumber: 3.3,
PhotoExposure: "exposure",
CameraID: uint(3),
CameraSrc: "exif",
LensID: uint(6),
LocationID: "1234",
LocationSrc: "geo",
PlaceID: "765",
PhotoCountry: "de"}
err := photo.Save()
if err != nil {
t.Fatal("error")
}
})
t.Run("existing photo", func(t *testing.T) {
err := PhotoFixture19800101_000002_D640C559.Save()
if err != nil {
t.Fatal("error")
}
})
}
func TestPhoto_ClassifyLabels(t *testing.T) {
t.Run("new photo", func(t *testing.T) {
m := PhotoFixturePhoto01
Db().Set("gorm:auto_preload", true).Model(&m).Related(&m.Labels)
labels := m.ClassifyLabels()
assert.Empty(t, labels)
})
t.Run("existing photo", func(t *testing.T) {
m := PhotoFixture19800101_000002_D640C559
Db().Set("gorm:auto_preload", true).Model(&m).Related(&m.Labels)
labels := m.ClassifyLabels()
assert.LessOrEqual(t, 2, labels.Len())
})
}