Backend: Refactor test fixtures

This commit is contained in:
Theresa Gresch 2020-05-11 14:41:20 +02:00
parent cdadf664ff
commit 7cce763864
7 changed files with 90 additions and 25 deletions

View file

@ -6,6 +6,22 @@ import (
type AlbumMap map[string]Album
func (m AlbumMap) Get(name string) Album {
if result, ok := m[name]; ok {
return result
}
return *NewAlbum(name)
}
func (m AlbumMap) Pointer(name string) *Album {
if result, ok := m[name]; ok {
return &result
}
return NewAlbum(name)
}
var AlbumFixtures = AlbumMap{
"christmas2030": {
ID: 1000000,
@ -57,9 +73,6 @@ var AlbumFixtures = AlbumMap{
},
}
var AlbumFixtureHoliday2030 = AlbumFixtures["holiday-2030"]
var AlbumFixtureBerlin2019 = AlbumFixtures["berlin-2019"]
// CreateAlbumFixtures inserts known entities into the database for testing.
func CreateAlbumFixtures() {
for _, entity := range AlbumFixtures {

View file

@ -6,6 +6,22 @@ import (
type CameraMap map[string]Camera
func (m CameraMap) Get(name string) Camera {
if result, ok := m[name]; ok {
return result
}
return *NewCamera(name, "")
}
func (m CameraMap) Pointer(name string) *Camera {
if result, ok := m[name]; ok {
return &result
}
return NewCamera(name, "")
}
var CameraFixtures = CameraMap{
"apple-iphone-se": {
ID: 1000000,
@ -81,8 +97,6 @@ var CameraFixtures = CameraMap{
},
}
var CameraFixtureEOS6D = CameraFixtures["canon-eos-6d"]
// CreateCameraFixtures inserts known entities into the database for testing.
func CreateCameraFixtures() {
for _, entity := range CameraFixtures {

View file

@ -3,7 +3,7 @@ package entity
type CountryMap map[string]Country
var CountryFixtures = CountryMap{
"apple-iphone-se": {
"germany": {
ID: "de",
CountrySlug: "germany",
CountryName: "Germany",

View file

@ -2,6 +2,22 @@ package entity
type KeywordMap map[string]Keyword
func (m KeywordMap) Get(name string) Keyword {
if result, ok := m[name]; ok {
return result
}
return *NewKeyword(name)
}
func (m KeywordMap) Pointer(name string) *Keyword {
if result, ok := m[name]; ok {
return &result
}
return NewKeyword(name)
}
var KeywordFixtures = KeywordMap{
"bridge": {
ID: 1000000,

View file

@ -2,7 +2,25 @@ package entity
import "time"
var PhotoAlbumFixtures = map[string]PhotoAlbum{
type PhotoAlbumMap map[string]PhotoAlbum
func (m PhotoAlbumMap) Get(name, photoUUID, albumUUID string) PhotoAlbum {
if result, ok := m[name]; ok {
return result
}
return *NewPhotoAlbum(photoUUID, albumUUID)
}
func (m PhotoAlbumMap) Pointer(name, photoUUID, albumUUID string) *PhotoAlbum {
if result, ok := m[name]; ok {
return &result
}
return NewPhotoAlbum(photoUUID, albumUUID)
}
var PhotoAlbumFixtures = PhotoAlbumMap{
"1": {
PhotoUUID: "pt9jtdre2lvl0yh7",
AlbumUUID: "at9lxuqxpogaaba8",
@ -10,7 +28,7 @@ var PhotoAlbumFixtures = map[string]PhotoAlbum{
CreatedAt: time.Date(2020, 3, 6, 2, 6, 51, 0, time.UTC),
UpdatedAt: time.Date(2020, 3, 28, 14, 6, 0, 0, time.UTC),
Photo: PhotoFixtures.Pointer("19800101_000002_D640C559"),
Album: &AlbumFixtureHoliday2030,
Album: AlbumFixtures.Pointer("holiday-2030"),
},
"2": {
PhotoUUID: "pt9jtdre2lvl0y11",
@ -19,13 +37,10 @@ var PhotoAlbumFixtures = map[string]PhotoAlbum{
CreatedAt: time.Date(2020, 2, 6, 2, 6, 51, 0, time.UTC),
UpdatedAt: time.Date(2020, 4, 28, 14, 6, 0, 0, time.UTC),
Photo: PhotoFixtures.Pointer("Photo04"),
Album: &AlbumFixtureBerlin2019,
Album: AlbumFixtures.Pointer("berlin-2019"),
},
}
var PhotoAlbumFixture1 = PhotoAlbumFixtures["1"]
var PhotoAlbumFixture2 = PhotoAlbumFixtures["2"]
// CreatePhotoAlbumFixtures inserts known entities into the database for testing.
func CreatePhotoAlbumFixtures() {
for _, entity := range PhotoAlbumFixtures {

View file

@ -22,6 +22,7 @@ func TestPhotoAlbum_TableName(t *testing.T) {
}
func TestPhotoAlbum_FirstOrCreate(t *testing.T) {
r := PhotoAlbumFixture1.FirstOrCreate()
m := PhotoAlbumFixtures.Get("1", "pt9jtdre2lvl0yh7", "at9lxuqxpogaaba8")
r := m.FirstOrCreate()
assert.Equal(t, "at9lxuqxpogaaba8", r.AlbumUUID)
}

View file

@ -60,14 +60,16 @@ var PhotoFixtures = PhotoMap{
PhotoMonth: 2,
Description: DescriptionFixtures.Get("lake", 1000000),
DescriptionSrc: "",
Camera: &CameraFixtureEOS6D,
Camera: CameraFixtures.Pointer("canon-eos-6d"),
Lens: nil,
Location: nil,
Place: nil,
Links: []Link{},
Keywords: []Keyword{},
Albums: []Album{},
Files: []File{},
Albums: []Album{
AlbumFixtures.Get("holiday-2030"),
},
Files: []File{},
Labels: []PhotoLabel{
LabelFixtures.PhotoLabel(1000000, "flower", 38, "image"),
LabelFixtures.PhotoLabel(1000000, "cake", 38, "manual"),
@ -112,7 +114,7 @@ var PhotoFixtures = PhotoMap{
PhotoMonth: 2,
Description: Description{},
DescriptionSrc: "",
Camera: &CameraFixtureEOS6D,
Camera: CameraFixtures.Pointer("canon-eos-6d"),
Lens: nil,
Location: nil,
Place: nil,
@ -267,14 +269,18 @@ var PhotoFixtures = PhotoMap{
Location: nil,
Place: nil,
Links: []Link{},
Keywords: []Keyword{},
Albums: []Album{},
Files: []File{},
Labels: []PhotoLabel{LabelFixtures.PhotoLabel(1000004, "batchdelete", 20, "image")},
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,
Keywords: []Keyword{
KeywordFixtures.Get("bridge"),
},
Albums: []Album{
AlbumFixtures.Get("berlin-2019"),
},
Files: []File{},
Labels: []PhotoLabel{LabelFixtures.PhotoLabel(1000004, "batchdelete", 20, "image")},
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,
},
"Photo05": {
ID: 1000005,