Skip to content

Commit 77cdc4e

Browse files
authored
Merge pull request #200 from Alexsaphir/feature-db-contains
Add __contains__ method to allow to easily check if an id reference a valid document
2 parents 92942b3 + 43fb38e commit 77cdc4e

6 files changed

Lines changed: 43 additions & 8 deletions

File tree

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pyArango
1313
:alt: downloads_week
1414
:target: https://pepy.tech/project/pyarango/week
1515

16-
.. image:: https://travis-ci.org/tariqdaouda/pyArango.svg?branch=1.2.2
17-
:target: https://travis-ci.org/tariqdaouda/pyArango
16+
.. image:: https://travis-ci.com/Alexsaphir/pyArango.svg?branch=master
17+
:target: https://travis-ci.com/github/Alexsaphir/pyArango
1818
.. image:: https://img.shields.io/badge/python-2.7%2C%203.5-blue.svg
1919
.. image:: https://img.shields.io/badge/arangodb-3.0-blue.svg
2020

pyArango/database.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,15 @@ def dropAllCollections(self):
220220
self[collection_name].delete()
221221
return
222222

223-
def AQLQuery(self, query, batchSize = 100, rawResults = False, bindVars = {}, options = {}, count = False, fullCount = False,
223+
def AQLQuery(self, query, batchSize = 100, rawResults = False, bindVars = None, options = None, count = False, fullCount = False,
224224
json_encoder = None, **moreArgs):
225225
"""Set rawResults = True if you want the query to return dictionnaries instead of Document objects.
226226
You can use **moreArgs to pass more arguments supported by the api, such as ttl=60 (time to live)"""
227+
if bindVars is None:
228+
bindVars = {}
229+
if options is None:
230+
options = {}
231+
227232
return AQLQuery(self, query, rawResults = rawResults, batchSize = batchSize, bindVars = bindVars, options = options, count = count, fullCount = fullCount,
228233
json_encoder = json_encoder, **moreArgs)
229234

@@ -455,8 +460,11 @@ def no_fetch_run(
455460
return
456461
raise AQLFetchError("No results should be returned for the query.")
457462

458-
def explainAQLQuery(self, query, bindVars={}, allPlans = False):
463+
def explainAQLQuery(self, query, bindVars = None, allPlans = False):
459464
"""Returns an explanation of the query. Setting allPlans to True will result in ArangoDB returning all possible plans. False returns only the optimal plan"""
465+
if bindVars is None:
466+
bindVars = {}
467+
460468
payload = {'query' : query, 'bindVars' : bindVars, 'allPlans' : allPlans}
461469
request = self.connection.session.post(self.getExplainURL(), data = json.dumps(payload, default=str))
462470
return request.json()
@@ -502,6 +510,14 @@ def transaction(self, collections, action, waitForSync = False, lockTimeout = No
502510
def __repr__(self):
503511
return "ArangoDB database: %s" % self.name
504512

513+
def __contains__(self, _id):
514+
"""allows to check if _id:str is the id of an existing document"""
515+
col, key = _id.split('/')
516+
try:
517+
return key in self[col]
518+
except KeyError:
519+
return False
520+
505521
def __getitem__(self, collectionName):
506522
"""use database[collectionName] to get a collection from the database"""
507523
try:

pyArango/document.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ class DocumentStore(object):
77
"""Store all the data of a document in hierarchy of stores and handles validation.
88
Does not store private information, these are in the document."""
99

10-
def __init__(self, collection, validators={}, initDct={}, patch=False, subStore=False, validateInit=False):
10+
def __init__(self, collection, validators=None, initDct=None, patch=False, subStore=False, validateInit=False):
11+
if validators is None:
12+
validators = {}
13+
if initDct is None:
14+
initDct = {}
15+
1116
self.store = {}
1217
self.patchStore = {}
1318
self.collection = collection
@@ -128,7 +133,7 @@ def __dict__(self):
128133
return dict(self.store) + dict(self.patchStore)
129134

130135
def __contains__(self, field):
131-
return field in store
136+
return field in self.store
132137

133138
def __getitem__(self, field):
134139
"""Get an element from the store"""

pyArango/query.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ def __init__(self, database, query, batchSize, bindVars, options, count, fullCou
156156
except QueryError as e:
157157
raise AQLQueryError( message = e.message, query = self.query, errors = e.errors)
158158

159-
def explain(self, bindVars={}, allPlans = False):
159+
def explain(self, bindVars = None, allPlans = False):
160160
"""Returns an explanation of the query. Setting allPlans to True will result in ArangoDB returning all possible plans. False returns only the optimal plan"""
161+
if bindVars is None:
162+
bindVars = {}
161163
return self.database.explainAQLQuery(self.query, bindVars, allPlans)
162164

163165
def _raiseInitFailed(self, request):

pyArango/tests/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,16 @@ def test_document_fetch_by_key(self):
336336
doc2 = collection.fetchDocument(doc._key)
337337
self.assertEqual(doc._id, doc2._id)
338338

339+
def test_database_contains_id(self):
340+
collection = self.db.createCollection(name="lala")
341+
doc = collection.createDocument()
342+
doc["name"] = 'iop'
343+
doc.save()
344+
result = doc["_id"] in self.db
345+
self.assertTrue(result)
346+
result = doc["_id"] + '1' in self.db
347+
self.assertFalse(result)
348+
339349
# @unittest.skip("stand by")
340350
def test_document_set_private_w_rest(self):
341351
collection = self.db.createCollection(name = "lala")

pyArango/theExceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ def __str__(self):
178178

179179
class ExportError(pyArangoException):
180180
""" Something went wrong using the export cursor """
181-
def __init__(self, message, errors = {} ):
181+
def __init__(self, message, errors = None ):
182+
if errors is None:
183+
errors = {}
182184
pyArangoException.__init__(self, message, errors)
183185

184186
class DocumentNotFoundError(pyArangoException):

0 commit comments

Comments
 (0)