-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
206 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
*.egg-info | ||
|
||
!/.travis.yml | ||
!/.landscape.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.