2020-05-08 10:21:03 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-11-21 18:08:41 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-08 10:21:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileShare_TableName(t *testing.T) {
|
|
|
|
fileShare := &FileShare{}
|
|
|
|
assert.Equal(t, "files_share", fileShare.TableName())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFileShare(t *testing.T) {
|
|
|
|
r := NewFileShare(123, 123, "test")
|
|
|
|
assert.IsType(t, &FileShare{}, r)
|
|
|
|
assert.Equal(t, uint(0x7b), r.FileID)
|
2022-10-15 21:54:11 +02:00
|
|
|
assert.Equal(t, uint(0x7b), r.ServiceID)
|
2020-05-08 10:21:03 +02:00
|
|
|
assert.Equal(t, "test", r.RemoteName)
|
|
|
|
assert.Equal(t, "new", r.Status)
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
func TestFirstOrCreateFileShare(t *testing.T) {
|
2020-07-09 11:50:58 +02:00
|
|
|
t.Run("not yet existing", func(t *testing.T) {
|
2022-10-15 21:54:11 +02:00
|
|
|
fileShare := &FileShare{FileID: 123, ServiceID: 888, RemoteName: "test888"}
|
2020-07-09 11:50:58 +02:00
|
|
|
result := FirstOrCreateFileShare(fileShare)
|
2020-05-26 11:00:39 +02:00
|
|
|
|
2020-07-09 11:50:58 +02:00
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result share should not be nil")
|
|
|
|
}
|
2020-05-26 11:00:39 +02:00
|
|
|
|
2020-07-09 11:50:58 +02:00
|
|
|
if result.FileID != fileShare.FileID {
|
|
|
|
t.Errorf("FileID should be the same: %d %d", result.FileID, fileShare.FileID)
|
|
|
|
}
|
2020-05-26 11:00:39 +02:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
if result.ServiceID != fileShare.ServiceID {
|
|
|
|
t.Errorf("ServiceID should be the same: %d %d", result.ServiceID, fileShare.ServiceID)
|
2020-07-09 11:50:58 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("existing", func(t *testing.T) {
|
|
|
|
fileShare := NewFileShare(778, 999, "NameForRemote")
|
|
|
|
result := FirstOrCreateFileShare(fileShare)
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result share should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.FileID != fileShare.FileID {
|
|
|
|
t.Errorf("FileID should be the same: %d %d", result.FileID, fileShare.FileID)
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
if result.ServiceID != fileShare.ServiceID {
|
|
|
|
t.Errorf("ServiceID should be the same: %d %d", result.ServiceID, fileShare.ServiceID)
|
2020-07-09 11:50:58 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileShare_Updates(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
fileShare := NewFileShare(123, 123, "NameBeforeUpdate")
|
|
|
|
|
|
|
|
assert.Equal(t, "NameBeforeUpdate", fileShare.RemoteName)
|
2022-10-15 21:54:11 +02:00
|
|
|
assert.Equal(t, uint(0x7b), fileShare.ServiceID)
|
2020-07-09 11:50:58 +02:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
err := fileShare.Updates(FileShare{RemoteName: "NameAfterUpdate", ServiceID: 999})
|
2020-07-09 11:50:58 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "NameAfterUpdate", fileShare.RemoteName)
|
2022-10-15 21:54:11 +02:00
|
|
|
assert.Equal(t, uint(0x3e7), fileShare.ServiceID)
|
2020-07-09 11:50:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileShare_Update(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
fileShare := NewFileShare(123, 123, "NameBeforeUpdate2")
|
|
|
|
assert.Equal(t, "NameBeforeUpdate2", fileShare.RemoteName)
|
2022-10-15 21:54:11 +02:00
|
|
|
assert.Equal(t, uint(0x7b), fileShare.ServiceID)
|
2020-07-09 11:50:58 +02:00
|
|
|
|
|
|
|
err := fileShare.Update("RemoteName", "new-name")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "new-name", fileShare.RemoteName)
|
2022-10-15 21:54:11 +02:00
|
|
|
assert.Equal(t, uint(0x7b), fileShare.ServiceID)
|
2020-07-09 11:50:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileShare_Save(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
fileShare := NewFileShare(123, 123, "Nameavc")
|
|
|
|
|
|
|
|
initialDate := fileShare.UpdatedAt
|
|
|
|
|
|
|
|
err := fileShare.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
afterDate := fileShare.UpdatedAt
|
|
|
|
|
|
|
|
assert.True(t, afterDate.After(initialDate))
|
|
|
|
})
|
2020-05-08 10:21:03 +02:00
|
|
|
}
|