From c0ab4c9a62f000c775609bc3880c5fd13ea0691f Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 24 Jul 2026 15:30:58 +0200 Subject: [PATCH] test: added tests for list service --- internal/domain/list_test.go | 84 ++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 internal/domain/list_test.go diff --git a/internal/domain/list_test.go b/internal/domain/list_test.go new file mode 100644 index 0000000..8d69e18 --- /dev/null +++ b/internal/domain/list_test.go @@ -0,0 +1,84 @@ +package domain_test + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/robindittmar/dttmr-api/internal/domain" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +type mockListRepo struct { + mock.Mock +} + +func (m *mockListRepo) CreateList(ctx context.Context, name string, userIDs []string) (*domain.List, error) { + args := m.Called(ctx, name, userIDs) + var list *domain.List + if l := args.Get(0); l != nil { + list = l.(*domain.List) + } + return list, args.Error(1) +} + +func TestListService_Create_Success(t *testing.T) { + expectedList := &domain.List{ + ID: "1", + Name: "My List", + CreatedAt: time.Now(), + ModifiedAt: time.Now(), + } + + repo := new(mockListRepo) + repo.On("CreateList", mock.Anything, "My List", []string{"user1", "user2"}).Return(expectedList, nil) + + service := domain.NewListService(repo) + list, err := service.Create(context.Background(), "My List", []string{"user1", "user2"}) + + require.NoError(t, err) + assert.Equal(t, expectedList, list) + repo.AssertExpectations(t) +} + +func TestListService_Create_EmptyName(t *testing.T) { + repo := new(mockListRepo) + service := domain.NewListService(repo) + + list, err := service.Create(context.Background(), "", []string{"user1"}) + + require.Error(t, err) + assert.EqualError(t, err, "list name must not be empty") + assert.Nil(t, list) + repo.AssertExpectations(t) +} + +func TestListService_Create_EmptyUsers(t *testing.T) { + repo := new(mockListRepo) + service := domain.NewListService(repo) + + list, err := service.Create(context.Background(), "My List", []string{}) + + require.Error(t, err) + assert.EqualError(t, err, "users must have at least one associated user") + assert.Nil(t, list) + repo.AssertExpectations(t) +} + +func TestListService_Create_RepoError(t *testing.T) { + expectedErr := errors.New("database error") + repo := new(mockListRepo) + repo.On("CreateList", mock.Anything, "My List", []string{"user1"}).Return(nil, expectedErr) + + service := domain.NewListService(repo) + + list, err := service.Create(context.Background(), "My List", []string{"user1"}) + + require.Error(t, err) + assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, list) + repo.AssertExpectations(t) +}