Backend: Add tests for entity

This commit is contained in:
Theresa Gresch 2020-10-19 17:06:09 +02:00
parent 1cd2dfbd22
commit b19e01396a
2 changed files with 152 additions and 1 deletions

View file

@ -0,0 +1,50 @@
package entity
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFirstOrCreateAddress(t *testing.T) {
t.Run("existing address", func(t *testing.T) {
address := Address{ID: 1234567, AddressLine1: "Line 1", AddressCountry: "DE"}
result := FirstOrCreateAddress(&address)
if result == nil {
t.Fatal("result should not be nil")
}
t.Log(result)
assert.Equal(t, 1234567, result.ID)
})
t.Run("not existing address", func(t *testing.T) {
address := &Address{}
result := FirstOrCreateAddress(address)
if result == nil {
t.Fatal("result should not be nil")
}
t.Log(result)
assert.Equal(t, 1234568, result.ID)
})
}
func TestAddress_String(t *testing.T) {
t.Run("success", func(t *testing.T) {
address := Address{ID: 1234567, AddressLine1: "Line 1", AddressLine2: "Line 2", AddressCity: "Berlin", AddressCountry: "DE"}
addressString := address.String()
assert.Equal(t, "Line 1, Berlin, DE", addressString)
})
}
func TestAddress_Unknown(t *testing.T) {
t.Run("success", func(t *testing.T) {
address := Address{ID: 1234567, AddressLine1: "Line 1", AddressLine2: "Line 2", AddressCity: "Berlin", AddressCountry: "DE"}
assert.False(t, address.Unknown())
})
}

View file

@ -37,7 +37,7 @@ func TestAddDuplicate(t *testing.T) {
t.Fatalf("mod time should be %d", time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix()) t.Fatalf("mod time should be %d", time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix())
} }
}) })
t.Run("error", func(t *testing.T) { t.Run("error filename empty", func(t *testing.T) {
err := AddDuplicate( err := AddDuplicate(
"", "",
"", "",
@ -50,4 +50,105 @@ func TestAddDuplicate(t *testing.T) {
t.Fatal("error expected") t.Fatal("error expected")
} }
}) })
t.Run("error filehash empty", func(t *testing.T) {
err := AddDuplicate(
"foobar.jpg",
"",
"",
661858,
time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix(),
)
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error mod time empty", func(t *testing.T) {
err := AddDuplicate(
"foobar.jpg",
"",
"3cad9168fa6acc5c5c2965ddf6ec465ca42fd818",
661858,
0,
)
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error fileRoot empty", func(t *testing.T) {
err := AddDuplicate(
"foobar.jpg",
"",
"3cad9168fa6acc5c5c2965ddf6ec465ca42fd818",
661858,
time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix(),
)
if err == nil {
t.Fatal("error expected")
}
})
}
func TestCreateDuplicate(t *testing.T) {
t.Run("error mod time 0", func(t *testing.T) {
duplicate := &Duplicate{FileName: "foobar.jpg", FileHash: "12345tghy", FileRoot: RootOriginals, ModTime: 0}
err := duplicate.Create()
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error filename empty", func(t *testing.T) {
duplicate := &Duplicate{FileName: "", FileHash: "12345tghy", FileRoot: RootOriginals, ModTime: time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix()}
err := duplicate.Create()
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error filehash empty", func(t *testing.T) {
duplicate := &Duplicate{FileName: "foobar.jpg", FileHash: "", FileRoot: RootOriginals, ModTime: time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix()}
err := duplicate.Create()
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error fileroot empty", func(t *testing.T) {
duplicate := &Duplicate{FileName: "foobar.jpg", FileHash: "jhg678", FileRoot: "", ModTime: time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix()}
err := duplicate.Create()
if err == nil {
t.Fatal("error expected")
}
})
}
func TestSaveDuplicate(t *testing.T) {
t.Run("error mod time 0", func(t *testing.T) {
duplicate := &Duplicate{FileName: "foobar.jpg", FileHash: "12345tghy", FileRoot: RootOriginals, ModTime: 0}
err := duplicate.Save()
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error filename empty", func(t *testing.T) {
duplicate := &Duplicate{FileName: "", FileHash: "12345tghy", FileRoot: RootOriginals, ModTime: time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix()}
err := duplicate.Save()
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error filehash empty", func(t *testing.T) {
duplicate := &Duplicate{FileName: "foobar.jpg", FileHash: "", FileRoot: RootOriginals, ModTime: time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix()}
err := duplicate.Save()
if err == nil {
t.Fatal("error expected")
}
})
t.Run("error fileroot empty", func(t *testing.T) {
duplicate := &Duplicate{FileName: "foobar.jpg", FileHash: "jhg678", FileRoot: "", ModTime: time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix()}
err := duplicate.Save()
if err == nil {
t.Fatal("error expected")
}
})
} }