github.com/cayleygraph/cayley@v0.7.7/graph/graphtest/testutil/testutil.go (about)

     1  package testutil
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/cayleygraph/cayley/graph"
     9  	"github.com/cayleygraph/cayley/writer"
    10  	"github.com/cayleygraph/quad"
    11  	"github.com/cayleygraph/quad/nquads"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  type DatabaseFunc func(t testing.TB) (graph.QuadStore, graph.Options, func())
    16  
    17  func LoadGraph(t testing.TB, path string) []quad.Quad {
    18  	var (
    19  		f   *os.File
    20  		err error
    21  	)
    22  	const levels = 5
    23  	for i := 0; i < levels; i++ {
    24  		f, err = os.Open(path)
    25  		if i+1 < levels && os.IsNotExist(err) {
    26  			path = filepath.Join("../", path)
    27  		} else if err != nil {
    28  			t.Fatalf("Failed to open %q: %v", path, err)
    29  		} else {
    30  			break
    31  		}
    32  	}
    33  	defer f.Close()
    34  	dec := nquads.NewReader(f, false)
    35  	quads, err := quad.ReadAll(dec)
    36  	if err != nil {
    37  		t.Fatalf("Failed to Unmarshal: %v", err)
    38  	}
    39  	return quads
    40  }
    41  
    42  func MakeWriter(t testing.TB, qs graph.QuadStore, opts graph.Options, data ...quad.Quad) graph.QuadWriter {
    43  	w, err := writer.NewSingleReplication(qs, opts)
    44  	require.NoError(t, err)
    45  	if len(data) > 0 {
    46  		err = w.AddQuadSet(data)
    47  		require.NoError(t, err)
    48  	}
    49  	return w
    50  }