Inicial
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package catalog_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/techxcar/backend/internal/catalog"
|
||||
)
|
||||
|
||||
func getTestConn(t *testing.T) *pgxpool.Conn {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("TEST_DATABASE_URL not set")
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), dsn)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { pool.Close() })
|
||||
conn, err := pool.Acquire(context.Background())
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { conn.Release() })
|
||||
_, err = conn.Exec(context.Background(), "SET search_path = tenant_test, public")
|
||||
require.NoError(t, err)
|
||||
return conn
|
||||
}
|
||||
|
||||
func TestCatalogCRUD(t *testing.T) {
|
||||
conn := getTestConn(t)
|
||||
ctx := context.Background()
|
||||
|
||||
item, err := catalog.CreateItem(ctx, conn, "MO-5W40", "Óleo Motor 5W40", "lubrificantes", "litro", 12.50)
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, item.ID)
|
||||
assert.Equal(t, "MO-5W40", item.Code)
|
||||
|
||||
list, err := catalog.ListItems(ctx, conn)
|
||||
require.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, len(list), 1)
|
||||
|
||||
updated, err := catalog.UpdateItem(ctx, conn, item.ID, "MO-5W40", "Óleo Motor 5W40 Sintético", "lubrificantes", "litro", 15.00, true)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 15.00, updated.BasePrice)
|
||||
|
||||
err = catalog.DeleteItem(ctx, conn, item.ID)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
Reference in New Issue
Block a user