91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package workorder_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/workorder"
|
|
)
|
|
|
|
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 TestWorkOrderCRUD(t *testing.T) {
|
|
conn := getTestConn(t)
|
|
ctx := context.Background()
|
|
|
|
wo, err := workorder.CreateWorkOrder(ctx, conn, "", "", "", "")
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, wo.ID)
|
|
assert.Equal(t, "open", wo.Status)
|
|
|
|
list, err := workorder.ListWorkOrders(ctx, conn, "")
|
|
require.NoError(t, err)
|
|
assert.GreaterOrEqual(t, len(list), 1)
|
|
|
|
detail, err := workorder.GetWorkOrderDetail(ctx, conn, wo.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, wo.ID, detail.ID)
|
|
assert.Empty(t, detail.Items)
|
|
assert.Empty(t, detail.StaffHours)
|
|
}
|
|
|
|
func TestWorkOrderTransition(t *testing.T) {
|
|
conn := getTestConn(t)
|
|
ctx := context.Background()
|
|
|
|
wo, err := workorder.CreateWorkOrder(ctx, conn, "", "", "", "")
|
|
require.NoError(t, err)
|
|
|
|
wo2, err := workorder.TransitionStatus(ctx, conn, wo.ID, "in_progress", "")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "in_progress", wo2.Status)
|
|
|
|
_, err = workorder.TransitionStatus(ctx, conn, wo.ID, "cancelled_invalid", "")
|
|
assert.Error(t, err, "invalid transition should error")
|
|
}
|
|
|
|
func TestAllowedTransitions(t *testing.T) {
|
|
tests := []struct {
|
|
from string
|
|
to string
|
|
valid bool
|
|
}{
|
|
{"open", "in_progress", true},
|
|
{"open", "cancelled", true},
|
|
{"open", "completed", false},
|
|
{"in_progress", "completed", true},
|
|
{"in_progress", "cancelled", true},
|
|
{"in_progress", "open", false},
|
|
{"completed", "invoiced", true},
|
|
{"completed", "cancelled", true},
|
|
{"invoiced", "cancelled", false},
|
|
}
|
|
for _, tt := range tests {
|
|
err := workorder.ValidateTransition(tt.from, tt.to)
|
|
if tt.valid {
|
|
assert.NoError(t, err, "%s->%s should be valid", tt.from, tt.to)
|
|
} else {
|
|
assert.Error(t, err, "%s->%s should be invalid", tt.from, tt.to)
|
|
}
|
|
}
|
|
}
|