package service_test import ( "context" "testing" "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "erp-mvp/core-service/internal/models" "erp-mvp/core-service/internal/service" ) // MockItemRepository мок для ItemRepository type MockItemRepository struct { mock.Mock } func (m *MockItemRepository) Create(ctx context.Context, item *models.Item) error { args := m.Called(ctx, item) return args.Error(0) } func (m *MockItemRepository) GetByID(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error) { args := m.Called(ctx, id, orgID) return args.Get(0).(*models.Item), args.Error(1) } func (m *MockItemRepository) GetByOrganization(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error) { args := m.Called(ctx, orgID) return args.Get(0).([]*models.Item), args.Error(1) } func (m *MockItemRepository) Update(ctx context.Context, item *models.Item) error { args := m.Called(ctx, item) return args.Error(0) } func (m *MockItemRepository) Delete(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error { args := m.Called(ctx, id, orgID) return args.Error(0) } func (m *MockItemRepository) Search(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error) { args := m.Called(ctx, orgID, query, category) return args.Get(0).([]*models.Item), args.Error(1) } // TestNewItemService тестирует создание ItemService func TestNewItemService(t *testing.T) { // Arrange mockRepo := &MockItemRepository{} // Act itemService := service.NewItemService(mockRepo) // Assert assert.NotNil(t, itemService) } // TestItemService_GetItems_Success тестирует успешное получение товаров func TestItemService_GetItems_Success(t *testing.T) { // Arrange mockRepo := &MockItemRepository{} itemService := service.NewItemService(mockRepo) orgID := uuid.New() expectedItems := []*models.Item{ { ID: uuid.New(), OrganizationID: orgID, Name: "Item 1", Description: "Description 1", Category: "electronics", }, { ID: uuid.New(), OrganizationID: orgID, Name: "Item 2", Description: "Description 2", Category: "clothing", }, } mockRepo.On("GetByOrganization", mock.Anything, orgID).Return(expectedItems, nil) // Act items, err := itemService.GetItems(context.Background(), orgID) // Assert assert.NoError(t, err) assert.Len(t, items, 2) assert.Equal(t, "Item 1", items[0].Name) assert.Equal(t, "Item 2", items[1].Name) mockRepo.AssertExpectations(t) } // TestItemService_CreateItem_Success тестирует успешное создание товара func TestItemService_CreateItem_Success(t *testing.T) { // Arrange mockRepo := &MockItemRepository{} itemService := service.NewItemService(mockRepo) orgID := uuid.New() req := &models.CreateItemRequest{ Name: "New Item", Description: "New Description", Category: "electronics", } mockRepo.On("Create", mock.Anything, mock.AnythingOfType("*models.Item")).Return(nil) // Act item, err := itemService.CreateItem(context.Background(), orgID, req) // Assert assert.NoError(t, err) assert.NotNil(t, item) assert.Equal(t, "New Item", item.Name) assert.Equal(t, "New Description", item.Description) assert.Equal(t, "electronics", item.Category) assert.Equal(t, orgID, item.OrganizationID) mockRepo.AssertExpectations(t) } // TestItemService_GetItem_Success тестирует успешное получение товара по ID func TestItemService_GetItem_Success(t *testing.T) { // Arrange mockRepo := &MockItemRepository{} itemService := service.NewItemService(mockRepo) orgID := uuid.New() itemID := uuid.New() expectedItem := &models.Item{ ID: itemID, OrganizationID: orgID, Name: "Test Item", Description: "Test Description", Category: "electronics", } mockRepo.On("GetByID", mock.Anything, itemID, orgID).Return(expectedItem, nil) // Act item, err := itemService.GetItem(context.Background(), itemID, orgID) // Assert assert.NoError(t, err) assert.NotNil(t, item) assert.Equal(t, "Test Item", item.Name) assert.Equal(t, itemID, item.ID) mockRepo.AssertExpectations(t) } // TestItemService_SearchItems_Success тестирует успешный поиск товаров func TestItemService_SearchItems_Success(t *testing.T) { // Arrange mockRepo := &MockItemRepository{} itemService := service.NewItemService(mockRepo) orgID := uuid.New() expectedItems := []*models.Item{ { ID: uuid.New(), OrganizationID: orgID, Name: "Search Result", Description: "Found item", Category: "electronics", }, } mockRepo.On("Search", mock.Anything, orgID, "search", "electronics").Return(expectedItems, nil) // Act items, err := itemService.SearchItems(context.Background(), orgID, "search", "electronics") // Assert assert.NoError(t, err) assert.Len(t, items, 1) assert.Equal(t, "Search Result", items[0].Name) mockRepo.AssertExpectations(t) } // TestItemService_UpdateItem_Success тестирует успешное обновление товара func TestItemService_UpdateItem_Success(t *testing.T) { // Arrange mockRepo := &MockItemRepository{} itemService := service.NewItemService(mockRepo) orgID := uuid.New() itemID := uuid.New() req := &models.CreateItemRequest{ Name: "Updated Item", Description: "Updated Description", Category: "clothing", } existingItem := &models.Item{ ID: itemID, OrganizationID: orgID, Name: "Old Item", Description: "Old Description", Category: "electronics", } mockRepo.On("GetByID", mock.Anything, itemID, orgID).Return(existingItem, nil) mockRepo.On("Update", mock.Anything, mock.AnythingOfType("*models.Item")).Return(nil) // Act item, err := itemService.UpdateItem(context.Background(), itemID, orgID, req) // Assert assert.NoError(t, err) assert.NotNil(t, item) assert.Equal(t, "Updated Item", item.Name) assert.Equal(t, "Updated Description", item.Description) assert.Equal(t, "clothing", item.Category) mockRepo.AssertExpectations(t) } // TestItemService_DeleteItem_Success тестирует успешное удаление товара func TestItemService_DeleteItem_Success(t *testing.T) { // Arrange mockRepo := &MockItemRepository{} itemService := service.NewItemService(mockRepo) orgID := uuid.New() itemID := uuid.New() mockRepo.On("Delete", mock.Anything, itemID, orgID).Return(nil) // Act err := itemService.DeleteItem(context.Background(), itemID, orgID) // Assert assert.NoError(t, err) mockRepo.AssertExpectations(t) }