From a0465e0e10400acb48c031b730e04ebc4ef16567 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Thu, 7 May 2020 16:45:33 +0200 Subject: [PATCH] Backend: Add tests to internal/entity --- internal/entity/account_test.go | 165 ++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 internal/entity/account_test.go diff --git a/internal/entity/account_test.go b/internal/entity/account_test.go new file mode 100644 index 000000000..caa6a5145 --- /dev/null +++ b/internal/entity/account_test.go @@ -0,0 +1,165 @@ +package entity + +import ( + "github.com/photoprism/photoprism/internal/form" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCreateAccount(t *testing.T) { + t.Run("success", func(t *testing.T) { + account := Account{AccName: "Foo", AccOwner: "bar", AccURL: "test.com", AccType: "webdav", AccKey: "123", AccUser: "testuser", AccPass: "testpass", + AccError: "", AccShare: true, AccSync: true, RetryLimit: 4, SharePath: "/home", ShareSize: "500", ShareExpires: 3500, SyncPath: "/sync", + SyncInterval: 5, SyncUpload: true, SyncDownload: false, SyncFilenames: true, SyncRaw: false} + + accountForm, err := form.NewAccount(account) + + if err != nil { + t.Fatal("error") + } + model, err := CreateAccount(accountForm) + + if err != nil { + t.Fatal("error") + } + + assert.Equal(t, "/home", model.SharePath) + assert.Equal(t, 3500, model.ShareExpires) + assert.Equal(t, "500", model.ShareSize) + assert.Equal(t, "refresh", model.SyncStatus) + assert.Equal(t, "Foo", model.AccName) + assert.Equal(t, "bar", model.AccOwner) + assert.Equal(t, "test.com", model.AccURL) + assert.Equal(t, "webdav", model.AccType) + assert.Equal(t, "123", model.AccKey) + assert.Equal(t, "testuser", model.AccUser) + assert.Equal(t, "testpass", model.AccPass) + assert.Equal(t, "", model.AccError) + assert.Equal(t, false, model.SyncDownload) + assert.Equal(t, true, model.AccShare) + assert.Equal(t, true, model.AccSync) + assert.Equal(t, 4, model.RetryLimit) + assert.Equal(t, "/sync", model.SyncPath) + assert.Equal(t, 5, model.SyncInterval) + assert.Equal(t, true, model.SyncUpload) + assert.Equal(t, true, model.SyncFilenames) + assert.Equal(t, false, model.SyncRaw) + }) +} + +func TestAccount_Save(t *testing.T) { + t.Run("success", func(t *testing.T) { + account := Account{AccName: "Foo", AccOwner: "bar", AccURL: "test.com", AccType: "test", AccKey: "123", AccUser: "testuser", AccPass: "testpass", + AccError: "", AccShare: true, AccSync: true, RetryLimit: 4, SharePath: "/home", ShareSize: "500", ShareExpires: 3500, SyncPath: "/sync", + SyncInterval: 5, SyncUpload: true, SyncDownload: false, SyncFilenames: true, SyncRaw: false} + + accountForm, err := form.NewAccount(account) + + if err != nil { + t.Fatal("error") + } + model, err := CreateAccount(accountForm) + + if err != nil { + t.Fatal("error") + } + + assert.Equal(t, "Foo", model.AccName) + assert.Equal(t, "bar", model.AccOwner) + assert.Equal(t, "test.com", model.AccURL) + + accountUpdate := Account{AccName: "NewName", AccOwner: "NewOwner", AccURL: "new.com"} + + UpdateForm, err := form.NewAccount(accountUpdate) + + err = model.Save(UpdateForm) + + if err != nil { + t.Fatal("error") + } + + assert.Equal(t, "NewName", model.AccName) + assert.Equal(t, "NewOwner", model.AccOwner) + assert.Equal(t, "new.com", model.AccURL) + + }) +} + +func TestAccount_Delete(t *testing.T) { + t.Run("success", func(t *testing.T) { + account := Account{AccName: "DeleteAccount", AccOwner: "Delete", AccURL: "test.com", AccType: "test", AccKey: "123", AccUser: "testuser", AccPass: "testpass", + AccError: "", AccShare: true, AccSync: true, RetryLimit: 4, SharePath: "/home", ShareSize: "500", ShareExpires: 3500, SyncPath: "/sync", + SyncInterval: 5, SyncUpload: true, SyncDownload: false, SyncFilenames: true, SyncRaw: false} + + accountForm, err := form.NewAccount(account) + + if err != nil { + t.Fatal("error") + } + model, err := CreateAccount(accountForm) + + if err != nil { + t.Fatal("error") + } + + err = model.Delete() + + if err != nil { + t.Fatal("error") + } + // TODO how to assert deletion? + + }) +} + +func TestAccount_Directories(t *testing.T) { + t.Run("success", func(t *testing.T) { + account := Account{AccName: "DirectoriesAccount", AccOwner: "Owner", AccURL: "http://webdav-dummy/", AccType: "webdav", AccKey: "123", AccUser: "admin", AccPass: "photoprism", + AccError: "", AccShare: true, AccSync: true, RetryLimit: 4, SharePath: "/home", ShareSize: "500", ShareExpires: 3500, SyncPath: "/sync", + SyncInterval: 5, SyncUpload: true, SyncDownload: false, SyncFilenames: true, SyncRaw: false} + + accountForm, err := form.NewAccount(account) + + if err != nil { + t.Fatal("error") + } + model, err := CreateAccount(accountForm) + + if err != nil { + t.Fatal("error") + } + + result, err := model.Directories() + + if err != nil { + t.Fatal("error") + } + assert.NotEmpty(t, result.Abs()) + assert.Contains(t, result.Abs(), "/Photos") + + }) + t.Run("no directory", func(t *testing.T) { + account := Account{AccName: "DirectoriesAccount", AccOwner: "Owner", AccURL: "http://webdav-dummy/", AccType: "xxx", AccKey: "123", AccUser: "admin", AccPass: "photoprism", + AccError: "", AccShare: true, AccSync: true, RetryLimit: 4, SharePath: "/home", ShareSize: "500", ShareExpires: 3500, SyncPath: "/sync", + SyncInterval: 5, SyncUpload: true, SyncDownload: false, SyncFilenames: true, SyncRaw: false} + + accountForm, err := form.NewAccount(account) + + if err != nil { + t.Fatal("error") + } + model, err := CreateAccount(accountForm) + + if err != nil { + t.Fatal("error") + } + + result, err := model.Directories() + + if err != nil { + t.Fatal("error") + } + + assert.Empty(t, result.Abs()) + }) +}