Exemplo n.º 1
0
    def test_create_doc(self):
        cb = self.cb
        key = self.gen_key('create_doc')
        cb.mutate_in(key, SD.upsert('new.path', 'newval'), upsert_doc=True)
        self.assertEqual('newval', cb.retrieve_in(key, 'new.path')[0])

        # Check 'insert_doc'

        self.assertRaises(E.KeyExistsError, cb.mutate_in,key, SD.upsert('new.path', 'newval'), insert_doc=True)
        cb.remove(key)

        cb.mutate_in(key, SD.upsert('new.path', 'newval'), insert_doc=True)
        self.assertEqual('newval', cb.retrieve_in(key, 'new.path')[0])
Exemplo n.º 2
0
    def test_enhanced_err_present_authorisation(self):
        import couchbase_core.subdocument as SD
        users = [('writer', ('s3cr3t', [('data_reader', 'default'),
                                        ('data_writer', 'default')])),
                 ('reader', ('s3cr3t', [('data_reader', 'default')]))]
        #self.mockclient._do_request("SET_ENHANCED_ERRORS",{"enabled":True})
        for user in users:
            print(str(user))
            (userid, password, roles) = user[0], user[1][0], user[1][1]
            # add user
            self.admin.user_upsert(AuthDomain.Local, userid, password, roles)
            time.sleep(1)
            try:
                connection = self.make_connection(username=userid,
                                                  password=password)

                key = self.gen_key('create_doc')
                connection.mutate_in(key, (SD.upsert('new.path', 'newval'), ),
                                     upsert_doc=True)
            except CouchbaseException as e:
                print(str(e))
                if userid == "writer":
                    raise e
                else:
                    self.assertRegexpMatches(
                        e.context, r".*Authorization failure.*",
                        "doesn't have correct Context field")
                    self.assertRegexpMatches(e.ref, r"(.*?)-(.*?)-.*",
                                             "doesn't have correct Ref field")
                    self.assertRegexpMatches(
                        str(e), r".*Context=Authorization failure.*,.*Ref=.*",
                        "exception as string doesn't contain both fields")
            finally:
                #remove user
                self.admin.user_remove(AuthDomain.Local, userid)
Exemplo n.º 3
0
    def test_counter_in(self):
        cb = self.cb
        key = self.gen_key('sdcounter')
        cb.upsert(key, {})

        rv = cb.mutate_in(key, SD.counter('counter', 100))
        self.assertTrue(rv.success)
        self.assertFalse(rv.cas == 0)
        self.assertEqual(100, rv[0])

        self.assertRaises(E.SubdocBadDeltaError, cb.mutate_in, key,
                          SD.counter('not_a_counter', 'blah'))

        # Do an upsert
        cb.mutate_in(key, SD.upsert('not_a_counter', 'blah'))

        self.assertRaises(E.SubdocPathMismatchError, cb.mutate_in, key,
                          SD.counter('not_a_counter', 25))

        self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key,
                          SD.counter('path.to.newcounter', 99))
        rv = cb.mutate_in(key,
                          SD.counter('path.to.newcounter', 99, create_parents=True))
        self.assertEqual(99, rv[0])

        # Increment first counter again
        rv = cb.mutate_in(key, SD.counter('counter', -25))
        self.assertEqual(75, rv[0])

        self.assertRaises(ValueError, SD.counter, 'counter', 0)
Exemplo n.º 4
0
    def test_create_doc(self):
        cb = self.cb
        key = self.gen_key('create_doc')
        cb.mutate_in(key, SD.upsert('new.path', 'newval'), upsert_doc=True)
        self.assertEqual('newval', cb.retrieve_in(key, 'new.path')[0])

        # Check 'insert_doc'

        # TODO: need to find a way to support this on LCB V4 API if it is still a desired use case - PYCBC-584
        try:
            self.assertRaises(E.KeyExistsError, cb.mutate_in,key, SD.upsert('new.path', 'newval'), insert_doc=True)
        except E.NotSupportedError:
            logging.error(traceback.format_exc())
        cb.remove(key)

        try:
            cb.mutate_in(key, SD.upsert('new.path', 'newval'), insert_doc=True)
            self.assertEqual('newval', cb.retrieve_in(key, 'new.path')[0])
        except E.NotSupportedError:
            logging.error(traceback.format_exc())
def upsert(path,                    # type: str,
           value,                   # type: JSON
           create_parents=False     # type: bool
           ):
    # type: (...) -> Spec
    """
    Upsert a value into a document at a given path

    :param str path:  Path at which to upsert the value.
    :param JSON value:  Value to upsert.
    :param create_parents: Whether or not to create parents if needed.

    :return: Spec
    """
    return SD.upsert(path, value, create_parents)
Exemplo n.º 6
0
    def test_xattrs_basic(self):
        cb = self.cb
        k = self.gen_key('xattrs')
        cb.upsert(k, {})

        # Try to upsert a single xattr
        rv = cb.mutate_in(
            k, SD.upsert('my.attr', 'value', xattr=True, create_parents=True))
        self.assertTrue(rv.success)

        body = cb.get(k)
        self.assertFalse('my' in body.value)
        self.assertFalse('my.attr' in body.value)

        # Try using lookup_in
        rv = cb.retrieve_in(k, 'my.attr')
        self.assertFalse(rv.exists('my.attr'))

        # Finally, use lookup_in with 'xattrs' attribute enabled
        rv = cb.lookup_in(k, SD.get('my.attr', xattr=True))
        self.assertTrue(rv.exists('my.attr'))
        self.assertEqual('value', rv['my.attr'])
Exemplo n.º 7
0
def update_subdoc():
    print("\n[kv-update-subdoc]")
    # tag::kv-update-subdoc[]
    mutate_in_result = hotel_collection.mutate_in(
        "hotel-123", [subdocument.upsert("pets_ok", True)])
    print("CAS:", mutate_in_result.cas)
Exemplo n.º 8
0
    def test_mutate_in(self):
        cb = self.cb
        key = self.gen_key('sdstore_upsert')
        cb.upsert(key, {})

        cb.mutate_in(key, SD.upsert('newDict', ['hello']))
        result = cb.retrieve_in(key, 'newDict')
        self.assertEqual(['hello'], result[0])

        # Create deep path without create_parents
        self.assertRaises(E.SubdocPathNotFoundError,
                          cb.mutate_in, key,
                          SD.upsert('path.with.missing.parents', 'value'))

        # Create deep path using create_parents
        cb.mutate_in(key,
                     SD.upsert('new.parent.path', 'value', create_parents=True))
        result = cb.retrieve_in(key, 'new.parent')
        self.assertEqual('value', result[0]['path'])

        # Test CAS operations
        self.assertTrue(result.cas)
        self.assertRaises(E.KeyExistsError, cb.mutate_in,
                          key, SD.upsert('newDict', None), cas=result.cas+1)

        # Try it again, using the CAS
        result2 = cb.mutate_in(key, SD.upsert('newDict', {}), cas=result.cas)
        self.assertNotEqual(result.cas, result2.cas)

        # Test insert, should fail
        self.assertRaises(E.SubdocPathExistsError, cb.mutate_in,
                          key, SD.insert('newDict', {}))

        # Test insert on new path, should succeed
        cb.mutate_in(key, SD.insert('anotherDict', {}))
        self.assertEqual({}, cb.retrieve_in(key, 'anotherDict')[0])

        # Test replace, should not fail
        cb.mutate_in(key, SD.replace('newDict', {'Hello': 'World'}))
        self.assertEqual('World', cb.retrieve_in(key, 'newDict')[0]['Hello'])

        # Test replace with missing value, should fail
        self.assertRaises(E.SubdocPathNotFoundError,
                          cb.mutate_in, key, SD.replace('nonexist', {}))

        # Test with empty string (should be OK)
        cb.mutate_in(key, SD.upsert('empty', ''))
        self.assertEqual('', cb.retrieve_in(key, 'empty')[0])

        # Test with null (None). Should be OK
        cb.mutate_in(key, SD.upsert('null', None))
        self.assertEqual(None, cb.retrieve_in(key, 'null')[0])

        # Test with empty path. Should throw some kind of error?
        self.assertRaises(
            (E.SubdocCantInsertValueError, E.SubdocEmptyPathError),
            cb.mutate_in, key, SD.upsert('', {}))

        cb.mutate_in(key, SD.upsert('array', [1, 2, 3]))
        self.assertRaises(E.SubdocPathMismatchError, cb.mutate_in, key,
                          SD.upsert('array.newKey', 'newVal'))
        self.assertRaises(E.SubdocPathInvalidError, cb.mutate_in, key,
                          SD.upsert('array[0]', 'newVal'))
        self.assertRaises(E.SubdocPathNotFoundError, cb.mutate_in, key,
                          SD.upsert('array[3].bleh', 'newVal'))