diff --git a/cmd/cayleyexport/cayleyexport_test.go b/cmd/cayleyexport/cayleyexport_test.go index a53f90a69..c951353fe 100644 --- a/cmd/cayleyexport/cayleyexport_test.go +++ b/cmd/cayleyexport/cayleyexport_test.go @@ -3,13 +3,12 @@ package main import ( "bytes" "fmt" + "net" "net/http" "testing" - "time" "github.com/cayleygraph/quad" "github.com/cayleygraph/quad/jsonld" - "github.com/phayes/freeport" "github.com/stretchr/testify/require" "github.com/cayleygraph/cayley/graph" @@ -27,40 +26,37 @@ var testData = []quad.Quad{ } func serializeTestData() string { - buffer := bytes.NewBuffer(nil) - writer := jsonld.NewWriter(buffer) - writer.WriteQuads(testData) - writer.Close() - return buffer.String() + buf := bytes.NewBuffer(nil) + w := jsonld.NewWriter(buf) + w.WriteQuads(testData) + w.Close() + return buf.String() } -func serve(addr string) { +func TestCayleyExport(t *testing.T) { qs := memstore.New(testData...) qw, err := graph.NewQuadWriter("single", qs, graph.Options{}) - if err != nil { - panic(err) - } + require.NoError(t, err) h := &graph.Handle{QuadStore: qs, QuadWriter: qw} chttp.SetupRoutes(h, &chttp.Config{}) - err = http.ListenAndServe(addr, nil) - if err != nil { - panic(err) - } -} -func TestCayleyExport(t *testing.T) { - port, err := freeport.GetFreePort() + lis, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - addr := fmt.Sprintf("127.0.0.1:%d", port) - uri := fmt.Sprintf("http://%s", addr) - go serve(addr) - time.Sleep(time.Second / 2) + t.Cleanup(func() { + lis.Close() + }) + + srv := &http.Server{ + Addr: lis.Addr().String(), + } + go srv.Serve(lis) + cmd := NewCmd() b := bytes.NewBufferString("") cmd.SetOut(b) cmd.SetArgs([]string{ "--uri", - uri, + fmt.Sprintf("http://%s", lis.Addr().String()), }) err = cmd.Execute() require.NoError(t, err) diff --git a/cmd/cayleyimport/cayleyimport_test.go b/cmd/cayleyimport/cayleyimport_test.go index f4b2821ac..72f58cd26 100644 --- a/cmd/cayleyimport/cayleyimport_test.go +++ b/cmd/cayleyimport/cayleyimport_test.go @@ -2,13 +2,14 @@ package main import ( "bytes" + "context" "fmt" + "net" "net/http" "path" "testing" - "time" - "github.com/phayes/freeport" + "github.com/cayleygraph/quad" "github.com/stretchr/testify/require" "github.com/cayleygraph/cayley/graph" @@ -16,27 +17,48 @@ import ( chttp "github.com/cayleygraph/cayley/internal/http" ) -func serve(addr string) { +var expectData = []quad.Quad{ + {quad.IRI("http://example.com/alice"), quad.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), quad.IRI("http://xmlns.com/foaf/0.1/Person"), quad.Value(nil)}, + {quad.IRI("http://example.com/alice"), quad.IRI("http://xmlns.com/foaf/0.1/knows"), quad.IRI("http://example.com/bob"), nil}, + {quad.IRI("http://example.com/alice"), quad.IRI("http://xmlns.com/foaf/0.1/name"), quad.String("Alice"), nil}, + {quad.IRI("http://example.com/bob"), quad.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), quad.IRI("http://xmlns.com/foaf/0.1/Person"), nil}, + {quad.IRI("http://example.com/bob"), quad.IRI("http://xmlns.com/foaf/0.1/knows"), quad.IRI("http://example.com/alice"), nil}, + {quad.IRI("http://example.com/bob"), quad.IRI("http://xmlns.com/foaf/0.1/name"), quad.String("Bob"), nil}, +} + +func allQuads(t testing.TB, qs graph.QuadStore) []quad.Quad { + ctx := context.Background() + it := qs.QuadsAllIterator().Iterate() + defer it.Close() + var out []quad.Quad + for it.Next(ctx) { + ref := it.Result() + q, err := qs.Quad(ref) + require.NoError(t, err) + out = append(out, q) + } + require.NoError(t, it.Err()) + return out +} + +func TestCayleyImport(t *testing.T) { qs := memstore.New() qw, err := graph.NewQuadWriter("single", qs, graph.Options{}) - if err != nil { - panic(err) - } + require.NoError(t, err) h := &graph.Handle{QuadStore: qs, QuadWriter: qw} chttp.SetupRoutes(h, &chttp.Config{}) - err = http.ListenAndServe(addr, nil) - if err != nil { - panic(err) - } -} -func TestCayleyImport(t *testing.T) { - port, err := freeport.GetFreePort() + lis, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err) - addr := fmt.Sprintf("127.0.0.1:%d", port) - uri := fmt.Sprintf("http://%s", addr) - go serve(addr) - time.Sleep(time.Second / 2) + t.Cleanup(func() { + lis.Close() + }) + + srv := &http.Server{ + Addr: lis.Addr().String(), + } + go srv.Serve(lis) + cmd := NewCmd() b := bytes.NewBufferString("") cmd.SetOut(b) @@ -44,9 +66,10 @@ func TestCayleyImport(t *testing.T) { cmd.SetArgs([]string{ fileName, "--uri", - uri, + fmt.Sprintf("http://%s", lis.Addr().String()), }) err = cmd.Execute() require.NoError(t, err) require.Empty(t, b.String()) + require.Equal(t, expectData, allQuads(t, qs)) } diff --git a/go.mod b/go.mod index c48b1d3e7..6b592e701 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,6 @@ require ( github.com/lib/pq v1.10.9 github.com/mattn/go-sqlite3 v1.14.17 github.com/peterh/liner v1.2.2 - github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 github.com/piprate/json-gold v0.3.0 github.com/prometheus/client_golang v1.16.0 github.com/spf13/cobra v1.7.0 diff --git a/go.sum b/go.sum index 8140763d5..1e130d19a 100644 --- a/go.sum +++ b/go.sum @@ -544,8 +544,6 @@ github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw= github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI= -github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI= -github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/piprate/json-gold v0.3.0 h1:a1vHx7Q1jOO1pjCtKwTI/WCzwaQwRt9VM7apK2uy200=