diff --git a/README.md b/README.md
index 9ff9bfd53f0a3058505d4b70861d40120fb3537d..e28adc9e0830f84e9f3c5f70fb14784907de293d 100644
--- a/README.md
+++ b/README.md
@@ -15,3 +15,6 @@ * User base root folder
* Albuns
* Testing. Since I still on initial iteration phases I'm not adding as many
testing as I'd like to. Once I set on most of the design I'll add testing.
+* Add testing agains db and memory[^1] in preparation for redis implementation
+
+[^1]: https://github.com/alicebob/miniredis
diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go
index 2f30fb5d1027286fc809f6a683bc42416ed86db3..a33d9b37458ee677835aff5a26543fe84a3ac6c6 100644
--- a/pkg/components/auth/controller.go
+++ b/pkg/components/auth/controller.go
@@ -67,7 +67,7 @@ if err != nil {
return err
}
- err = c.userRepository.Create(ctx, &user.CreateUser{
+ _, err = c.userRepository.Create(ctx, &user.CreateUser{
Username: string(username),
Password: hash,
Path: string(path),
diff --git a/pkg/components/auth/controller_test.go b/pkg/components/auth/controller_test.go
index 6b4e3cd03ecdb792c56141c5e68ba1a53b343815..50bf69b08d8dea4e8d4d5e3dbf4e706701984139 100644
--- a/pkg/components/auth/controller_test.go
+++ b/pkg/components/auth/controller_test.go
@@ -17,7 +17,7 @@
type (
scene struct {
ctx context.Context
- mockRepository *MockUserRepository
+ mockRepository *MockAuthRepository
controller Controller
}
@@ -27,6 +27,12 @@ username string
password []byte
}
+ MockAuthRepository struct {
+ index uint
+ users []*mockUser
+ err error
+ }
+
MockUserRepository struct {
index uint
users []*mockUser
@@ -35,16 +41,17 @@ }
)
var (
- _ Repository = &MockUserRepository{}
+ _ Repository = &MockAuthRepository{}
key = []byte("6368616e676520746869732070617373")
)
func setUp() *scene {
+ mockAuthRepository := &MockAuthRepository{}
mockUserRepository := &MockUserRepository{}
return &scene{
ctx: context.Background(),
- mockRepository: mockUserRepository,
- controller: *NewController(mockUserRepository, nil, key),
+ mockRepository: mockAuthRepository,
+ controller: *NewController(mockAuthRepository, mockUserRepository, key),
}
}
@@ -93,11 +100,11 @@ Username: m.username,
}
}
-func (m *MockUserRepository) GetLastId() uint {
+func (m *MockAuthRepository) GetLastId() uint {
return m.index
}
-func (m *MockUserRepository) List(ctx context.Context) ([]*user.User, error) {
+func (m *MockAuthRepository) List(ctx context.Context) ([]*user.User, error) {
if m.err != nil {
return nil, m.err
}
@@ -105,7 +112,7 @@
return lo.Map(m.users, toUser), nil
}
-func (m *MockUserRepository) Get(ctx context.Context, id uint) (*user.User, error) {
+func (m *MockAuthRepository) Get(ctx context.Context, id uint) (*user.User, error) {
if m.err != nil {
return nil, m.err
}
@@ -118,7 +125,7 @@ }
return nil, errors.New("Item not found")
}
-func (m *MockUserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) {
+func (m *MockAuthRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) {
if m.err != nil {
return 0, m.err
}
@@ -131,7 +138,7 @@ }
return 0, errors.New("Item not found")
}
-func (m *MockUserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) {
+func (m *MockAuthRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) {
if m.err != nil {
return nil, m.err
}
@@ -144,7 +151,7 @@ }
return nil, errors.New("Item not found")
}
-func (m *MockUserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) {
+func (m *MockAuthRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) {
if m.err != nil {
return 0, m.err
}
@@ -160,7 +167,7 @@
return m.index, nil
}
-func (m *MockUserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error {
+func (m *MockAuthRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error {
if m.err != nil {
return m.err
}
@@ -177,7 +184,7 @@ func remove[T any](slice []T, s int) []T {
return append(slice[:s], slice[s+1:]...)
}
-func (r *MockUserRepository) Delete(ctx context.Context, id uint) error {
+func (r *MockAuthRepository) Delete(ctx context.Context, id uint) error {
if r.err != nil {
return r.err
}
@@ -189,3 +196,19 @@ }
}
return nil
}
+
+func (m *MockUserRepository) List(ctx context.Context) ([]*user.User, error) {
+ panic("not implemented") // TODO: Implement
+}
+
+func (m *MockUserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) {
+ panic("not implemented") // TODO: Implement
+}
+
+func (m *MockUserRepository) Update(ctx context.Context, id uint, updateUser *user.UpdateUser) error {
+ panic("not implemented") // TODO: Implement
+}
+
+func (m *MockUserRepository) Any(ctx context.Context) (bool, error) {
+ panic("not implemented") // TODO: Implement
+}
diff --git a/pkg/components/user/model.go b/pkg/components/user/model.go
index ce1b3a5986e2782ab36ae08dd05f02862f2d01a4..0ff6d0ab9a5b77cbfef5fab9f5b717d6e066119e 100644
--- a/pkg/components/user/model.go
+++ b/pkg/components/user/model.go
@@ -27,7 +27,7 @@ }
Repository interface {
List(ctx context.Context) ([]*User, error)
- Create(ctx context.Context, createUser *CreateUser) error
+ Create(ctx context.Context, createUser *CreateUser) (uint, error)
Update(ctx context.Context, id uint, updateUser *UpdateUser) error
Any(ctx context.Context) (bool, error)
}
diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go
index a02b67b669b46c634051daf69000a5a9044a0336..a0884f4f6e1db95c98c490fbb6ca6e10ac214de4 100644
--- a/pkg/database/sql/user.go
+++ b/pkg/database/sql/user.go
@@ -137,7 +137,7 @@
return userPassword.Password, nil
}
-func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) error {
+func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) {
user := &User{
Username: createUser.Username,
Name: createUser.Name,
@@ -148,10 +148,10 @@ result := self.db.
WithContext(ctx).
Create(user)
if result.Error != nil {
- return result.Error
+ return 0, result.Error
}
- return nil
+ return user.Model.ID, nil
}
func (self *UserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error {
diff --git a/pkg/database/sql/user_test.go b/pkg/database/sql/user_test.go
index 473ce032516e298768da908b7e64f84e4f705bce..f0d89addaf626140630730ce1d27ee65780a0c1f 100644
--- a/pkg/database/sql/user_test.go
+++ b/pkg/database/sql/user_test.go
@@ -48,7 +48,7 @@ defer tearDown()
repository := NewUserRepository(db)
- err := repository.Create(context.Background(), &user.CreateUser{
+ id, err := repository.Create(context.Background(), &user.CreateUser{
Username: "new_username",
Name: "new_name",
})
@@ -56,12 +56,12 @@ if err != nil {
t.Fatalf("Error creating: %s", err.Error())
}
- got, err := repository.Get(context.Background(), 1)
+ got, err := repository.Get(context.Background(), id)
if err != nil {
t.Fatalf("Error getting: %s", err.Error())
}
want := &user.User{
- ID: 1,
+ ID: id,
Username: "new_username",
Name: "new_name",
}
@@ -78,7 +78,7 @@ defer tearDown()
repository := NewUserRepository(db)
- err := repository.Create(context.Background(), &user.CreateUser{
+ id, err := repository.Create(context.Background(), &user.CreateUser{
Username: "username",
Name: "name",
})
@@ -86,7 +86,7 @@ if err != nil {
t.Fatalf("Error creating user: %s", err.Error())
}
- err = repository.Update(context.Background(), 1, &user.UpdateUser{
+ err = repository.Update(context.Background(), id, &user.UpdateUser{
Username: "new_username",
Name: "new_name",
})
@@ -99,7 +99,7 @@ if err != nil {
t.Fatalf("Error getting user: %s", err.Error())
}
want := &user.User{
- ID: 1,
+ ID: id,
Username: "new_username",
Name: "new_name",
}
diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go
index 649272e6714bfd5f0f85355c4f6b439eb1e6620f..bc23b9087ca3fab75190d884ec56b4d6472297f0 100644
--- a/pkg/ext/middleware.go
+++ b/pkg/ext/middleware.go
@@ -4,9 +4,10 @@ import (
"encoding/base64"
"time"
- "git.sr.ht/~gabrielgio/img/pkg/components/user"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
+
+ "git.sr.ht/~gabrielgio/img/pkg/components/user"
)
func HTML(next fasthttp.RequestHandler) fasthttp.RequestHandler {
diff --git a/pkg/worker/file_scanner.go b/pkg/worker/file_scanner.go
index fda869c156fa47a69f6166faebb6f17a3df20f2c..a51f60bbc11afea841fce4787e37e11b54992812 100644
--- a/pkg/worker/file_scanner.go
+++ b/pkg/worker/file_scanner.go
@@ -77,10 +77,6 @@ if exists {
return nil
}
- if errResp != nil {
- return errResp
- }
-
return f.repository.Create(ctx, &media.CreateMedia{
Name: name,
Path: path,