github.com/cayleygraph/cayley@v0.7.7/graph/gaedatastore/quadstore_test.go (about)

     1  // Copyright 2014 The Cayley Authors. All rights reserved.  //
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  //     http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // +build appengine appenginevm
    15  
    16  package gaedatastore
    17  
    18  import (
    19  	"errors"
    20  	"net/http"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/cayleygraph/cayley/graph"
    25  	"github.com/cayleygraph/cayley/graph/graphtest"
    26  	"github.com/cayleygraph/cayley/graph/graphtest/testutil"
    27  	"github.com/cayleygraph/quad"
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"google.golang.org/appengine/aetest"
    31  )
    32  
    33  // This is a simple test graph.
    34  //
    35  //    +---+                        +---+
    36  //    | A |-------               ->| F |<--
    37  //    +---+       \------>+---+-/  +---+   \--+---+
    38  //                 ------>|#B#|      |        | E |
    39  //    +---+-------/      >+---+      |        +---+
    40  //    | C |             /            v
    41  //    +---+           -/           +---+
    42  //      ----    +---+/             |#G#|
    43  //          \-->|#D#|------------->+---+
    44  //              +---+
    45  //
    46  var simpleGraph = graphtest.MakeQuadSet()
    47  var simpleGraphUpdate = []quad.Quad{
    48  	quad.MakeRaw("A", "follows", "B", ""),
    49  	quad.MakeRaw("F", "follows", "B", ""),
    50  	quad.MakeRaw("C", "follows", "D", ""),
    51  	quad.MakeRaw("X", "follows", "B", ""),
    52  }
    53  
    54  type pair struct {
    55  	query string
    56  	value int64
    57  }
    58  
    59  func createInstance() (aetest.Instance, *http.Request, error) {
    60  	inst, err := aetest.NewInstance(&aetest.Options{
    61  		AppID:                       "",
    62  		StronglyConsistentDatastore: true,
    63  		StartupTimeout:              15 * time.Second,
    64  	})
    65  	if err != nil {
    66  		return nil, nil, errors.New("Creation of new instance failed")
    67  	}
    68  	req1, err := inst.NewRequest("POST", "/api/v1/write", nil)
    69  	if err != nil {
    70  		return nil, nil, errors.New("Creation of new request failed")
    71  	}
    72  	return inst, req1, nil
    73  }
    74  
    75  func makeGAE(t testing.TB) (graph.QuadStore, graph.Options, func()) {
    76  	inst, r, err := createInstance()
    77  	require.NoError(t, err)
    78  	qs, err := newQuadStore("", nil)
    79  	if err != nil {
    80  		inst.Close()
    81  		t.Fatal(err)
    82  	}
    83  	qs, err = qs.(*QuadStore).ForRequest(r)
    84  	if err != nil {
    85  		inst.Close()
    86  		t.Fatal(err)
    87  	}
    88  	return qs, nil, func() {
    89  		qs.Close()
    90  		inst.Close()
    91  	}
    92  }
    93  
    94  func TestGAEAll(t *testing.T) {
    95  	graphtest.TestAll(t, makeGAE, &graphtest.Config{
    96  		NoPrimitives: true,
    97  		UnTyped:      true,
    98  	})
    99  }
   100  
   101  func TestIterators(t *testing.T) {
   102  	qs, opts, closer := makeGAE(t)
   103  	defer closer()
   104  
   105  	testutil.MakeWriter(t, qs, opts, graphtest.MakeQuadSet()...)
   106  
   107  	require.Equal(t, int64(11), qs.Size(), "Incorrect number of quads")
   108  
   109  	var expected = []quad.Quad{
   110  		quad.Make("C", "follows", "B", ""),
   111  		quad.Make("C", "follows", "D", ""),
   112  	}
   113  
   114  	it := qs.QuadIterator(quad.Subject, qs.ValueOf(quad.Raw("C")))
   115  	graphtest.ExpectIteratedQuads(t, qs, it, expected, false)
   116  
   117  	// Test contains
   118  	it = qs.QuadIterator(quad.Label, qs.ValueOf(quad.Raw("status_graph")))
   119  	gqs := qs.(*QuadStore)
   120  	key := gqs.createKeyForQuad(quad.Make("G", "status", "cool", "status_graph"))
   121  	token := &Token{quadKind, key.StringID()}
   122  
   123  	require.True(t, it.Contains(token), "Contains failed")
   124  }