Skip to content
Merged
90 changes: 90 additions & 0 deletions drivers/pg/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,93 @@ func TestValueMapperMapsCompositeArrays(t *testing.T) {
require.Equal(t, graph.StringKind("MemberOf"), relationships[0].Kind)
})
}

func TestAsKindID(t *testing.T) {
testsCases := []struct {
value any
expectedValue int16
expectedBoolean bool
}{
{
value: float64(1),
expectedValue: 0,
expectedBoolean: false,
},
{
value: int(32767),
expectedValue: 32767,
expectedBoolean: true,
},
{
value: int(32768),
expectedValue: 0,
expectedBoolean: false,
},
{
value: int8(1),
expectedValue: 1,
expectedBoolean: true,
},
{
value: int16(1),
expectedValue: 1,
expectedBoolean: true,
},
{
value: int32(-32769),
expectedValue: 0,
expectedBoolean: false,
},
{
value: int32(32768),
expectedValue: 0,
expectedBoolean: false,
},
{
value: int32(32767),
expectedValue: 32767,
expectedBoolean: true,
},
{
value: int64(-32769),
expectedValue: 0,
expectedBoolean: false,
},
{
value: int64(32768),
expectedValue: 0,
expectedBoolean: false,
},
{
value: int64(32767),
expectedValue: 32767,
expectedBoolean: true,
},
{
value: uint32(32768),
expectedValue: 0,
expectedBoolean: false,
},
{
value: uint32(32767),
expectedValue: 32767,
expectedBoolean: true,
},
{
value: uint64(32767),
expectedValue: 32767,
expectedBoolean: true,
},
{
value: uint64(32768),
expectedValue: 0,
expectedBoolean: false,
},
}

for _, testCase := range testsCases {
actualValue, actualBoolean := asKindID(testCase.value)
require.Equal(t, int16(testCase.expectedValue), actualValue)
require.Equal(t, testCase.expectedBoolean, actualBoolean)
}
}
25 changes: 25 additions & 0 deletions graph/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package graph

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSortNodeSetById(t *testing.T) {
nodeSet := NewNodeSet()

nodeA := NewNode(11, nil, StringKind("a"))
nodeB := NewNode(2, nil, StringKind("b"))
nodeC := NewNode(43, nil, StringKind("c"))

nodeSet.Add(nodeA)
nodeSet.Add(nodeB)
nodeSet.Add(nodeC)

nodes := SortNodeSetById(nodeSet)

require.Equal(t, "2", nodes[0].ID.String())
require.Equal(t, "11", nodes[1].ID.String())
require.Equal(t, "43", nodes[2].ID.String())
}
54 changes: 21 additions & 33 deletions integration/cypher_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package integration
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -200,30 +199,24 @@ func runWithTemplateFixture(t *testing.T, ctx context.Context, db graph.Database
t.Fatal("template cases must define an inline fixture")
}

var (
queryErrorObserved = false
err = db.WriteTransaction(ctx, func(tx graph.Transaction) error {
idMap, err := opengraph.WriteGraphTx(tx, tc.Fixture)
if err != nil {
return fmt.Errorf("creating fixture: %w", err)
}

result := tx.Query(tc.Cypher, tc.Params)
defer result.Close()
assertion.checkResult(t, result, newAssertionContext(idMap))
if assertion.expectQueryError {
queryErrorObserved = true
}
queryErrorObserved := false
session := &Session{DB: db, Ctx: ctx}
err := session.WithRollbackFixture(t, tc.Fixture, false, func(tx graph.Transaction, idMap opengraph.IDMap) error {
result := tx.Query(tc.Cypher, tc.Params)
defer result.Close()
assertion.checkResult(t, result, newAssertionContext(idMap))
if assertion.expectQueryError {
queryErrorObserved = true
}

return errFixtureRollback
})
)
return nil
})

if assertion.expectQueryError && queryErrorObserved && err != nil {
return
}

if !errors.Is(err, errFixtureRollback) {
if err != nil {
t.Fatalf("unexpected transaction error: %v", err)
}
}
Expand All @@ -239,17 +232,11 @@ func runMetamorphicFamily(t *testing.T, ctx context.Context, db graph.Database,
t.Fatal("metamorphic cases must define at least two queries")
}

err := db.WriteTransaction(ctx, func(tx graph.Transaction) error {
idMap, err := opengraph.WriteGraphTx(tx, family.Fixture)
if err != nil {
return fmt.Errorf("creating fixture: %w", err)
}

var (
assertCtx = newAssertionContext(idMap)
baselineName string
baseline []string
)
session := &Session{DB: db, Ctx: ctx}
err := session.WithRollbackFixture(t, family.Fixture, false, func(tx graph.Transaction, idMap opengraph.IDMap) error {
assertCtx := newAssertionContext(idMap)
var baselineName string
var baseline []string

for _, query := range family.Queries {
var (
Expand Down Expand Up @@ -282,10 +269,10 @@ func runMetamorphicFamily(t *testing.T, ctx context.Context, db graph.Database,
t.Fatal("all metamorphic queries were skipped")
}

return errFixtureRollback
return nil
})

if !errors.Is(err, errFixtureRollback) {
if err != nil {
t.Fatalf("unexpected transaction error: %v", err)
}
}
Expand Down Expand Up @@ -333,7 +320,8 @@ func comparisonModeSignature(t *testing.T, result queryResult, ctx assertionCont
var signature []string
switch mode {
case "row_count":
signature = []string{fmt.Sprintf("%d", len(result.rows))}
row := fmt.Sprintf("%d", len(result.rows))
signature = []string{row}
case "scalar_values":
signature = sortedSignatures(firstScalarSignatures(t, result))
case "ordered_scalar_values":
Expand Down
40 changes: 13 additions & 27 deletions integration/cypher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package integration
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -94,7 +93,7 @@ func TestCypher(t *testing.T) {
groups[ds].files = append(groups[ds].files, cf)
}

db, ctx := SetupDB(t, datasetNames...)
db, ctx := SetupDB(t, CleanupGraph, datasetNames...)

for _, g := range groups {
ClearGraph(t, db, ctx)
Expand Down Expand Up @@ -257,9 +256,6 @@ func parseAssertion(t *testing.T, raw json.RawMessage) caseAssertion {
}
}

// errFixtureRollback is returned to unconditionally roll back inline fixture data.
var errFixtureRollback = errors.New("fixture rollback")

// runReadOnly executes a test case against the pre-loaded dataset.
func runReadOnly(t *testing.T, ctx context.Context, db graph.Database, idMap opengraph.IDMap, tc testCase, assertion caseAssertion) {
t.Helper()
Expand Down Expand Up @@ -291,34 +287,24 @@ func runReadOnly(t *testing.T, ctx context.Context, db graph.Database, idMap ope
func runWithFixture(t *testing.T, ctx context.Context, db graph.Database, tc testCase, assertion caseAssertion) {
t.Helper()

var (
queryErrorObserved = false
err = db.WriteTransaction(ctx, func(tx graph.Transaction) error {
if err := tx.Nodes().Delete(); err != nil {
return fmt.Errorf("clearing graph before fixture: %w", err)
}

idMap, err := opengraph.WriteGraphTx(tx, tc.Fixture)
if err != nil {
return fmt.Errorf("creating fixture: %w", err)
}

result := tx.Query(tc.Cypher, tc.Params)
defer result.Close()
assertion.checkResult(t, result, newAssertionContext(idMap))
if assertion.expectQueryError {
queryErrorObserved = true
}
queryErrorObserved := false
session := &Session{DB: db, Ctx: ctx}
err := session.WithRollbackFixture(t, tc.Fixture, true, func(tx graph.Transaction, idMap opengraph.IDMap) error {
result := tx.Query(tc.Cypher, tc.Params)
defer result.Close()
assertion.checkResult(t, result, newAssertionContext(idMap))
if assertion.expectQueryError {
queryErrorObserved = true
}

return errFixtureRollback
})
)
return nil
})

if assertion.expectQueryError && queryErrorObserved && err != nil {
return
}

if !errors.Is(err, errFixtureRollback) {
if err != nil {
t.Fatalf("unexpected transaction error: %v", err)
}
}
Expand Down
Loading
Loading