def test_retrieval_by_id(self): """Exercise collection retrieval with a single ID""" (p, c, ah) = (self.profile, self.collection, self.auth_header) wbo_id = '1234' w = WBO(wbo_id=wbo_id, parent=c, collection=c, modified=WBO.get_time_now(), sortindex=1000, payload='payload-%s' % wbo_id, payload_size=9) w.put() url = '/sync/1.0/%s/storage/%s?id=%s' % (p.user_name, c.name, w.wbo_id) resp = self.app.get(url, headers=ah) result_data = simplejson.loads(resp.body) self.log.debug('RESPONSE %s' % resp.body) self.assertEqual(w.wbo_id, result_data[0]) url = '/sync/1.0/%s/storage/%s?id=%s&full=1' % (p.user_name, c.name, w.wbo_id) resp = self.app.get(url, headers=ah) result_data = simplejson.loads(resp.body) self.log.debug('RESPONSE %s' % resp.body) self.assertEqual(w.payload, result_data[0]['payload'])
def build_wbo_parents_and_predecessors(self): (p, c, ah) = (self.profile, self.collection, self.auth_header) id_sets = dict([(kind, set([w[kind] for w in self.wbo_values])) for kind in ('parentid', 'predecessorid')]) for kind, id_set in id_sets.items(): for wbo_id in id_set: w = WBO(parent=c, collection=c, modified=WBO.get_time_now(), wbo_id=wbo_id, payload=simplejson.dumps({'random': 'xxx'})) w.put()
def build_wbo_set(self, num_wbos=15): (p, c, ah) = (self.profile, self.collection, self.auth_header) self.build_wbo_parents_and_predecessors() wbos = [] for values in self.wbo_values: w = WBO(parent=c, collection=c, modified=WBO.get_time_now(), wbo_id=values['id'], parentid=values['parentid'], predecessorid=values['predecessorid'], sortindex=values['sortindex'], payload=values['payload']) w.put() wbos.append(w) time.sleep(0.1) # HACK: Delay to ensure modified stamps vary return wbos
def test_retrieval_by_index_above_and_below(self): """Exercise collection retrieval on sortindex range""" (p, c, ah) = (self.profile, self.collection, self.auth_header) wbo_sortindexes = (-100, -10, -1, 0, 1, 10, 23, 100, 999, 1000, 9999) wbos = [] for idx in range(len(wbo_sortindexes)): sortindex = wbo_sortindexes[idx] wbo_id = '%s' % idx w = WBO(wbo_id=wbo_id, parent=c, collection=c, modified=WBO.get_time_now(), sortindex=sortindex, payload='payload-%s' % wbo_id, payload_size=9) w.put() self.log.debug("WBO %s" % simplejson.dumps(w.to_dict())) wbos.append(w) # TODO: Try a variety of ranges here? (index_above, index_below) = (-10, 1000) expected_ids = [ w.wbo_id for w in wbos if index_above < w.sortindex and w.sortindex < index_below ] url = '/sync/1.0/%s/storage/%s?index_above=%s&index_below=%s' % ( p.user_name, c.name, index_above, index_below) resp = self.app.get(url, headers=ah) result_data = simplejson.loads(resp.body) expected_ids.sort() result_data.sort() self.log.debug("URL %s" % url) self.log.debug("EXPECTED %s" % simplejson.dumps(expected_ids)) self.log.debug("RESULT %s" % resp.body) self.assertEqual(expected_ids, result_data)
def test_retrieval_by_multiple_ids(self): """Exercise collection retrieval with multiple IDs""" (p, c, ah) = (self.profile, self.collection, self.auth_header) wbos = [ WBO(wbo_id='%s' % wbo_id, parent=c, collection=c, modified=WBO.get_time_now(), sortindex=1000, payload='payload-%s' % wbo_id, payload_size=9) for wbo_id in range(10) ] for w in wbos: w.put() wbo_ids = [w.wbo_id for w in wbos] url = '/sync/1.0/%s/storage/%s?ids=%s' % (p.user_name, c.name, ','.join(wbo_ids)) resp = self.app.get(url, headers=ah) result_data = simplejson.loads(resp.body) wbo_ids.sort() result_data.sort() self.assertEqual(wbo_ids, result_data) self.assertEqual(len(wbo_ids), int(resp.headers['X-Weave-Records'])) url = '/sync/1.0/%s/storage/%s?ids=%s&full=1' % (p.user_name, c.name, ','.join(wbo_ids)) resp = self.app.get(url, headers=ah) result_data = simplejson.loads(resp.body) result_data.sort(lambda a, b: cmp(a['id'], b['id'])) for idx in range(len(wbos)): self.assertEqual(wbos[idx].payload, result_data[idx]['payload']) self.assertEqual(len(wbo_ids), int(resp.headers['X-Weave-Records']))
def test_item_validation(self): """Exercise WBO data validation""" (p, c, ah) = (self.profile, self.collection, self.auth_header) too_long_id = ''.join('x' for x in range(100)) self.assert_('invalid id' in WBO.validate({'id': ''})) self.assert_('invalid id' in WBO.validate({'id': 'foo/bar'})) self.assert_('invalid id' in WBO.validate({'id': too_long_id})) self.assert_('invalid id' not in WBO.validate({'id': 'abcd'})) self.assert_('invalid collection' in WBO.validate({})) self.assert_('invalid collection' in WBO.validate( {'collection': Collection(name=too_long_id, profile=p)})) self.assert_('invalid predecessorid' in WBO.validate( { 'collection': c, 'predecessorid': too_long_id })) self.assert_( 'invalid predecessorid' in WBO.validate({ 'collection': c, 'predecessorid': 'abcdef' })) w = WBO(parent=c, collection=c, wbo_id='abcdef', modified=WBO.get_time_now(), payload='test') w.put() self.assert_('invalid predecessorid' not in WBO.validate( { 'collection': c, 'predecessorid': 'abcdef' })) self.assert_('invalid predecessorid' in WBO.validate( { 'collection': c, 'predecessorid': too_long_id })) self.assert_( 'invalid predecessorid' in WBO.validate({ 'collection': c, 'predecessorid': 'defghi' })) w = WBO(parent=c, collection=c, wbo_id='defghi', modified=WBO.get_time_now(), payload='test') w.put() self.assert_('invalid predecessorid' not in WBO.validate( { 'collection': c, 'predecessorid': 'abcdef' })) self.assert_( 'invalid modified date' in WBO.validate({'modified': 'abc'})) self.assert_('no modification date' in WBO.validate({})) self.assert_('no modification date' in WBO.validate({'modified': ''})) self.assert_( 'invalid sortindex' in WBO.validate({'sortindex': 'abcd'})) self.assert_( 'invalid sortindex' in WBO.validate({'sortindex': -1000000000})) self.assert_( 'invalid sortindex' in WBO.validate({'sortindex': 1000000000})) self.assert_('payload needs to be json-encoded' in WBO.validate( {'payload': 'abcd'})) self.assert_('payload too large' in WBO.validate( {'payload': 'x'.join('x' for x in range(500000))}))