Skip to content

Commit

Permalink
Merge branch 'develop' into includeStatsOption
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgarton committed Dec 29, 2015
2 parents ee4465c + ac09075 commit 123c019
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ _cgo_export.*
_testmain.go

*.exe

# Vim swap files
*.swp

# IntelliJ project folder
.idea
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,3 @@ case to demonstrate the problem.

This is Free Software, released under the terms of the [GPL
v3](http://www.gnu.org/copyleft/gpl.html).


Please feel free to make a donation to the author, to support the development
of this and other Free Software packages.

[![Donate with PayPal](https://img.shields.io/badge/donate-paypal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QT3XGGX2SR88U)
1 change: 1 addition & 0 deletions cypher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestCypherParameters(t *testing.T) {
MATCH path = (n)-[r]->(m)
WHERE m.name = {name}
RETURN id(n), id(r), id(m)
ORDER by id(n), id(r), id(m)
`,
Parameters: map[string]interface{}{
"startName": "I",
Expand Down
81 changes: 63 additions & 18 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
package neoism

import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"regexp"
"strings"
"testing"
)

Expand Down Expand Up @@ -71,18 +75,10 @@ func TestDropIndex(t *testing.T) {
}

func cleanupIndexes(t *testing.T, db *Database) {
labels, err := db.Labels()
indexes, err := allIndexes(db)
if err != nil {
t.Fatal(err)
}
indexes := []*Index{}
for _, l := range labels {
idxs, err := db.Indexes(l)
if err != nil {
t.Fatal(err)
}
indexes = append(indexes, idxs...)
}
qs := make([]*CypherQuery, len(indexes))
for i, idx := range indexes {
// Cypher doesn't support properties in DROP statements
Expand Down Expand Up @@ -203,18 +199,10 @@ func TestDropUniqueConstraint(t *testing.T) {
}

func cleanupUniqueConstraints(t *testing.T, db *Database) {
labels, err := db.Labels()
constraints, err := allConstraints(db)
if err != nil {
t.Fatal(err)
}
constraints := []*UniqueConstraint{}
for _, l := range labels {
cstrs, err := db.UniqueConstraints(l, "")
if err != nil {
t.Fatal(err)
}
constraints = append(constraints, cstrs...)
}
qs := make([]*CypherQuery, len(constraints))
for i, cstr := range constraints {
l := cstr.Label
Expand All @@ -230,3 +218,60 @@ func cleanupUniqueConstraints(t *testing.T, db *Database) {
t.Fatal(err)
}
}

var (
indRegex = regexp.MustCompile(`^ +ON +:(.*)\((.*)\) +ONLINE +$`)
constraintRegex = regexp.MustCompile(`^ +ON +\(.*\:(.*)\) +ASSERT +.*\.(.*) +IS UNIQUE *`)
re = regexp.MustCompile("(.*/db/)data/")
)

func allIndexes(db *Database) ([]*Index, error) {
schemaLines, err := fetchSchema(db)
if err != nil {
return nil, err
}
var indexes []*Index
for _, line := range schemaLines {
for _, match := range indRegex.FindAllStringSubmatch(line, -1) {
indexes = append(indexes, &Index{db: db, Label: match[1], PropertyKeys: []string{match[2]}})
}
}
return indexes, nil
}

func allConstraints(db *Database) ([]*UniqueConstraint, error) {
schemaLines, err := fetchSchema(db)
if err != nil {
return nil, err
}
var constraints []*UniqueConstraint
for _, line := range schemaLines {
for _, match := range constraintRegex.FindAllStringSubmatch(line, -1) {
constraints = append(constraints, &UniqueConstraint{db: db, Label: match[1], Type: "UNIQUENESS", PropertyKeys: []string{match[2]}})
}
}
return constraints, nil
}

func fetchSchema(db *Database) ([]string, error) {
url := re.ReplaceAllString(db.Url, "${1}manage/server/console/")

resp, err := http.Post(url, "application/json", strings.NewReader(`{"command":"schema","engine":"shell"}`))
if err != nil {
return nil, err
}
defer resp.Body.Close()

var data []interface{}

dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&data); err != nil {
return nil, err
}

if len(data) != 2 {
return nil, fmt.Errorf("unexpected index response length %d", len(data))
}

return strings.Split(data[0].(string), "\n"), nil
}

0 comments on commit 123c019

Please sign in to comment.