def get(self): bucket = RiakBucket(client=None, name="tornadotest") test_object = RiakObject(client=None, bucket=bucket, key="test_object1") test_object.set_content_type("text/plain") test_object.set_data("This is a test object") self.client.put(self.on_response, test_object)
def test_object_valid_key(self): a = RiakObject(None, 'bucket', 'key') self.assertIsInstance(a, RiakObject, 'valid key name is rejected') try: b = RiakObject(None, 'bucket', '') except ValueError: b = None self.assertIsNone(b, 'empty object key not allowed')
def test_object_eq(self): a = RiakObject(None, 'bucket', 'key') b = RiakObject(None, 'bucket', 'key') self.assertEqual(a, b) default_bt = BucketType(None, "default") bucket_a = RiakBucket('client', 'a', default_bt) bucket_b = RiakBucket('client', 'a', default_bt) c = RiakObject(None, bucket_a, 'key') d = RiakObject(None, bucket_b, 'key') self.assertEqual(c, d)
def _decoded_contents(self, resp, old_obj): contents = [] for c in resp.content: new_obj = RiakObject(old_obj.client, old_obj.bucket, old_obj.key) new_obj.vclock = resp.vclock contents.append(self.decode_content(c, new_obj)) if contents: ret = contents[0] if len(contents) > 1: ret.siblings = contents[:] return ret else: return old_obj
def test_object_nq(self): a = RiakObject(None, 'bucket', 'key') b = RiakObject(None, 'bucket', 'not key') c = RiakObject(None, 'not bucket', 'key') self.assertNotEqual(a, b, 'matched with different keys') self.assertNotEqual(a, c, 'matched with different buckets') default_bt = BucketType(None, "default") foo_bt = BucketType(None, "foo") bucket_a = RiakBucket('client', 'a', default_bt) bucket_b = RiakBucket('client', 'a', foo_bt) c = RiakObject(None, bucket_a, 'key') d = RiakObject(None, bucket_b, 'key') self.assertNotEqual(c, d)
def test_multiput_errors(self): """ Unrecoverable errors are captured along with the bucket/key and not propagated. """ client = self.create_client(http_port=DUMMY_HTTP_PORT, pb_port=DUMMY_PB_PORT) bucket = client.bucket(self.bucket_name) k1 = self.randname() k2 = self.randname() o1 = RiakObject(client, bucket, k1) o2 = RiakObject(client, bucket, k2) if PY2: o1.encoded_data = k1 o2.encoded_data = k2 else: o1.data = k1 o2.data = k2 objs = [o1, o2] for robj in objs: robj.content_type = 'text/plain' results = client.multiput(objs, return_body=True) for failure in results: self.assertIsInstance(failure, tuple) self.assertIsInstance(failure[0], RiakObject) if PY2: self.assertIsInstance(failure[1], StandardError) # noqa else: self.assertIsInstance(failure[1], Exception) client.close()
def test_multiput_pool_options(self): sz = 4 client = self.create_client(multiput_pool_size=sz) self.assertEqual(sz, client._multiput_pool._size) bucket = client.bucket(self.bucket_name) k1 = self.randname() k2 = self.randname() o1 = RiakObject(client, bucket, k1) o2 = RiakObject(client, bucket, k2) if PY2: o1.encoded_data = k1 o2.encoded_data = k2 else: o1.data = k1 o2.data = k2 objs = [o1, o2] for robj in objs: robj.content_type = 'text/plain' results = client.multiput(objs, return_body=False) for obj in results: if client.protocol == 'pbc': self.assertIsInstance(obj, RiakObject) self.assertFalse(obj.exists) self.assertEqual(obj.content_type, 'text/plain') else: self.assertIsNone(obj) client.close()
def test_multiput_pool_size(self): """ The pool size for multiputs can be configured at client initiation time. Multiput still works as expected. """ client = self.create_client(multiput_pool_size=2) self.assertEqual(2, client._multiput_pool._size) bucket = client.bucket(self.bucket_name) k1 = self.randname() k2 = self.randname() o1 = RiakObject(client, bucket, k1) o2 = RiakObject(client, bucket, k2) if PY2: o1.encoded_data = k1 o2.encoded_data = k2 else: o1.data = k1 o2.data = k2 objs = [o1, o2] for robj in objs: robj.content_type = 'text/plain' results = client.multiput(objs, return_body=True) for obj in results: self.assertIsInstance(obj, RiakObject) self.assertTrue(obj.exists) self.assertEqual(obj.content_type, 'text/plain') if PY2: self.assertEqual(obj.key, obj.encoded_data) else: self.assertEqual(obj.key, obj.data) client.close()
def new(self, key=None, data=None, content_type='application/json', encoded_data=None): """A shortcut for manually instantiating a new :class:`~riak.riak_object.RiakObject` or a new :class:`~riak.datatypes.Datatype`, based on the presence and value of the :attr:`datatype <BucketType.datatype>` bucket property. When the bucket contains a :class:`~riak.datatypes.Datatype`, all arguments are ignored except ``key``, otherwise they are used to initialize the :class:`~riak.riak_object.RiakObject`. :param key: Name of the key. Leaving this to be None (default) will make Riak generate the key on store. :type key: str :param data: The data to store in a :class:`~riak.riak_object.RiakObject`, see :attr:`RiakObject.data <riak.riak_object.RiakObject.data>`. :type data: object :param content_type: The media type of the data stored in the :class:`~riak.riak_object.RiakObject`, see :attr:`RiakObject.content_type <riak.riak_object.RiakObject.content_type>`. :type content_type: str :param encoded_data: The encoded data to store in a :class:`~riak.riak_object.RiakObject`, see :attr:`RiakObject.encoded_data <riak.riak_object.RiakObject.encoded_data>`. :type encoded_data: str :rtype: :class:`~riak.riak_object.RiakObject` or :class:`~riak.datatypes.Datatype` """ if self.bucket_type.datatype: return TYPES[self.bucket_type.datatype](bucket=self, key=key) if PY2: try: if isinstance(data, string_types): data = data.encode('ascii') except UnicodeError: raise TypeError('Unicode data values are not supported.') obj = RiakObject(self._client, self, key) obj.content_type = content_type if data is not None: obj.data = data if encoded_data is not None: obj.encoded_data = encoded_data return obj
def index_bucket(bucket_name, project): """ Associates a bucket with a project. Args: bucket_name: A string containing the bucket name. project: A string containing the project ID. """ bucket = riak_connection.bucket(config['METADATA_BUCKET']) obj = RiakObject(riak_connection, bucket, bucket_name) obj.content_type = 'application/json' obj.data = '{}' obj.add_index('project_bin', project) obj.store()
def get(self, key, r=None, pr=None, timeout=None, include_context=None, basic_quorum=None, notfound_ok=None): """ Retrieve an :class:`~riak.riak_object.RiakObject` or :class:`~riak.datatypes.Datatype`, based on the presence and value of the :attr:`datatype <BucketType.datatype>` bucket property. :param key: Name of the key. :type key: string :param r: R-Value of the request (defaults to bucket's R) :type r: integer :param pr: PR-Value of the request (defaults to bucket's PR) :type pr: integer :param timeout: a timeout value in milliseconds :type timeout: int :param include_context: if the bucket contains datatypes, include the opaque context in the result :type include_context: bool :param basic_quorum: whether to use the "basic quorum" policy for not-founds :type basic_quorum: bool :param notfound_ok: whether to treat not-found responses as successful :type notfound_ok: bool :rtype: :class:`RiakObject <riak.riak_object.RiakObject>` or :class:`~riak.datatypes.Datatype` """ if self.bucket_type.datatype: return self._client.fetch_datatype(self, key, r=r, pr=pr, timeout=timeout, include_context=include_context, basic_quorum=basic_quorum, notfound_ok=notfound_ok) else: obj = RiakObject(self._client, self, key) return obj.reload(r=r, pr=pr, timeout=timeout, basic_quorum=basic_quorum, notfound_ok=notfound_ok)
def test_object_hash(self): a = RiakObject(None, 'bucket', 'key') b = RiakObject(None, 'bucket', 'key') c = RiakObject(None, 'bucket', 'not key') self.assertEqual(hash(a), hash(b), 'same object has different hashes') self.assertNotEqual(hash(a), hash(c), 'different object has same hash') default_bt = BucketType(None, "default") foo_bt = BucketType(None, "foo") bucket_a = RiakBucket('client', 'a', default_bt) bucket_b = RiakBucket('client', 'a', foo_bt) d = RiakObject(None, bucket_a, 'key') e = RiakObject(None, bucket_a, 'key') f = RiakObject(None, bucket_b, 'key') g = RiakObject(None, bucket_b, 'not key') self.assertEqual(hash(d), hash(e), 'same object, same bucket_type has different hashes') self.assertNotEqual( hash(e), hash(f), 'same object, different bucket type has the ' 'same hash') self.assertNotEqual( hash(d), hash(g), 'different object, different bucket ' 'type has same hash')
def put(self, key, data): obj = RiakObject(self.client, self.bucket, key=key) obj.data = data obj.store()
def test_object_nq(self): a = RiakObject(None, 'bucket', 'key') b = RiakObject(None, 'bucket', 'not key') c = RiakObject(None, 'not bucket', 'key') self.assertNotEqual(a, b, 'matched with different keys') self.assertNotEqual(a, c, 'matched with different buckets')
def test_object_hash(self): a = RiakObject(None, 'bucket', 'key') b = RiakObject(None, 'bucket', 'key') c = RiakObject(None, 'bucket', 'not key') self.assertEqual(hash(a), hash(b), 'same object has different hashes') self.assertNotEqual(hash(a), hash(c), 'different object has same hash')
def index_bucket(bucket_name, project): """ Associates a bucket with a project. """ bucket = riak_connection.bucket(metadata_bucket) obj = RiakObject(riak_connection, bucket, bucket_name) obj.add_index('project', project) obj.store()
def test_object_eq(self): a = RiakObject(None, 'bucket', 'key') b = RiakObject(None, 'bucket', 'key') self.assertEqual(a, b)