Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for __getitem__ improve coverage by 6% #4

Merged
merged 1 commit into from
Oct 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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