blitzdb icon indicating copy to clipboard operation
blitzdb copied to clipboard

FileBackend.filter() does not support the sort_by arguement

Open dalethestirling opened this issue 10 years ago • 1 comments

I have been using this feature for a project and have noticed that when I build the query

db.filter(data, {}, sort_by='seq')

It would return a QuerySet object in the same order as if i used the query

db.filter(data, {})

This is true even when i intentionally saved the records to the Document class in a non sequential order.

I tested using this code:

from blitzdb import FileBackend, Document

db = FileBackend('this.db')

class data(Document):
  pass


db.begin()
a = data({'seq': 3})
b = data({'seq': 5})
c = data({'seq': 1})
d = data({'seq': 4})
e = data({'seq': 2})

db.save(a)
db.save(b)
db.save(c)
db.save(d)
db.save(e)

db.commit()

# Filter 1
db.filter(data, {})[0]['seq']
# v0.2.4 return: 3
# fix return: 3

# Filter 2
db.filter(data, {}).sort('seq')[0]['seq']
# v0.2.4 return: 1
# fix return: 1

# Filter 3
from blitzdb.queryset import QuerySet as BaseQS
db.filter(data, {}).sort('seq', BaseQS.DESCENDING)[0]['seq']
# v0.2.4 return: 5
# fix return: 5

# Filter 4
db.filter(data, {}, sort_by='seq')[0]['seq']
# v0.2.4 return: 3 
# fix return: 1

I have built a fix for this and will add the pull request. I have also added tests to test_sorting.py

dalethestirling avatar Sep 25 '14 08:09 dalethestirling

Fix in pull request https://github.com/adewes/blitzdb/pull/26

dalethestirling avatar Sep 25 '14 08:09 dalethestirling