Skip to content

Commit

Permalink
Merge pull request #4 from mahdihaghverdi/getitem-tests
Browse files Browse the repository at this point in the history
add tests for `__getitem__` improve coverage by 6%
  • Loading branch information
seyed-dev authored Oct 28, 2023
2 parents 784529c + 3427512 commit ec2bc60
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/test_aggify.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from aggify import Aggify, F, Q, Cond
from mongoengine import Document, StringField, IntField

Expand All @@ -17,6 +19,45 @@ class BaseModel(Document):


class TestAggify:
def test__getitem__int_zero(self):
aggify = Aggify(BaseModel)
thing = aggify[0]
assert not thing.pipelines

def test__getitem__int_non_zero(self):
aggify = Aggify(BaseModel)
thing = aggify[1]
assert isinstance(thing, Aggify)
assert len(thing.pipelines) == 1
assert thing.pipelines[-1]['$limit'] == 1

thing = aggify[2]
assert thing.pipelines[-1]['$limit'] == 2
assert len(thing.pipelines) == 2

thing = thing[3]
assert thing.pipelines[-1]['$limit'] == 3
assert len(thing.pipelines) == 3

def test__getitem__slice(self):
aggify = Aggify(BaseModel)
thing = aggify[0:10]
assert isinstance(thing, Aggify)
assert thing.pipelines[-1]['$limit'] == 10
assert len(thing.pipelines) == 1 # cause start is zero and it is falsy

aggify = Aggify(BaseModel)
thing = aggify[2:10]
assert len(thing.pipelines) == 2
skip, limit = thing.pipelines[-2:]
assert skip['$skip'] == 2
assert limit['$limit'] == 8

def test__getitem__value_error(self):
with pytest.raises(ValueError) as err:
Aggify(BaseModel)['hello']

assert 'invalid' in err.__str__().lower()

# Test filtering and projection
def test_filter_and_project(self):
Expand Down

0 comments on commit ec2bc60

Please sign in to comment.