Example #1
0
// 18.5.12. Get typed relationships
func TestGetTypedRelationships(t *testing.T) {
	db := connectTest(t)
	defer cleanup(t, db)
	// Create
	relType0 := rndStr(t)
	relType1 := rndStr(t)
	n0, _ := db.CreateNode(Props{})
	n1, _ := db.CreateNode(Props{})
	r0, _ := n0.Relate(relType0, n1.Id(), Props{})
	r1, _ := n0.Relate(relType1, n1.Id(), Props{})
	// Check one type of relationship
	rels, err := n0.Relationships(relType0)
	if err != nil {
		t.Error(err)
	}
	assert.Equalf(t, len(rels), 1, "Wrong number of relationships")
	_, present := rels.Map()[r0.Id()]
	assert.Tf(t, present, "Missing expected relationship")
	// Check two types of relationship together
	rels, err = n0.Relationships(relType0, relType1)
	if err != nil {
		t.Error(err)
	}
	assert.Equalf(t, len(rels), 2, "Wrong number of relationships")
	for _, r := range []*Relationship{r0, r1} {
		_, present := rels.Map()[r.Id()]
		assert.Tf(t, present, "Missing expected relationship")
	}
}
Example #2
0
// 18.9.9. Find node by exact match
func TestFindNodeByExactMatch(t *testing.T) {
	db := connectTest(t)
	defer cleanup(t, db)
	// Create
	idxName := rndStr(t)
	key0 := rndStr(t)
	key1 := rndStr(t)
	value0 := rndStr(t)
	value1 := rndStr(t)
	idx0, _ := db.CreateLegacyNodeIndex(idxName, "", "")
	defer idx0.Delete()
	n0, _ := db.CreateNode(Props{})
	n1, _ := db.CreateNode(Props{})
	n2, _ := db.CreateNode(Props{})
	// These two will be located by Find() below
	idx0.Add(n0, key0, value0)
	idx0.Add(n1, key0, value0)
	// These two will NOT be located by Find() below
	idx0.Add(n2, key1, value0)
	idx0.Add(n2, key0, value1)
	//
	nodes, err := idx0.Find(key0, value0)
	if err != nil {
		t.Error(err)
	}
	// This query should have returned a map containing just two nodes, n1 and n0.
	assert.Equal(t, len(nodes), 2)
	_, present := nodes[n0.Id()]
	assert.Tf(t, present, "Find() failed to return node with id "+strconv.Itoa(n0.Id()))
	_, present = nodes[n1.Id()]
	assert.Tf(t, present, "Find() failed to return node with id "+strconv.Itoa(n1.Id()))
}
Example #3
0
// 18.6.1. Get relationship types
func TestGetRelationshipTypes(t *testing.T) {
	db := connectTest(t)
	defer cleanup(t, db)
	relTypes := []string{}
	for i := 0; i < 10; i++ {
		relTypes = append(relTypes, rndStr(t))
	}
	// Create relationships
	n0, _ := db.CreateNode(Props{})
	n1, _ := db.CreateNode(Props{})
	rels := []*Relationship{}
	for _, rt := range relTypes {
		aRel, _ := n0.Relate(rt, n1.Id(), Props{})
		rels = append(rels, aRel)
	}
	// Get all relationship types, and confirm the list of types contains at least
	// all those randomly-generated values in relTypes.  It cannot be guaranteed
	// that the database will not contain other relationship types beyond these.
	foundRelTypes, err := db.RelTypes()
	if err != nil {
		t.Error(err)
	}
	for _, rt := range relTypes {
		assert.Tf(t, sort.SearchStrings(foundRelTypes, rt) < len(foundRelTypes),
			"Could not find expected relationship type: "+rt)
	}
}
Example #4
0
// 18.7.1. Set property on node
func TestSetPropertyOnNode(t *testing.T) {
	db := connectTest(t)
	defer cleanup(t, db)
	// Create
	n0, _ := db.CreateNode(Props{})
	key := rndStr(t)
	value := rndStr(t)
	err := n0.SetProperty(key, value)
	if err != nil {
		t.Error(err)
	}
	// Confirm
	props, _ := n0.Properties()
	checkVal, present := props[key]
	assert.Tf(t, present, "Expected property key not found")
	assert.Tf(t, checkVal == value, "Expected property value not found")
}
Example #5
0
// 18.5.2. Create relationship
func TestCreateRelationship(t *testing.T) {
	db := connectTest(t)
	defer cleanup(t, db)
	// Create
	n0, _ := db.CreateNode(Props{})
	n1, _ := db.CreateNode(Props{})
	r0, err := n0.Relate("knows", n1.Id(), Props{})
	if err != nil {
		t.Error(err)
	}
	// Confirm relationship exists on both nodes
	rels, _ := n0.Outgoing("knows")
	_, present := rels.Map()[r0.Id()]
	assert.Tf(t, present, "Outgoing relationship not present on origin node.")
	rels, _ = n1.Incoming("knows")
	_, present = rels.Map()[r0.Id()]
	assert.Tf(t, present, "Incoming relationship not present on destination node.")
}
Example #6
0
// 18.9.10. Find node by query
func TestFindNodeByQuery(t *testing.T) {
	db := connectTest(t)
	defer cleanup(t, db)
	// Create
	idx0, _ := db.CreateLegacyNodeIndex("test index", "", "")
	defer idx0.Delete()
	key0 := rndStr(t)
	key1 := rndStr(t)
	value0 := rndStr(t)
	value1 := rndStr(t)
	n0, _ := db.CreateNode(Props{})
	idx0.Add(n0, key0, value0)
	idx0.Add(n0, key1, value1)
	n1, _ := db.CreateNode(Props{})
	idx0.Add(n1, key0, value0)
	n2, _ := db.CreateNode(Props{})
	idx0.Add(n2, rndStr(t), rndStr(t))
	// Retrieve
	luceneQuery0 := fmt.Sprintf("%v:%v AND %v:%v", key0, value0, key1, value1) // Retrieve n0 only
	luceneQuery1 := fmt.Sprintf("%v:%v", key0, value0)                         // Retrieve n0 and n1
	nodes0, err := idx0.Query(luceneQuery0)
	if err != nil {
		t.Error(err)
	}
	nodes1, err := idx0.Query(luceneQuery1)
	if err != nil {
		t.Error(err)
	}
	// Confirm
	assert.Equalf(t, len(nodes0), 1, "Query should have returned only one Node.")
	_, present := nodes0[n0.Id()]
	assert.Tf(t, present, "Query() failed to return node with id "+strconv.Itoa(n0.Id()))
	assert.Equalf(t, len(nodes1), 2, "Query should have returned exactly 2 Nodes.")
	_, present = nodes1[n0.Id()]
	assert.Tf(t, present, "Query() failed to return node with id "+strconv.Itoa(n0.Id()))
	_, present = nodes1[n1.Id()]
	assert.Tf(t, present, "Query() failed to return node with id "+strconv.Itoa(n1.Id()))
}
Example #7
0
// 18.5.10. Get incoming relationships
func TestGetIncomingRelationships(t *testing.T) {
	db := connectTest(t)
	defer cleanup(t, db)
	// Create
	n0, _ := db.CreateNode(Props{})
	n1, _ := db.CreateNode(Props{})
	n0.Relate("knows", n1.Id(), Props{})
	r1, _ := n1.Relate("knows", n0.Id(), Props{})
	n0.Relate("knows", n1.Id(), Props{})
	// Check relationships
	rels, err := n0.Incoming()
	if err != nil {
		t.Error(err)
	}
	assert.Equalf(t, len(rels), 1, "Wrong number of relationships")
	_, present := rels.Map()[r1.Id()]
	assert.Tf(t, present, "Missing expected relationship")
}