Addressing code review comments and adding a query validation

This commit is contained in:
Samir Faci 2024-10-09 08:30:33 -04:00
parent 288ebdc373
commit 600e0a7ce7

View file

@ -2,8 +2,10 @@ package postgres
import ( import (
"database/sql" "database/sql"
"github.com/go-jet/jet/v2/internal/jet"
"github.com/go-jet/jet/v2/internal/utils/ptr" "github.com/go-jet/jet/v2/internal/utils/ptr"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"log/slog"
"testing" "testing"
"time" "time"
@ -932,41 +934,79 @@ func TestTimeExpression(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
func TestIntervalUpsert(t *testing.T) { func TestIntervalSetFunctionality(t *testing.T) {
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) { updateQuery := `
stmt := SELECT(Employee.AllColumns).FROM(Employee). UPDATE test_sample.employee
WHERE(Employee.EmployeeID.EQ(Int(1))) SET pto_accrual = INTERVAL '3 HOUR'
WHERE employee.employee_id = $1
RETURNING employee.employee_id AS "employee.employee_id",
employee.first_name AS "employee.first_name",
employee.last_name AS "employee.last_name",
employee.employment_date AS "employee.employment_date",
employee.manager_id AS "employee.manager_id",
employee.pto_accrual AS "employee.pto_accrual";
`
insertQuery := `
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id, pto_accrual)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (employee_id) DO UPDATE
SET pto_accrual = excluded.pto_accrual
RETURNING employee.employee_id AS "employee.employee_id",
employee.first_name AS "employee.first_name",
employee.last_name AS "employee.last_name",
employee.employment_date AS "employee.employment_date",
employee.manager_id AS "employee.manager_id",
employee.pto_accrual AS "employee.pto_accrual";
`
//Validate initial dataset testCases := []struct {
var windy model.Employee expectedQuery string
err := stmt.Query(tx, &windy) name string
assert.Equal(t, windy.EmployeeID, int32(1)) duration string
assert.Equal(t, windy.FirstName, "Windy") expectedInterval string
assert.Equal(t, windy.LastName, "Hays") statement func(employee *model.Employee) jet.Statement
assert.Equal(t, *windy.PtoAccrual, "22:00:00") }{
assert.Nil(t, err) {
windy.PtoAccrual = ptr.Of("3h") name: "updateQuery",
//Update data expectedQuery: updateQuery,
updateStmt := Employee.UPDATE(Employee.PtoAccrual).SET( duration: "3h",
Employee.PtoAccrual.SET(INTERVAL(3, HOUR)), expectedInterval: "03:00:00",
).WHERE(Employee.EmployeeID.EQ(Int(1))).RETURNING(Employee.AllColumns) statement: func(employee *model.Employee) jet.Statement {
return Employee.UPDATE(Employee.PtoAccrual).SET(
Employee.PtoAccrual.SET(INTERVAL(3, HOUR)),
).WHERE(Employee.EmployeeID.EQ(Int(1))).RETURNING(Employee.AllColumns)
},
},
{
expectedQuery: insertQuery,
name: "insertQuery",
duration: "5h",
expectedInterval: "05:00:00",
statement: func(employee *model.Employee) jet.Statement {
return Employee.INSERT(Employee.AllColumns).
MODEL(employee).
ON_CONFLICT(Employee.EmployeeID).
DO_UPDATE(SET(
Employee.PtoAccrual.SET(Employee.EXCLUDED.PtoAccrual),
)).RETURNING(Employee.AllColumns)
},
},
}
err = updateStmt.Query(tx, &windy) for _, tc := range testCases {
err = stmt.Query(tx, &windy) slog.Info("Running test", slog.Any("test", tc.name))
assert.Nil(t, err) testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
assert.Equal(t, *windy.PtoAccrual, "03:00:00") var windy model.Employee
//Upsert dataset with a different value windy.PtoAccrual = ptr.Of(tc.duration)
windy.PtoAccrual = ptr.Of("5h") stmt := tc.statement(&windy)
insertStmt := Employee.INSERT(Employee.AllColumns).
MODEL(&windy). testutils.AssertStatementSql(t, stmt, tc.expectedQuery)
ON_CONFLICT(Employee.EmployeeID). err := stmt.Query(tx, &windy)
DO_UPDATE(SET( assert.Nil(t, err)
Employee.PtoAccrual.SET(Employee.EXCLUDED.PtoAccrual), assert.Equal(t, *windy.PtoAccrual, tc.expectedInterval)
)).RETURNING(Employee.AllColumns)
err = insertStmt.Query(tx, &windy) })
assert.Nil(t, err) }
assert.Equal(t, *windy.PtoAccrual, "05:00:00")
})
} }
func TestInterval(t *testing.T) { func TestInterval(t *testing.T) {