Backend: Add unit tests for internal/entity
This commit is contained in:
parent
4196420dc7
commit
d6c910c16b
5 changed files with 129 additions and 12 deletions
|
@ -22,18 +22,48 @@ func TestPhotoAlbum_TableName(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestFirstOrCreatePhotoAlbum(t *testing.T) {
|
||||
model := PhotoAlbumFixtures.Get("1", "pt9jtdre2lvl0yh7", "at9lxuqxpogaaba8")
|
||||
result := FirstOrCreatePhotoAlbum(&model)
|
||||
t.Run("existing album", func(t *testing.T) {
|
||||
model := PhotoAlbumFixtures.Get("1", "pt9jtdre2lvl0yh7", "at9lxuqxpogaaba8")
|
||||
result := FirstOrCreatePhotoAlbum(&model)
|
||||
|
||||
if result == nil {
|
||||
t.Fatal("result should not be nil")
|
||||
}
|
||||
if result == nil {
|
||||
t.Fatal("result should not be nil")
|
||||
}
|
||||
|
||||
if result.AlbumUID != model.AlbumUID {
|
||||
t.Errorf("AlbumUID should be the same: %s %s", result.AlbumUID, model.AlbumUID)
|
||||
}
|
||||
if result.AlbumUID != model.AlbumUID {
|
||||
t.Errorf("AlbumUID should be the same: %s %s", result.AlbumUID, model.AlbumUID)
|
||||
}
|
||||
|
||||
if result.PhotoUID != model.PhotoUID {
|
||||
t.Errorf("PhotoUID should be the same: %s %s", result.PhotoUID, model.PhotoUID)
|
||||
}
|
||||
if result.PhotoUID != model.PhotoUID {
|
||||
t.Errorf("PhotoUID should be the same: %s %s", result.PhotoUID, model.PhotoUID)
|
||||
}
|
||||
})
|
||||
t.Run("not yet existing album", func(t *testing.T) {
|
||||
model := &PhotoAlbum{}
|
||||
result := FirstOrCreatePhotoAlbum(model)
|
||||
|
||||
if result == nil {
|
||||
t.Fatal("result should not be nil")
|
||||
}
|
||||
|
||||
if result.AlbumUID != model.AlbumUID {
|
||||
t.Errorf("AlbumUID should be the same: %s %s", result.AlbumUID, model.AlbumUID)
|
||||
}
|
||||
|
||||
if result.PhotoUID != model.PhotoUID {
|
||||
t.Errorf("PhotoUID should be the same: %s %s", result.PhotoUID, model.PhotoUID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPhotoAlbum_Save(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
p := PhotoAlbum{}
|
||||
|
||||
err := p.Create()
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -64,4 +64,28 @@ func TestPhotoLabel_Save(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
t.Run("photo not nil and label not nil", func(t *testing.T) {
|
||||
label := &Label{LabelName: "ToBeRestored"}
|
||||
photo := &Photo{}
|
||||
|
||||
photoLabel := PhotoLabel{Photo: photo, Label: label}
|
||||
err := photoLabel.Save()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPhotoLabel_Update(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
photoLabel := PhotoLabel{LabelID: 555, PhotoID: 888}
|
||||
assert.Equal(t, uint(0x22b), photoLabel.LabelID)
|
||||
|
||||
err := photoLabel.Update("LabelID", 8)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, uint(0x8), photoLabel.LabelID)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package entity
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
@ -41,3 +42,14 @@ func TestPhoto_GetTakenAt(t *testing.T) {
|
|||
t.Fatalf("utc time should be 2020-02-04T10:54:34: %s", utcTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPhoto_CountryName(t *testing.T) {
|
||||
t.Run("Unknown", func(t *testing.T) {
|
||||
m := Photo{PhotoCountry: "xx"}
|
||||
assert.Equal(t, "Unknown", m.CountryName())
|
||||
})
|
||||
t.Run("Germany", func(t *testing.T) {
|
||||
m := Photo{PhotoCountry: "de"}
|
||||
assert.Equal(t, "Germany", m.CountryName())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -42,6 +42,12 @@ func TestPhoto_EstimateCountry(t *testing.T) {
|
|||
assert.Equal(t, "ca", m.CountryCode())
|
||||
assert.Equal(t, "Canada", m.CountryName())
|
||||
})
|
||||
t.Run("photo has latlng", func(t *testing.T) {
|
||||
m := Photo{PhotoTitle: "Port Lands / Gardiner Expressway / Toronto", PhotoLat: 13.333, PhotoLng: 40.998, PhotoName: "20120910_231851_CA06E1AD", OriginalName: "demo/Toronto/port-lands--gardiner-expressway--toronto_7999515645_o.jpg"}
|
||||
m.EstimateCountry()
|
||||
assert.Equal(t, "zz", m.CountryCode())
|
||||
assert.Equal(t, "Unknown", m.CountryName())
|
||||
})
|
||||
}
|
||||
|
||||
func TestPhoto_Optimize(t *testing.T) {
|
||||
|
@ -59,4 +65,11 @@ func TestPhoto_Optimize(t *testing.T) {
|
|||
t.Error("photo should NOT be updated")
|
||||
}
|
||||
})
|
||||
t.Run("photo withouth id", func(t *testing.T) {
|
||||
photo := Photo{}
|
||||
bool, err := photo.Optimize()
|
||||
assert.Error(t, err)
|
||||
assert.False(t, bool)
|
||||
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package entity
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPhoto_Yaml(t *testing.T) {
|
||||
t.Run("create from fixture", func(t *testing.T) {
|
||||
|
@ -15,3 +18,38 @@ func TestPhoto_Yaml(t *testing.T) {
|
|||
t.Logf("YAML: %s", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPhoto_SaveAsYaml(t *testing.T) {
|
||||
t.Run("create from fixture", func(t *testing.T) {
|
||||
m := PhotoFixtures.Get("Photo01")
|
||||
m.PreloadFiles()
|
||||
err := m.SaveAsYaml("test")
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestPhoto_LoadFromYaml(t *testing.T) {
|
||||
t.Run("create from fixture", func(t *testing.T) {
|
||||
m := PhotoFixtures.Get("Photo01")
|
||||
m.PreloadFiles()
|
||||
err := m.LoadFromYaml("test")
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestPhoto_YamlFileName(t *testing.T) {
|
||||
t.Run("create from fixture", func(t *testing.T) {
|
||||
m := PhotoFixtures.Get("Photo01")
|
||||
m.PreloadFiles()
|
||||
assert.Equal(t, "xxx/2790/02/yyy/Photo01.yml", m.YamlFileName("xxx", "yyy"))
|
||||
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue