Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/simple/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def delete_data(client):
}
}"""
variables1 = {'$a': 'Bob'}
res1 = client.query(query1, variables=variables1)
res1 = client.txn(read_only=True).query(query1, variables=variables1)
ppl1 = json.loads(res1.json)
for person in ppl1['all']:
print('Query to find Uid for Bob :')
Expand Down Expand Up @@ -142,7 +142,7 @@ def query_data(client):
}"""

variables = {'$a': 'Alice'}
res = client.query(query, variables=variables)
res = client.txn(read_only=True).query(query, variables=variables)
ppl = json.loads(res.json)

# Print results.
Expand Down Expand Up @@ -178,7 +178,7 @@ def query_data01(client):
}"""

variables01 = {'$b': 'Bob'}
res01 = client.query(query01, variables=variables01)
res01 = client.txn(read_only=True).query(query01, variables=variables01)
ppl01 = json.loads(res01.json)

print('Number of people named "Bob": {}'.format(len(ppl01['all'])))
Expand Down
4 changes: 2 additions & 2 deletions examples/tls/mutualtls_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def main():
txn.commit()
finally:
txn.discard()

# Query
res = client.query('''
res = client.txn(read_only=True).query('''
query dgraph($name: string) {
data(func: eq(name, $name)) {
uid
Expand Down
19 changes: 0 additions & 19 deletions pydgraph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,6 @@ def alter(self, operation, timeout=None, metadata=None, credentials=None):
else:
raise error


def query(self, query, variables=None, timeout=None, metadata=None,
credentials=None):
"""Runs a query via this client."""
new_metadata = self.add_login_metadata(metadata)
txn = self.txn(read_only=True)

try:
return txn.query(query, variables=variables, timeout=timeout,
metadata=new_metadata, credentials=credentials)
except Exception as error:
if util.is_jwt_expired(error):
self.retry_login()
new_metadata = self.add_login_metadata(metadata)
return txn.query(query, variables=variables, timeout=timeout,
metadata=new_metadata, credentials=credentials)
else:
raise error

def txn(self, read_only=False, best_effort=False):
"""Creates a transaction."""
return txn.Txn(self, read_only=read_only, best_effort=best_effort)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_acct_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def assert_changes(self, firsts, accounts):
}}
}}""".format(' '.join(firsts))
logging.debug(query)
result = json.loads(self.client.query(query).json)
result = json.loads(self.client.txn(read_only=True).query(query).json)

account_set = set()
for acct in result['all']:
Expand Down
7 changes: 3 additions & 4 deletions tests/test_essentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ class TestEssentials(helper.ClientIntegrationTestCase):
"""Tests mutation after query behavior."""

def testMutationAfterQuery(self):
"""Tests what happens when making a mutation on a txn after querying
on the client."""
"""Tests what happens when making a mutation on a txn after querying."""

_ = self.client.query('{firsts(func: has(first)) { uid first }}')
_ = self.client.txn(read_only=True).query('{firsts(func: has(first)) { uid first }}')

txn = self.client.txn()
mutation = txn.mutate(set_nquads='_:node <first> "Node name first" .')
Expand All @@ -43,7 +42,7 @@ def testMutationAfterQuery(self):
txn.commit()

query = '{{node(func: uid({uid:s})) {{ uid }} }}'.format(uid=created)
reread = self.client.query(query)
reread = self.client.txn(read_only=True).query(query)
self.assertEqual(created, json.loads(reread.json).get('node')[0]['uid'])


Expand Down
2 changes: 1 addition & 1 deletion tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_mutation_and_query(self):
}
}"""

response = self.client.query(query, variables={'$a': 'Alice'})
response = self.client.txn().query(query, variables={'$a': 'Alice'})
self.assertEqual([{'name': 'Alice', 'follows': [{'name': 'Greg'}]}],
json.loads(response.json).get('me'))
self.assertTrue(is_number(response.latency.parsing_ns),
Expand Down
24 changes: 11 additions & 13 deletions tests/test_txn.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_commit_now(self):
name
}}
}}""".format(uid=uid)
resp = self.client.query(query)
resp = self.client.txn(read_only=True).query(query)
self.assertEqual([{'name': 'Manish'}], json.loads(resp.json).get('me'))

def test_discard(self):
Expand All @@ -98,7 +98,7 @@ def test_discard(self):
name
}}
}}""".format(uid=uid)
resp = self.client.query(query)
resp = self.client.txn(read_only=True).query(query)
self.assertEqual([{'name': 'Manish'}], json.loads(resp.json).get('me'))

def test_mutate_error(self):
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_read_before_start_ts(self):
}}
}}""".format(uid=uid)

resp = self.client.query(query)
resp = self.client.txn(read_only=True).query(query)
self.assertEqual([], json.loads(resp.json).get('me'))

def test_read_after_start_ts(self):
Expand All @@ -163,7 +163,7 @@ def test_read_after_start_ts(self):
}}
}}""".format(uid=uid)

resp = self.client.query(query)
resp = self.client.txn(read_only=True).query(query)
self.assertEqual([{'name': 'Manish'}], json.loads(resp.json).get('me'))

def test_read_before_and_after_start_ts(self):
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_read_before_and_after_start_ts(self):
# once txn3 is committed, other txns observe the update
txn3.commit()

resp4 = self.client.query(query)
resp4 = self.client.txn(read_only=True).query(query)
self.assertEqual([{'name': 'Manish2'}], json.loads(resp4.json).get('me'))

def test_read_from_new_client(self):
Expand All @@ -218,7 +218,7 @@ def test_read_from_new_client(self):
}}
}}""".format(uid=uid)

resp2 = client2.query(query)
resp2 = client2.txn(read_only=True).query(query)
self.assertEqual([{'name': 'Manish'}], json.loads(resp2.json).get('me'))
self.assertTrue(resp2.txn.start_ts > 0)

Expand All @@ -227,7 +227,7 @@ def test_read_from_new_client(self):
self.assertTrue(assigned.context.start_ts > 0)
txn2.commit()

resp = self.client.query(query)
resp = self.client.txn(read_only=True).query(query)
self.assertEqual([{'name': 'Manish2'}], json.loads(resp.json).get('me'))

def test_read_only_txn(self):
Expand All @@ -236,14 +236,12 @@ def test_read_only_txn(self):

query = '{ me() {} }'

# Using client.query helper method
resp1 = self.client.query(query)
resp1 = self.client.txn(read_only=True).query(query)
start_ts1 = resp1.txn.start_ts
resp2 = self.client.query(query)
resp2 = self.client.txn(read_only=True).query(query)
start_ts2 = resp2.txn.start_ts
self.assertEqual(start_ts1, start_ts2)

# Using client.txn method
txn = self.client.txn(read_only=True)
resp1 = txn.query(query)
start_ts1 = resp1.txn.start_ts
Expand Down Expand Up @@ -361,7 +359,7 @@ def test_commit_conflict(self):
}}
}}""".format(uid=uid)

resp4 = self.client.query(query)
resp4 = self.client.txn(read_only=True).query(query)
self.assertEqual([{'name': 'Jan the man'}], json.loads(resp4.json).get('me'))

def test_mutate_conflict(self):
Expand Down Expand Up @@ -408,7 +406,7 @@ def test_conflict_ignore(self):
}
}"""

resp = self.client.query(query)
resp = self.client.txn(read_only=True).query(query)
self.assertEqual([{'uid': uid1}, {'uid': uid2}], json.loads(resp.json).get('me'))

def test_read_index_key_same_txn(self):
Expand Down