@@ -2,8 +2,10 @@ package postgres
2
2
3
3
import (
4
4
"database/sql"
5
+ "github.com/go-jet/jet/v2/internal/jet"
5
6
"github.com/go-jet/jet/v2/internal/utils/ptr"
6
7
"github.com/stretchr/testify/assert"
8
+ "log/slog"
7
9
"testing"
8
10
"time"
9
11
@@ -932,41 +934,79 @@ func TestTimeExpression(t *testing.T) {
932
934
require .NoError (t , err )
933
935
}
934
936
935
- func TestIntervalUpsert (t * testing.T ) {
936
- testutils .ExecuteInTxAndRollback (t , db , func (tx * sql.Tx ) {
937
- stmt := SELECT (Employee .AllColumns ).FROM (Employee ).
938
- WHERE (Employee .EmployeeID .EQ (Int (1 )))
939
-
940
- //Validate initial dataset
941
- var windy model.Employee
942
- err := stmt .Query (tx , & windy )
943
- assert .Equal (t , windy .EmployeeID , int32 (1 ))
944
- assert .Equal (t , windy .FirstName , "Windy" )
945
- assert .Equal (t , windy .LastName , "Hays" )
946
- assert .Equal (t , * windy .PtoAccrual , "22:00:00" )
947
- assert .Nil (t , err )
948
- windy .PtoAccrual = ptr .Of ("3h" )
949
- //Update data
950
- updateStmt := Employee .UPDATE (Employee .PtoAccrual ).SET (
951
- Employee .PtoAccrual .SET (INTERVAL (3 , HOUR )),
952
- ).WHERE (Employee .EmployeeID .EQ (Int (1 ))).RETURNING (Employee .AllColumns )
953
-
954
- err = updateStmt .Query (tx , & windy )
955
- err = stmt .Query (tx , & windy )
956
- assert .Nil (t , err )
957
- assert .Equal (t , * windy .PtoAccrual , "03:00:00" )
958
- //Upsert dataset with a different value
959
- windy .PtoAccrual = ptr .Of ("5h" )
960
- insertStmt := Employee .INSERT (Employee .AllColumns ).
961
- MODEL (& windy ).
962
- ON_CONFLICT (Employee .EmployeeID ).
963
- DO_UPDATE (SET (
964
- Employee .PtoAccrual .SET (Employee .EXCLUDED .PtoAccrual ),
965
- )).RETURNING (Employee .AllColumns )
966
- err = insertStmt .Query (tx , & windy )
967
- assert .Nil (t , err )
968
- assert .Equal (t , * windy .PtoAccrual , "05:00:00" )
969
- })
937
+ func TestIntervalSetFunctionality (t * testing.T ) {
938
+ updateQuery := `
939
+ UPDATE test_sample.employee
940
+ SET pto_accrual = INTERVAL '3 HOUR'
941
+ WHERE employee.employee_id = $1
942
+ RETURNING employee.employee_id AS "employee.employee_id",
943
+ employee.first_name AS "employee.first_name",
944
+ employee.last_name AS "employee.last_name",
945
+ employee.employment_date AS "employee.employment_date",
946
+ employee.manager_id AS "employee.manager_id",
947
+ employee.pto_accrual AS "employee.pto_accrual";
948
+ `
949
+ insertQuery := `
950
+ INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id, pto_accrual)
951
+ VALUES ($1, $2, $3, $4, $5, $6)
952
+ ON CONFLICT (employee_id) DO UPDATE
953
+ SET pto_accrual = excluded.pto_accrual
954
+ RETURNING employee.employee_id AS "employee.employee_id",
955
+ employee.first_name AS "employee.first_name",
956
+ employee.last_name AS "employee.last_name",
957
+ employee.employment_date AS "employee.employment_date",
958
+ employee.manager_id AS "employee.manager_id",
959
+ employee.pto_accrual AS "employee.pto_accrual";
960
+ `
961
+
962
+ testCases := []struct {
963
+ expectedQuery string
964
+ name string
965
+ duration string
966
+ expectedInterval string
967
+ statement func (employee * model.Employee ) jet.Statement
968
+ }{
969
+ {
970
+ name : "updateQuery" ,
971
+ expectedQuery : updateQuery ,
972
+ duration : "3h" ,
973
+ expectedInterval : "03:00:00" ,
974
+ statement : func (employee * model.Employee ) jet.Statement {
975
+ return Employee .UPDATE (Employee .PtoAccrual ).SET (
976
+ Employee .PtoAccrual .SET (INTERVAL (3 , HOUR )),
977
+ ).WHERE (Employee .EmployeeID .EQ (Int (1 ))).RETURNING (Employee .AllColumns )
978
+ },
979
+ },
980
+ {
981
+ expectedQuery : insertQuery ,
982
+ name : "insertQuery" ,
983
+ duration : "5h" ,
984
+ expectedInterval : "05:00:00" ,
985
+ statement : func (employee * model.Employee ) jet.Statement {
986
+ return Employee .INSERT (Employee .AllColumns ).
987
+ MODEL (employee ).
988
+ ON_CONFLICT (Employee .EmployeeID ).
989
+ DO_UPDATE (SET (
990
+ Employee .PtoAccrual .SET (Employee .EXCLUDED .PtoAccrual ),
991
+ )).RETURNING (Employee .AllColumns )
992
+ },
993
+ },
994
+ }
995
+
996
+ for _ , tc := range testCases {
997
+ slog .Info ("Running test" , slog .Any ("test" , tc .name ))
998
+ testutils .ExecuteInTxAndRollback (t , db , func (tx * sql.Tx ) {
999
+ var windy model.Employee
1000
+ windy .PtoAccrual = ptr .Of (tc .duration )
1001
+ stmt := tc .statement (& windy )
1002
+
1003
+ testutils .AssertStatementSql (t , stmt , tc .expectedQuery )
1004
+ err := stmt .Query (tx , & windy )
1005
+ assert .Nil (t , err )
1006
+ assert .Equal (t , * windy .PtoAccrual , tc .expectedInterval )
1007
+
1008
+ })
1009
+ }
970
1010
}
971
1011
972
1012
func TestInterval (t * testing.T ) {
0 commit comments