-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4dd651
commit 4428d11
Showing
6 changed files
with
170 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2023 zGraph Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tests_test | ||
|
||
import ( | ||
"testing" | ||
|
||
_ "github.com/vescale/zgraph" | ||
) | ||
|
||
// An example in https://pgql-lang.org/spec/1.5/#edge-patterns. | ||
var initStudentNetwork = []string{ | ||
`CREATE GRAPH student_network`, | ||
`USE student_network`, | ||
`CREATE LABEL Person`, | ||
`CREATE LABEL University`, | ||
`CREATE LABEL knows`, | ||
`CREATE LABEL studentOf`, | ||
`INSERT VERTEX x LABELS (Person) PROPERTIES (x.name = 'Kathrine', x.dob = DATE '1994-01-15')`, | ||
`INSERT VERTEX x LABELS (Person) PROPERTIES (x.name = 'Riya', x.dob = DATE '1995-03-20')`, | ||
`INSERT VERTEX x LABELS (Person) PROPERTIES (x.name = 'Lee', x.dob = DATE '1996-01-20')`, | ||
`INSERT VERTEX x LABELS (University) PROPERTIES (x.name = 'UC Berkeley')`, | ||
`INSERT EDGE e BETWEEN x AND y LABELS ( knows ) FROM MATCH (x), MATCH (y) WHERE x.name = 'Kathrine' AND y.name = 'Lee'`, | ||
`INSERT EDGE e BETWEEN x AND y LABELS ( knows ) FROM MATCH (x), MATCH (y) WHERE x.name = 'Kathrine' AND y.name = 'Riya'`, | ||
`INSERT EDGE e BETWEEN x AND y LABELS ( knows ) FROM MATCH (x), MATCH (y) WHERE x.name = 'Lee' AND y.name = 'Kathrine'`, | ||
`INSERT EDGE e BETWEEN x AND y LABELS ( studentOf ) FROM MATCH (x), MATCH (y) WHERE x.name = 'Kathrine' AND y.name = 'UC Berkeley'`, | ||
`INSERT EDGE e BETWEEN x AND y LABELS ( studentOf ) FROM MATCH (x), MATCH (y) WHERE x.name = 'Riya' AND y.name = 'UC Berkeley'`, | ||
`INSERT EDGE e BETWEEN x AND y LABELS ( studentOf ) FROM MATCH (x), MATCH (y) WHERE x.name = 'Lee' AND y.name = 'UC Berkeley'`, | ||
} | ||
|
||
func TestStudentNetwork(t *testing.T) { | ||
runDataDrivenTest(t, "testdata/student_network", initStudentNetwork) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
simple | ||
SELECT a.name AS a, b.name AS b FROM MATCH (a:Person) -[e:knows]-> (b:Person); | ||
---- | ||
a,b | ||
Kathrine,Lee | ||
Kathrine,Riya | ||
Lee,Kathrine | ||
|
||
more-complex | ||
SELECT p2.name AS friend, u.name AS university | ||
FROM MATCH (u:University) <-[anon1:studentOf]- (p1:Person) -[anon2:knows]-> (p2:Person) -[anon3:studentOf]-> (u) | ||
WHERE p1.name = 'Lee'; | ||
---- | ||
friend,university | ||
Kathrine,UC Berkeley | ||
|
||
binding-an-element-multiple-times | ||
SELECT p1.name AS p1, p2.name AS p2, p3.name AS p3 | ||
FROM MATCH (p1:Person) -[anon1:knows]-> (p2:Person) -[anon2:knows]-> (p3:Person) | ||
WHERE p1.name = 'Lee'; | ||
---- | ||
p1,p2,p3 | ||
Lee,Kathrine,Lee | ||
Lee,Kathrine,Riya |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2023 zGraph Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tests_test | ||
|
||
import ( | ||
"database/sql" | ||
"sort" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/datadriven" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func runDataDrivenTest(t *testing.T, path string, initSQLs []string) { | ||
t.Helper() | ||
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string { | ||
db, err := initDB(t.TempDir(), initSQLs) | ||
require.NoError(t, err) | ||
defer db.Close() | ||
|
||
rows, err := db.Query(d.Input) | ||
require.NoError(t, err) | ||
defer rows.Close() | ||
|
||
result, err := combineRows(rows) | ||
require.NoError(t, err) | ||
// Sort the result to make it deterministic. | ||
// TODO: It's not a good idea to sort the result, we should make query result deterministic instead. | ||
lines := strings.Split(strings.TrimSpace(result), "\n") | ||
sort.Strings(lines[1:]) | ||
result = strings.Join(lines, "\n") | ||
return result | ||
}) | ||
} | ||
|
||
func initDB(path string, sqls []string) (*sql.DB, error) { | ||
db, err := sql.Open("zgraph", path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, q := range sqls { | ||
_, err = db.Exec(q) | ||
if err != nil { | ||
_ = db.Close() | ||
return nil, err | ||
} | ||
} | ||
return db, nil | ||
} | ||
|
||
func combineRows(rows *sql.Rows) (string, error) { | ||
cols, err := rows.Columns() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var sb strings.Builder | ||
for i, col := range cols { | ||
sb.WriteString(col) | ||
if i+1 == len(cols) { | ||
sb.WriteByte('\n') | ||
} else { | ||
sb.WriteString(",") | ||
} | ||
} | ||
|
||
dest := make([]any, len(cols)) | ||
for i := 0; i < len(cols); i++ { | ||
var anyStr sql.NullString | ||
dest[i] = &anyStr | ||
} | ||
for rows.Next() { | ||
if err := rows.Scan(dest...); err != nil { | ||
return "", err | ||
} | ||
for i := 0; i < len(cols); i++ { | ||
sb.WriteString(dest[i].(*sql.NullString).String) | ||
if i+1 == len(cols) { | ||
sb.WriteByte('\n') | ||
} else { | ||
sb.WriteString(",") | ||
} | ||
} | ||
} | ||
return sb.String(), nil | ||
} |