Skip to content

Commit

Permalink
Merge branch 'release/v0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed Mar 21, 2016
2 parents 3330559 + ee9e7ed commit 78210a5
Show file tree
Hide file tree
Showing 21 changed files with 206 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.egg-info

!/.travis.yml
!/.landscape.yml
6 changes: 6 additions & 0 deletions .landscape.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
python-targets:
- 2
- 3
pep8:
full: true
max-line-length: 79
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Join the chat at https://gitter.im/lig/mnj](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/lig/mnj?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://travis-ci.org/lig/mnj.svg?branch=develop)](https://travis-ci.org/lig/mnj)
[![Coverage Status](https://coveralls.io/repos/github/lig/mnj/badge.svg?branch=develop)](https://coveralls.io/github/lig/mnj?branch=develop)
[![Code Health](https://landscape.io/github/lig/mnj/develop/landscape.svg?style=flat)](https://landscape.io/github/lig/mnj/develop)

# Mnj

Expand Down
51 changes: 0 additions & 51 deletions mnj/__main__.py

This file was deleted.

6 changes: 5 additions & 1 deletion mnj/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
class OperatorMeta(type):

def __new__(cls, name, bases, attrs):
attrs['Sname'] = '$' + name.rstrip('_')
name_chunks = name.rstrip('_').split('_')
attrs['Sname'] = (
'$' +
name_chunks[0] +
''.join(chunk.capitalize() for chunk in name_chunks[1:]))
return type.__new__(cls, name, bases, attrs)


Expand Down
6 changes: 5 additions & 1 deletion mnj/operators/query/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from . import comparison, element, evaluation, logical
from . import array, bitwise, comparison, element, evaluation, logical
from .array import *
from .bitwise import *
from .comparison import *
from .element import *
from .evaluation import *
from .logical import *


__all__ = (
array.__all__ +
bitwise.__all__ +
comparison.__all__ +
element.__all__ +
evaluation.__all__ +
Expand Down
34 changes: 34 additions & 0 deletions mnj/operators/query/array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from mnj.document.query import Query
from mnj.operators.base import Operator, UnaryOperator
from mnj.operators.exceptions import MnjOperatorError


__all__ = ['all_', 'elem_match_', 'size_']


class all_(Operator):

def __init__(self, *values):
Operator.__init__(self, *values)


class elem_match_(UnaryOperator):

def __init__(self, query):
UnaryOperator.__init__(self, query)

def prepare(self, value):
return UnaryOperator.prepare(self, Query(value))


class size_(UnaryOperator):

def __init__(self, size):
UnaryOperator.__init__(self, size)

def prepare(self, value):

if not isinstance(value, int):
raise MnjOperatorError('`size` must be integer')

return UnaryOperator.prepare(self, value)
50 changes: 50 additions & 0 deletions mnj/operators/query/bitwise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from bson.binary import Binary
import six

from mnj.operators.base import UnaryOperator
from mnj.operators.exceptions import MnjOperatorError
from collections import Iterable


__all__ = [
'bits_all_set_', 'bits_any_set_', 'bits_all_clear_', 'bits_any_clear_']


class _bitwise(UnaryOperator):

def __init__(self, bitmask):
UnaryOperator.__init__(self, bitmask)

def prepare(self, value):

if isinstance(value, (Binary, int,)):
return value

if isinstance(value, six.binary_type):
return Binary(value)

if (
isinstance(value, Iterable) and
all(isinstance(item, int) for item in value)
):
return value

raise MnjOperatorError(
'`bitmask` must be one of: `bson.Binary`, `int`, `{}` or'
' iterable of integers'.format(six.binary_type.__name__))


class bits_all_set_(_bitwise):
pass


class bits_any_set_(_bitwise):
pass


class bits_all_clear_(_bitwise):
pass


class bits_any_clear_(_bitwise):
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def run_tests(self):

setup(
name='mnj',
version='0.2dev1',
version='0.2',
packages=find_packages(),
install_requires=install_requires,
tests_require=[
Expand Down
9 changes: 6 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ def database(mongo_client):
def data(database):
data = database.data
data.drop()
data.insert({'a': 1, 'b': 2})
data.insert({'a': 3, 'b': 4})
data.insert({'a': 5, 'b': 6})
data.insert({'_id': '11', 'a': 1, 'b': 1})
data.insert({'_id': '22', 'a': 2, 'b': 2})
data.insert({'_id': '33', 'a': 3, 'b': 3})
data.insert({'_id': '14', 'a': 1, 'b': 4})
data.insert({'_id': '25', 'a': 2, 'b': 5})
data.insert({'_id': '36', 'a': 3, 'b': 6})
yield data


Expand Down
26 changes: 26 additions & 0 deletions tests/functional/sample_queries/test_simple_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from mnj import *


def test_simple_001(data):
cur = data.find(q(a=1))
assert {doc['_id'] for doc in cur} == {'11', '14'}


def test_simple_002(data):
cur = data.find(q(a=ne_(2)))
assert {doc['_id'] for doc in cur} == {'11', '33', '14', '36'}


def test_simple_003(data):
cur = data.find(q(b=gt_(3)))
assert {doc['_id'] for doc in cur} == {'14', '25', '36'}


def test_simple_004(data):
cur = data.find(q(a=3) | q(b=2))
assert {doc['_id'] for doc in cur} == {'22', '33', '36'}


def test_simple_005(data):
cur = data.find(and_(q(a=1), q(b=4)))
assert {doc['_id'] for doc in cur} == {'14'}
16 changes: 16 additions & 0 deletions tests/functional/sample_queries/test_various_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from mnj import *


def test_various_001(data):
cur = data.find(q(a=in_(1, 2)))
assert {doc['_id'] for doc in cur} == {'11', '22', '14', '25'}


def test_various_002(data):
cur = data.find(q(b=mod_(3, 1)))
assert {doc['_id'] for doc in cur} == {'11', '14'}


def test_various_003(data):
cur = data.find(q(b=not_(gt_(2))) & q(b=ne_(1)))
assert {doc['_id'] for doc in cur} == {'22'}
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions tests/unit/operators/query/test_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from mnj import *
from mnj.operators.exceptions import MnjOperatorError


def test_all():
query = all_('bar', 42, None)
assert query == {'$all': ('bar', 42, None,)}


def test_elem_match():
query = elem_match_(q(gte_(80), lt_(85)))
assert query == {'$elemMatch': {'$gte': 80, '$lt': 85}}

query = elem_match_({'$gte': 80, '$lt': 85})
assert query == {'$elemMatch': {'$gte': 80, '$lt': 85}}


def test_size():
query = size_(2)
assert query == {'$size': 2}

with pytest.raises(MnjOperatorError):
size_('two')

with pytest.raises(MnjOperatorError):
size_('2')

with pytest.raises(MnjOperatorError):
size_(2.0)
24 changes: 24 additions & 0 deletions tests/unit/operators/query/test_bitwise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from bson.binary import Binary
import pytest

from mnj import *


@pytest.mark.parametrize('func,op', [
(bits_all_set_, '$bitsAllSet'),
(bits_any_set_, '$bitsAnySet'),
(bits_all_clear_, '$bitsAllClear'),
(bits_any_clear_, '$bitsAnyClear'),
])
def test_bitwise(func, op):
query = func(Binary(b'*'))
assert query == {op: Binary(b'*')}

query = func(b'*')
assert query == {op: Binary(b'*')}

query = func(42)
assert query == {op: 42}

query = func([1, 3, 5])
assert query == {op: [1, 3, 5]}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 78210a5

Please sign in to comment.