Backend: Add tests to internal/entity

This commit is contained in:
Theresa Gresch 2020-05-07 18:08:12 +02:00
parent 4c6aec9912
commit 5c0d12c4d9
2 changed files with 125 additions and 0 deletions

View file

@ -0,0 +1,92 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestDescription_FirstOrCreate(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: ""}
err := description.FirstOrCreate()
if err != nil {
t.Fatal("error")
}
}
func TestDescription_NoDescription(t *testing.T) {
t.Run("no description", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: ""}
assert.Equal(t, true, description.NoDescription())
})
t.Run("description", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: "test", PhotoKeywords: "test cat dog", PhotoSubject: "animals", PhotoArtist: "Bender", PhotoNotes: "notes", PhotoCopyright: "copy"}
assert.Equal(t, false, description.NoDescription())
})
}
func TestDescription_NoKeywords(t *testing.T) {
t.Run("no keywords", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: ""}
assert.Equal(t, true, description.NoKeywords())
})
t.Run("keywords", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: "test", PhotoKeywords: "test cat dog", PhotoSubject: "animals", PhotoArtist: "Bender", PhotoNotes: "notes", PhotoCopyright: "copy"}
assert.Equal(t, false, description.NoKeywords())
})
}
func TestDescription_NoSubject(t *testing.T) {
t.Run("no subject", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: ""}
assert.Equal(t, true, description.NoSubject())
})
t.Run("subject", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: "test", PhotoKeywords: "test cat dog", PhotoSubject: "animals", PhotoArtist: "Bender", PhotoNotes: "notes", PhotoCopyright: "copy"}
assert.Equal(t, false, description.NoSubject())
})
}
func TestDescription_NoNotes(t *testing.T) {
t.Run("no notes", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: ""}
assert.Equal(t, true, description.NoNotes())
})
t.Run("notes", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: "test", PhotoKeywords: "test cat dog", PhotoSubject: "animals", PhotoArtist: "Bender", PhotoNotes: "notes", PhotoCopyright: "copy"}
assert.Equal(t, false, description.NoNotes())
})
}
func TestDescription_NoArtist(t *testing.T) {
t.Run("no artist", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: ""}
assert.Equal(t, true, description.NoArtist())
})
t.Run("artist", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: "test", PhotoKeywords: "test cat dog", PhotoSubject: "animals", PhotoArtist: "Bender", PhotoNotes: "notes", PhotoCopyright: "copy"}
assert.Equal(t, false, description.NoArtist())
})
}
func TestDescription_NoCopyright(t *testing.T) {
t.Run("no copyright", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: ""}
assert.Equal(t, true, description.NoCopyright())
})
t.Run("copyright", func(t *testing.T) {
description := &Description{PhotoID: 123, PhotoDescription: "test", PhotoKeywords: "test cat dog", PhotoSubject: "animals", PhotoArtist: "Bender", PhotoNotes: "notes", PhotoCopyright: "copy"}
assert.Equal(t, false, description.NoCopyright())
})
}

View file

@ -7,6 +7,21 @@ import (
"github.com/stretchr/testify/assert"
)
func TestFirstFileByHash(t *testing.T) {
t.Run("not existing file", func(t *testing.T) {
f, err := FirstFileByHash("xxx")
assert.Equal(t, "record not found", err.Error())
assert.Equal(t, uint(0), f.ID)
})
t.Run("existing file", func(t *testing.T) {
f, err := FirstFileByHash("2cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
if err != nil {
t.Fatal("error")
}
assert.Equal(t, uint(0xf4240), f.ID)
})
}
func TestFile_DownloadFileName(t *testing.T) {
t.Run("photo with title", func(t *testing.T) {
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
@ -34,3 +49,21 @@ func TestFile_DownloadFileName(t *testing.T) {
assert.Equal(t, "123Hash.jpg", filename)
})
}
func TestFile_Changed(t *testing.T) {
t.Run("different modified times", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
time := time.Date(2020, 01, 15, 0, 0, 0, 0, time.UTC)
assert.Equal(t, true, file.Changed(500, time))
})
t.Run("different sizes", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 600, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
time := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
assert.Equal(t, true, file.Changed(500, time))
})
t.Run("no change", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
time := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
assert.Equal(t, false, file.Changed(500, time))
})
}