Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Added create_edges() method description
Browse files Browse the repository at this point in the history
  • Loading branch information
mklymyshyn committed Dec 14, 2013
1 parent 1f39ac8 commit 88766e3
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 15 deletions.
17 changes: 13 additions & 4 deletions arango/aql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .cursor import Cursor

__all__ = ("AQLQuery", "F", "V")
__all__ = ("AQLQuery", "F", "V", "S")

logger = logging.getLogger(__name__)

Expand All @@ -13,6 +13,11 @@ class Variable(object):
"""
def __init__(self, value):
self.value = value
self.invert = False

def __invert__(self):
self.invert = True
return self

def __repr__(self):
return "<AQL Variable: {}>".format(self.value)
Expand Down Expand Up @@ -40,7 +45,10 @@ def proceed_list(self, l):
result.append(item.build_query())
continue
if issubclass(type(item), Variable):
result.append(item.value)
if item.invert is True:
result.append('"{}"'.format(item.value))
else:
result.append(item.value)
continue

result.append(item)
Expand Down Expand Up @@ -74,7 +82,6 @@ def build_query(self):
"""
Proceed list of arguments
"""

return "{}({})".format(self.name, ", ".join(
self.proceed_list(self.args)))

Expand Down Expand Up @@ -457,12 +464,14 @@ def build_query(self):
self._built_query = query
return query

def execute(self):
def execute(self, wrapper=None):
"""
Execute query: create cursor, put binded variables
and return instance of :py:attr:`arango.cursor.Cursor` object
"""
self.cursor_args.update({"bindVars": self.bind_vars})
if wrapper is not None:
self.cursor_args.update({"wrapper": wrapper})

return Cursor(
self.connection, self.build_query(), **self.cursor_args)
Expand Down
4 changes: 4 additions & 0 deletions arango/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class RequestsBase(object):
"""
@classmethod
def build_response(cls, status, message, headers, body):
# NB: py3k
if str(type(body)) == "<class 'bytes'>":
body = body.decode("utf-8")

d = {
"text": body,
"headers": headers,
Expand Down
16 changes: 13 additions & 3 deletions arango/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class Collections(object):
"""connection) for Collections"""

COLLECTION_DOCUMENTS, COLLECTION_EDGES = 2, 3
COLLECTIONS_LIST_URL = "/_api/collection"

def __init__(self, connection):
Expand Down Expand Up @@ -217,14 +217,24 @@ def info(self, resource=""):
self.COLLECTION_DETAILS_PATH.format(self.name, resource)
).data

def create(self, waitForSync=False, **kwargs):
def create_edges(self, *args, **kwargs):
"""
Create new **Edges Collection** - sepcial
kind of collections to keep information about edges.
"""
kwargs.update({"type": Collections.COLLECTION_EDGES})
return self.create(*args, **kwargs)

def create(self, waitForSync=False,
type=Collections.COLLECTION_DOCUMENTS, **kwargs):
"""
Create new **Collection**. You can specify
``waitForSync`` argument (boolean) to wait until
collection will be synced to disk
"""
params = {"waitForSync": waitForSync,
"name": self.name}
"name": self.name,
"type": type}
params.update(kwargs)

response = self.connection.post(
Expand Down
7 changes: 3 additions & 4 deletions arango/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .exceptions import EdgeAlreadyCreated, EdgeNotYetCreated, \
EdgeIncompatibleDataType, \
DocumentIncompatibleDataType
from .utils import proxied_document_ref
from .utils import proxied_document_ref, json


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -258,8 +258,7 @@ def create(self, from_doc, to_doc, body=None, **kwargs):
params = {
"collection": self.collection.cid,
"from": from_doc_id,
"to": to_doc_id
}
"to": to_doc_id}

params.update(kwargs)

Expand All @@ -274,7 +273,7 @@ def create(self, from_doc, to_doc, body=None, **kwargs):
self.EDGE_PATH,
**params
),
data=body or {})
data=json.dumps(body or {}))

# define document ID
if response.status in [200, 201, 202]:
Expand Down
16 changes: 15 additions & 1 deletion arango/tests/tests_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,28 @@ class TestCollection(TestsBase):
def setUp(self):
super(TestCollection, self).setUp()
self.c = self.conn.collection.test
self.c_e = self.conn.collection.test_edges

def test_cid(self):
assert_equal(self.c.cid, "test")

def test_create(self):
self.c.create()

test_data = {"name": "test", "waitForSync": False}
test_data = {
"name": "test", "waitForSync": False,
"type": Collections.COLLECTION_DOCUMENTS}
test_args = {"data": json.dumps(test_data)}

assert_true(Client.post.called)
assert_equal(Client.post.call_args[1], test_args)

def test_create_edges(self):
self.c_e.create_edges()

test_data = {
"name": "test_edges", "waitForSync": False,
"type": Collections.COLLECTION_EDGES}
test_args = {"data": json.dumps(test_data)}

assert_true(Client.post.called)
Expand Down
13 changes: 13 additions & 0 deletions arango/tests/tests_collection_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ def test_collection_creation(self):
created = c.collection.test.create(waitForSync=True)
assert_true(created.cid in c.collection())

def test_edges_collection_creation(self):
c = self.conn
logger.info("Creating edges collection 'test'")

created = c.collection.test_edges.create_edges()
self.wait()

assert_true(created.cid in c.collection())
c.collection.test_edges.delete()

self.wait()
assert_false(created.cid in c.collection())

def test_colletion_deletion(self):
c = self.conn

Expand Down
7 changes: 6 additions & 1 deletion docs/source/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ It's quite easy to create collection::

assert len(c.collections()) == 1

# here we creating edges collection
c.test_edges.create_edges()

assert len(c.collections()) == 2

Collection ``test`` being created.

.. note::
Expand Down Expand Up @@ -74,7 +79,7 @@ documentation which describe :term:`Collections REST Api`

.. autoclass:: arango.collection.Collection
:members: cid, info, properties, query,
create, delete, rename,
create, create_edges, delete, rename,
count, __len__,
index, documents, edges,
load, unload, truncate
Expand Down
2 changes: 1 addition & 1 deletion docs/source/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ List of exceptions
Raised in case edge not found in database

``DatabaseAlreadyExist``
Raised during execution of database creation method
Raised during execution of database creation method
6 changes: 5 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Driver for Python is not entirely completed. It supports
**Collections**, **Documents**, **Indexes** **Cursors**
and partially **Edges**.

Presentation about
`Graph Databases and Python <http://www.slideshare.net/MaxKlymyshyn/odessapy2013-pdf>`_
with real-world examples how to work with **arango-python**.

.. _arangodb-description:

**ArangoDB** is an open-source database with a flexible data model
Expand Down Expand Up @@ -90,7 +94,7 @@ Contents


Arango versions, Platforms and Python versions
-----------------------------
----------------------------------------------

Supported versions of ArangoDB: **1.1x** and **1.2x**

Expand Down

0 comments on commit 88766e3

Please sign in to comment.