def test_lookup_in(self): cb = self.cb # Create the document key = self.gen_key('sdget') cb.upsert(key, { 'path1': 'value1' }) result = cb.retrieve_in(key, 'path1') self.assertEqual((0, 'value1'), result.get(0)) self.assertEqual((0, 'value1'), result.get('path1')) self.assertEqual('value1', result[0]) self.assertEqual('value1', result['path1']) self.assertTrue(result.cas) # Try when path is not found rv = cb.retrieve_in(key, 'path2') self.assertEqual(E.SubdocMultipleErrors.CODE, rv.rc) self.assertEqual(E.SubdocPathNotFoundError.CODE, rv.get(0)[0]) self.assertRaises(E.SubdocPathNotFoundError, rv.__getitem__, 0) self.assertRaises(E.SubdocPathNotFoundError, rv.__getitem__, 'path2') # Try when there is a mismatch self.assertRaises(E.SubdocPathMismatchError, cb.retrieve_in, key, 'path1[0]') # Try existence result = cb.lookup_in(key, SD.exists('path1')) self.assertTrue(result.exists('path1')) self.assertTrue(result.exists(0)) # Not found result = cb.lookup_in(key, SD.exists('p')) self.assertEqual(E.SubdocMultipleErrors.CODE, result.rc) self.assertEqual(E.SubdocPathNotFoundError.CODE, result.get(0)[0]) # Ensure that we complain about a missing path self.assertRaises((IndexError, KeyError), result.get, 33) self.assertRaises((IndexError, KeyError), result.get, 'non-requested') # Test with quiet result = cb.lookup_in(key, SD.exists('p'), quiet=True) self.assertFalse(result.exists('p')) self.assertFalse(result.exists(0)) # Insert a non-JSON document bkey = self.gen_key('sdget_nonjson') cb.upsert(bkey, 'value', format=FMT_UTF8) self.assertRaises(E.DocumentNotJsonError, cb.lookup_in, bkey, SD.exists('path')) # Empty paths fail for get_in self.assertRaises(E.SubdocEmptyPathError, cb.retrieve_in, key, '') # Try on non-existing document. Should fail self.assertRaises(E.NotFoundError, cb.retrieve_in, 'non-exist', 'path')
def test_lookup_in_one_path_not_found(self): cas = self.coll.upsert(self.KEY, {"a": "aaa", "b": [1, 2, 3, 4]}).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.lookup_in(self.KEY, ( SD.exists("a"), SD.exists("qzzxy"), )) self.assertTrue(result.exists(0)) self.assertFalse(result.exists(1))
def test_lookup_in(self): cb = self.cb # Create the document key = self.gen_key('sdget') cb.upsert(key, {'path1': 'value1'}) result = cb.retrieve_in(key, 'path1') self.assertEqual((0, 'value1'), result.get(0)) self.assertEqual((0, 'value1'), result.get('path1')) self.assertEqual('value1', result[0]) self.assertEqual('value1', result['path1']) self.assertTrue(result.cas) # Try when path is not found rv = cb.retrieve_in(key, 'path2') self.assertEqual(E.SubdocMultipleErrors.CODE, rv.rc) self.assertEqual(E.SubdocPathNotFoundError.CODE, rv.get(0)[0]) self.assertRaises(E.SubdocPathNotFoundError, rv.__getitem__, 0) self.assertRaises(E.SubdocPathNotFoundError, rv.__getitem__, 'path2') # Try when there is a mismatch self.assertRaises(E.SubdocPathMismatchError, cb.retrieve_in, key, 'path1[0]') # Try existence result = cb.lookup_in(key, SD.exists('path1')) self.assertTrue(result.exists('path1')) self.assertTrue(result.exists(0)) # Not found result = cb.lookup_in(key, SD.exists('p')) self.assertEqual(E.SubdocMultipleErrors.CODE, result.rc) self.assertEqual(E.SubdocPathNotFoundError.CODE, result.get(0)[0]) # Ensure that we complain about a missing path self.assertRaises((IndexError, KeyError), result.get, 33) self.assertRaises((IndexError, KeyError), result.get, 'non-requested') # Test with quiet result = cb.lookup_in(key, SD.exists('p'), quiet=True) self.assertFalse(result.exists('p')) self.assertFalse(result.exists(0)) # Insert a non-JSON document bkey = self.gen_key('sdget_nonjson') cb.upsert(bkey, 'value', format=FMT_UTF8) self.assertRaises(E.DocumentNotJsonError, cb.lookup_in, bkey, SD.exists('path')) # Empty paths fail for get_in self.assertRaises(E.SubdocEmptyPathError, cb.retrieve_in, key, '') # Try on non-existing document. Should fail self.assertRaises(E.NotFoundError, cb.retrieve_in, 'non-exist', 'path')
def test_multi_lookup(self): cb = self.cb key = self.gen_key('sdmlookup') cb.upsert( key, { 'field1': 'value1', 'field2': 'value2', 'array': [1, 2, 3], 'boolean': False }) rvs = cb.lookup_in(key, SD.get('field1'), SD.exists('field2'), SD.exists('field3'), quiet=True) self.assertFalse(rvs.success) self.assertEqual(3, rvs.result_count) self.assertEqual((0, 'value1'), rvs.get(0)) self.assertEqual((0, 'value1'), rvs.get('field1')) self.assertEqual('value1', rvs[0]) self.assertEqual('value1', rvs['field1']) self.assertEqual((0, None), rvs.get(1)) self.assertEqual((0, None), rvs.get('field2')) self.assertEqual(None, rvs[1]) self.assertEqual(None, rvs['field2']) self.assertTrue(rvs.exists('field2')) self.assertTrue(rvs.exists(1)) self.assertTrue(1 in rvs) self.assertTrue('field2' in rvs) self.assertEqual((E.SubdocPathNotFoundError.CODE, None), rvs.get('field3')) self.assertEqual((E.SubdocPathNotFoundError.CODE, None), rvs.get(2)) self.assertFalse(rvs.exists('field3')) self.assertFalse(rvs.exists(2)) def _getix(rv_, ix): return rv_[ix] self.assertRaises(E.SubdocPathNotFoundError, _getix, rvs, 2) self.assertRaises(E.SubdocPathNotFoundError, _getix, rvs, 'field3') self.assertFalse(rvs.exists('field3')) # See what happens when we mix operations self.assertRaises(E.CouchbaseError, cb.lookup_in, key, SD.get('field1'), SD.insert('a', 'b')) # Empty path (invalid) self.assertRaises(E.CouchbaseError, cb.lookup_in, SD.get(''))
def test_lookup_in_one_path_not_found(self): cas = self.coll.upsert(self.KEY, {"a": "aaa", "b": [1, 2, 3, 4]}).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.lookup_in(self.KEY, ( SD.exists("a"), SD.exists("qzzxy"), )) self.assertRaisesRegex(PathNotFoundException, "qzzxy", result.exists, 0) self.assertRaisesRegex(PathNotFoundException, "qzzxy", result.exists, 1)
def test_multi_lookup(self): cb = self.cb key = self.gen_key('sdmlookup') cb.upsert(key, { 'field1': 'value1', 'field2': 'value2', 'array': [1, 2, 3], 'boolean': False }) rvs = cb.lookup_in( key, SD.get('field1'), SD.exists('field2'), SD.exists('field3'), quiet=True ) self.assertFalse(rvs.success) self.assertEqual(3, rvs.result_count) self.assertEqual((0, 'value1'), rvs.get(0)) self.assertEqual((0, 'value1'), rvs.get('field1')) self.assertEqual('value1', rvs[0]) self.assertEqual('value1', rvs['field1']) self.assertEqual((0, None), rvs.get(1)) self.assertEqual((0, None), rvs.get('field2')) self.assertEqual(None, rvs[1]) self.assertEqual(None, rvs['field2']) self.assertTrue(rvs.exists('field2')) self.assertTrue(rvs.exists(1)) self.assertTrue(1 in rvs) self.assertTrue('field2' in rvs) self.assertEqual((E.SubdocPathNotFoundError.CODE, None), rvs.get('field3')) self.assertEqual((E.SubdocPathNotFoundError.CODE, None), rvs.get(2)) self.assertFalse(rvs.exists('field3')) self.assertFalse(rvs.exists(2)) def _getix(rv_, ix): return rv_[ix] self.assertRaises(E.SubdocPathNotFoundError, _getix, rvs, 2) self.assertRaises(E.SubdocPathNotFoundError, _getix, rvs, 'field3') self.assertFalse(rvs.exists('field3')) # See what happens when we mix operations self.assertRaises(E.CouchbaseError, cb.lookup_in, key, SD.get('field1'), SD.insert('a', 'b')) # Empty path (invalid) self.assertRaises(E.CouchbaseError, cb.lookup_in, SD.get(''))
def test_lookup_in_simple_exists_bad_path(self): cas = self.coll.upsert(self.KEY, {"a": "aaa", "b": [1, 2, 3, 4]}).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.lookup_in(self.KEY, (SD.exists("qzzxy"), )) self.assertEqual(result.cas, cas) self.assertFalse(result.exists(0)) self.assertRaises(PathNotFoundException, result.content_as[bool], 0)
def test_lookup_in_simple_exists(self): cas = self.coll.upsert(self.KEY, {"a": "aaa", "b": [1, 2, 3, 4]}).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.lookup_in(self.KEY, (SD.exists("b"), )) self.assertEqual(result.cas, cas) self.assertTrue(result.exists(0)) # no content; only valid path, returns None so is False self.assertFalse(result.content_as[bool](0))
def exists(self, client, key='', path='', value=None): try: if self.use_sdk_client: client.lookup_in(key, SD.exists(path)) # xattr not supported? else: client.exists_sd(key, path) except Exception: raise
def verify_exists(self, client, key='', path='', xattr=None): new_path = self.generate_path(self.nesting_level, path) try: if self.is_sdk_client: # xattr not supported?---------- client.lookup_in(key, SD.exists(new_path, xattr=xattr)) else: client.exists_sd(key, new_path) except Exception as e: self.log.error(e) msg = "Unable to get key {0} for path {1} after {2} tries".format(key, path, 1) return False, msg return True, ""
def test_lookup_in_multiple_specs(self): if self.is_mock: raise SkipTest("mock doesn't support getting xattrs (like $document.expiry)") cas = self.coll.upsert(self.KEY, {"a": "aaa", "b": {"c": {"d": "yo!"}}}).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.lookup_in(self.KEY, (SD.with_expiry(), SD.get("a"), SD.exists("b"), SD.get("b.c"))) self.assertTrue(result.success) self.assertIsNone(result.expiry) self.assertEquals("aaa", result.content_as[str](1)) self.assertTrue(result.exists(2)) self.assertDictEqual({"d": "yo!"}, result.content_as[dict](3))
def test_eventing_crc_and_fiid(self): body = self.create_save_function_body( self.function_name, HANDLER_CODE.BUCKET_OP_SOURCE_DOC_MUTATION, dcp_stream_boundary="from_now") # deploy eventing function self.deploy_function(body) url = 'couchbase://{ip}/{name}'.format(ip=self.master.ip, name=self.src_bucket_name) bucket = Bucket(url, username="******", password="******") for docid in ['customer123', 'customer1234', 'customer12345']: bucket.upsert(docid, {'a': 1}) self.verify_eventing_results(self.function_name, 3, skip_stats_validation=True) # check for fiid and crc for docid in ['customer123', 'customer1234', 'customer12345']: fiid = bucket.lookup_in(docid, SD.exists('_eventing.fiid', xattr=True)) self.log.info(fiid.exists('_eventing.fiid')) crc = bucket.lookup_in(docid, SD.exists('_eventing.crc', xattr=True)) self.log.info(crc.exists('_eventing.crc')) if not fiid.exists('_eventing.fiid') and not crc.exists( '_eventing.crc'): self.fail("No fiid : {} or crc : {} found:".format(fiid, crc)) self.undeploy_function(body) for docid in ['customer123', 'customer1234', 'customer12345']: fiid = bucket.lookup_in(docid, SD.exists('_eventing.fiid', xattr=True)) crc = bucket.lookup_in(docid, SD.exists('_eventing.crc', xattr=True)) if not fiid.exists('_eventing.fiid') and not crc.exists( '_eventing.crc'): self.fail("No fiid : {} or crc : {} found after undeployment:". format(fiid, crc))
def test_fiid_crc_with_pause_resume(self): body = self.create_save_function_body( self.function_name, HANDLER_CODE.BUCKET_OP_SOURCE_DOC_MUTATION, dcp_stream_boundary="from_now") # deploy eventing function self.deploy_function(body) url = 'couchbase://{ip}/{name}'.format(ip=self.master.ip, name=self.src_bucket_name) bucket = Bucket(url, username="******", password="******") for docid in ['customer123', 'customer1234', 'customer12345']: bucket.upsert(docid, {'a': 1}) self.verify_eventing_results(self.function_name, 3, skip_stats_validation=True) #get fiid and crc fiid_value = bucket.lookup_in('customer123', SD.exists('_eventing.fiid', xattr=True))['_eventing.fiid'] crc_value = bucket.lookup_in('customer123', SD.exists('_eventing.crc', xattr=True))['_eventing.crc'] self.log.info("Fiid: {} and CRC: {}".format(fiid_value, crc_value)) # check for fiid and crc for docid in ['customer1234', 'customer12345']: fiid = bucket.lookup_in(docid, SD.exists('_eventing.fiid', xattr=True)) crc = bucket.lookup_in(docid, SD.exists('_eventing.crc', xattr=True)) if fiid_value != fiid['_eventing.fiid'] or crc_value != crc[ '_eventing.crc']: self.fail("fiid {} or crc {} values are not same:".format( fiid, crc)) self.pause_function(body) for docid in ['customer12553', 'customer1253', 'customer12531']: bucket.upsert(docid, {'a': 1}) self.resume_function(body) self.verify_eventing_results(self.function_name, 6, skip_stats_validation=True) for docid in [ 'customer12553', 'customer1253', 'customer12531', 'customer123', 'customer1234', 'customer12345' ]: fiid = bucket.lookup_in(docid, SD.exists('_eventing.fiid', xattr=True)) crc = bucket.lookup_in(docid, SD.exists('_eventing.crc', xattr=True)) if fiid_value != fiid['_eventing.fiid'] or crc_value != crc[ '_eventing.crc']: self.fail("fiid {} or crc {} values are not same:".format( fiid, crc)) self.undeploy_and_delete_function(body)
def set_doc(self, client, key, value, exp, flags, xattr=None): if self.is_sdk_client: if xattr: # for verification xattr in subdoc client.cb.set(key, {}, exp, flags, ) if isinstance(value, dict): for k, v in value.iteritems(): print k, ":", v rv = client.cb.mutate_in(key, SD.upsert(k, v, xattr=xattr)) self.log.info("xattr '%s' added successfully?: %s" % (k, rv.success)) self.assertTrue(rv.success) rv = client.cb.lookup_in(key, SD.exists(k, xattr=xattr)) self.log.info("xattr '%s' exists?: %s" % (k, rv.success)) try: self.assertTrue(rv.success) except Exception as e: raise e else: self.fail("Unable to handle non-json docs. Please review behavior") else: client.set(key, value, exp, flags,) else: client.set(key, exp, flags, value)
def set_doc(self, client, key, value, exp, flags, xattr=None): if self.is_sdk_client: if xattr: # for verification xattr in subdoc client.cb.set(key, {}, exp, flags, ) if isinstance(value, dict): for k, v in value.items(): print(k, ":", v) rv = client.cb.mutate_in(key, SD.upsert(k, v, xattr=xattr)) self.log.info("xattr '%s' added successfully?: %s" % (k, rv.success)) self.assertTrue(rv.success) rv = client.cb.lookup_in(key, SD.exists(k, xattr=xattr)) self.log.info("xattr '%s' exists?: %s" % (k, rv.success)) try: self.assertTrue(rv.success) except Exception as e: raise e else: self.fail("Unable to handle non-json docs. Please review behavior") else: client.set(key, value, exp, flags,) else: client.set(key, exp, flags, value)
collection = couchbase.collection.Collection() result = collection.lookup_in("customer123", [SD.get("addresses.delivery.country")]) country = result.content_as[str](0) # "United Kingdom" #end::content_as[] """ ---- .Check existence of sub-document path [source,csharp] ---- """ #tag::exists[] result = collection.lookup_in("customer123", [SD.exists("purchases.pending[-1]")]) print("Path exists? {}".format(result.content_as[bool](0))) """ # Path exists? false #end::exists[] ---- Multiple operations can be combined as well: .Combine multiple lookup operations [source,csharp] ---- """ #tag::combine[] result = collection.lookup_in( "customer123",
def test_lookup_in_simple_exists(self): cas = self.coll.upsert(self.KEY, {"a": "aaa", "b": [1, 2, 3, 4]}).cas self.try_n_times(10, 3, self._cas_matches, self.KEY, cas) result = self.coll.lookup_in(self.KEY, (SD.exists("b"), )) self.assertEqual(result.cas, cas) self.assertTrue(result.exists(0))
cb = Bucket('couchbase://localhost/default') cb.upsert('docid', {'name': 'Mark', 'email': '*****@*****.**', 'array': [1, 2, 3, 4]}) # Do it the simple way: rv = cb.retrieve_in('docid', 'name', 'array[1]') print('Name is: {0}, array[1] is: {1}'.format(rv[0], rv[1])) # If all results are successful: name, array_2ndelem = rv print('Name is: {0}, Array[1] is: {1}'.format(name, array_2ndelem)) # Perform mixed-mode operations rv = cb.lookup_in('docid', SD.get('name'), SD.get('array[1]'), SD.exists('non-exist')) print('Name is', rv[0]) print('Array[1] is', rv[1]) print('non-exist exists?', rv.exists(2)) # See what happens when we try to reference a failed path: try: rv[2] except E.SubdocPathNotFoundError: print('Using subscript access raises exception for missing item') # If we try to get a non-existent document, it will fail as normal try: cb.retrieve_in('non-exist', 'pth1', 'pth2', 'pth3') except E.NotFoundError: print('Document itself not found!')
collection.remove("customer123") collection.insert("customer123", json_doc) # tag::lookup_in[] result = collection.lookup_in("customer123", [SD.get("addresses.delivery.country")]) country = result.content_as[str](0) # "United Kingdom" # end::lookup_in[] print(country) # fixed in v. 3.1.0; prior to 3.1. result.exists(index) # would throw an exception if the path did not exist # tag::lookup_in_exists[] result = collection.lookup_in( "customer123", [ SD.exists("purchases.pending[-1]")]) print('Path exists: {}.'.format(result.exists(0))) # Path exists: False. # end::lookup_in_exists[] # NOTE: result.content_as[bool](1) would return False # this is b/c when checking if a path exists # no content is returned and None evaluates to False try: print("Path exists {}.".format(result.content_as[bool](0))) except PathNotFoundException: print("Path does not exist") # tag::lookup_in_multi[] result = collection.lookup_in(