updated advanced map/reduce test to include scope; misc cleanup in queryset

This commit is contained in:
blackbrrr 2010-02-23 22:26:05 -06:00
parent 008a62e4e9
commit a19a7b976c
2 changed files with 128 additions and 125 deletions

View File

@ -12,7 +12,7 @@ REPR_OUTPUT_SIZE = 20
class DoesNotExist(Exception): class DoesNotExist(Exception):
pass pass
class MultipleObjectsReturned(Exception): class MultipleObjectsReturned(Exception):
@ -99,16 +99,14 @@ class Q(object):
# Construct the JS that uses this op # Construct the JS that uses this op
operation_js = Q.OPERATORS[op.strip('$')] % { operation_js = Q.OPERATORS[op.strip('$')] % {
'field': key, 'field': key,
'value': op_value_name 'value': op_value_name}
}
js.append(operation_js) js.append(operation_js)
else: else:
js_scope[value_name] = value js_scope[value_name] = value
# Construct the JS for this field # Construct the JS for this field
field_js = Q.OPERATORS[op.strip('$')] % { field_js = Q.OPERATORS[op.strip('$')] % {
'field': key, 'field': key,
'value': value_name 'value': value_name}
}
js.append(field_js) js.append(field_js)
return ' && '.join(js) return ' && '.join(js)
@ -330,7 +328,7 @@ class QuerySet(object):
may be provided as a keyword argument called :attr:`defaults`. may be provided as a keyword argument called :attr:`defaults`.
""" """
defaults = query.get('defaults', {}) defaults = query.get('defaults', {})
if query.has_key('defaults'): if 'defaults' in query:
del query['defaults'] del query['defaults']
self.__call__(*q_objs, **query) self.__call__(*q_objs, **query)
@ -431,6 +429,7 @@ class QuerySet(object):
if scope: if scope:
mr_args['scope'] = scope mr_args['scope'] = scope
if limit: if limit:
mr_args['limit'] = limit mr_args['limit'] = limit

View File

@ -505,8 +505,8 @@ class QuerySetTest(unittest.TestCase):
Song.drop_collection() Song.drop_collection()
def test_map_reduce_finalize(self): def test_map_reduce_finalize(self):
"""Ensure finalize is running by simulating "hotness" """Ensure that map, reduce, and finalize run and introduce "scope"
ranking with Reddit algorithm. by simulating "hotness" ranking with Reddit algorithm.
""" """
from time import mktime from time import mktime
@ -552,8 +552,7 @@ class QuerySetTest(unittest.TestCase):
map_f = """ map_f = """
function() { function() {
emit(this._id, {up_delta: this.up_votes - this.down_votes, emit(this._id, {up_delta: this.up_votes - this.down_votes,
reddit_epoch: new Date(2005, 12, 8, 7, 46, 43, 0).getTime(), sub_date: this.submitted.getTime() / 1000})
sub_date: this.submitted.getTime()})
} }
""" """
@ -564,8 +563,7 @@ class QuerySetTest(unittest.TestCase):
x = data.up_delta; x = data.up_delta;
// calculate time diff between reddit epoch and submission // calculate time diff between reddit epoch and submission
sec_since_epoch = data.sub_date - data.reddit_epoch; sec_since_epoch = data.sub_date - reddit_epoch;
sec_since_epoch /= 1000;
// calculate 'Y' // calculate 'Y'
if(x > 0) { if(x > 0) {
@ -596,9 +594,15 @@ class QuerySetTest(unittest.TestCase):
} }
""" """
reddit_epoch = mktime(datetime(2005, 12, 8, 7, 46, 43).timetuple())
scope = {'reddit_epoch': reddit_epoch}
# ensure both artists are found # ensure both artists are found
results = Link.objects.order_by("-value") results = Link.objects.order_by("-value")
results = results.map_reduce(map_f, reduce_f, finalize_f=finalize_f) results = results.map_reduce(map_f,
reduce_f,
finalize_f=finalize_f,
scope=scope)
results = list(results) results = list(results)
# assert troublesome Buzz article is ranked 1st # assert troublesome Buzz article is ranked 1st