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

View File

@ -505,8 +505,8 @@ class QuerySetTest(unittest.TestCase):
Song.drop_collection()
def test_map_reduce_finalize(self):
"""Ensure finalize is running by simulating "hotness"
ranking with Reddit algorithm.
"""Ensure that map, reduce, and finalize run and introduce "scope"
by simulating "hotness" ranking with Reddit algorithm.
"""
from time import mktime
@ -552,8 +552,7 @@ class QuerySetTest(unittest.TestCase):
map_f = """
function() {
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()})
sub_date: this.submitted.getTime() / 1000})
}
"""
@ -564,8 +563,7 @@ class QuerySetTest(unittest.TestCase):
x = data.up_delta;
// calculate time diff between reddit epoch and submission
sec_since_epoch = data.sub_date - data.reddit_epoch;
sec_since_epoch /= 1000;
sec_since_epoch = data.sub_date - reddit_epoch;
// calculate 'Y'
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
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)
# assert troublesome Buzz article is ranked 1st