From d8407ab3898f69dd941ab76fbe902a3c5b369ee7 Mon Sep 17 00:00:00 2001 From: Samir Faci Date: Mon, 7 Oct 2024 20:02:49 -0400 Subject: [PATCH] Switching to explicit table driven tests --- internal/testutils/test_utils.go | 11 +---- tests/postgres/sample_test.go | 74 +++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/internal/testutils/test_utils.go b/internal/testutils/test_utils.go index 68556eae..cfedde4f 100644 --- a/internal/testutils/test_utils.go +++ b/internal/testutils/test_utils.go @@ -9,6 +9,7 @@ import ( "github.com/go-jet/jet/v2/internal/jet" "github.com/go-jet/jet/v2/internal/utils/throw" "github.com/go-jet/jet/v2/qrm" + "github.com/google/go-cmp/cmp" "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -17,9 +18,6 @@ import ( "runtime" "testing" "time" - "unsafe" - - "github.com/google/go-cmp/cmp" ) // UnixTimeComparer will compare time equality while ignoring time zone @@ -278,13 +276,6 @@ func AssertDeepEqual(t *testing.T, actual, expected interface{}, option ...cmp.O } } -// PointerComparer allows for comparison of the value of a given pointer -var PointerComparer = cmp.Comparer(func(x, y func() string) bool { - px := *(*unsafe.Pointer)(unsafe.Pointer(&x)) - py := *(*unsafe.Pointer)(unsafe.Pointer(&y)) - return px == py -}) - func assertQueryString(t *testing.T, actual, expected string) { if !assert.Equal(t, actual, expected) { printDiff(actual, expected) diff --git a/tests/postgres/sample_test.go b/tests/postgres/sample_test.go index 0f192768..f73baff1 100644 --- a/tests/postgres/sample_test.go +++ b/tests/postgres/sample_test.go @@ -2,8 +2,10 @@ package postgres import ( "database/sql" + "fmt" "github.com/go-jet/jet/v2/internal/utils/ptr" "github.com/google/uuid" + "github.com/stretchr/testify/assert" "testing" "github.com/stretchr/testify/require" @@ -355,35 +357,65 @@ ORDER BY employee.employee_id; testutils.AssertDebugStatementSql(t, query, expectedSQL) type Manager model.Employee - - var dest []struct { + type EmployeeAndManager struct { model.Employee - Manager *Manager } + var dest []EmployeeAndManager + err = query.Query(db, &dest) require.NoError(t, err) require.Equal(t, len(dest), 8) - testutils.AssertDeepEqual(t, dest[0].Employee, model.Employee{ - EmployeeID: 1, - FirstName: "Windy", - LastName: "Hays", - EmploymentDate: testutils.TimestampWithTimeZone("1999-01-08 03:05:06.1 +0000 UTC", 1), - ManagerID: nil, - PtoAccrual: ptr.Of("22:00:00"), - }, testutils.PointerComparer) - - require.True(t, dest[0].Manager == nil) - - testutils.AssertDeepEqual(t, dest[7].Employee, model.Employee{ - EmployeeID: 8, - FirstName: "Salley", - LastName: "Lester", - EmploymentDate: testutils.TimestampWithTimeZone("1999-01-08 04:05:06 +0100 CET", 1), - ManagerID: ptr.Of(int32(3)), - }) + testcases := []struct { + expected model.Employee + actual EmployeeAndManager + hasManager bool + }{ + { + hasManager: false, + actual: dest[0], + expected: model.Employee{ + EmployeeID: 1, + FirstName: "Windy", + LastName: "Hays", + EmploymentDate: testutils.TimestampWithTimeZone("1999-01-08 03:05:06.1 +0000 UTC", 1), + ManagerID: nil, + PtoAccrual: ptr.Of("22:00:00"), + }, + }, + { + hasManager: true, + actual: dest[7], + expected: model.Employee{ + EmployeeID: 8, + FirstName: "Salley", + LastName: "Lester", + EmploymentDate: testutils.TimestampWithTimeZone("1999-01-08 03:05:06 +0000 UTC", 1), + ManagerID: ptr.Of(int32(3)), + }, + }, + } + for _, tc := range testcases { + actual := tc.actual + expected := tc.expected + assert.Equal(t, expected.EmployeeID, actual.EmployeeID) + assert.Equal(t, expected.FirstName, actual.FirstName) + assert.Equal(t, expected.LastName, actual.LastName) + assert.Equal(t, expected.EmploymentDate.Unix(), actual.EmploymentDate.Unix()) + assert.Equal(t, expected.ManagerID, actual.ManagerID) + if expected.PtoAccrual != nil || actual.PtoAccrual != nil { + fmt.Printf("actual: %v, expected: %v\n", actual.PtoAccrual, expected.PtoAccrual) + fmt.Printf("actual: %v, expected: %v\n", *actual.PtoAccrual, *expected.PtoAccrual) + assert.Equal(t, *expected.PtoAccrual, *actual.PtoAccrual) + } + if tc.hasManager { + assert.True(t, actual.Manager != nil) + } else { + assert.True(t, actual.Manager == nil) + } + } } func TestWierdNamesTable(t *testing.T) {