Inicial
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package client_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/client"
|
||||
)
|
||||
|
||||
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 TestClientCRUD(t *testing.T) {
|
||||
conn := getTestConn(t)
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := client.CreateClient(ctx, conn, "João Silva", "123456789", "912345678", "joao@example.com", "Rua A", "")
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, c.ID)
|
||||
assert.Equal(t, "João Silva", c.Name)
|
||||
|
||||
list, err := client.ListClients(ctx, conn)
|
||||
require.NoError(t, err)
|
||||
assert.GreaterOrEqual(t, len(list), 1)
|
||||
|
||||
got, err := client.GetClient(ctx, conn, c.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, c.ID, got.ID)
|
||||
|
||||
updated, err := client.UpdateClient(ctx, conn, c.ID, "João Santos", "987654321", "921000000", "joao2@example.com", "Rua B", "nota")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "João Santos", updated.Name)
|
||||
|
||||
err = client.DeleteClient(ctx, conn, c.ID)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestVehicleCRUD(t *testing.T) {
|
||||
conn := getTestConn(t)
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := client.CreateClient(ctx, conn, "Test Client", "", "", "", "", "")
|
||||
require.NoError(t, err)
|
||||
|
||||
v, err := client.CreateVehicle(ctx, conn, c.ID, "AA-00-AA", "Toyota", "Yaris", 2020, "", 50000, "")
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, v.ID)
|
||||
assert.Equal(t, "AA-00-AA", v.Plate)
|
||||
|
||||
list, err := client.ListVehiclesByClient(ctx, conn, c.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, list, 1)
|
||||
|
||||
updated, err := client.UpdateVehicle(ctx, conn, v.ID, "BB-11-BB", "Toyota", "Yaris", 2021, "", 60000, "nota")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "BB-11-BB", updated.Plate)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user