def testMultiFieldId(self): '''Here we check for multifield specific stuff like the instance related keys (keys which are related to the instance rather than the model).''' # get instance and field, the field has no data here obj, field = self.get_object_and_field() # get the object id id = to_string(obj.id) # get the field database key field_key = to_string(field.id) self.assertTrue(id in field_key) # backend = obj.session.backend keys = backend.instance_keys(obj) if backend.name == 'redis': # field id should be in instance keys self.assertTrue(field.id in keys) lkeys = list(backend.model_keys(self.model._meta)) # the field has no data, so there is no key in the database self.assertFalse(field.id in lkeys) # # Lets add data self.adddata(obj) # The field id should be in the server keys if backend.name == 'redis': lkeys = list(backend.model_keys(self.model._meta)) self.assertTrue(field.id in lkeys) obj.delete() lkeys = list(backend.model_keys(self.model._meta)) self.assertFalse(field.id in lkeys)
def testValue(self): models = self.mapper v = models.simplemodel(code='cgfgcgf', somebytes=to_string('hello')) self.assertEqual(v.somebytes, b'hello') self.assertFalse(v.id) yield models.session().add(v) v = yield models.simplemodel.get(id=v.id) self.assertEqual(v.somebytes, b'hello')
def test_to_bytes(self): self.assertEqual(to_bytes(b'ciao'),b'ciao') b = b'perch\xc3\xa9' u = b.decode('utf-8') l = u.encode('latin') self.assertEqual(to_bytes(b,'latin'),l) self.assertEqual(to_string(l,'latin'),u) self.assertEqual(to_bytes(1), b'1')
def loads(self, svalue): if svalue is not None: try: svalue = to_string(svalue,self.charset) return json.loads(svalue, object_hook = self.decoder_hook) except: logger.critical('Unhandled exception while loading Json\ field {0}'.format(self), exc_info = True)
def test_to_bytes(self): self.assertEqual(to_bytes(b'ciao'), b'ciao') b = b'perch\xc3\xa9' u = b.decode('utf-8') l = u.encode('latin') self.assertEqual(to_bytes(b, 'latin'), l) self.assertEqual(to_string(l, 'latin'), u) self.assertEqual(to_bytes(1), b'1')
def testUnicode(self): unicode_string = unichr(500) + to_string('ciao') + unichr(300) self.model(code = unicode_string).save() m = self.model.objects.get(id = 1) self.assertEqual(m.code, unicode_string) if ispy3k: self.assertEqual(str(m),unicode_string) else: code = unicode_string.encode('utf-8') self.assertEqual(str(m),code)
def testUnicode(self): models = self.mapper unicode_string = unichr(500) + to_string('ciao') + unichr(300) m = yield models.simplemodel.new(code=unicode_string) m = yield models.simplemodel.get(id=m.id) self.assertEqual(m.code, unicode_string) if ispy3k: self.assertEqual(str(m), unicode_string) else: code = unicode_string.encode('utf-8') self.assertEqual(str(m), code)
def test_get_and_set(self): # get and set can't be tested independently of each other self.assertEquals(self.client.get('a'), None) byte_string = b'value' integer = 5 unicode_string = chr(3456) + to_string('abcd') + chr(3421) self.assert_(self.client.set('byte_string', byte_string)) self.assert_(self.client.set('integer', 5)) self.assert_(self.client.set('unicode_string', unicode_string)) self.assertEquals(self.client.get('byte_string'), byte_string) self.assertEquals(int(self.client.get('integer')), integer) self.assertEquals(self.client.get('unicode_string').decode('utf-8'), unicode_string)
def search(self, value, maxelem = -1): '''Search for ``value`` in the ordered dataset and return an iterator of suggested words''' M = len(value) if M < self.minlen: raise StopIteration rank = self.data.rank(value) if rank is not None: if maxelem > 0: end = rank + maxelem elif maxelem < 0: end = maxelem else: end = rank elems = self.data.range(rank, end) echar = self.endchar N = len(echar) for elem in elems: elem = to_string(elem) if elem[:M] != value: raise StopIteration if elem.endswith(echar): yield elem[:-N]
def testItems(self): d = self.fill() for k,v in d.data.items(): k = to_string(k) self.assertEqual(v,self.data.pop(k)) self.assertEqual(len(self.data),0)
def testValue(self): v = SimpleModel(code='one', somebytes=to_string('hello')) self.assertEqual(v.somebytes, b'hello') v.save() v = SimpleModel.objects.get(code = 'one') self.assertEqual(v.somebytes,b'hello')
def pk_to_python(self, id): return to_string(id)
def _items(self, slic): # Unwind the database query backend = self.backend meta = self.meta start, stop = self.get_redis_slice(slic) order = self.order(start, stop) fields = self.qs.fields or None if fields == ("id",): fields_attributes = fields elif fields: fields, fields_attributes = meta.backend_fields(fields) else: fields_attributes = () args = [self.query_key, backend.basekey(meta), len(fields_attributes)] args.extend(fields_attributes) if order: args.append("explicit") args.extend(order) else: if meta.ordering: order = "DESC" if meta.ordering.desc else "ASC" else: order = "" args.append(order) args.append(start) args.append(stop) options = {"fields": fields, "fields_attributes": fields_attributes} data = backend.client.script_call("load_query", *args, **options) result = backend.make_objects(meta, data) return self.load_related(result) if fields: pipe = client.pipeline() hmget = pipe.hmget for id in ids: hmget(bkey(meta, OBJ, to_string(id)), fields_attributes) # Load data if ids: meta = self.meta bkey = backend.basekey pipe = None fields = self.qs.fields or None fields_attributes = None if fields: fields, fields_attributes = meta.backend_fields(fields) if fields: pipe = client.pipeline() hmget = pipe.hmget for id in ids: hmget(bkey(meta, OBJ, to_string(id)), fields_attributes) else: pipe = client.pipeline() hgetall = pipe.hgetall for id in ids: hgetall(bkey(meta, OBJ, to_string(id))) if pipe is not None: result = backend.make_objects(meta, ids, pipe.execute(), fields, fields_attributes) else: result = backend.make_objects(meta, ids) return self.load_related(result) else: return ids
def __unicode__(self): return to_string('%s.%s' % (self.meta, self.name))
'session_data', 'instance_session_result', 'query_result', 'range_lookups', 'getdb', 'settings', 'async' ] query_result = namedtuple('query_result', 'key count') # tuple containing information about a commit/delete operation on the backend # server. Id is the id in the session, persistent is a boolean indicating # if the instance is persistent on the backend, bid is the id in the backend. instance_session_result = namedtuple('instance_session_result', 'iid persistent id deleted score') session_data = namedtuple('session_data', 'meta dirty deletes queries structures') session_result = namedtuple('session_result', 'meta results') pass_through = lambda x: x str_lower_case = lambda x: to_string(x).lower() range_lookups = { 'gt': int_or_float, 'ge': int_or_float, 'lt': int_or_float, 'le': int_or_float, 'contains': pass_through, 'startswith': pass_through, 'endswith': pass_through, 'icontains': str_lower_case, 'istartswith': str_lower_case, 'iendswith': str_lower_case }
def __unicode__(self): return to_string('%s.%s' % (self.meta,self.name))
def testKeys(self): d = self.fill() for k in d.data.keys(): k = to_string(k) self.data.pop(k) self.assertEqual(len(self.data),0)