Skip to content

Commit

Permalink
added tests for basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
red-crown committed Mar 7, 2016
1 parent 911df14 commit d111e3f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.napoleon'
]

# Add any paths that contain templates here, relative to this directory.
Expand Down Expand Up @@ -290,3 +291,8 @@

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False

# -- Options for Napoleon extension ---------------------------------------
napoleon_google_docstring = True
napoleon_include_private_with_doc = True
napoleon_include_special_with_doc = True
7 changes: 7 additions & 0 deletions docs/modules.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rcquerybuilder
==============

.. toctree::
:maxdepth: 4

rcquerybuilder
30 changes: 30 additions & 0 deletions docs/rcquerybuilder.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
rcquerybuilder package
======================

Submodules
----------

rcquerybuilder.builder module
-----------------------------

.. automodule:: rcquerybuilder.builder
:members:
:undoc-members:
:show-inheritance:

rcquerybuilder.pipeline module
------------------------------

.. automodule:: rcquerybuilder.pipeline
:members:
:undoc-members:
:show-inheritance:


Module contents
---------------

.. automodule:: rcquerybuilder
:members:
:undoc-members:
:show-inheritance:
62 changes: 62 additions & 0 deletions rcquerybuilder/builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
"""redCrown MongoDB QueryBuilder
This module contains the builder and query classes for usage with ``pymongo``.
Example:
Basic Usage (select/find queries)::
import pymongo
mongo = pymongo.MongoClient()
db = mongo['db_foobar']
qb = Builder(collection=db['collection_foobar'])
qb.field('name').is_not_in(['Matthew', 'Boris']) \\
.field('age').gte(21) \\
.field('attributes').is_type('object')
# The query object forwards it's query to the collection
# on on the ``execute()`` call.
query = qb.get_query()
# cursor is a ``pymongo.cursor.Cursor`` instance.
cursor = qb.get_query().execute()
# Which is equivalent to:
collection.find({'name': {'$nin': ['Matthew', 'Boris']},
'age': {'$gte': 21},
'attributes': {'$type': 3}})
Update Queries::
qb.update(multi=True) \\
.field('foo').equals('bar').set('buzz') \\
.field('totals').gt(10) \\
.field('counter').inc(1) \\
.field('some_list').push({'name': 'testing', 'value': 'cool'})
update_result = qb.get_query().execute()
# Which is equivalent to:
collection.update_many(
{'foo': 'bar', 'totals': {'$gt': 10}},
{
'$set': {'foo': 'buzz'},
'$inc': {'counter': 1},
'$push': {'some_list': {'name': 'testing', 'value': 'cool'}}
}
)
"""


def expr():
"""Get a new ``Expr`` instance.
Returns:
Expr: The ``Expr`` instance.
"""
return Expr()


class Builder(object):
def __init__(self, collection):
self.collection = collection
Expand Down
44 changes: 44 additions & 0 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,50 @@ def test_basic_fluent_api(self):

assert query_list == {'name': 'foobar', 'fizz': {'$ne': None}}

def test_find_query(self):
qb = Builder(collection=None)
qb.field('name').is_not_in(['Matthew', 'Boris']) \
.field('age').gte(21) \
.field('attributes').is_type('object')

query_list = qb.get_query_list()

assert {'name': {'$nin': ['Matthew', 'Boris']},
'age': {'$gte': 21},
'attributes': {'$type': 3}} == query_list

def test_update_query(self):
qb = Builder(collection=None)

qb.update(multi=True) \
.field('foo').equals('bar').set('buzz') \
.field('totals').gt(10) \
.field('counter').inc(1) \
.field('some_list').push({'name': 'testing', 'value': 'cool'})

query_parts = qb.get_query().query

assert {'foo': 'bar',
'totals': {'$gt': 10}} == query_parts['query']

assert {'$set': {'foo': 'buzz'},
'$inc': {'counter': 1},
'$push': {'some_list': {'name': 'testing', 'value': 'cool'}}} == query_parts['newObj']

def test_insert_query(self):
qb = Builder(collection=None)

qb.insert() \
.field('name').set('awesome') \
.field('age').set(21) \
.field('attributes').set([0, 1, 2, 3])

insert_query = qb.get_query().query['newObj']

assert {'name': 'awesome',
'age': 21,
'attributes': [0, 1, 2, 3]} == insert_query


if __name__ == '__main__':
unittest.main()

0 comments on commit d111e3f

Please sign in to comment.