-
Notifications
You must be signed in to change notification settings - Fork 1
/
cozo_test.go
100 lines (83 loc) · 1.95 KB
/
cozo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2022, The Cozo Project Authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package cozo
import (
"testing"
)
func TestDb(t *testing.T) {
db, err := New("mem", "", nil)
if err != nil {
t.Error(err)
}
_, _ = db.Run("?[a,b,c] <- [[1,2,3]] :create s{a, b, c}", nil)
{
res, _ := db.Run("?[a,b,c] := *s[a,b,c]", nil)
if len(res.Rows) != 1 || len(res.Rows[0]) != 3 {
t.Error("Bad number of rows")
}
}
{
_, err := db.Run("?[x] <- [[1,2,3]]", nil)
if err == nil {
t.Error("expect an error")
}
}
{
_ = db.Backup("test.db")
db2, _ := New("mem", "", nil)
_ = db2.Restore("test.db")
res, err := db2.Run("?[a,b,c] := *s[a,b,c]", nil)
if err != nil {
t.Error(err)
}
if len(res.Rows) != 1 || len(res.Rows[0]) != 3 {
t.Error("Bad number of rows")
}
}
{
data, err := db.ExportRelations([]string{"s"})
if err != nil {
t.Error(err)
}
db3, _ := New("mem", "", nil)
_, _ = db3.Run(":create s {a, b, c}", nil)
res, err := db3.Run("?[a,b,c] := *s[a,b,c]", nil)
if err != nil {
t.Error(err)
}
if len(res.Rows) != 0 {
t.Error("Bad number of rows")
}
_ = db3.ImportRelations(data)
res, err = db3.Run("?[a,b,c] := *s[a,b,c]", nil)
if err != nil {
t.Error(err)
}
if len(res.Rows) != 1 || len(res.Rows[0]) != 3 {
t.Error("Bad number of rows")
}
db4, _ := New("mem", "", nil)
_, _ = db4.Run(":create s {a, b, c}", nil)
res, err = db4.Run("?[a,b,c] := *s[a,b,c]", nil)
if err != nil {
t.Error(err)
}
if len(res.Rows) != 0 {
t.Error("Bad number of rows")
}
_ = db4.ImportRelationsFromBackup("test.db", []string{"s"})
res, err = db4.Run("?[a,b,c] := *s[a,b,c]", nil)
if err != nil {
t.Error(err)
}
if len(res.Rows) != 1 || len(res.Rows[0]) != 3 {
t.Error("Bad number of rows")
}
}
db.Close()
}