def test_add_json_ext(self): try: import numpy as np except ImportError: self.skipTest("NumPy not installed") test = True try: Underverse.add_json_ext(np.ndarray, lambda obj: obj.tolist(), lambda obj: np.array(obj)) except: raise true = False self.assertTrue(test)
class KeyValueTestCase(unittest.TestCase): def setUp(self): self.uv = Underverse() def tearDown(self): self.uv.close() def test_add_key(self): # """creates a verse""" self.test = self.uv.test self.test.put('key', 5) test = True try: self.test.get('key') except Exception, e: test = False self.assertTrue(test)
import unittest, os from load_tests import LoadTestCase from find_tests import FindTestCase from mapreduce_tests import MapReduceTestCase from kv_test import KeyValueTestCase from underverse import Underverse from underverse.model import Document from test_data_gen import Person if __name__ == '__main__': # if not os.path.exists('speed_test_smaller_obj.sql'): uv = Underverse() test = uv.test_obj test.purge() data = [] for p in range(250): data.append(Person()) test.add(data) uv.dump('speed_test_smaller_obj.sql') if not os.path.exists('speed_test_smaller.sql'): uv = Underverse() test = uv.test test.purge() data = [] for p in range(250): data.append(Person().__dict__) test.add(data) uv.dump('speed_test_smaller.sql')
def setUp(self): self.uv = Underverse()
class LoadTestCase(unittest.TestCase): def setUp(self): self.uv = Underverse() def tearDown(self): self.uv.close() def test_create_verse(self): # """creates a verse""" self.users = self.uv.users self.assertIn(self.users._name, self.uv) def test_load_dict(self): # """loads a single python dict""" self.users = self.uv.users user = { 'name': 'ed', 'fullname':'Ed Jones', 'password':'******'} self.users.add(user) u = self.users.find_one(Document.name == 'ed', Document.password == '3d5_p455w0r6', Document.fullname == 'Ed Jones') # print u # print user # print (u.name == user['name'], u.fullname == user['fullname'], u.password == user['password']) # print (u['name'] == user['name'] and u['fullname'] == user['fullname'] and u['password'] == user['password']) self.assertTrue(u['name'] == user['name'] and u['fullname'] == user['fullname'] and u['password'] == user['password']) # exit() def test_load_dict_len(self): # """loads a single python dict""" self.users = self.uv.users user = { 'name': 'ed', 'fullname':'Ed Jones', 'password':'******'} self.users.add(user) self.assertTrue(len(self.users) == 1) def test_load_multiple_objs(self): # """loads several python class objects""" self.users = self.uv.users for person in range(50): self.users.add(Person()) self.assertTrue(len(self.users) == 50) def test_bulk_load(self): # """bulk loads several python class objects""" self.users = self.uv.users self.users.add([Person() for i in range(50)]) self.assertTrue(len(self.users) == 50) def test_dump(self): self.users = self.uv.users self.users.add([Person() for i in range(50)]) self.uv.dump('test.sql') self.assertTrue(os.path.exists('test.sql')) def test_load(self): self.uv.load('test.sql') self.assertTrue(len(self.uv.users) == 50) # @unittest.skip("takes a while") def test_load_large(self): self.uv.load('speed_test_smaller.sql') self.assertTrue(len(self.uv.test) == 250) def test_update(self): self.users = self.uv.users user = { 'name': 'ed', 'fullname':'Ed Jones', 'password':'******'} self.users.add(user) u = self.users.find_one(Document.name == 'ed', Document.password == '3d5_p455w0r6', Document.fullname == 'Ed Jones') u.age = 25 self.users.update(u) u = self.users.find_one(Document.name == 'ed', Document.password == '3d5_p455w0r6', Document.fullname == 'Ed Jones') self.assertTrue(hasattr(u, 'age') and u.age == 25) def test_update_list(self): self.users = self.uv.users user = { 'name': 'ed', 'fullname':'Ed Jones', 'password':'******'} self.users.add(user) u = self.users.find_one(Document.name == 'ed', Document.password == '3d5_p455w0r6', Document.fullname == 'Ed Jones') u.friends = ['Michael', 'Luke', 'Amy'] self.users.update(u) u = self.users.find_one(Document.name == 'ed', Document.password == '3d5_p455w0r6', Document.fullname == 'Ed Jones') self.assertTrue(hasattr(u, 'friends') and type(u.friends) == list and len(u.friends) == 3) def test_update_np_array(self): try: import numpy as np except ImportError: self.skipTest("NumPy not installed") self.users = self.uv.users user = { 'name': 'ed', 'fullname':'Ed Jones', 'password':'******'} self.users.add(user) u = self.users.find_one(Document.name == 'ed', Document.password == '3d5_p455w0r6', Document.fullname == 'Ed Jones') u.list = np.arange(5) self.users.update(u) u = self.users.find_one(Document.name == 'ed', Document.password == '3d5_p455w0r6', Document.fullname == 'Ed Jones') self.assertTrue(hasattr(u, 'list') and type(u.list) == np.ndarray) def test_add_json_ext(self): try: import numpy as np except ImportError: self.skipTest("NumPy not installed") test = True try: Underverse.add_json_ext(np.ndarray, lambda obj: obj.tolist(), lambda obj: np.array(obj)) except: raise true = False self.assertTrue(test) def test_load_and_read_objects(self): try: import numpy as np except ImportError: self.skipTest("NumPy not installed") test = self.uv.test test.add(AnotherTestClass('test #1')) good = True from underverse import NecRow for r in test: if not (type(r) == AnotherTestClass or type(r) == NecRow): good = False if not (type(r.a) == Comment): good = False if not (type(r.msg.n) == np.ndarray): good = False if not (type(r.msg.m) == Comment): good = False if not (type(r.msg) == Message): good = False self.assertTrue(good) def test_dump_objects(self): try: import numpy as np except ImportError: self.skipTest("NumPy not installed") test = self.uv.test test.add(AnotherTestClass('test #1')) self.uv.dump('obj_testing.sql') self.assertTrue(True) def test_load_objects(self): try: import numpy as np except ImportError: self.skipTest("NumPy not installed") # Underverse.create_mappers(AnotherTestClass, Comment, Message) self.uv.load('obj_testing.sql') good = True from underverse import NecRow for r in self.uv.test: if not (type(r) == AnotherTestClass or type(r) == NecRow): good = False if not (type(r.a) == Comment): good = False if not (type(r.msg.n) == np.ndarray): good = False if not (type(r.msg.m) == Comment): good = False if not callable(r.msg.m.len): good = False if not (type(r.msg) == Message): good = False self.assertTrue(good) def test_load_example(self): table = self.uv.helion #you can either add one row at a time table.add({'a':1, 'b': 2}) # or do bulk inserts array = [ {'a':1,'b':2,'c':3}, {'a':4,'b':5,'c':6}, {'a':7,'b':8,'c':9}] table.add(array) self.assertTrue(True) def test_load_add_column(self): table = self.uv.data # or do bulk inserts array = [ {'a':1,'b':2,'c':3}, {'a':4,'b':5,'c':6}, {'a':7,'b':8,'c':9}] table.add(array) table.add_column([1, 2, 3], 'd') self.assertTrue(len(list(table.d)) == 3) def test_load_array(self): table = self.uv.data # or do bulk inserts array = [[1,2,3],[4,5,6],[7,8,9]] table.from_array(array, names=['x', 'y', 'z']) self.assertTrue(len(list(table.x)) == 3) def test_load_datetime(self): table = self.uv.data # # if the object is a datetime.datetime, then it's converted to a Python list # # and saved in a Python dict with '__datetime__' as it's key # datetime_encoder = (lambda obj: True if isinstance(obj, datetime.datetime) else False, # lambda obj: {'__datetime__' : datetime.datetime.strftime(obj, "%Y-%m-%d %H:%M:%S.%f")}) # # when decoding, if the dict has a key '__datetime__', # # then the list is converted to a datetime.datetime object # datetime_decoder = (lambda obj: True if '__datetime__' in obj else False, # lambda obj: datetime.datetime.strptime(obj['__datetime__'], "%Y-%m-%d %H:%M:%S.%f")) # Underverse.add_json_ext(datetime_encoder, datetime_decoder, '__datetime__') dt = datetime.datetime.now() # print datetime.datetime.strftime(dt, "%Y-%m-%d %H:%M:%S.%f") table.add({'datetime': dt}) self.assertTrue(len(list(table.datetime)) == 1) d = table.find_one() # print d.datetime self.assertTrue(type(d.datetime) == datetime.datetime) def test_load_date(self): table = self.uv.data2 # # if the object is a datetime.date, then it's converted to a Python list # # and saved in a Python dict with '__date__' as it's key # date_encoder = (lambda obj: True if isinstance(obj, datetime.date) else False, # lambda obj: {'__date__' : datetime.datetime.strftime(obj, "%Y-%m-%d")}) # # when decoding, if the dict has a key '__date__', # # then the list is converted to a datetime.date object # date_decoder = (lambda obj: True if '__date__' in obj else False, # lambda obj: datetime.datetime.strptime(obj['__date__'], "%Y-%m-%d").date()) # Underverse.add_json_ext(date_encoder, date_decoder, '__date__') dt = datetime.datetime.now().date() # print datetime.datetime.strftime(dt, "%Y-%m-%d %H:%M:%S.%f") table.add({'date': dt}) self.assertTrue(len(list(table.date)) == 1) d = table.find_one() # print d # print d.date, type(d.date) self.assertTrue(type(d.date) == datetime.date) def test_load_time(self): table = self.uv.data2 # # if the object is a datetime.time, then it's converted to a Python list # # and saved in a Python dict with '__time__' as it's key # time_encoder = (lambda obj: True if isinstance(obj, datetime.time) else False, # lambda obj: {'__time__' : obj.strftime("%H:%M:%S.%f")}) # # when decoding, if the dict has a key '__time__', # # then the list is converted to a datetime.time object # time_decoder = (lambda obj: True if '__time__' in obj else False, # lambda obj: datetime.datetime.strptime(obj['__time__'], "%H:%M:%S.%f").time()) # Underverse.add_json_ext(time_encoder, time_decoder, '__time__') dt = datetime.datetime.now().time() # print datetime.datetime.strftime(dt, "%Y-%m-%d %H:%M:%S.%f") table.add({'time': dt}) self.assertTrue(len(list(table.time)) == 1) d = table.find_one() # print d # print d.time, type(d.time) self.assertTrue(type(d.time) == datetime.time)
def setUp(self): self.uv = Underverse() self.uv.load('speed_test_smaller.sql')
class FindTestCase(unittest.TestCase): def setUp(self): self.uv = Underverse() self.uv.load('speed_test_smaller.sql') def tearDown(self): self.uv.close() def test_find_one(self): self.test = self.uv.test me = self.uv.test.find_one(Document.name == 'Max') # print me, type(me) self.assertTrue(me.name == 'Max') def test_eq(self): eq = True ppl = self.uv.test.find(Document.name == 'Max') for person in ppl: if person.name != 'Max': eq = False self.assertTrue(eq) def test_ne(self): test = True ppl = self.uv.test.find(Document.name != 'Max') for person in ppl: if person.name == 'Max': test = False self.assertTrue(test) def test_lt(self): test = True ppl = self.uv.test.find(Document.name < 27) for person in ppl: if person.name >= 27: test = False self.assertTrue(test) def test_lte(self): test = True ppl = self.uv.test.find(Document.name <= 27) for person in ppl: if person.name > 27: test = False self.assertTrue(test) def test_gt(self): test = True ppl = self.uv.test.find(Document.name > 27) for person in ppl: if person.name <= 27: test = False self.assertTrue(test) def test_gte(self): test = True ppl = self.uv.test.find(Document.name >= 27) for person in ppl: if person.name < 27: test = False self.assertTrue(test) def test_len(self): test = True ppl = self.uv.test.find(Document.name.len(3)) for person in ppl: if len(person.name) != 3: test = False self.assertTrue(test) def test_btw(self): test = True ppl = self.uv.test.find(Document.age.btw(18, 25)) for person in ppl: if person.age <= 18 and person.age <= 25: test = False self.assertTrue(test) def test_in(self): test = True ppl = self.uv.test.find(Document.age.in_([18, 25])) for person in ppl: if not person.age in [18, 25]: test = False self.assertTrue(test) def test_nin(self): test = True ppl = self.uv.test.find(Document.age.nin([18, 25])) for person in ppl: if person.age in [18, 25]: test = False self.assertTrue(test) def test_match(self): import random, re test = True ips = self.uv.ips for i in range(250): ips.add({'ip': '.'.join(str(random.randint(0, 255)) for i in range(4))}) ppl = ips.find(Document.ip.match('^\d+\.\d+\.\d+\.\d+$')) for ip in ips: if not re.compile('^\d+\.\d+\.\d+\.\d+$').match(ip.ip): test = False self.assertTrue(test) def test_match2(self): import random, re test = True ips = self.uv.ips for i in range(250): ips.add({'ip': '.'.join(str(random.randint(0, 255)) for i in range(4))+'@8080'}) ppl = ips.find(Document.ip.match('^\d+\.\d+\.\d+\.\d+$')) for ip in ips: if not re.compile('^\d+\.\d+\.\d+\.\d+$').match(ip.ip): test = False self.assertFalse(test) def test_search(self): import random, re test = True ips = self.uv.ips for i in range(250): ips.add({'ip': '.'.join(str(random.randint(0, 255)) for i in range(4))+'@8080'}) ppl = ips.find(Document.ip.search('\d+\.\d+\.\d+\.\d+')) for ip in ips: if not re.compile('\d+\.\d+\.\d+\.\d+').search(ip.ip): test = False self.assertTrue(test) def test_orderby(self): test = True ppl = self.uv.test.find(Document.orderby('age')) age = -1 for person in ppl: if person.age < age: test = False age = person.age self.assertTrue(test) def test_orderby2(self): test = True # print type(self.uv.test.find()) ppl = self.uv.test.find(Document.orderby('-age')) age = 100 for person in ppl: if person.age > age: test = False age = person.age self.assertTrue(test) def test_orderby3(self): test = True ppl = self.uv.test.find(Document.orderby('name','-age', 'college')) # print age = 100 name = 'aa' for person in ppl: # print person.name, person.age, person.college if person.name < name: test = False if person.name != name: age = 100 if person.age > age: test = False age = person.age def test_orderby4(self): test = True ppl = self.uv.test.find(Document.orderby()) # print prev = '' for person in ppl: # print person.name, person.age, person.college if str(person) < prev: test = False prev = str(person) def test_orderby5(self): test = True ppl = self.uv.test.orderby('name') prev = '' for person in ppl: if person.name < prev: test = False prev = person.name self.assertTrue(test) def test_orderby6(self): test = True ppl = self.uv.test.orderby('name','-age', 'college') # print age = 100 name = 'aa' for person in ppl: # print person.name, person.age, person.college if person.name < name: test = False if person.name != name: age = 100 if person.age > age: test = False age = person.age def test_orderby7(self): test = True ppl = self.uv.test.all().orderby('name','-age', 'college') # print age = 100 name = 'aa' for person in ppl: # print person.name, person.age, person.college if person.name < name: test = False if person.name != name: age = 100 if person.age > age: test = False age = person.age def test_orderby8(self): test = True ppl = self.uv.test.find(Document.age > 25).orderby('name','-age', 'college') # print age = 100 name = 'aa' for person in ppl: # print person.name, person.age, person.college if person.name < name: test = False if person.name != name: age = 100 if person.age > age: test = False age = person.age def test_find_all(self): self.test = self.uv.test length = len(list(self.test)) results = self.test.find() self.assertTrue(len(list(results)) == length) def test_find_call(self): self.test = self.uv.test length = len(list(self.test.find(Document.name == 'Max'))) results = self.test(Document.name == 'Max') self.assertTrue(len(list(results)) == length) def test_find_limit(self): self.test = self.uv.test # length = len(list(self.test)) results = self.test.find(Document.limit(5)) self.assertTrue(len(list(results)) == 5) def test_find_skip(self): self.test = self.uv.test length = len(list(self.test)) results = self.test.find(Document.skip(5)) self.assertTrue(len(list(results)) == length - 5) def test_limit(self): self.test = self.uv.test # length = len(list(self.test)) results = self.test.limit(5) self.assertTrue(len(list(results)) == 5) def test_skip(self): self.test = self.uv.test length = len(list(self.test)) results = self.test.skip(5) self.assertTrue(len(list(results)) == length - 5) def test_limit_skip(self): self.test = self.uv.test results = self.test.limit(5).skip(2) self.assertTrue(len(list(results)) == 3) def test_skip_limit(self): self.test = self.uv.test results = self.test.skip(5).limit(2) self.assertTrue(len(list(results)) == 2) def test_get_attr(self): self.test = self.uv.test names = list(self.test.all().name) self.assertTrue(type(names) == list and len(names) == 250) def test_get_nested(self): self.test = self.uv.test2 # self.uv.create_mappers(Comment, User) for i in range(5): ed_user = User('ed', 'Ed Jones', '3d5_p455w0r6') ed_user.comment = Comment('hey%s' % i) self.test.add(ed_user) u = self.test.find(Document.comment.text == 'hey1') # print list(u) self.assertTrue(u != None) for i in u: self.assertTrue(i.comment.text == 'hey1') def test_get_nested2(self): self.test2 = self.uv.test2 # self.uv.create_mappers(Comment, User) for i in range(5): ed_user = User('ed', 'Ed Jones', '3d5_p455w0r6') ed_user.comment = Comment('hey%s' % i) self.test2.add(ed_user) from underverse.model import Document as D u = self.test2.find_one(Document.comment.text == 'hey1') # print Document.comment.text == 'hey1' # for t in self.test2: # print t self.assertTrue(u != None) self.assertTrue(u.comment.text == 'hey1') def test_and(self): test = self.uv.test from underverse.predicates import AND r = test.find(AND(Document.name == 'Max', Document.age < 25)) for x in r: self.assertTrue(x.name == 'Max' and x.age < 25) def test_or(self): test = self.uv.test from underverse.predicates import OR r = test.find(OR(Document.name == 'Max', Document.name == 'Zaphod')) for x in r: self.assertIn(x.name, ['Max', 'Zaphod']) def test_complex_andor(self): test = self.uv.test from underverse.predicates import AND, OR # r = test.find(Document.name.in_(['Max', 'Zaphod']), Document.age < 35) # r = test.find(OR(Document.name == 'Max', Document.name == 'Zaphod'), Document.age < 35) r = test.find(OR(Document.age.btw(30, 35), Document.age.btw(60, 65))) for x in r: # print x self.assertTrue((x.age > 30 and x.age < 35) or (x.age > 60 and x.age < 65)) def test_complex_andor2(self): test = self.uv.test from underverse.predicates import AND, OR # r = test.find(Document.name.in_(['Max', 'Zaphod']), Document.age < 35) # r = test.find(OR(Document.name == 'Max', Document.name == 'Zaphod'), Document.age < 35) # select * from test where ((age between 30 and 35) or (age between 60 and 65)) and (name = 'Billy' or name = 'Zaphod')); r = test.find(OR(Document.age.btw(30, 35), Document.age.btw(60, 65)), OR(Document.name == 'Billy', Document.name == 'Zaphod')) # r = test.find(OR(D.age.btw(30, 35), D.age.btw(60, 65)), OR(D.name == 'Billy', D.name == 'Zaphod')) for x in r: # print x self.assertTrue((x.age > 30 and x.age < 35) or (x.age > 60 and x.age < 65)) self.assertIn(x.name, ['Billy', 'Zaphod']) def test_complex_andor3(self): test = self.uv.test from underverse.predicates import AND, OR # r = test.find(Document.name.in_(['Max', 'Zaphod']), Document.age < 35) # r = test.find(OR(Document.name == 'Max', Document.name == 'Zaphod'), Document.age < 35) # a = AND(Document.name == 'Zaphod', Document.age.btw(60, 65)) # print a # a2 = AND(Document.name == 'Billy', Document.age == 31) # print a2 # print OR(AND(Document.name == 'Zaphod', Document.age.btw(60, 65)), AND(Document.name == 'Billy', Document.age == 31)) # select * from test where (name = 'Billy' and age = 31) or (name = 'Zaphod' and (age between 60 and 65))); r = test.find(OR(AND(Document.name == 'Zaphod', Document.age.btw(60, 65)), AND(Document.name == 'Billy', Document.age == 31))) # r = test.find(OR(D.age.btw(30, 35), D.age.btw(60, 65)), OR(D.name == 'Billy', D.name == 'Zaphod')) for x in r: # print x self.assertTrue((x.age > 30 and x.age < 35) or (x.age > 60 and x.age < 65)) self.assertIn(x.name, ['Billy', 'Zaphod']) def test_paginate(self): test = self.uv.test_paging for i in range(15): test.add({'a': i, 'b': i*2, 'c': i*3}) for page in test.paginate(5): # print page # print len(page) self.assertTrue(len(page) == 5)
if __name__ == '__main__': # suite = unittest.TestSuite() # suite.addTest(FindTestCase('test_paginate')) suite = unittest.TestLoader().loadTestsFromTestCase(FindTestCase) unittest.TextTestRunner(verbosity=2).run(suite) exit() import json, numpy as np, time, random from underverse import NecRow # import time # print type(np.arange(5)) uv = Underverse() class AnotherTestClass(object): """docstring for AnotherTestClass""" def __init__(self, text): super(AnotherTestClass, self).__init__() self.a = Comment(text) self.msg = Message(2,3,4) self.array = range(3) def _sum(self, value): return sum(self.array) + value class Comment(object): """docstring for Comment""" def __init__(self, text):
class MapReduceTestCase(unittest.TestCase): def setUp(self): self.uv = Underverse() self.uv.load('speed_test_smaller.sql') self.uv.load('speed_test_smaller_obj.sql') # Underverse.create_mappers(Person) # print Underverse.class_names def tearDown(self): self.uv.close() def test_simple_reduce(self): self.test = self.uv.test count = self.test.simple_reduce(len) self.assertTrue(count == 250) def test_simple_reduce(self): self.test = self.uv.test count = SubVerse(self.test).simple_reduce(len) self.assertTrue(count == 250) def test_simple_reduce2(self): self.test = self.uv.test count = self.test.all().simple_reduce(len) self.assertTrue(count == 250) def test_groupby(self): self.test = self.uv.test unique_names = self.test.unique('name') groups = self.test.groupby('name') self.assertTrue(len(list(unique_names)) == len(list(groups))) def test_groupby2(self): self.test = self.uv.test worked = True # print self.test.groupby('name') for name, ppl in self.test.groupby('name'): if len(ppl) != len(list(self.test.find(D.name == name))): worked = False self.assertTrue(worked) def test_groupby3(self): self.test = self.uv.test unique_names = self.test.unique('name', 'age') groups = self.test.groupby('name', 'age') self.assertTrue(len(list(unique_names)) == len(list(groups))) def test_groupby4(self): self.test = self.uv.test worked = True for name, ppl in self.test.groupby('name'): if type(ppl) != SubVerse: worked = False self.assertTrue(worked) def test_map(self): self.test = self.uv.test worked = True def name_mapper(array): for doc in array: yield doc.name, doc for name, ppl in self.test.map(name_mapper): if type(ppl) != SubVerse: worked = False self.assertTrue(worked) def test_map2(self): self.test = self.uv.test unique_names = self.test.unique('name') worked = True def name_mapper(array): for doc in array: yield doc.name, doc groups = self.test.map(name_mapper) self.assertTrue(len(list(unique_names)) == len(list(groups))) def test_map3(self): self.test = self.uv.test worked = True def name_mapper(array): for doc in array: yield (doc.name, doc.age // 10 * 10), doc for name, age, ppl in SubVerse(self.test.orderby('name', 'age')).map(name_mapper): # print name, age, len(ppl) pass self.assertTrue(worked) # ================== def test_map_obj(self): self.test = self.uv.test_obj worked = True def name_mapper(array): for doc in array: # print type(doc), doc.name # print doc.__Person__, type(doc.__Person__) yield doc.name, doc # name_mapper = lambda x: [(y.name, y) for y in x] for name, ppl in self.test.map(name_mapper): # print name, len(ppl) if type(ppl) != SubVerse: worked = False self.assertTrue(worked) def test_map_obj2(self): self.test = self.uv.test_obj unique_names = self.test.unique('name') worked = True def name_mapper(array): for doc in array: yield doc.name, doc groups = self.test.map(name_mapper) g = len(list(groups)) us = len(list(unique_names)) self.assertTrue(g == us) def test_mapreduce(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, doc for name, count in test.mapreduce(name_mapper, len): found = test.find(D.name == name).simple_reduce(len) # print name, count, found self.assertTrue(count == found) def test_mapreduce2(self): test = self.uv.test def name_mapper(array): for doc in array: yield (doc.name, doc.age), doc for name, age, count in test.mapreduce(name_mapper, len): found = test.find(D.name == name, D.age == age).simple_reduce(len) # print name, age, count, found self.assertTrue(count == found) def test_reduce(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, 1 results = test.map(name_mapper) red = test.reduce(results, len, sort='-reduce') prev = 2**32 for name, count in red: # print name, count self.assertTrue(count <= prev) prev = count red = test.reduce(results, sum, sort='-reduce') prev = 2**32 for name, count in red: # print name, count self.assertTrue(count <= prev) prev = count def test_reduce2(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, doc results = test.map(name_mapper) red = test.reduce(results, len, sort='-map') prev = None for name, count in red: # print name, count if prev != None: self.assertTrue(name <= prev) prev = name def test_reduce3(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, doc results = test.map(name_mapper) red = test.reduce(results, len, sort='reduce') prev = -1 for name, count in red: # print name, count self.assertTrue(count >= prev) prev = count def test_reduce4(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, doc results = test.map(name_mapper) red = test.reduce(results, len, sort='map') prev = None for name, count in red: # print name, count if prev != None: self.assertTrue(name >= prev) prev = name def test_mapreduce_sort(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, 1 red = test.mapreduce(name_mapper, len, sort='-reduce') prev = 2**32 for name, count in red: # print name, count self.assertTrue(count <= prev) prev = count def test_mapreduce_sort2(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, doc red = test.mapreduce(name_mapper, len, sort='-map') prev = None for name, count in red: # print name, count if prev != None: self.assertTrue(name <= prev) prev = name def test_mapreduce_sort3(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, doc red = test.mapreduce(name_mapper, len, sort='reduce') prev = -1 for name, count in red: # print name, count self.assertTrue(count >= prev) prev = count def test_mapreduce_sort4(self): test = self.uv.test def name_mapper(array): for doc in array: yield doc.name, doc red = test.mapreduce(name_mapper, len, sort='map') prev = None for name, count in red: # print name, count if prev != None: self.assertTrue(name >= prev) prev = name