Skip to content

Commit

Permalink
Switching to explicit table driven tests
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Oct 8, 2024
1 parent 9ce34f5 commit d8407ab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
11 changes: 1 addition & 10 deletions internal/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,9 +18,6 @@ import (
"runtime"
"testing"
"time"
"unsafe"

"github.com/google/go-cmp/cmp"
)

// UnixTimeComparer will compare time equality while ignoring time zone
Expand Down Expand Up @@ -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)
Expand Down
74 changes: 53 additions & 21 deletions tests/postgres/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d8407ab

Please sign in to comment.