2020-05-18 22:18:58 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-07-08 18:09:18 +02:00
|
|
|
"time"
|
2020-11-21 18:08:41 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-18 22:18:58 +02:00
|
|
|
)
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
func TestFirstOrCreateDetails(t *testing.T) {
|
2020-07-08 18:09:18 +02:00
|
|
|
t.Run("not existing details", func(t *testing.T) {
|
|
|
|
details := &Details{PhotoID: 123, Keywords: ""}
|
|
|
|
details = FirstOrCreateDetails(details)
|
2020-05-26 11:00:39 +02:00
|
|
|
|
2020-07-08 18:09:18 +02:00
|
|
|
if details == nil {
|
|
|
|
t.Fatal("details should not be nil")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("existing details", func(t *testing.T) {
|
|
|
|
details := &Details{PhotoID: 1000000}
|
|
|
|
details = FirstOrCreateDetails(details)
|
|
|
|
|
|
|
|
if details == nil {
|
|
|
|
t.Fatal("details should not be nil")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
details := &Details{PhotoID: 0}
|
|
|
|
assert.Nil(t, FirstOrCreateDetails(details))
|
|
|
|
})
|
2020-05-18 22:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_NoKeywords(t *testing.T) {
|
|
|
|
t.Run("no keywords", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: ""}
|
|
|
|
|
|
|
|
assert.Equal(t, true, description.NoKeywords())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.False(t, description.HasKeywords())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
t.Run("keywords", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "test cat dog", Subject: "animals", Artist: "Bender", Notes: "notes", Copyright: "copy"}
|
|
|
|
|
|
|
|
assert.Equal(t, false, description.NoKeywords())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.True(t, description.HasKeywords())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_NoSubject(t *testing.T) {
|
|
|
|
t.Run("no subject", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Subject: ""}
|
|
|
|
|
|
|
|
assert.Equal(t, true, description.NoSubject())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.False(t, description.HasSubject())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
t.Run("subject", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "test cat dog", Subject: "animals", Artist: "Bender", Notes: "notes", Copyright: "copy"}
|
|
|
|
|
|
|
|
assert.Equal(t, false, description.NoSubject())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.True(t, description.HasSubject())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_NoNotes(t *testing.T) {
|
|
|
|
t.Run("no notes", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Notes: ""}
|
|
|
|
|
|
|
|
assert.Equal(t, true, description.NoNotes())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.False(t, description.HasNotes())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
t.Run("notes", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "test cat dog", Subject: "animals", Artist: "Bender", Notes: "notes", Copyright: "copy"}
|
|
|
|
|
|
|
|
assert.Equal(t, false, description.NoNotes())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.True(t, description.HasNotes())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_NoArtist(t *testing.T) {
|
|
|
|
t.Run("no artist", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Artist: ""}
|
|
|
|
|
|
|
|
assert.Equal(t, true, description.NoArtist())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.False(t, description.HasArtist())
|
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
t.Run("artist", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "test cat dog", Subject: "animals", Artist: "Bender", Notes: "notes", Copyright: "copy"}
|
|
|
|
|
|
|
|
assert.Equal(t, false, description.NoArtist())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.True(t, description.HasArtist())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_NoCopyright(t *testing.T) {
|
|
|
|
t.Run("no copyright", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Copyright: ""}
|
|
|
|
|
|
|
|
assert.Equal(t, true, description.NoCopyright())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.False(t, description.HasCopyright())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
t.Run("copyright", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "test cat dog", Subject: "animals", Artist: "Bender", Notes: "notes", Copyright: "copy"}
|
|
|
|
|
|
|
|
assert.Equal(t, false, description.NoCopyright())
|
2021-02-24 11:38:28 +01:00
|
|
|
assert.True(t, description.HasCopyright())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_NoLicense(t *testing.T) {
|
|
|
|
t.Run("no license", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, License: ""}
|
|
|
|
|
|
|
|
assert.Equal(t, true, description.NoLicense())
|
|
|
|
assert.False(t, description.HasLicense())
|
|
|
|
})
|
|
|
|
t.Run("license", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "test cat dog", Subject: "animals", Artist: "Bender", Notes: "notes", License: "copy"}
|
|
|
|
|
|
|
|
assert.Equal(t, false, description.NoLicense())
|
|
|
|
assert.True(t, description.HasLicense())
|
2020-05-18 22:18:58 +02:00
|
|
|
})
|
|
|
|
}
|
2020-07-06 19:15:57 +02:00
|
|
|
|
|
|
|
func TestNewDetails(t *testing.T) {
|
|
|
|
t.Run("add to photo", func(t *testing.T) {
|
2020-12-19 19:15:32 +01:00
|
|
|
p := NewPhoto(true)
|
2020-07-21 10:44:41 +02:00
|
|
|
|
2021-08-19 21:12:38 +02:00
|
|
|
assert.Equal(t, UnknownTitle, p.PhotoTitle)
|
2020-07-21 10:44:41 +02:00
|
|
|
|
2020-07-06 19:15:57 +02:00
|
|
|
d := NewDetails(p)
|
|
|
|
p.Details = &d
|
|
|
|
d.Subject = "Foo Bar"
|
|
|
|
d.Keywords = "Baz"
|
|
|
|
|
|
|
|
err := p.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("PHOTO: %#v", p)
|
|
|
|
t.Logf("DETAILS: %#v", d)
|
|
|
|
})
|
|
|
|
}
|
2020-07-08 18:09:18 +02:00
|
|
|
|
|
|
|
func TestDetails_Create(t *testing.T) {
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
details := Details{PhotoID: 0}
|
|
|
|
|
|
|
|
assert.Error(t, details.Create())
|
|
|
|
})
|
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-07-08 18:36:37 +02:00
|
|
|
details := Details{PhotoID: 1236799955432}
|
2020-07-08 18:09:18 +02:00
|
|
|
|
|
|
|
err := details.Create()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_Save(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-07-08 18:36:37 +02:00
|
|
|
details := Details{PhotoID: 123678955432, UpdatedAt: time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)}
|
2020-07-08 18:09:18 +02:00
|
|
|
initialDate := details.UpdatedAt
|
|
|
|
|
|
|
|
err := details.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
afterDate := details.UpdatedAt
|
|
|
|
|
|
|
|
assert.True(t, afterDate.After(initialDate))
|
|
|
|
})
|
2020-07-09 11:27:44 +02:00
|
|
|
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
details := Details{PhotoID: 0}
|
|
|
|
|
|
|
|
assert.Error(t, details.Save())
|
|
|
|
})
|
2020-07-08 18:09:18 +02:00
|
|
|
}
|
2021-02-24 11:38:28 +01:00
|
|
|
|
|
|
|
func TestDetails_SetKeywords(t *testing.T) {
|
|
|
|
t.Run("no keywords", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: ""}
|
|
|
|
assert.False(t, description.HasKeywords())
|
|
|
|
|
|
|
|
description.SetKeywords("", "manual")
|
|
|
|
assert.False(t, description.HasKeywords())
|
|
|
|
})
|
|
|
|
t.Run("new keywords have no priority", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "cat, brown", KeywordsSrc: SrcManual}
|
|
|
|
assert.Equal(t, "cat, brown", description.Keywords)
|
|
|
|
|
|
|
|
description.SetKeywords("dog", SrcMeta)
|
|
|
|
assert.Equal(t, "cat, brown", description.Keywords)
|
|
|
|
})
|
|
|
|
t.Run("new keywords set", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Keywords: "cat, brown", KeywordsSrc: SrcMeta}
|
|
|
|
assert.Equal(t, "cat, brown", description.Keywords)
|
|
|
|
|
|
|
|
description.SetKeywords("dog", SrcMeta)
|
2021-05-04 15:02:54 +02:00
|
|
|
assert.Equal(t, "brown, cat, dog", description.Keywords)
|
2021-02-24 11:38:28 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_SetSubject(t *testing.T) {
|
|
|
|
t.Run("no subject", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Subject: ""}
|
|
|
|
assert.False(t, description.HasSubject())
|
|
|
|
|
|
|
|
description.SetSubject("", "manual")
|
|
|
|
assert.False(t, description.HasSubject())
|
|
|
|
})
|
|
|
|
t.Run("new subject has no priority", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Subject: "My cat", SubjectSrc: SrcManual}
|
|
|
|
assert.Equal(t, "My cat", description.Subject)
|
|
|
|
|
|
|
|
description.SetSubject("My dog", SrcMeta)
|
|
|
|
assert.Equal(t, "My cat", description.Subject)
|
|
|
|
})
|
|
|
|
t.Run("new subject set", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Subject: "My cat", SubjectSrc: SrcMeta}
|
|
|
|
assert.Equal(t, "My cat", description.Subject)
|
|
|
|
|
|
|
|
description.SetSubject("My dog", SrcMeta)
|
|
|
|
assert.Equal(t, "My dog", description.Subject)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_SetNotes(t *testing.T) {
|
|
|
|
t.Run("no notes", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Notes: ""}
|
|
|
|
assert.False(t, description.HasNotes())
|
|
|
|
|
|
|
|
description.SetNotes("", "manual")
|
|
|
|
assert.False(t, description.HasNotes())
|
|
|
|
})
|
|
|
|
t.Run("new notes has no priority", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Notes: "My old notes", NotesSrc: SrcManual}
|
|
|
|
assert.Equal(t, "My old notes", description.Notes)
|
|
|
|
|
|
|
|
description.SetNotes("My new notes", SrcAuto)
|
|
|
|
assert.Equal(t, "My old notes", description.Notes)
|
|
|
|
})
|
|
|
|
t.Run("new notes set", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Notes: "My old notes", NotesSrc: SrcMeta}
|
|
|
|
assert.Equal(t, "My old notes", description.Notes)
|
|
|
|
|
|
|
|
description.SetNotes("My new notes", SrcManual)
|
|
|
|
assert.Equal(t, "My new notes", description.Notes)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_SetArtist(t *testing.T) {
|
|
|
|
t.Run("no artist", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Artist: ""}
|
|
|
|
assert.False(t, description.HasArtist())
|
|
|
|
|
|
|
|
description.SetArtist("", "manual")
|
|
|
|
assert.False(t, description.HasArtist())
|
|
|
|
})
|
|
|
|
t.Run("new artist has no priority", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Artist: "Hans", ArtistSrc: SrcManual}
|
|
|
|
assert.Equal(t, "Hans", description.Artist)
|
|
|
|
|
|
|
|
description.SetArtist("Maria", SrcAuto)
|
|
|
|
assert.Equal(t, "Hans", description.Artist)
|
|
|
|
})
|
|
|
|
t.Run("new artist set", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Artist: "Hans", ArtistSrc: SrcMeta}
|
|
|
|
assert.Equal(t, "Hans", description.Artist)
|
|
|
|
|
|
|
|
description.SetArtist("Maria", SrcManual)
|
|
|
|
assert.Equal(t, "Maria", description.Artist)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_SetCopyright(t *testing.T) {
|
|
|
|
t.Run("no copyright", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Copyright: ""}
|
|
|
|
assert.False(t, description.HasCopyright())
|
|
|
|
|
|
|
|
description.SetCopyright("", "manual")
|
|
|
|
assert.False(t, description.HasCopyright())
|
|
|
|
})
|
|
|
|
t.Run("new copyright has no priority", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Copyright: "2018", CopyrightSrc: SrcManual}
|
|
|
|
assert.Equal(t, "2018", description.Copyright)
|
|
|
|
|
|
|
|
description.SetCopyright("3000", SrcAuto)
|
|
|
|
assert.Equal(t, "2018", description.Copyright)
|
|
|
|
})
|
|
|
|
t.Run("new copyright set", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, Copyright: "2018", CopyrightSrc: SrcMeta}
|
|
|
|
assert.Equal(t, "2018", description.Copyright)
|
|
|
|
|
|
|
|
description.SetCopyright("3000", SrcManual)
|
|
|
|
assert.Equal(t, "3000", description.Copyright)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDetails_SetLicense(t *testing.T) {
|
|
|
|
t.Run("no license", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, License: ""}
|
|
|
|
assert.False(t, description.HasLicense())
|
|
|
|
|
|
|
|
description.SetLicense("", "manual")
|
|
|
|
assert.False(t, description.HasLicense())
|
|
|
|
})
|
|
|
|
t.Run("new license has no priority", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, License: "old", LicenseSrc: SrcManual}
|
|
|
|
assert.Equal(t, "old", description.License)
|
|
|
|
|
|
|
|
description.SetLicense("new", SrcAuto)
|
|
|
|
assert.Equal(t, "old", description.License)
|
|
|
|
})
|
|
|
|
t.Run("new license set", func(t *testing.T) {
|
|
|
|
description := &Details{PhotoID: 123, License: "old", LicenseSrc: SrcMeta}
|
|
|
|
assert.Equal(t, "old", description.License)
|
|
|
|
|
|
|
|
description.SetLicense("new", SrcManual)
|
|
|
|
assert.Equal(t, "new", description.License)
|
|
|
|
})
|
|
|
|
}
|