Skip to content

Commit

Permalink
Merge pull request #212 from octabytes/bugfix/fix-ordering-in-cursor
Browse files Browse the repository at this point in the history
Fix order in cursor
  • Loading branch information
AxeemHaider committed Sep 5, 2023
2 parents cd019ef + a898fd0 commit e5b8547
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Added CHANGELOG.md to the project ([Adrian Dankiv](https://github.com/adr-007))
* Improved model.to_dict() functionality. Now it uses field names by default and includes `include_id`, `include_key`, and `include_parent` arguments ([Adrian Dankiv](https://github.com/adr-007))
* Added "count" method to FilterQuery ([Adrian Dankiv](https://github.com/adr-007))
* Fix reverse order in cursor ([Adrian Dankiv](https://github.com/adr-007))

### Changes
* Updated node version to 17.x in python-package-testing.yml ([Azeem Haider](https://github.com/AzeemHaider))
Expand Down
6 changes: 0 additions & 6 deletions src/fireo/queries/filter_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from enum import Enum
from functools import partial

from google.api_core.exceptions import MethodNotImplemented
Expand All @@ -20,11 +19,6 @@
)


class Ordering(str, Enum):
ASCENDING = "Asc"
DESCENDING = "Desc"


class FilterQuery(BaseQuery):
"""Filter documents on the bases of where clauses
Expand Down
4 changes: 3 additions & 1 deletion src/fireo/utils/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from datetime import datetime
from typing import Optional, TYPE_CHECKING

from google.cloud.firestore_v1 import Query

from fireo.fields import DateTime
from fireo.utils.utils import get_nested_field_by_dotted_name

Expand Down Expand Up @@ -38,7 +40,7 @@ def extract(cls, query: 'FilterQuery') -> 'Cursor':

if query._order:
cursor['order'] = ','.join(
('-' if direction == 'DESC' else '') + name
('-' if direction == Query.DESCENDING else '') + name
for name, direction in query._order
)

Expand Down
15 changes: 15 additions & 0 deletions src/tests/v2.1.1/test_cursor_ordering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fireo.fields import TextField
from fireo.models import Model


class MyModel(Model):
field = TextField(column_name='my_field')


def test_reverse_order_by():
cursor = MyModel.collection.order('-field').fetch().cursor

MyModel.collection.create(field='a')
MyModel.collection.create(field='b')

assert [m.field for m in MyModel.collection.cursor(cursor).fetch()] == ['b', 'a']

0 comments on commit e5b8547

Please sign in to comment.