Files
bamort/template/backend/items/items_test.go
T
2026-04-01 15:16:12 +02:00

38 lines
776 B
Go

package items_test
import (
"myapp/database"
"myapp/testutils"
"testing"
"myapp/items"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupTestEnvironment(t *testing.T) {
testutils.SetupTestEnvironment(t)
database.ConnectDatabase()
}
func TestCreateAndListItems(t *testing.T) {
setupTestEnvironment(t)
// Create a test item directly via GORM.
item := items.Item{
UserID: 1,
Name: "Test Item",
Description: "A sample item for testing",
}
err := database.DB.Create(&item).Error
require.NoError(t, err)
assert.NotZero(t, item.ID)
// List items for the same user.
var found []items.Item
err = database.DB.Where("user_id = ?", 1).Find(&found).Error
require.NoError(t, err)
assert.NotEmpty(t, found)
}