adewes / blitzdb

Blitz is a document-oriented database for Python that is backend-agnostic. It comes with a flat-file database for JSON documents and provides MongoDB-like querying capabilities.
http://blitzdb.readthedocs.org
MIT License
330 stars 37 forks source link

FileBackend.filter() does not support the sort_by arguement #25

Open dalethestirling opened 10 years ago

dalethestirling commented 10 years ago

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 commented 10 years ago

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