Skip to content

Commit

Permalink
Add Cursor Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 committed Mar 22, 2024
1 parent 3a437c2 commit 5961fb4
Showing 1 changed file with 55 additions and 7 deletions.
62 changes: 55 additions & 7 deletions pantherdb/pantherdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def find(self, **kwargs) -> Cursor | List[PantherDocument | dict]:
result = [d for _, d in self._find(documents, **kwargs) if d is not None]

if self.return_cursor:
return Cursor(result)
return Cursor(result, kwargs)
return result

def first(self, **kwargs) -> PantherDocument | dict | None:
Expand Down Expand Up @@ -450,24 +450,72 @@ def json(self) -> str:


class Cursor:
def __init__(self, documents: List[dict | PantherDocument]):
def __init__(self, documents: List[dict | PantherDocument], kwargs: dict):
self.documents = documents
self._cursor = 0
self.filter = kwargs # Used in Panther
self._cursor = -1
self._limit = None
self._sorts = None
self._skip = None
self.cls = None
self.response_type = None
self._condition_applied = False

def next(self):
if not self._condition_applied:
self._apply_conditions()

self._cursor += 1
if self._limit and self._cursor > self._limit:
raise StopIteration

try:
result = self.documents[self._cursor]
except IndexError:
raise StopIteration
self._cursor += 1

if self.response_type:
return self.response_type(result)
return result

__next__ = next

def __getitem__(self, index: int | slice) -> Union[Cursor, dict, ...]:
return self.documents[index]
if not self._condition_applied:
self._apply_conditions()

result = self.documents[index]
if isinstance(index, int) and self.response_type:
return self.response_type(result)
return result

def sort(self, sorts: List[Tuple[str, int]]):
for sort in sorts[::-1]:
self.documents.sort(key=lambda x: x[sort[0]], reverse=bool(sort[1] == -1))
self._sorts = sorts
return self

def skip(self, skip):
self._skip = skip
return self

def limit(self, limit: int):
self._limit = limit
return self

def _apply_conditions(self):
self._apply_sort()
self._apply_skip()
self._apply_limit()
self._condition_applied = True

def _apply_sort(self):
if self._sorts:
for sort in self._sorts[::-1]:
self.documents.sort(key=lambda x: x[sort[0]], reverse=bool(sort[1] == -1))

def _apply_skip(self):
if self._skip:
self.documents = self.documents[self._skip:]

def _apply_limit(self):
if self._limit:
self.documents = self.documents[:self._limit]

0 comments on commit 5961fb4

Please sign in to comment.