Backend: Add tests to internal/entity

This commit is contained in:
Theresa Gresch 2020-05-12 13:52:09 +02:00
parent 687c85ed73
commit c0b6eaa9d5
13 changed files with 352 additions and 47 deletions

View File

@ -0,0 +1,34 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestAlbumMap_Get(t *testing.T) {
t.Run("get existing album", func(t *testing.T) {
r := AlbumFixtures.Get("christmas2030")
assert.Equal(t, "at9lxuqxpogaaba7", r.AlbumUUID)
assert.Equal(t, "christmas2030", r.AlbumSlug)
assert.IsType(t, Album{}, r)
})
t.Run("get not existing album", func(t *testing.T) {
r := AlbumFixtures.Get("Fusion 3333")
assert.Equal(t, "fusion-3333", r.AlbumSlug)
assert.IsType(t, Album{}, r)
})
}
func TestAlbumMap_Pointer(t *testing.T) {
t.Run("get existing album pointer", func(t *testing.T) {
r := AlbumFixtures.Pointer("christmas2030")
assert.Equal(t, "at9lxuqxpogaaba7", r.AlbumUUID)
assert.Equal(t, "christmas2030", r.AlbumSlug)
assert.IsType(t, &Album{}, r)
})
t.Run("get not existing album pointer", func(t *testing.T) {
r := AlbumFixtures.Pointer("Fusion 444")
assert.Equal(t, "fusion-444", r.AlbumSlug)
assert.IsType(t, &Album{}, r)
})
}

View File

@ -0,0 +1,34 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCameraMap_Get(t *testing.T) {
t.Run("get existing camera", func(t *testing.T) {
r := CameraFixtures.Get("apple-iphone-se")
assert.Equal(t, uint(0xf4240), r.ID)
assert.Equal(t, "iPhone SE", r.CameraModel)
assert.IsType(t, Camera{}, r)
})
t.Run("get not existing camera", func(t *testing.T) {
r := CameraFixtures.Get("fantasy Cam")
assert.Equal(t, "fantasy-cam", r.CameraSlug)
assert.IsType(t, Camera{}, r)
})
}
func TestCameraMap_Pointer(t *testing.T) {
t.Run("get existing camera pointer", func(t *testing.T) {
r := CameraFixtures.Pointer("apple-iphone-se")
assert.Equal(t, uint(0xf4240), r.ID)
assert.Equal(t, "iPhone SE", r.CameraModel)
assert.IsType(t, &Camera{}, r)
})
t.Run("get not existing camera pointer", func(t *testing.T) {
r := CameraFixtures.Pointer("GOPRO")
assert.Equal(t, "gopro", r.CameraSlug)
assert.IsType(t, &Camera{}, r)
})
}

View File

@ -31,4 +31,14 @@ var DescriptionFixtures = DescriptionMap{
PhotoCopyright: "copy",
PhotoLicense: "MIT",
},
"blacklist": {
PhotoID: 1000001,
PhotoDescription: "photo description blacklist",
PhotoKeywords: "screenshot, info",
PhotoNotes: "notes",
PhotoSubject: "Blacklist",
PhotoArtist: "Hans",
PhotoCopyright: "copy",
PhotoLicense: "MIT",
},
}

View File

@ -0,0 +1,36 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestDescriptionMap_Get(t *testing.T) {
t.Run("get existing description", func(t *testing.T) {
r := DescriptionFixtures.Get("lake", 1000000)
assert.Equal(t, uint(0xf4240), r.PhotoID)
assert.Equal(t, "photo description lake", r.PhotoDescription)
assert.IsType(t, Description{}, r)
})
t.Run("get not existing description", func(t *testing.T) {
r := DescriptionFixtures.Get("fantasy description", 123)
assert.Equal(t, uint(123), r.PhotoID)
assert.Equal(t, "", r.PhotoDescription)
assert.IsType(t, Description{}, r)
})
}
func TestDescriptionMap_Pointer(t *testing.T) {
t.Run("get existing description pointer", func(t *testing.T) {
r := DescriptionFixtures.Pointer("lake", 1000000)
assert.Equal(t, uint(0xf4240), r.PhotoID)
assert.Equal(t, "photo description lake", r.PhotoDescription)
assert.IsType(t, &Description{}, r)
})
t.Run("get not existing description pointer", func(t *testing.T) {
r := DescriptionFixtures.Pointer("fantasy 2", 345)
assert.Equal(t, uint(345), r.PhotoID)
assert.Equal(t, "", r.PhotoDescription)
assert.IsType(t, &Description{}, r)
})
}

View File

@ -0,0 +1,34 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestKeywordMap_Get(t *testing.T) {
t.Run("get existing keyword", func(t *testing.T) {
r := KeywordFixtures.Get("bridge")
assert.Equal(t, uint(1000000), r.ID)
assert.Equal(t, "bridge", r.Keyword)
assert.IsType(t, Keyword{}, r)
})
t.Run("get not existing keyword", func(t *testing.T) {
r := KeywordFixtures.Get("Fusion")
assert.Equal(t, "fusion", r.Keyword)
assert.IsType(t, Keyword{}, r)
})
}
func TestKeywordMap_Pointer(t *testing.T) {
t.Run("get existing keyword pointer", func(t *testing.T) {
r := KeywordFixtures.Pointer("bridge")
assert.Equal(t, uint(1000000), r.ID)
assert.Equal(t, "bridge", r.Keyword)
assert.IsType(t, &Keyword{}, r)
})
t.Run("get not existing keyword pointer", func(t *testing.T) {
r := KeywordFixtures.Pointer("sweets")
assert.Equal(t, "sweets", r.Keyword)
assert.IsType(t, &Keyword{}, r)
})
}

View File

@ -0,0 +1,39 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestLabelMap_Get(t *testing.T) {
t.Run("get existing label", func(t *testing.T) {
r := LabelFixtures.Get("landscape")
assert.Equal(t, "lt9k3pw1wowuy3c2", r.LabelUUID)
assert.Equal(t, "landscape", r.LabelSlug)
assert.IsType(t, Label{}, r)
})
t.Run("get not existing label", func(t *testing.T) {
r := LabelFixtures.Get("monstera")
assert.Equal(t, "monstera", r.LabelSlug)
assert.IsType(t, Label{}, r)
})
}
func TestLabelMap_Pointer(t *testing.T) {
t.Run("get existing label pointer", func(t *testing.T) {
r := LabelFixtures.Pointer("landscape")
assert.Equal(t, "lt9k3pw1wowuy3c2", r.LabelUUID)
assert.Equal(t, "landscape", r.LabelSlug)
assert.IsType(t, &Label{}, r)
})
t.Run("get not existing label pointer", func(t *testing.T) {
r := LabelFixtures.Pointer("monstera Leaf")
assert.Equal(t, "monstera-leaf", r.LabelSlug)
assert.IsType(t, &Label{}, r)
})
}
func TestLabelMap_PhotoLabel(t *testing.T) {
r := LabelFixtures.PhotoLabel(123, "landscape", 25, "")
assert.IsType(t, PhotoLabel{}, r)
}

View File

@ -0,0 +1,36 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestPhotoAlbumMap_Get(t *testing.T) {
t.Run("get existing photoalbum", func(t *testing.T) {
r := PhotoAlbumFixtures.Get("1", "", "")
assert.Equal(t, "at9lxuqxpogaaba8", r.AlbumUUID)
assert.Equal(t, "pt9jtdre2lvl0yh7", r.PhotoUUID)
assert.IsType(t, PhotoAlbum{}, r)
})
t.Run("get not existing photoalbum", func(t *testing.T) {
r := PhotoAlbumFixtures.Get("x", "1234", "5678")
assert.Equal(t, "5678", r.AlbumUUID)
assert.Equal(t, "1234", r.PhotoUUID)
assert.IsType(t, PhotoAlbum{}, r)
})
}
func TestPhotoAlbumMap_Pointer(t *testing.T) {
t.Run("get existing photoalbum pointer", func(t *testing.T) {
r := PhotoAlbumFixtures.Pointer("1", "", "")
assert.Equal(t, "at9lxuqxpogaaba8", r.AlbumUUID)
assert.Equal(t, "pt9jtdre2lvl0yh7", r.PhotoUUID)
assert.IsType(t, &PhotoAlbum{}, r)
})
t.Run("get not existing photoalbum pointer", func(t *testing.T) {
r := PhotoAlbumFixtures.Pointer("xy", "xxx", "yyy")
assert.Equal(t, "yyy", r.AlbumUUID)
assert.Equal(t, "xxx", r.PhotoUUID)
assert.IsType(t, &PhotoAlbum{}, r)
})
}

View File

@ -457,7 +457,7 @@ var PhotoFixtures = PhotoMap{
LensID: 0,
PlaceID: "85d1ea7d382c",
LocationID: "85d1ea7d382c",
LocationSrc: "",
LocationSrc: "manual",
TimeZone: "",
PhotoCountry: "zz",
PhotoYear: 2014,
@ -805,7 +805,7 @@ var PhotoFixtures = PhotoMap{
PhotoCountry: "",
PhotoYear: 0,
PhotoMonth: 0,
Description: DescriptionFixtures.Get("lake", 1000015),
Description: DescriptionFixtures.Get("blacklist", 1000015),
DescriptionSrc: "location",
Camera: nil,
Lens: nil,

View File

@ -0,0 +1,34 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestPhotoMap_Get(t *testing.T) {
t.Run("get existing photo", func(t *testing.T) {
r := PhotoFixtures.Get("19800101_000002_D640C559")
assert.Equal(t, "pt9jtdre2lvl0yh7", r.PhotoUUID)
assert.Equal(t, "19800101_000002_D640C559", r.PhotoName)
assert.IsType(t, Photo{}, r)
})
t.Run("get not existing photo", func(t *testing.T) {
r := PhotoFixtures.Get("TestName")
assert.Equal(t, "TestName", r.PhotoName)
assert.IsType(t, Photo{}, r)
})
}
func TestPhotoMap_Pointer(t *testing.T) {
t.Run("get existing photo pointer", func(t *testing.T) {
r := PhotoFixtures.Pointer("19800101_000002_D640C559")
assert.Equal(t, "pt9jtdre2lvl0yh7", r.PhotoUUID)
assert.Equal(t, "19800101_000002_D640C559", r.PhotoName)
assert.IsType(t, &Photo{}, r)
})
t.Run("get not existing photo pointer", func(t *testing.T) {
r := PhotoFixtures.Pointer("TestName2")
assert.Equal(t, "TestName2", r.PhotoName)
assert.IsType(t, &Photo{}, r)
})
}

View File

@ -18,4 +18,7 @@ func TestPhoto_QualityScore(t *testing.T) {
t.Run("PhotoFixturePhoto07 - score < 3 bit edited", func(t *testing.T) {
assert.Equal(t, 3, PhotoFixtures.Pointer("Photo07").QualityScore())
})
t.Run("PhotoFixturePhoto15 - description with blacklist", func(t *testing.T) {
assert.Equal(t, 2, PhotoFixtures.Pointer("Photo15").QualityScore())
})
}

View File

@ -31,9 +31,18 @@ func TestSavePhotoForm(t *testing.T) {
CameraSrc: "exif",
LensID: uint(6),
LocationID: "1234",
LocationSrc: "geo",
LocationSrc: "manual",
PlaceID: "765",
PhotoCountry: "de",
Description: form.Description{
PhotoID: uint(1000008),
PhotoDescription: "test",
PhotoKeywords: "test cat dog",
PhotoSubject: "animals",
PhotoArtist: "Bender",
PhotoNotes: "notes",
PhotoCopyright: "copy",
PhotoLicense: ""},
}
m := PhotoFixtures["Photo08"]
@ -55,7 +64,7 @@ func TestSavePhotoForm(t *testing.T) {
assert.Equal(t, false, m.PhotoVideo)
assert.Equal(t, float32(7.9999), m.PhotoLat)
assert.NotNil(t, m.EditedAt)
t.Log(m.Description.PhotoKeywords)
})
}
@ -438,22 +447,20 @@ func TestPhoto_SetTitle(t *testing.T) {
func TestPhoto_SetDescription(t *testing.T) {
t.Run("empty description", func(t *testing.T) {
m := PhotoFixtures.Get("Photo15")
assert.Equal(t, "photo description lake", m.Description.PhotoDescription)
assert.Equal(t, "photo description blacklist", m.Description.PhotoDescription)
m.SetDescription("", "manual")
assert.Equal(t, "photo description lake", m.Description.PhotoDescription)
assert.Equal(t, "photo description blacklist", m.Description.PhotoDescription)
})
t.Run("description not from the same source", func(t *testing.T) {
m := PhotoFixtures.Get("Photo15")
assert.Equal(t, "photo description lake", m.Description.PhotoDescription)
assert.Equal(t, "photo description blacklist", m.Description.PhotoDescription)
m.SetDescription("new photo description", "image")
assert.Equal(t, "photo description lake", m.Description.PhotoDescription)
assert.Equal(t, "photo description blacklist", m.Description.PhotoDescription)
})
t.Run("success", func(t *testing.T) {
m := PhotoFixtures.Get("Photo15")
m2 := PhotoFixtures.Get("19800101_000002_D640C559")
assert.Equal(t, "photo description lake", m.Description.PhotoDescription)
assert.Equal(t, "photo description blacklist", m.Description.PhotoDescription)
m.SetDescription("new photo description", "manual")
assert.Equal(t, "photo description lake", m2.Description.PhotoDescription)
assert.Equal(t, "new photo description", m.Description.PhotoDescription)
})
}
@ -486,7 +493,7 @@ func TestPhoto_SetTakenAt(t *testing.T) {
assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt)
assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal)
m.SetTakenAt(time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC),
time.Time{}, "", "location")
time.Time{}, "test", "location")
assert.Equal(t, time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), m.TakenAt)
assert.Equal(t, time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal)
})

View File

@ -0,0 +1,36 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestPlaceMap_Get(t *testing.T) {
t.Run("get existing place", func(t *testing.T) {
r := PlaceFixtures.Get("teotihuacan")
assert.Equal(t, "Teotihuacán", r.LocCity)
assert.Equal(t, "Mexico", r.LocState)
assert.IsType(t, Place{}, r)
})
t.Run("get not existing place", func(t *testing.T) {
r := PlaceFixtures.Get("xxx")
assert.Equal(t, "Unknown", r.LocCity)
assert.Equal(t, "Unknown", r.LocState)
assert.IsType(t, Place{}, r)
})
}
func TestPlaceMap_Pointer(t *testing.T) {
t.Run("get existing place pointer", func(t *testing.T) {
r := PlaceFixtures.Pointer("teotihuacan")
assert.Equal(t, "Teotihuacán", r.LocCity)
assert.Equal(t, "Mexico", r.LocState)
assert.IsType(t, &Place{}, r)
})
t.Run("get not existing place pointer", func(t *testing.T) {
r := PlaceFixtures.Pointer("xxx")
assert.Equal(t, "Unknown", r.LocCity)
assert.Equal(t, "Unknown", r.LocState)
assert.IsType(t, &Place{}, r)
})
}

View File

@ -6,43 +6,45 @@ import (
"github.com/ulule/deepcopier"
)
type Description struct {
PhotoID uint `json:"PhotoID" deepcopier:"skip"`
PhotoDescription string `json:"PhotoDescription"`
PhotoKeywords string `json:"PhotoKeywords"`
PhotoNotes string `json:"PhotoNotes"`
PhotoSubject string `json:"PhotoSubject"`
PhotoArtist string `json:"PhotoArtist"`
PhotoCopyright string `json:"PhotoCopyright"`
PhotoLicense string `json:"PhotoLicense"`
}
// Photo represents a photo edit form.
type Photo struct {
TakenAt time.Time `json:"TakenAt"`
TakenAtLocal time.Time `json:"TakenAtLocal"`
TakenSrc string `json:"TakenSrc"`
TimeZone string `json:"TimeZone"`
PhotoTitle string `json:"PhotoTitle"`
TitleSrc string `json:"TitleSrc"`
Description struct {
PhotoID uint `json:"PhotoID" deepcopier:"skip"`
PhotoDescription string `json:"PhotoDescription"`
PhotoKeywords string `json:"PhotoKeywords"`
PhotoNotes string `json:"PhotoNotes"`
PhotoSubject string `json:"PhotoSubject"`
PhotoArtist string `json:"PhotoArtist"`
PhotoCopyright string `json:"PhotoCopyright"`
PhotoLicense string `json:"PhotoLicense"`
} `json:"Description"`
DescriptionSrc string `json:"DescriptionSrc"`
PhotoFavorite bool `json:"PhotoFavorite"`
PhotoPrivate bool `json:"PhotoPrivate"`
PhotoVideo bool `json:"PhotoVideo"`
PhotoReview bool `json:"PhotoReview"`
PhotoLat float32 `json:"PhotoLat"`
PhotoLng float32 `json:"PhotoLng"`
PhotoAltitude int `json:"PhotoAltitude"`
PhotoIso int `json:"PhotoIso"`
PhotoFocalLength int `json:"PhotoFocalLength"`
PhotoFNumber float32 `json:"PhotoFNumber"`
PhotoExposure string `json:"PhotoExposure"`
CameraID uint `json:"CameraID"`
CameraSrc string `json:"CameraSrc"`
LensID uint `json:"LensID"`
LocationID string `json:"LocationID"`
LocationSrc string `json:"LocationSrc"`
PlaceID string `json:"PlaceID"`
PhotoCountry string `json:"PhotoCountry"`
TakenAt time.Time `json:"TakenAt"`
TakenAtLocal time.Time `json:"TakenAtLocal"`
TakenSrc string `json:"TakenSrc"`
TimeZone string `json:"TimeZone"`
PhotoTitle string `json:"PhotoTitle"`
TitleSrc string `json:"TitleSrc"`
Description Description `json:"Description"`
DescriptionSrc string `json:"DescriptionSrc"`
PhotoFavorite bool `json:"PhotoFavorite"`
PhotoPrivate bool `json:"PhotoPrivate"`
PhotoVideo bool `json:"PhotoVideo"`
PhotoReview bool `json:"PhotoReview"`
PhotoLat float32 `json:"PhotoLat"`
PhotoLng float32 `json:"PhotoLng"`
PhotoAltitude int `json:"PhotoAltitude"`
PhotoIso int `json:"PhotoIso"`
PhotoFocalLength int `json:"PhotoFocalLength"`
PhotoFNumber float32 `json:"PhotoFNumber"`
PhotoExposure string `json:"PhotoExposure"`
CameraID uint `json:"CameraID"`
CameraSrc string `json:"CameraSrc"`
LensID uint `json:"LensID"`
LocationID string `json:"LocationID"`
LocationSrc string `json:"LocationSrc"`
PlaceID string `json:"PlaceID"`
PhotoCountry string `json:"PhotoCountry"`
}
func NewPhoto(m interface{}) (f Photo, err error) {