Skip to content

Commit 4da27dc

Browse files
authored
Fix tests now that we do indexing in background (#120)
1 parent be3c974 commit 4da27dc

File tree

13 files changed

+76
-11
lines changed

13 files changed

+76
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ op = pydgraph.Operation(schema=schema)
9090
client.alter(op)
9191
```
9292

93+
Starting Dgraph version `20.3.0`, indexes are computed in the background.
94+
This requires that you wait for indexing to complete before running queries.
95+
You could do that using following code.
96+
97+
```python
98+
pydgraph.util.wait_for_indexing(client, "name", ["exact"], False, False)
99+
```
100+
93101
`Operation` contains other fields as well, including drop predicate and drop all.
94102
Drop all is useful if you wish to discard all the data, and start from a clean
95103
slate, without bringing the instance down.

examples/simple/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ set up the Dgraph cluster:
1515
version: "3.2"
1616
services:
1717
zero:
18-
image: dgraph/dgraph:v1.1.0
18+
image: dgraph/dgraph:latest
1919
restart: on-failure
2020
command: dgraph zero --my=zero:5080
2121
server:
22-
image: dgraph/dgraph:v1.1.0
22+
image: dgraph/dgraph:latest
2323
ports:
2424
- 8080:8080
2525
- 9080:9080

examples/simple/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pydgraph>=1.0a1.0
1+
pydgraph>=2.0.2

examples/simple/simple.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def create_client(client_stub):
1616

1717
# Drop All - discard all data and start from a clean slate.
1818
def drop_all(client):
19-
return client.alter(pydgraph.Operation(drop_all=True))
19+
client.alter(pydgraph.Operation(drop_all=True))
2020

2121

2222
# Set schema.
@@ -38,7 +38,9 @@ def set_schema(client):
3838
dob
3939
}
4040
"""
41-
return client.alter(pydgraph.Operation(schema=schema))
41+
client.alter(pydgraph.Operation(schema=schema))
42+
pydgraph.util.wait_for_indexing(client, "name", ["exact"], False, False)
43+
pydgraph.util.wait_for_indexing(client, "friend", [], False, True)
4244

4345

4446
# Create data using JSON.

examples/tls/tls_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def main():
3434
'''
3535
op = pydgraph.Operation(schema=schema)
3636
client.alter(op)
37+
pydgraph.util.wait_for_indexing(client, "name", ["exact"], False, False)
3738

3839
# Mutate
3940
dgraph = {

pydgraph/util.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
"""Various utility functions."""
1616

17+
import json
1718
import sys
19+
import time
1820

1921
from pydgraph.meta import VERSION
2022

@@ -34,3 +36,32 @@ def is_string(string):
3436

3537
def is_jwt_expired(exception):
3638
return 'Token is expired' in str(exception)
39+
40+
41+
def wait_for_indexing(client, pred, tokenizers, count, reverse):
42+
"""Waits for indexes to be built."""
43+
tokenizers.sort()
44+
query = "schema(pred: [{}]){{tokenizer reverse count}}".format(pred)
45+
while True:
46+
resp = client.txn(read_only=True).query(query)
47+
if has_indexes(resp, tokenizers, count, reverse):
48+
break
49+
time.sleep(0.1)
50+
51+
def has_indexes(resp, tokenizers, count, reverse):
52+
schema = json.loads(resp.json)
53+
if len(schema["schema"]) != 1:
54+
return False
55+
index = schema["schema"][0]
56+
if len(index.get("tokenizer", [])) != len(tokenizers):
57+
return False
58+
if index.get("count", False) != count or index.get("reverse", False) != reverse:
59+
return False
60+
# if no tokenizer is expected
61+
if len(index.get("tokenizer", [])) == 0:
62+
return True
63+
index["tokenizer"].sort()
64+
for i in range(len(tokenizers)):
65+
if tokenizers[i] != index["tokenizer"][i]:
66+
return False
67+
return True

tests/helper.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
__author__ = 'Garvit Pahal <[email protected]>'
1818
__maintainer__ = 'Martin Martinez Rivera <[email protected]>'
1919

20+
import time
2021
import unittest
2122

2223
import pydgraph
@@ -58,4 +59,11 @@ def setUp(self):
5859
"""Sets up the client."""
5960

6061
self.client = create_client(self.TEST_SERVER_ADDR)
61-
self.client.login("groot", "password")
62+
while True:
63+
try:
64+
self.client.login("groot", "password")
65+
break
66+
except Exception as e:
67+
if not "user not found" in str(e):
68+
raise e
69+
time.sleep(0.1)

tests/test_acct_upsert.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def setUp(self):
5252
age: int @index(int) @upsert .
5353
when: int .
5454
""")
55+
pydgraph.util.wait_for_indexing(self.client, "first", ["term"], False, False)
56+
pydgraph.util.wait_for_indexing(self.client, "last", ["hash"], False, False)
5557

5658
def test_account_upsert(self):
5759
"""Run upserts concurrently."""
@@ -111,6 +113,7 @@ def assert_changes(self, firsts, accounts):
111113
def upsert_account(addr, account, success_ctr, retry_ctr):
112114
"""Runs upsert operation."""
113115
client = helper.create_client(addr)
116+
client.login("groot", "password")
114117
query = """{{
115118
acct(func:eq(first, "{first}")) @filter(eq(last, "{last}") AND eq(age, {age})) {{
116119
uid
@@ -167,6 +170,7 @@ def upsert_account(addr, account, success_ctr, retry_ctr):
167170
def upsert_account_upsert_block(addr, account, success_ctr, retry_ctr):
168171
"""Runs upsert operation."""
169172
client = helper.create_client(addr)
173+
client.login("groot", "password")
170174
query = """{{
171175
acct(func:eq(first, "{first}")) @filter(eq(last, "{last}") AND eq(age, {age})) {{
172176
u as uid

tests/test_acl.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import unittest
2424

2525
from . import helper
26-
26+
import pydgraph
2727

2828
class TestACL(helper.ClientIntegrationTestCase):
2929
user_id = 'alice'
@@ -68,6 +68,8 @@ def change_permission(self, permission):
6868
bash_command = "dgraph acl -a " + self.server_addr + " mod -g " + self.group_id + \
6969
" -p name -m " + str(permission) + " -x password"
7070
self.run_command(bash_command)
71+
# wait for ACL cache to be refreshed.
72+
time.sleep(6)
7173

7274
def insert_sample_data(self):
7375
txn = self.client.txn()
@@ -126,11 +128,12 @@ def try_writing(self, expected):
126128
def try_altering(self, expected):
127129
try:
128130
helper.set_schema(self.alice_client, 'name: string @index(exact, term) .')
131+
pydgraph.util.wait_for_indexing(self.alice_client, "name", ["exact", "term"], False, False)
129132
if not expected:
130133
self.fail("Acl test failed: Alter successful without permission")
131134
except Exception as e:
132135
if expected:
133-
self.fail("Acl test failed: Alter failed for altreble predicate.\n" + str(e))
136+
self.fail("Acl test failed: Alter failed for alterable predicate.\n" + str(e))
134137

135138

136139
def suite():

tests/test_queries.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def setUp(self):
3535

3636
helper.drop_all(self.client)
3737
helper.set_schema(self.client, 'name: string @index(term) .')
38+
pydgraph.util.wait_for_indexing(self.client, "name", ["term"], False, False)
3839

3940
def test_mutation_and_query(self):
4041
"""Runs mutation and verifies queries see the results."""

tests/test_txn.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def setUp(self):
3030

3131
helper.drop_all(self.client)
3232
helper.set_schema(self.client, 'name: string @index(fulltext) @upsert .')
33+
pydgraph.util.wait_for_indexing(self.client, "name", ["fulltext"], False, False)
3334

3435
def test_query_after_commit(self):
3536
txn = self.client.txn()
@@ -211,6 +212,7 @@ def test_read_from_new_client(self):
211212
txn.commit()
212213

213214
client2 = helper.create_client(self.TEST_SERVER_ADDR)
215+
client2.login("groot", "password")
214216
query = """{{
215217
me(func: uid("{uid:s}")) {{
216218
name
@@ -258,6 +260,7 @@ def test_best_effort_txn(self):
258260

259261
helper.drop_all(self.client)
260262
helper.set_schema(self.client, 'name: string @index(exact) .')
263+
pydgraph.util.wait_for_indexing(self.client, "name", ["exact"], False, False)
261264

262265
txn = self.client.txn()
263266
_ = txn.mutate(set_obj={'name': 'Manish'})
@@ -384,6 +387,7 @@ def test_read_index_key_same_txn(self):
384387

385388
helper.drop_all(self.client)
386389
helper.set_schema(self.client, 'name: string @index(exact) .')
390+
pydgraph.util.wait_for_indexing(self.client, "name", ["exact"], False, False)
387391

388392
txn = self.client.txn()
389393
response = txn.mutate(set_obj={'name': 'Manish'})
@@ -407,6 +411,7 @@ def test_non_string_variable(self):
407411
in an Exception."""
408412
helper.drop_all(self.client)
409413
helper.set_schema(self.client, 'name: string @index(exact) .')
414+
pydgraph.util.wait_for_indexing(self.client, "name", ["exact"], False, False)
410415

411416
txn = self.client.txn()
412417
query = """

tests/test_type_system.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import logging
2121
import unittest
2222

23-
from tests import helper
24-
23+
from . import helper
24+
import pydgraph
2525

2626
class TestTypeSystem(helper.ClientIntegrationTestCase):
2727
def setUp(self):
@@ -38,6 +38,7 @@ def setUp(self):
3838
"""
3939

4040
helper.set_schema(self.client, schema)
41+
pydgraph.util.wait_for_indexing(self.client, "name", ["term", "exact"], False, False)
4142

4243
def test_type_deletion_failure(self):
4344
"""It tries to delete all predicates of a node without having any type"""

tests/test_upsert_block.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import json
2222

2323
from . import helper
24-
24+
import pydgraph
2525

2626
class TestUpsertBlock(helper.ClientIntegrationTestCase):
2727
"""Tests for Upsert Block"""
@@ -30,6 +30,7 @@ def setUp(self):
3030
super(TestUpsertBlock, self).setUp()
3131
helper.drop_all(self.client)
3232
helper.set_schema(self.client, 'name: string @index(term) @upsert .')
33+
pydgraph.util.wait_for_indexing(self.client, "name", ["term"], False, False)
3334

3435
def test_upsert_block_one_mutation(self):
3536
txn = self.client.txn()

0 commit comments

Comments
 (0)