-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
240 lines (191 loc) · 8.24 KB
/
tests.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import json
import pytest
import os
import pyxtdb
import edn_format
XTDB_URL = os.environ.get('XTDB_URL', 'http://localhost:3000')
@pytest.fixture
def node():
# create a connection and evict all records
node = pyxtdb.Node(XTDB_URL)
records = node.find('?e').where('?e :xt/id')
ids = [rec[0] for rec in records]
result = node.evict(ids).submit()
return node
@pytest.fixture
def billies_db(node):
records = [
{'xt/id': 'billyi', 'name': 'Billy', 'last-name': 'Idol', 'profession': 'singer'},
{'xt/id': 'billyj', 'name': 'Billy', 'last-name': 'Joel', 'profession': 'singer'},
{'xt/id': 'billyb', 'name': 'Billy', 'last-name': 'Bob', 'profession': 'actor'},
]
result = node.put(records).submit()
return node
def billies(result):
return len([rec for rec in result if rec[0]=='billy'])
def test_put_and_query(billies_db):
node = billies_db
result = node.put({'xt/id': 'billy', 'name': 'Billy', 'last-name': 'Elliot'}).submit()
# Fetch ALL records
result = node.query(r'{:find [?e], :where [[?e :xt/id]]}')
# One should have xt/id "billy"
assert billies(result) == 1
# Ensure it's gone again after deletion.
result = node.evict('billy').submit()
# Fetch ALL records
result = node.query(r'{:find [?e], :where [[?e :xt/id]]}')
# None should have xt/id "billy"
assert billies(result) == 0
def test_bad_query_inputs(billies_db):
node = billies_db
with pytest.raises(pyxtdb.BadEDNString) as e:
result = node.query(query='{') # brace not closed
assert 'EOF Reached' in str(e)
with pytest.raises(pyxtdb.BadEDNString) as e:
result = node.query(query='{:find [?e] :where [[?e :xt/id]]}',
in_args='[1]]') # too many right brackets
assert 'VECTOR_END' in str(e)
with pytest.raises(pyxtdb.XtdbError) as e:
result = node.query(query='{:find [?e]} :where [bogus]')
assert "Client Error" in str(e)
def test_query_with_args(billies_db):
node = billies_db
# scalar argument
result = node.query(query='{:find [?e] :where [[?e :last-name last]] :in [last]}',
in_args=['Joel'])
assert len(result) == 1
# multiple scalars
result = node.query(query='{:find [?e] :where [[?e :name first] [?e :last-name last]] :in [first last]}',
in_args=['Billy', 'Joel'])
assert len(result) == 1
# tuple
result = node.query(query='{:find [?e] :where [[?e :name first] [?e :last-name last]] :in [[first last]]}',
in_args=[['Billy', 'Joel']])
assert len(result) == 1
# tuple and scalar
result = node.query(
query="""
{
:find [?e]
:where [
[?e :name first]
[?e :last-name last]
[?e :profession job]
]
:in [[first last] job]
}
""",
in_args=[['Billy', 'Joel'], 'singer'])
assert len(result) == 1
# collection
result = node.query(query='{:find [?e] :where [[?e :name first] [?e :last-name last]] :in [[[first last]]]}',
in_args=[[['Billy', 'Joel'], ['Billy', 'Idol']]])
assert len(result) == 2
# in_args can be edn-formatted string rather than a collection
result = node.query(query='{:find [?e] :where [[?e :name first] [?e :last-name last]] :in [[[first last]]]}',
in_args='[[["Billy" "Joel"] ["Billy" "Idol"]]]')
assert len(result) == 2
# query can be edn parsed from a string
q = edn_format.loads('{:find [?e] :where [[?e :name first] [?e :last-name last]] :in [first last]}')
result = node.query(query=q, in_args=["Billy", "Joel"])
assert len(result) == 1
# or edn constructed by hand
result = node.query(
query= \
{ edn_format.Keyword('find') : [edn_format.Symbol('e')],
edn_format.Keyword('where') : [[edn_format.Symbol('e'), edn_format.Keyword('last-name'), edn_format.Symbol('name')]],
edn_format.Keyword('in') : [edn_format.Symbol('name')] },
in_args=['Joel'])
assert len(result) == 1
def test_query_model(billies_db):
node = billies_db
# Fetch records with name "Billy"
result = node.find('?e').where('?e :name "Billy"')
assert len(list(result)) == 3
assert result.error == None
# Fetch records with last name "Joel"
result = node.find('?e').where('?e :last-name "Joel"')
assert len(list(result)) == 1
assert result.error == None
def test_kwargs():
known_args = ['my-foo', 'my-bar?', 'my-json', 'my-edn']
# confirm keyword underscores become hyphens
params = pyxtdb.Node._parse_kwargs(known_args, {'my_foo': 1})
assert 'my-foo' in params
assert params['my-foo'] == 1
# confirm keyword Qs become ?s
params = pyxtdb.Node._parse_kwargs(known_args, {'my_barQ': 1})
assert 'my-bar?' in params
assert params['my-bar?'] == 1
# raise exception if keyword not in known_args
with pytest.raises(pyxtdb.UnknownParameter) as e:
params = pyxtdb.Node._parse_kwargs(known_args, {'unknown': 'xyzzy'})
assert 'Unknown parameter: unknown' in str(e.value)
# automatic json conversion for keywords ending -json
params = pyxtdb.Node._parse_kwargs(known_args, {'my_json': {'a': 1}})
assert params['my-json'] == '{"a": 1}'
# automatic edn conversion for keywords ending -edn
params = pyxtdb.Node._parse_kwargs(known_args, {'my_edn': {'a': 1}})
assert params['my-edn'] == '{"a" 1}'
def test_txops(node):
# add a record
tx = node.put({'xt/id': 14, 'band': 'King Crimson', 'name': 'Greg', 'last-name': 'Lake'}) \
.submit()
assert 'txId' in tx and 'txTime' in tx
result = node.query('{:find [?e] :where [[?e :band "King Crimson"]]}')
assert len(result) == 1
# add multiple records
records = [
{'xt/id': 10, 'band': 'King Crimson', 'name': 'Bill', 'last-name': 'Bruford'},
{'xt/id': 11, 'band': 'King Crimson', 'name': 'Robert', 'last-name': 'Fripp'},
{'xt/id': 12, 'band': 'King Crimson', 'name': 'Tony', 'last-name': 'Levin'},
{'xt/id': 13, 'band': 'King Crimson', 'name': 'Adrian', 'last-name': 'Belew'}
]
tx = node.put(records).submit()
assert 'txId' in tx and 'txTime' in tx
result = node.query('{:find [(pull ?e [*])] :where [[?e :band "King Crimson"]]}')
assert len(result) == 5
records = [x[0] for x in result]
# malformed records
with pytest.raises(pyxtdb.XtdbError) as e:
result = node.put({'malformed': 'this has no xt/id'}).submit()
# delete one record
tx = node.delete(eid=14).submit()
assert 'txId' in tx and 'txTime' in tx
result = node.query('{:find [?e] :where [[?e :band "King Crimson"]]}')
assert len(result) == 4
# delete multiple records
tx = node.delete(eid=[11,13]).submit()
assert 'txId' in tx and 'txTime' in tx
result = node.query('{:find [?e] :where [[?e :band "King Crimson"]]}')
assert len(result) == 2
# delete nonexistent record is NOT an error
tx = node.delete(eid=9999).submit()
assert 'txId' in tx and 'txTime' in tx
# evict nonexistent record is NOT an error
tx = node.delete(eid=9999).submit()
assert 'txId' in tx and 'txTime' in tx
# use match to update the two remaining records
for rec in records:
tx = node.match(rec['xt/id'], rec) \
.put({**rec, 'album': 'Discipline'}) \
.submit()
assert 'txId' in tx and 'txTime' in tx
# still only two records
result = node.query('{:find [?e] :where [[?e :band "King Crimson"]]}')
assert len(result) == 2
# but now they have an album
result = node.query('{:find [?e] :where [[?e :band "King Crimson"] [?e :album "Discipline"]]}')
assert len(result) == 2
# use match against Nil to re-insert the deleted records
for rec in records:
tx = node.match(rec['xt/id'], None) \
.put({**rec, 'album': 'Discipline'}) \
.submit()
assert 'txId' in tx and 'txTime' in tx
# now we have 5 records
result = node.query('{:find [?e] :where [[?e :band "King Crimson"]]}')
assert len(result) == 5
# and they have an album
result = node.query('{:find [?e] :where [[?e :band "King Crimson"] [?e :album "Discipline"]]}')
assert len(result) == 5