From b19e01396a7c7050e1cdb747ecea74c0b5aa8b78 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Mon, 19 Oct 2020 17:06:09 +0200 Subject: [PATCH] Backend: Add tests for entity --- internal/entity/address_test.go | 50 +++++++++++++++ internal/entity/duplicate_test.go | 103 +++++++++++++++++++++++++++++- 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 internal/entity/address_test.go diff --git a/internal/entity/address_test.go b/internal/entity/address_test.go new file mode 100644 index 000000000..7d8762139 --- /dev/null +++ b/internal/entity/address_test.go @@ -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()) + }) +} diff --git a/internal/entity/duplicate_test.go b/internal/entity/duplicate_test.go index 64d20c68c..5e322df8d 100644 --- a/internal/entity/duplicate_test.go +++ b/internal/entity/duplicate_test.go @@ -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.Run("error", func(t *testing.T) { + t.Run("error filename empty", func(t *testing.T) { err := AddDuplicate( "", "", @@ -50,4 +50,105 @@ func TestAddDuplicate(t *testing.T) { 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") + } + }) }