feat: добавлены тесты для достижения 30% покрытия
- AuthHandler: 5 тестов (5.3% покрытия) - AuthMiddleware: 6 тестов (88.9% покрытия) - Repository: дополнительные тесты (34.2% покрытия) Общее покрытие: 17.6% (было 9.6%) Все тесты проходят успешно! Следующий этап: добавление тестов для остальных handlers и service layer
This commit is contained in:
@@ -299,6 +299,78 @@ func TestItemRepository_Create(t *testing.T) {
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// TestItemRepository_GetByID тестирует получение товара по ID
|
||||
func TestItemRepository_GetByID(t *testing.T) {
|
||||
// Arrange
|
||||
db, mock, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
repo := repository.NewItemRepository(db)
|
||||
|
||||
itemID := uuid.New()
|
||||
expectedItem := &models.Item{
|
||||
ID: itemID,
|
||||
Name: "Test Item",
|
||||
Description: "Test Description",
|
||||
Category: "electronics",
|
||||
}
|
||||
|
||||
orgID := uuid.New()
|
||||
|
||||
// Ожидаем SQL запрос
|
||||
rows := sqlmock.NewRows([]string{"id", "organization_id", "name", "description", "category", "created_at"}).
|
||||
AddRow(itemID, orgID, expectedItem.Name, expectedItem.Description, expectedItem.Category, time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM items").
|
||||
WithArgs(itemID, orgID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
// Act
|
||||
item, err := repo.GetByID(context.Background(), itemID, orgID)
|
||||
|
||||
// Assert
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, item)
|
||||
assert.Equal(t, expectedItem.ID, item.ID)
|
||||
assert.Equal(t, expectedItem.Name, item.Name)
|
||||
assert.Equal(t, expectedItem.Category, item.Category)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// TestItemRepository_GetByOrganization тестирует получение товаров по организации
|
||||
func TestItemRepository_GetByOrganization(t *testing.T) {
|
||||
// Arrange
|
||||
db, mock, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
repo := repository.NewItemRepository(db)
|
||||
|
||||
orgID := uuid.New()
|
||||
itemID1 := uuid.New()
|
||||
itemID2 := uuid.New()
|
||||
|
||||
// Ожидаем SQL запрос
|
||||
rows := sqlmock.NewRows([]string{"id", "organization_id", "name", "description", "category", "created_at"}).
|
||||
AddRow(itemID1, orgID, "Item 1", "Description 1", "electronics", time.Now()).
|
||||
AddRow(itemID2, orgID, "Item 2", "Description 2", "clothing", time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM items").
|
||||
WithArgs(orgID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
// Act
|
||||
items, err := repo.GetByOrganization(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)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// TestOperationsRepository_PlaceItem тестирует размещение товара
|
||||
func TestOperationsRepository_PlaceItem(t *testing.T) {
|
||||
// Arrange
|
||||
@@ -371,3 +443,36 @@ func TestOperationsRepository_Search(t *testing.T) {
|
||||
assert.Len(t, results, 1)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// TestLocationRepository_GetByOrganization тестирует получение локаций по организации
|
||||
func TestLocationRepository_GetByOrganization(t *testing.T) {
|
||||
// Arrange
|
||||
db, mock, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
repo := repository.NewLocationRepository(db)
|
||||
|
||||
orgID := uuid.New()
|
||||
locationID1 := uuid.New()
|
||||
locationID2 := uuid.New()
|
||||
|
||||
// Ожидаем SQL запрос
|
||||
rows := sqlmock.NewRows([]string{"id", "organization_id", "parent_id", "name", "address", "type", "coordinates", "created_at"}).
|
||||
AddRow(locationID1, orgID, nil, "Warehouse A", "123 Main St", "warehouse", `{"lat": 55.7558, "lng": 37.6176}`, time.Now()).
|
||||
AddRow(locationID2, orgID, &locationID1, "Shelf 1", "Warehouse A, Section 1", "shelf", `{"row": 1, "column": 1}`, time.Now())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM storage_locations").
|
||||
WithArgs(orgID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
// Act
|
||||
locations, err := repo.GetByOrganization(context.Background(), orgID)
|
||||
|
||||
// Assert
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, locations, 2)
|
||||
assert.Equal(t, "Warehouse A", locations[0].Name)
|
||||
assert.Equal(t, "Shelf 1", locations[1].Name)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user