From d6c910c16b979d9be07ae585dfbd7543d670e4b2 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Fri, 10 Jul 2020 15:06:37 +0200 Subject: [PATCH] Backend: Add unit tests for internal/entity --- internal/entity/photo_album_test.go | 52 ++++++++++++++++++++------ internal/entity/photo_label_test.go | 24 ++++++++++++ internal/entity/photo_location_test.go | 12 ++++++ internal/entity/photo_optimize_test.go | 13 +++++++ internal/entity/photo_yaml_test.go | 40 +++++++++++++++++++- 5 files changed, 129 insertions(+), 12 deletions(-) diff --git a/internal/entity/photo_album_test.go b/internal/entity/photo_album_test.go index 33e0c4c79..e9e593090 100644 --- a/internal/entity/photo_album_test.go +++ b/internal/entity/photo_album_test.go @@ -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) + } + }) } diff --git a/internal/entity/photo_label_test.go b/internal/entity/photo_label_test.go index 82d747427..d2b78b38c 100644 --- a/internal/entity/photo_label_test.go +++ b/internal/entity/photo_label_test.go @@ -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) + }) } diff --git a/internal/entity/photo_location_test.go b/internal/entity/photo_location_test.go index 871527d64..281ef7aba 100644 --- a/internal/entity/photo_location_test.go +++ b/internal/entity/photo_location_test.go @@ -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()) + }) +} diff --git a/internal/entity/photo_optimize_test.go b/internal/entity/photo_optimize_test.go index 8194293be..cd2011a25 100644 --- a/internal/entity/photo_optimize_test.go +++ b/internal/entity/photo_optimize_test.go @@ -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) + + }) } diff --git a/internal/entity/photo_yaml_test.go b/internal/entity/photo_yaml_test.go index 8f44d1335..2a2923b32 100644 --- a/internal/entity/photo_yaml_test.go +++ b/internal/entity/photo_yaml_test.go @@ -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")) + + }) +}