Ejemplo n.º 1
0
    print(
        "\nremove_by_index_range('l', 3, COUNT, 3): {}\nRemaining: {}".format(
            bins[2][1], bins[3][1]
        )
    )
    # remove_by_index_range('l', 3, COUNT, 3): 3
    # Remaining: [1, 2, 3, 11, 10, 12, 13]
    print("\nremove_by_index_range('l', 0, NONE, 3)\nRemaining: {}".format(bins[4][1]))
    # remove_by_index_range('l', 0, NONE, 3)
    # Remaining: [11, 10, 12, 13]

    # remove all elements from index -2 (second from the end) and also append a new
    # element to the end of the list
    ops = [
        listops.list_remove_by_index_range("l", -2, aerospike.LIST_RETURN_NONE),
        listops.list_append("l", [1, 3, 3, 7, 0]),  # returns new list length
        operations.read("l"),
    ]
    key, metadata, bins = client.operate_ordered(key, ops)
    print("\nremove_by_index_range('l', -2, NONE)")
    print("append('l', [1, 3, 3, 7, 0])")
    print("{}".format(bins[1][1]))
    # remove_by_index_range('l', -2, NONE)
    # append('l', [1, 3, 3, 7, 0])
    # [11, 10, [1, 3, 3, 7, 0]]

    # remove all but the last 2 elements from the list nested within the
    # last element of the outer list
    ctx = [cdt_ctx.cdt_ctx_list_index(-1)]
    ops = [
        listops.list_remove_by_index_range(
class TestOperate(object):
    def setup_class(cls):
        """
        Setup class.
        """
        # hostlist, user, password = TestBaseClass.get_hosts()

        # config_no_typechecks = {'hosts': hostlist, 'strict_types': False}
        # if user is None and password is None:
        #     TestOperate.client_no_typechecks = aerospike.client(
        #         config_no_typechecks).connect()
        # else:
        #     TestOperate.client_no_typechecks = aerospike.client(
        #         config_no_typechecks).connect(user, password)
        cls.client_no_typechecks = TestBaseClass.get_new_connection(
            {'strict_types': False})

    def teardown_class(cls):
        TestOperate.client_no_typechecks.close()

    @pytest.fixture(autouse=True)
    def setup(self, request, as_connection):
        """
        Setup Method
        """
        keys = []

        for i in range(5):
            key = ('test', 'demo', i)
            rec = {'name': 'name%s' % (str(i)), 'age': i}
            as_connection.put(key, rec)

        key = ('test', 'demo', 6)
        rec = {"age": 6.3}
        as_connection.put(key, rec)
        keys.append(key)

        key = ('test', 'demo', 'bytearray_key')
        rec = {"bytearray_bin": bytearray("asd;as[d'as;d", "utf-8")}
        as_connection.put(key, rec)

        keys.append(key)

        key = ('test', 'demo', 'bytes_key')
        rec = {"bytes_bin": b''}
        as_connection.put(key, rec)

        keys.append(key)

        key = ('test', 'demo', 'list_key')
        rec = {"int_bin": [1, 2, 3, 4], "string_bin": ['a', 'b', 'c', 'd']}
        as_connection.put(key, rec)

        keys.append(key)

        key = ('test', 'demo', 'existing_key')
        rec = {
            "dict": {
                "a": 1
            },
            "bytearray": bytearray("abc", "utf-8"),
            "float": 3.4,
            "list": ['a']
        }
        as_connection.put(key, rec)

        keys.append(key)

        def teardown():
            """
            Teardown Method
            """
            for i in range(5):
                key = ('test', 'demo', i)
                try:
                    as_connection.remove(key)
                except e.RecordNotFound:
                    pass
            for key in keys:
                try:
                    as_connection.remove(key)
                except e.RecordNotFound:
                    pass

        request.addfinalizer(teardown)

    @pytest.mark.parametrize(
        "key, llist, expected",
        [
            (('test', 'demo', 1), [
                operations.prepend("name", u"ram"),
                operations.increment("age", 3),
                operations.read("name")
            ], {
                'name': 'ramname1'
            }),
            (
                ('test', 'demo', 1),  # with_write_float_value
                [
                    operations.write("write_bin", {"no": 89.8}),
                    operations.read("write_bin")
                ],
                {
                    'write_bin': {
                        u'no': 89.8
                    }
                }),
            (
                ('test', 'demo', 1),  # write positive
                [
                    operations.write("write_bin", {"no": 89}),
                    operations.read("write_bin")
                ],
                {
                    'write_bin': {
                        u'no': 89
                    }
                }),
            (
                ('test', 'demo', 1),  # write_tuple_positive
                [
                    operations.write("write_bin", ('a', 'b', 'c')),
                    operations.read("write_bin")
                ],
                {
                    'write_bin': ('a', 'b', 'c')
                }),
            (
                ('test', 'demo', 1),  # with_bin_bytearray
                [
                    operations.prepend("asd[;asjk", "ram"),
                    operations.read("asd[;asjk")
                ],
                {
                    'asd[;asjk': 'ram'
                }),
            (
                ('test', 'demo',
                 'bytearray_key'),  # with_operator append_val bytearray
                [
                    operations.append("bytearray_bin", bytearray(
                        "abc", "utf-8")),
                    operations.read("bytearray_bin")
                ],
                {
                    'bytearray_bin': bytearray("asd;as[d'as;dabc", "utf-8")
                }),
            (
                ('test', 'demo', 'bytearray_new'
                 ),  # with_operator append_val bytearray_newrecord
                [
                    operations.append("bytearray_bin",
                                      bytearray("asd;as[d'as;d", "utf-8")),
                    operations.read("bytearray_bin")
                ],
                {
                    'bytearray_bin': bytearray("asd;as[d'as;d", "utf-8")
                }),
            (
                ('test', 'demo',
                 'bytes_key'),  # with_operator append_val bytes
                [
                    operations.append("bytes_bin", b"abc"),
                    operations.read("bytes_bin")
                ],
                {
                    'bytes_bin': b'abc'
                }),
            (
                ('test', 'demo',
                 'bytes_new'),  # with_operator append_val bytes_newrecord
                [
                    operations.append("bytes_bin", b"asd;as[d'as;d"),
                    operations.read("bytes_bin")
                ],
                {
                    'bytes_bin': b"asd;as[d'as;d"
                }),
            (
                ('test', 'demo',
                 'bytearray_key'),  # with_operatorprepend_valbytearray
                [
                    operations.prepend("bytearray_bin",
                                       bytearray("abc", "utf-8")),
                    operations.read("bytearray_bin")
                ],
                {
                    'bytearray_bin': bytearray("abcasd;as[d'as;d", "utf-8")
                }),
            (
                ('test', 'demo', 'bytearray_new'
                 ),  # with_operatorprepend_valbytearray_newrecord
                [
                    operations.prepend("bytearray_bin",
                                       bytearray("asd;as[d'as;d", "utf-8")),
                    operations.read("bytearray_bin")
                ],
                {
                    'bytearray_bin': bytearray("asd;as[d'as;d", "utf-8")
                }),
            (
                ('test', 'demo',
                 'bytes_key'),  # with_operator prepend_val bytes
                [
                    operations.prepend("bytes_bin", b"abc"),
                    operations.read("bytes_bin")
                ],
                {
                    'bytes_bin': b'abc'
                }),
            (
                ('test', 'demo',
                 'bytes_new'),  # with_operator prepend_val bytes_newrecord
                [
                    operations.prepend("bytes_bin", b"asd;as[d'as;d"),
                    operations.read("bytes_bin")
                ],
                {
                    'bytes_bin': b"asd;as[d'as;d"
                })
        ])
    def test_pos_operate_with_correct_paramters(self, key, llist, expected):
        """
        Invoke operate() with correct parameters
        """

        key, _, bins = self.as_connection.operate(key, llist)

        assert bins == expected
        self.as_connection.remove(key)

    @pytest.mark.parametrize("key, llist, expected", [
        (('test', 'demo', 1), [
            operations.write("write_bin", {"no": 89.8}),
            operations.write("write_bin2", {"no": 100}),
            operations.delete(),
        ], {}),
    ])
    def test_pos_operate_delete_with_correct_paramters(self, key, llist,
                                                       expected):
        """
        Invoke operate() with correct parameters
        """

        key, _, bins = self.as_connection.operate(key, llist)

        assert bins == expected

    def test_pos_operate_with_increment_positive_float_value(self):
        """
        Invoke operate() with correct parameters
        """
        if TestOperate.skip_old_server is True:
            pytest.skip("Server does not support increment on float type")
        key = ('test', 'demo', 6)
        llist = [operations.increment("age", 3.5), operations.read("age")]

        _, _, bins = self.as_connection.operate(key, llist)

        assert bins == {'age': 9.8}

    def test_pos_operate_with_correct_policy(self):
        """
        Invoke operate() with correct policy
        """
        key = ('test', 'demo', 1)
        policy = {
            'timeout': 1000,
            'key': aerospike.POLICY_KEY_SEND,
            'commit_level': aerospike.POLICY_COMMIT_LEVEL_MASTER
        }

        llist = [
            operations.append("name", "aa"),
            operations.increment("age", 3),
            operations.read("name")
        ]

        _, _, bins = self.as_connection.operate(key, llist, {}, policy)

        assert bins == {'name': 'name1aa'}

        self.as_connection.remove(key)

    @pytest.mark.parametrize("key, policy, meta, llist", [
        (('test', 'demo', 1), {
            'total_timeout': 1000,
            'key': aerospike.POLICY_KEY_SEND,
            'gen': aerospike.POLICY_GEN_IGNORE,
            'commit_level': aerospike.POLICY_COMMIT_LEVEL_ALL
        }, {
            'gen': 10,
            'ttl': 1200
        }, [
            operations.append("name", "aa"),
            operations.increment("age", 3),
            operations.read("name")
        ]),
    ])
    def test_pos_operate_with_policy_gen_ignore(self, key, policy, meta,
                                                llist):
        """
        Invoke operate() with gen ignore.
        """

        key, meta, bins = self.as_connection.operate(key, llist, meta, policy)

        assert bins == {'name': 'name1aa'}

    def test_pos_operate_with_policy_gen_EQ(self):
        """
        Invoke operate() with gen EQ positive.
        """
        key = ('test', 'demo', 1)
        policy = {'gen': aerospike.POLICY_GEN_EQ}
        (key, meta) = self.as_connection.exists(key)
        gen = meta['gen']
        meta = {'gen': gen}

        llist = [
            operations.append("name", "aa"),
            operations.increment("age", 3),
            operations.read("name")
        ]
        (key, meta, bins) = self.as_connection.operate(key, llist, meta,
                                                       policy)

        assert bins == {'name': 'name1aa'}

    @pytest.mark.parametrize("key, llist",
                             [(('test', 'demo', 1), [operations.touch(4000)]),
                              (('test', 'demo', 1), [operations.touch(4000)])])
    def test_pos_operate_touch_operation_with_bin_and_value_combination(
            self, key, llist):
        """
        Invoke operate() with touch value with bin and value combination.
        """

        self.as_connection.operate(key, llist)

        (key, meta) = self.as_connection.exists(key)

        assert meta['ttl'] is not None

    def test_pos_operate_with_policy_gen_EQ_not_equal(self):
        """
        Invoke operate() with a mismatched generation, and verify
        it does not succeed
        """
        key = ('test', 'demo', 1)
        policy = {'gen': aerospike.POLICY_GEN_EQ}

        (_, meta) = self.as_connection.exists(key)
        gen = meta['gen']
        meta = {
            'gen': gen + 5,
        }
        llist = [
            operations.append("name", "aa"),
            operations.increment("age", 3),
        ]

        with pytest.raises(e.RecordGenerationError):
            self.as_connection.operate(key, llist, meta, policy)

        _, _, bins = self.as_connection.get(key)
        assert bins == {"age": 1, 'name': 'name1'}

    def test_pos_operate_with_policy_gen_GT_mismatch(self):
        """
        Invoke operate() with gen GT policy, with amatching generation
        then verify that the operation was rejected properly
        """
        key = ('test', 'demo', 1)
        policy = {'gen': aerospike.POLICY_GEN_GT}
        _, meta = self.as_connection.exists(key)
        gen = meta['gen']
        meta = {'gen': gen}

        llist = [
            operations.append("name", "aa"),
            operations.increment("age", 3)
        ]

        with pytest.raises(e.RecordGenerationError):
            self.as_connection.operate(key, llist, meta, policy)

        _, _, bins = self.as_connection.get(key)
        assert bins == {'age': 1, 'name': 'name1'}

    def test_pos_operate_touch_with_meta(self):
        """
        Invoke operate() OPERATE_TOUCH using meta to pass in ttl.
        """
        key = ('test', 'demo', 1)
        meta = {'ttl': 1200}

        llist = [operations.touch()]

        self.as_connection.operate(key, llist, meta)

        _, meta = self.as_connection.exists(key)

        assert meta['ttl'] <= 1200 and meta['ttl'] >= 1150

    def test_pos_operate_with_policy_gen_GT(self):
        """
        Invoke operate() with gen GT positive.
        """
        key = ('test', 'demo', 1)
        policy = {'gen': aerospike.POLICY_GEN_GT}
        _, meta = self.as_connection.exists(key)
        gen = meta['gen']
        meta = {'gen': gen + 5}

        llist = [
            operations.append("name", "aa"),
            operations.increment("age", 3),
            operations.read("name")
        ]
        _, _, bins = self.as_connection.operate(key, llist, meta, policy)

        assert bins == {'name': 'name1aa'}

    def test_pos_operate_with_nonexistent_key(self):
        """
        Invoke operate() with non-existent key
        """
        new_key = ('test', 'demo', "key11")
        llist = [operations.prepend("loc", "mumbai"), operations.read("loc")]
        _, _, bins = self.as_connection.operate(new_key, llist)

        assert bins == {'loc': 'mumbai'}
        self.as_connection.remove(new_key)

    def test_pos_operate_with_nonexistent_bin(self):
        """
        Invoke operate() with non-existent bin
        """
        key = ('test', 'demo', 1)
        llist = [operations.append("addr", "pune"), operations.read("addr")]
        _, _, bins = self.as_connection.operate(key, llist)

        assert bins == {'addr': 'pune'}

    def test_pos_operate_increment_nonexistent_key(self):
        """
        Invoke operate() with increment with nonexistent_key
        """
        key = ('test', 'demo', "non_existentkey")
        llist = [operations.increment("age", 5)]

        self.as_connection.operate(key, llist)

        _, _, bins = self.as_connection.get(key)

        assert bins == {"age": 5}

        self.as_connection.remove(key)

    def test_pos_operate_with_correct_paramters_without_connection(self):
        """
        Invoke operate() with correct parameters without connection
        """
        key = ('test', 'demo', 1)
        config = {'hosts': [('127.0.0.1', 3000)]}
        client1 = aerospike.client(config)
        llist = [operations.touch()]

        with pytest.raises(e.ClusterError):
            client1.operate(key, llist)

    def test_pos_operate_write_set_to_aerospike_null(self):
        """
        Invoke operate() with write command with bin set to aerospike_null
        """
        key = ('test', 'demo', 'null_record')

        bins = {"name": "John", "no": 3}

        self.as_connection.put(key, bins)

        llist = [
            operations.write("no", aerospike.null()),
            operations.read("no")
        ]

        _, _, bins = self.as_connection.operate(key, llist)
        assert {} == bins

        self.as_connection.remove(key)

    @pytest.mark.parametrize(
        "key, llist, expected",
        [
            (
                ('test', 'demo', 'prepend_int'),  # prepend_with_int
                [operations.prepend("age", 4),
                 operations.read("age")],
                {
                    'age': 4
                }),
            (
                ('test', 'demo', 'append_dict'),  # append_with_dict
                [
                    operations.append("dict", {
                        "a": 1,
                        "b": 2
                    }),
                    operations.read("dict")
                ],
                {
                    'dict': {
                        "a": 1,
                        "b": 2
                    }
                }),
            (
                ('test', 'demo', 'incr_string'),  # incr_with_string
                [
                    operations.increment("name", "aerospike"),
                    operations.read("name")
                ],
                {
                    'name': 'aerospike'
                }),
        ])
    def test_pos_operate_new_record(self, key, llist, expected):
        """
        Invoke operate() with prepend command on a new record
        """
        _, _, bins = TestOperate.client_no_typechecks.operate(key, llist)
        assert expected == bins
        TestOperate.client_no_typechecks.remove(key)

    @pytest.mark.parametrize(
        "key, llist",
        [
            (('test', 'demo', 1),
             [operations.prepend("age", 4),
              operations.read("age")]),
            (
                ('test', 'demo', 'existing_key'),  # Existing list
                [operations.prepend("list", ['c']),
                 operations.read("list")]),
            (
                ('test', 'demo', 'existing_key'),  # Existing dict
                [operations.append("dict", {"c": 2}),
                 operations.read("dict")]),
            (
                ('test', 'demo', 'existing_key'),  # Exiting float
                [operations.append("float", 3.4),
                 operations.read("float")]),
            (
                ('test', 'demo', 1),  # Existing string
                [
                    operations.increment("name", "aerospike"),
                    operations.read("name")
                ]),
            (
                ('test', 'demo', 'existing_key'),  # Existing Bytearray
                [
                    operations.increment("bytearray", bytearray(
                        "abc", "utf-8")),
                    operations.read("bytearray")
                ]),
        ])
    def test_pos_operate_prepend_with_existing_record(self, key, llist):
        """
        Invoke operate() with prepend command on a existing record
        """

        with pytest.raises(e.BinIncompatibleType):
            TestOperate.client_no_typechecks.operate(key, llist)

        TestOperate.client_no_typechecks.remove(key)

    def test_pos_operate_incr_with_geospatial_new_record(self):
        """
        Invoke operate() with incr command on a new record
        """
        key = ('test', 'demo', 'geospatial_key')

        llist = [
            operations.increment(
                "geospatial",
                aerospike.GeoJSON({
                    "type": "Point",
                    "coordinates": [42.34, 58.62]
                })),
            operations.read("geospatial")
        ]

        _, _, bins = TestOperate.client_no_typechecks.operate(key, llist)

        assert bins['geospatial'].unwrap() == {
            'coordinates': [42.34, 58.62],
            'type': 'Point'
        }
        TestOperate.client_no_typechecks.remove(key)

    def test_pos_operate_with_bin_length_extra_nostricttypes(self):
        """
        Invoke operate() with bin length extra. Strict types disabled
        """
        key = ('test', 'demo', 1)

        max_length = 'a' * 21

        llist = [
            operations.prepend("name", "ram"),
            operations.increment(max_length, 3)
        ]

        TestOperate.client_no_typechecks.operate(key, llist)

        _, _, bins = TestOperate.client_no_typechecks.get(key)

        assert bins == {"name": "ramname1", "age": 1}

    def test_pos_operate_prepend_set_to_aerospike_null(self):
        """
        Invoke operate() with prepend command with bin set to aerospike_null
        """
        key = ('test', 'demo', 'null_record')

        bins = {"name": "John", "no": 3}

        assert 0 == self.as_connection.put(key, bins)

        (key, _, bins) = self.as_connection.get(key)

        assert {"name": "John", "no": 3} == bins

        llist = [
            operations.prepend("no", aerospike.null()),
            operations.read("no")
        ]

        try:
            (key, _, bins) = self.as_connection.operate(key, llist)

        except e.InvalidRequest as exception:
            assert exception.code == 4
        self.as_connection.remove(key)

    @pytest.mark.parametrize(
        "list, result, bin, expected",
        [
            ([
                list_operations.list_append("int_bin", 7),
                list_operations.list_get("int_bin", 4)
            ], {
                "int_bin": 7
            }, "int_bin", [1, 2, 3, 4, 7]),
            ([
                list_operations.list_append_items("int_bin", [7, 9]),
                list_operations.list_get_range("int_bin", 3, 3),
            ], {
                'int_bin': [4, 7, 9]
            }, "int_bin", [1, 2, 3, 4, 7, 9]),
            ([
                list_operations.list_insert("int_bin", 2, 7),
                list_operations.list_pop("int_bin", 2)
            ], {
                'int_bin': 7
            }, "int_bin", [1, 2, 3, 4]),
            ([
                list_operations.list_insert_items("int_bin", 2, [7, 9]),
                list_operations.list_pop_range("int_bin", 2, 2)
            ], {
                'int_bin': [7, 9]
            }, "int_bin", [1, 2, 3, 4]),
            ([
                list_operations.list_set("int_bin", 2, 18),
                list_operations.list_get("int_bin", 2)
            ], {
                'int_bin': 18
            }, "int_bin", [1, 2, 18, 4]),
            (
                [
                    list_operations.list_set("int_bin", 6, 10),
                    list_operations.list_get("int_bin", 6)
                ],
                {
                    'int_bin': 10
                },
                "int_bin",
                [1, 2, 3, 4, None, None, 10
                 ]  # Inserting outside of the range adds nils in between
            )
        ])
    def test_pos_operate_with_list_addition_operations(self, list, result, bin,
                                                       expected):
        """
        Invoke operate() with list addition operations
        """
        key = ('test', 'demo', 'list_key')

        key, _, bins = self.as_connection.operate(key, list)

        assert bins == result

        key, _, bins = self.as_connection.get(key)
        assert bins[bin] == expected

    @pytest.mark.parametrize(
        "list, bin, expected",
        [([list_operations.list_remove("int_bin", 2)], "int_bin", [1, 2, 4]),
         ([list_operations.list_remove_range("int_bin", 2, 2)
           ], "int_bin", [1, 2]),
         ([list_operations.list_trim("int_bin", 2, 2)], "int_bin", [3, 4]),
         ([list_operations.list_clear("int_bin")], "int_bin", [])])
    def test_pos_operate_with_list_remove_operations(self, list, bin,
                                                     expected):
        """
        Invoke operate() with list remove operations
        """
        key = ('test', 'demo', 'list_key')

        self.as_connection.operate(key, list)

        key, _, bins = self.as_connection.get(key)

        assert bins[bin] == expected

    def test_pos_operate_with_list_size(self):
        """
        Invoke operate() with list_size operation
        """
        key = ('test', 'demo', 'list_key')
        list = [list_operations.list_size("int_bin")]

        key, _, bins = self.as_connection.operate(key, list)

        assert bins == {'int_bin': 4}

    def test_list_increment_with_valid_value(self):
        '''
        previous list was [1, 2, 3, 4]
        new should be [1, 2, 23, 4]
        '''
        key = ('test', 'demo', 'list_key')
        list = [list_operations.list_increment("int_bin", 2, 20)]

        _, _, bins = self.as_connection.operate(key, list)

        assert bins == {'int_bin': 23}
        _, _, bins = self.as_connection.get(key)

        assert bins['int_bin'] == [1, 2, 23, 4]

    def test_list_increment_with_incorrect_value_type(self):
        '''
        previous list was [1, 2, 3, 4]
        new should be [1, 2, 23, 4]
        '''
        key = ('test', 'demo', 'list_key')
        list = [list_operations.list_increment("int_bin", 2, "twenty")]

        with pytest.raises(e.AerospikeError):
            self.as_connection.operate(key, list)

    def test_pos_operate_with_list_get_range_val_out_of_bounds(self):
        """
        Invoke operate() with list_get_range operation and value out of bounds
        """
        key = ('test', 'demo', 'list_key')
        list = [list_operations.list_get_range("int_bin", 2, 9)]

        _, _, bins = self.as_connection.operate(key, list)

        assert bins == {'int_bin': [3, 4]}

    def test_pos_operate_with_list_trim_val_with_negative_value(self):
        """
        Invoke operate() with list_trimoperation and value is negative
        """
        key = ('test', 'demo', 'list_key')
        list = [list_operations.list_trim("int_bin", 1, -9)]

        self.as_connection.operate(key, list)

        _, _, bins = self.as_connection.get(key)

        assert bins['int_bin'] == [2, 3, 4]

    def test_pos_operate_with_list_insert_index_negative(self):
        """
        Invoke operate() with list_insert and item index is a negative value
        """
        key = ('test', 'demo', 'list_key')
        list = [list_operations.list_insert("int_bin", -2, 9)]

        self.as_connection.operate(key, list)

        _, _, bins = self.as_connection.get(key)

        assert bins['int_bin'] == [1, 2, 9, 3, 4]

    @pytest.mark.parametrize("list, result, bin, expected", [
        ([
            list_operations.list_append("string_bin", {"new_val": 1}),
            list_operations.list_get("string_bin", 4)
        ], {
            "string_bin": {
                "new_val": 1
            }
        }, "string_bin", ['a', 'b', 'c', 'd', {
            'new_val': 1
        }]),
        ([
            list_operations.list_append_items("string_bin", [['z', 'x'],
                                                             ('y', 'w')]),
            list_operations.list_get_range("string_bin", 3, 3)
        ], {
            "string_bin": ['d', ['z', 'x'], ('y', 'w')]
        }, "string_bin", ['a', 'b', 'c', 'd', ['z', 'x'], ('y', 'w')]),
        ([
            list_operations.list_insert("string_bin", 2, True),
            list_operations.list_pop("string_bin", 2)
        ], {
            'string_bin': True
        }, "string_bin", ['a', 'b', 'c', 'd']),
        ([
            list_operations.list_insert_items(
                "string_bin", 2, [bytearray("abc", "utf-8"), u"xyz"]),
            list_operations.list_pop_range("string_bin", 2, 2)
        ], {
            'string_bin': [bytearray(b'abc'), 'xyz']
        }, "string_bin", ['a', 'b', 'c', 'd']),
    ])
    def test_pos_operate_with_list_operations_different_datatypes(
            self, list, result, bin, expected):
        """
        Invoke operate() with list operations using different datatypes
        """
        key = ('test', 'demo', 'list_key')

        _, _, bins = self.as_connection.operate(key, list)

        assert bins == result

        _, _, bins = self.as_connection.get(key)

        assert bins[bin] == expected

    # Negative Tests
    def test_neg_operate_with_no_parameters(self):
        """
        Invoke operate() without any mandatory parameters.
        """
        with pytest.raises(TypeError) as typeError:
            self.as_connection.operate()
        assert "key" in str(typeError.value)

    @pytest.mark.parametrize("key, llist, expected",
                             [(('test', 'demo', 'bad_key'), [
                                 operations.delete(),
                             ], e.RecordNotFound)])
    def test_pos_operate_delete_with_incorrect_paramters(
            self, key, llist, expected):
        """
        Invoke operate() with correct parameters
        """

        with pytest.raises(expected):
            self.as_connection.operate(key, llist)

    def test_neg_operate_list_operation_bin_notlist(self):
        """
        Invoke operate() with a list operation and bin does not contain list
        """
        key = ('test', 'demo', 1)
        list = [list_operations.list_insert("age", 2, 9)]

        with pytest.raises(e.BinIncompatibleType):
            self.as_connection.operate(key, list)

    def test_neg_operate_append_items_not_a_list(self):
        """
        Invoke operate() with list addition operations negative
        """
        key = ('test', 'demo', 1)
        ops = [list_operations.list_append_items("int_bin", 7)]
        with pytest.raises(e.ParamError):
            self.as_connection.operate(key, ops)

    @pytest.mark.parametrize("list",
                             [([list_operations.list_get("int_bin", 7)]),
                              ([
                                  list_operations.list_clear("int_bin"),
                                  list_operations.list_pop("int_bin", 2)
                              ]),
                              ([
                                  list_operations.list_clear("int_bin"),
                                  list_operations.list_remove("int_bin", 2)
                              ])])
    def test_neg_operate_list_invalid_requests(self, list):
        """
        Invoke operate() with list addition operations negative
        """
        key = ('test', 'demo', 'list_key')
        with pytest.raises(e.OpNotApplicable):
            self.as_connection.operate(key, list)

    def test_neg_operate_with_bin_length_extra(self):
        """
        Invoke operate() with bin length extra. Strict types enabled
        """
        key = ('test', 'demo', 1)

        max_length = 'a' * 21

        llist = [
            operations.prepend("name", "ram"),
            operations.increment(max_length, 3)
        ]

        with pytest.raises(e.BinNameError):
            self.as_connection.operate(key, llist)

    def test_neg_operate_empty_string_key(self):
        """
        Invoke operate() with empty string key
        """
        llist = [operations.prepend("name", "ram")]
        with pytest.raises(e.ParamError):
            self.as_connection.operate("", llist)

    def test_neg_operate_with_extra_parameter(self):
        """
        Invoke operate() with extra parameter.
        """
        key = ('test', 'demo', 1)
        policy = {'timeout': 1000}
        llist = [operations.prepend("name", "ram")]
        with pytest.raises(TypeError) as typeError:
            self.as_connection.operate(key, llist, {}, policy, "")

    def test_neg_operate_policy_is_string(self):
        """
        Invoke operate() with policy is string
        """
        key = ('test', 'demo', 1)
        llist = [operations.prepend("name", "ram")]
        try:
            self.as_connection.operate(key, llist, {}, "")

        except e.ParamError as exception:
            assert exception.code == -2

    def test_neg_operate_key_is_none(self):
        """
        Invoke operate() with key is none
        """
        llist = [operations.prepend("name", "ram")]
        try:
            self.as_connection.operate(None, llist)

        except e.ParamError as exception:
            assert exception.code == -2

    def test_neg_operate_append_value_integer(self):
        """
        Invoke operate() with append value is of type integer
        """
        key = ('test', 'demo', 1)
        llist = [operations.append("name", 12)]

        try:
            self.as_connection.operate(key, llist)
        except e.ParamError as exception:
            assert exception.code == -2

    def test_neg_operate_with_incorrect_polic(self):
        """
        Invoke operate() with incorrect policy
        """
        key = ('test', 'demo', 1)
        policy = {'total_timeout': 0.5}
        llist = [operations.prepend("name", "ram")]

        with pytest.raises(e.ParamError):
            self.as_connection.operate(key, llist, {}, policy)
    # get_by_rank_range('l', 3, REVERSE_INDEX, 3): [5, 3, 2]

    print("get_by_rank_range('l', 3, RANK, 3): {}".format(bins[3][1]))
    # get_by_rank_range('l', 3, RANK, 3): [3, 4, 5]

    print("get_by_rank_range('l', 3, REVERSE_RANK, 3): {}".format(bins[4][1]))
    # get_by_rank_range('l', 3, REVERSE_RANK, 3): [2, 3, 4]

    print("get_by_rank_range('l', 3, COUNT, 3): {}".format(bins[5][1]))
    # get_by_rank_range('l', 3, COUNT, 3): 3

    # read all elements from rank -3 (third from the end in rank order)
    # and also append a new element to the end of the list
    ops = [
        listops.list_get_by_rank_range("l", -3, aerospike.LIST_RETURN_VALUE),
        listops.list_append("l", [1, 3, 3, 7, 0]),
        operations.read("l"),
    ]
    key, metadata, bins = client.operate_ordered(key, ops)
    print("\nget_by_rank_range('l', -3, VALUE): {}".format(bins[0][1]))
    # get_by_rank_range('l', -3, VALUE): [9, 11, 26]
    print("list_append('l', [1, 3, 3, 7, 0])")
    # list_append('l', [1, 3, 3, 7, 0])
    print("read('l'): {}".format(bins[2][1]))
    # read('l'): [1, 4, 7, 3, 9, 9, 26, 11, [1, 3, 3, 7, 0]]

    # try to perform a list operation on an index range outside of the current list
    try:
        key, metadata, bins = client.operate(
            key,
            [
class TestPredEveryWhere(object):
    @pytest.fixture(autouse=True)
    def setup(self, request, as_connection):
        """
        Setup Method
        """
        self.keys = []
        self.test_data = [{
            'account_id': j,
            'user_name': 'user' + str(j),
            'acct_balance': j * 10,
            'charges': [j + 5, j + 10],
            'meta': {
                'date': '11/4/2019'
            }
        } for j in range(1, 5)]
        self.test_data.append({'string_list': ['s1', 's2', 's3', 's4']})
        self.test_data.append(
            {'map_bin': {
                'k1': 1,
                'k2': 2,
                'k3': 3,
                'k4': 4
            }})

        georec = {
            'id': 1,
            'point': geo_point,
            'region': geo_circle,
            'geolist': [geo_point]
        }

        self.test_data.append(georec)
        self.test_data_bin = 'test_data'
        self.keys = [('test', 'pred_evry', i + 1)
                     for i, _ in enumerate(self.test_data)]
        # print('self keys is: ', self.keys)

        for key, data in zip(self.keys, self.test_data):
            self.as_connection.put(key, data)

        # cleanup
        yield

        for key in self.keys:
            try:
                self.as_connection.remove(key)
            except e.AerospikeError:
                pass

    @pytest.mark.parametrize(
        "ops, predexp, expected_bins, expected_res, key_num",
        [
            (  # test integer equal
                [operations.increment("account_id", 1)], [
                    as_predexp.integer_bin('account_id'),
                    as_predexp.integer_value(3),
                    as_predexp.integer_equal()
                ], {
                    'account_id': 4,
                    'user_name': 'user3',
                    'acct_balance': 30,
                    'charges': [8, 13],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {}, 3),
            (  # test string equal
                [operations.increment("account_id", 1)], [
                    as_predexp.string_bin('user_name'),
                    as_predexp.string_value('user3'),
                    as_predexp.string_equal()
                ], {
                    'account_id': 4,
                    'user_name': 'user3',
                    'acct_balance': 30,
                    'charges': [8, 13],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {}, 3),
            (  # test and
                [
                    list_operations.list_remove_by_index_range(
                        'charges', 0, 3, aerospike.LIST_RETURN_COUNT),
                    operations.increment("acct_balance", -23)
                ], [
                    as_predexp.integer_bin('acct_balance'),
                    as_predexp.integer_value(10),
                    as_predexp.integer_greatereq(),
                    as_predexp.integer_bin('acct_balance'),
                    as_predexp.integer_value(50),
                    as_predexp.integer_lesseq(),
                    as_predexp.predexp_and(2)
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 17,
                    'charges': [],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {
                    'charges': [0, 1]
                }, 4),
            (  # test or
                [map_operations.map_put('meta', 'lupdated', 'now')], [
                    as_predexp.string_bin('user_name'),
                    as_predexp.string_value('user2'),
                    as_predexp.string_equal(),
                    as_predexp.integer_bin('acct_balance'),
                    as_predexp.integer_value(50),
                    as_predexp.integer_greatereq(),
                    as_predexp.predexp_or(2)
                ], {
                    'account_id': 2,
                    'user_name': 'user2',
                    'acct_balance': 20,
                    'charges': [7, 12],
                    'meta': {
                        'date': '11/4/2019',
                        'lupdated': 'now'
                    }
                }, {
                    'meta': 2
                }, 2),
            (  # test integer greater
                [map_operations.map_clear('meta')], [
                    as_predexp.integer_bin('account_id'),
                    as_predexp.integer_value(2),
                    as_predexp.integer_greater()
                ], {
                    'account_id': 3,
                    'user_name': 'user3',
                    'acct_balance': 30,
                    'charges': [8, 13],
                    'meta': {}
                }, {
                    'meta': None
                }, 3),
            (  # test integer greatereq
                [map_operations.map_clear('meta')], [
                    as_predexp.integer_bin('account_id'),
                    as_predexp.integer_value(2),
                    as_predexp.integer_greatereq()
                ], {
                    'account_id': 3,
                    'user_name': 'user3',
                    'acct_balance': 30,
                    'charges': [8, 13],
                    'meta': {}
                }, {
                    'meta': None
                }, 3),
            (  # test integer less
                [list_operations.list_clear('charges')], [
                    as_predexp.integer_bin('account_id'),
                    as_predexp.integer_value(5),
                    as_predexp.integer_less()
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 40,
                    'charges': [],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {}, 4),
            (  # test integer lesseq
                [list_operations.list_clear('charges')], [
                    as_predexp.integer_bin('account_id'),
                    as_predexp.integer_value(4),
                    as_predexp.integer_lesseq()
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 40,
                    'charges': [],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {}, 4),
            (  # test string unequal
                [list_operations.list_append('charges', 2)], [
                    as_predexp.string_bin('user_name'),
                    as_predexp.string_value('user2'),
                    as_predexp.string_unequal()
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 40,
                    'charges': [9, 14, 2],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {
                    'charges': 3
                }, 4),
            (  # test not
                [list_operations.list_append('charges', 2)], [
                    as_predexp.string_bin('user_name'),
                    as_predexp.string_value('user4'),
                    as_predexp.string_unequal(),
                    as_predexp.predexp_not()
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 40,
                    'charges': [9, 14, 2],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {
                    'charges': 3
                }, 4),
            (  # test string regex
                [list_operations.list_append('charges', 2)], [
                    as_predexp.string_bin('user_name'),
                    as_predexp.string_value('.*4.*'),
                    as_predexp.string_regex(aerospike.REGEX_ICASE)
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 40,
                    'charges': [9, 14, 2],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {
                    'charges': 3
                }, 4),
            (  # test list or int
                [list_operations.list_append('charges', 2)], [
                    as_predexp.integer_var('list_val'),
                    as_predexp.integer_value(14),
                    as_predexp.integer_equal(),
                    as_predexp.list_bin('charges'),
                    as_predexp.list_iterate_or('list_val')
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 40,
                    'charges': [9, 14, 2],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {
                    'charges': 3
                }, 4),
            (  # test list and int
                [list_operations.list_append('charges', 2)], [
                    as_predexp.integer_var('list_val'),
                    as_predexp.integer_value(120),
                    as_predexp.integer_less(),
                    as_predexp.list_bin('charges'),
                    as_predexp.list_iterate_or('list_val')
                ], {
                    'account_id': 4,
                    'user_name': 'user4',
                    'acct_balance': 40,
                    'charges': [9, 14, 2],
                    'meta': {
                        'date': '11/4/2019'
                    }
                }, {
                    'charges': 3
                }, 4),
            (  # test list or str
                [list_operations.list_append('string_list', 's5')], [
                    as_predexp.string_var('list_val'),
                    as_predexp.string_value('s2'),
                    as_predexp.string_equal(),
                    as_predexp.list_bin('string_list'),
                    as_predexp.list_iterate_or('list_val')
                ], {
                    'string_list': ['s1', 's2', 's3', 's4', 's5']
                }, {
                    'string_list': 5
                }, 5),
            (  # test list and str
                [
                    list_operations.list_remove_by_index_range(
                        'string_list', 0, aerospike.LIST_RETURN_VALUE, 2)
                ], [
                    as_predexp.string_var('list_val'),
                    as_predexp.string_value('.*s.*'),
                    as_predexp.string_regex(aerospike.REGEX_ICASE),
                    as_predexp.list_bin('string_list'),
                    as_predexp.list_iterate_and('list_val')
                ], {
                    'string_list': ['s3', 's4']
                }, {
                    'string_list': ['s1', 's2']
                }, 5),
            (  # test map_key_iterate_or
                [map_operations.map_put('map_bin', 'k5', 5)], [
                    as_predexp.string_var('map_key'),
                    as_predexp.string_value('k3'),
                    as_predexp.string_equal(),
                    as_predexp.map_bin('map_bin'),
                    as_predexp.mapkey_iterate_or('map_key')
                ], {
                    'map_bin': {
                        'k1': 1,
                        'k2': 2,
                        'k3': 3,
                        'k4': 4,
                        'k5': 5
                    }
                }, {
                    'map_bin': 5
                }, 6),
            (  # test map_key_iterate_and
                [map_operations.map_put('map_bin', 'k5', 5)], [
                    as_predexp.string_var('map_key'),
                    as_predexp.string_value('k7'),
                    as_predexp.string_unequal(),
                    as_predexp.map_bin('map_bin'),
                    as_predexp.mapkey_iterate_and('map_key')
                ], {
                    'map_bin': {
                        'k1': 1,
                        'k2': 2,
                        'k3': 3,
                        'k4': 4,
                        'k5': 5
                    }
                }, {
                    'map_bin': 5
                }, 6),
            (  # test mapkey_iterate_and
                [map_operations.map_put('map_bin', 'k5', 5)], [
                    as_predexp.string_var('map_key'),
                    as_predexp.string_value('k7'),
                    as_predexp.string_unequal(),
                    as_predexp.map_bin('map_bin'),
                    as_predexp.mapkey_iterate_and('map_key')
                ], {
                    'map_bin': {
                        'k1': 1,
                        'k2': 2,
                        'k3': 3,
                        'k4': 4,
                        'k5': 5
                    }
                }, {
                    'map_bin': 5
                }, 6),
            (  # test mapval_iterate_and
                [map_operations.map_put('map_bin', 'k5', 5)], [
                    as_predexp.integer_var('map_val'),
                    as_predexp.integer_value(7),
                    as_predexp.integer_unequal(),
                    as_predexp.map_bin('map_bin'),
                    as_predexp.mapval_iterate_and('map_val')
                ], {
                    'map_bin': {
                        'k1': 1,
                        'k2': 2,
                        'k3': 3,
                        'k4': 4,
                        'k5': 5
                    }
                }, {
                    'map_bin': 5
                }, 6),
            (  # test mapval_iterate_or
                [
                    map_operations.map_get_by_key('map_bin', 'k1',
                                                  aerospike.MAP_RETURN_VALUE)
                ], [
                    as_predexp.integer_var('map_val'),
                    as_predexp.integer_value(3),
                    as_predexp.integer_less(),
                    as_predexp.map_bin('map_bin'),
                    as_predexp.mapval_iterate_or('map_val')
                ], {
                    'map_bin': {
                        'k1': 1,
                        'k2': 2,
                        'k3': 3,
                        'k4': 4
                    }
                }, {
                    'map_bin': 1
                }, 6)
        ])
    def test_predexp_key_operate(self, ops, predexp, expected_bins,
                                 expected_res, key_num):
        """
        Invoke the C client aerospike_key_operate with predexp.
        """
        key = ('test', 'pred_evry', key_num)

        _, _, res = self.as_connection.operate(key,
                                               ops,
                                               policy={'predexp': predexp})
        assert res == expected_res

        _, _, bins = self.as_connection.get(key)
        assert bins == expected_bins

    @pytest.mark.parametrize(
        "ops, predexp, expected_bins, expected_res, key_num",
        [(  # test mapval_iterate_or
            [
                map_operations.map_put_items('map_bin', {
                    'k5': 5,
                    'k6': 6
                }),
                map_operations.map_get_by_key('map_bin', 'k1',
                                              aerospike.MAP_RETURN_VALUE)
            ], [
                as_predexp.integer_var('map_val'),
                as_predexp.integer_value(3),
                as_predexp.integer_less(),
                as_predexp.map_bin('map_bin'),
                as_predexp.mapval_iterate_or('map_val')
            ], {
                'map_bin': {
                    'k1': 1,
                    'k2': 2,
                    'k3': 3,
                    'k4': 4,
                    'k5': 5,
                    'k6': 6
                }
            }, [('map_bin', 6), ('map_bin', 1)], 6)])
    def test_predexp_key_operate_ordered(self, ops, predexp, expected_bins,
                                         expected_res, key_num):
        """
        Invoke the C client aerospike_key_operate with predexp using operate_ordered.
        """
        key = ('test', 'pred_evry', key_num)

        _, _, res = self.as_connection.operate_ordered(
            key, ops, policy={'predexp': predexp})
        assert res == expected_res

        _, _, bins = self.as_connection.get(key)
        assert bins == expected_bins

    @pytest.mark.parametrize(
        "ops, predexp, expected, key_num",
        [(  # test mapval_iterate_or
            [
                map_operations.map_get_by_key('map_bin', 'k1',
                                              aerospike.MAP_RETURN_VALUE)
            ], [
                as_predexp.integer_var('map_val'),
                as_predexp.integer_value(3),
                as_predexp.integer_less(),
                as_predexp.map_bin('map_bin'),
                as_predexp.mapval_iterate_or('map_val'),
                as_predexp.predexp_not()
            ], e.FilteredOut, 6)])
    def test_predexp_key_operate_ordered_negative(self, ops, predexp, expected,
                                                  key_num):
        """
        Invoke the C client aerospike_key_operate with predexp using operate_ordered with expected failures.
        """
        key = ('test', 'pred_evry', key_num)

        with pytest.raises(expected):
            _, _, res = self.as_connection.operate_ordered(
                key, ops, policy={'predexp': predexp})

    @pytest.mark.parametrize(
        "ops, predexp, key_num, bin",
        [
            (  # test geojson_within
                [operations.increment('id', 1)], [
                    as_predexp.geojson_bin('point'),
                    as_predexp.geojson_value(geo_circle.dumps()),
                    as_predexp.geojson_within()
                ], 7, 'point'),
            (  # test geojson_contains
                [operations.increment('id', 1)], [
                    as_predexp.geojson_bin('region'),
                    as_predexp.geojson_value(geo_point.dumps()),
                    as_predexp.geojson_contains()
                ], 7, 'point'),
        ])
    def test_predexp_key_operate_geojson(self, ops, predexp, key_num, bin):
        """
        Invoke the C client aerospike_key_operate with predexp.
        """
        key = ('test', 'pred_evry', key_num)

        _, _, _ = self.as_connection.operate(key,
                                             ops,
                                             policy={'predexp': predexp})

        _, _, bins = self.as_connection.get(key)
        assert bins['id'] == 2

    # NOTE: may fail due to clock skew
    def test_predexp_key_operate_record_last_updated(self):
        """
        Invoke the C client aerospike_key_operate with a record_last_updated predexp.
        """

        for i in range(5):
            key = 'test', 'pred_lut', i
            self.as_connection.put(key, {'time': 'earlier'})

        cutoff_nanos = (10**9) * int(time.time() + 2)
        time.sleep(5)

        for i in range(5, 10):
            key = 'test', 'pred_lut', i
            self.as_connection.put(key, {'time': 'later'})

        results = []

        predexp = [
            as_predexp.rec_last_update(),
            as_predexp.integer_value(cutoff_nanos),
            as_predexp.integer_less()
        ]

        ops = [operations.read('time')]

        for i in range(10):
            try:
                key = 'test', 'pred_lut', i
                _, _, res = self.as_connection.operate(
                    key, ops, policy={'predexp': predexp})
                results.append(res)
            except:
                pass
            self.as_connection.remove(key)

        assert len(results) == 5
        for res in results:
            assert res['time'] == 'earlier'

    # NOTE: may fail due to clock skew
    def test_predexp_key_operate_record_void_time(self):
        """
        Invoke the C client aerospike_key_operate with a rec_void_time predexp.
        """

        for i in range(5):
            key = 'test', 'pred_ttl', i
            self.as_connection.put(key, {'time': 'earlier'}, meta={'ttl': 100})

        # 150 second range for record TTLs should be enough, we are storing with
        # Current time + 100s and current time +5000s, so only one of the group should be found
        void_time_range_start = (10**9) * int(time.time() + 50)
        void_time_range_end = (10**9) * int(time.time() + 150)

        for i in range(5, 10):
            key = 'test', 'pred_ttl', i
            self.as_connection.put(key, {'time': 'later'}, meta={'ttl': 1000})

        results = []

        predexp = [
            as_predexp.rec_void_time(),
            as_predexp.integer_value(void_time_range_start),
            as_predexp.integer_greater(),
            as_predexp.rec_void_time(),
            as_predexp.integer_value(void_time_range_end),
            as_predexp.integer_less(),
            as_predexp.predexp_and(2)
        ]

        ops = [operations.read('time')]

        for i in range(10):
            try:
                key = 'test', 'pred_ttl', i
                _, _, res = self.as_connection.operate(
                    key, ops, policy={'predexp': predexp})
                results.append(res)
            except:
                pass
            self.as_connection.remove(key)

        assert len(results) == 5
        for res in results:
            assert res['time'] == 'earlier'

    def test_predexp_key_operate_record_digest_modulo(self):
        """
        Invoke the C client aerospike_key_operate with a rec_digest_modulo predexp.
        """

        less_than_128 = 0
        for i in range(100):
            key = 'test', 'demo', i
            if aerospike.calc_digest(*key)[-1] < 128:
                less_than_128 += 1
            self.as_connection.put(key, {'dig_id': i})

        results = []

        predexp = [
            as_predexp.rec_digest_modulo(256),
            as_predexp.integer_value(128),
            as_predexp.integer_less()
        ]

        ops = [operations.read('dig_id')]

        for i in range(100):
            try:
                key = 'test', 'demo', i
                _, _, res = self.as_connection.operate(
                    key, ops, policy={'predexp': predexp})
                results.append(res)
            except:
                pass
            self.as_connection.remove(key)

        assert len(results) == less_than_128

    @pytest.mark.parametrize(
        "ops, predexp, expected, key_num",
        [
            (  # filtered out
                [operations.increment("account_id", 1)], [
                    as_predexp.integer_bin('account_id'),
                    as_predexp.integer_value(5),
                    as_predexp.integer_equal()
                ], e.FilteredOut, 3),
            (  # incorrect bin type
                [
                    list_operations.list_remove_by_index_range(
                        'charges', 0, 3, aerospike.LIST_RETURN_COUNT),
                    operations.increment("acct_balance", -23)
                ],
                [
                    as_predexp.integer_bin('acct_balance'),
                    as_predexp.string_value(10),  #incorrect bin type
                    as_predexp.integer_greatereq(),
                    as_predexp.integer_bin('acct_balance'),
                    as_predexp.integer_value(50),
                    as_predexp.integer_lesseq(),
                    as_predexp.predexp_and(2)
                ],
                e.ParamError,
                4),
            (  # filtered out
                [map_operations.map_put('meta', 'lupdated', 'now')], [
                    as_predexp.string_bin('user_name'),
                    as_predexp.string_value('user2'),
                    as_predexp.string_equal(),
                    as_predexp.integer_bin('acct_balance'),
                    as_predexp.integer_value(50),
                    as_predexp.integer_greatereq(),
                    as_predexp.predexp_or(2),
                    as_predexp.predexp_not()
                ], e.FilteredOut, 2),
            (  # empty predexp list
                [map_operations.map_put('meta', 'lupdated', 'now')
                 ], [], e.InvalidRequest, 2),
            (  # predexp not in list
                [map_operations.map_put('meta', 'lupdated', 'now')
                 ], 'bad predexp', e.ParamError, 2),
        ])
    def test_predexp_key_operate_negative(self, ops, predexp, expected,
                                          key_num):
        """
        Invoke the C client aerospike_key_operate with predexp. Expecting failures.
        """
        key = ('test', 'pred_evry', key_num)

        with pytest.raises(expected):
            self.as_connection.operate(key, ops, policy={'predexp': predexp})

    @pytest.mark.parametrize("predexp, rec_place, rec_bin, expected",
                             [([
                                 as_predexp.integer_bin('account_id'),
                                 as_predexp.integer_value(2),
                                 as_predexp.integer_equal()
                             ], 1, 'account_id', 2),
                              ([
                                  as_predexp.string_bin('user_name'),
                                  as_predexp.string_value('user2'),
                                  as_predexp.string_equal(),
                              ], 1, 'account_id', 2),
                              ([
                                  as_predexp.string_bin('user_name'),
                                  as_predexp.string_value('user2'),
                                  as_predexp.string_equal(),
                                  as_predexp.integer_bin('acct_balance'),
                                  as_predexp.integer_value(30),
                                  as_predexp.integer_greatereq(),
                                  as_predexp.predexp_or(2)
                              ], 2, 'account_id', 3)])
    def test_pos_get_many_with_predexp(self, predexp, rec_place, rec_bin,
                                       expected):
        '''
        Proper call to get_many with predexp in policy
        '''
        records = self.as_connection.get_many(self.keys, {'predexp': predexp})

        #assert isinstance(records, list)
        # assert records[2][2]['age'] == 2
        assert records[rec_place][2][rec_bin] == expected

    def test_pos_get_many_with_large_predexp(self):
        '''
        Proper call to get_many with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(4),
            as_predexp.integer_equal(),
            as_predexp.string_bin('user_name'),
            as_predexp.string_value('user3'),
            as_predexp.string_equal(),
            as_predexp.integer_var('list_val'),
            as_predexp.integer_value(12),
            as_predexp.integer_less(),
            as_predexp.list_bin('charges'),
            as_predexp.list_iterate_and('list_val'),
            as_predexp.predexp_or(3)
        ]

        matched_recs = []
        records = self.as_connection.get_many(self.keys, {'predexp': predexp})
        for rec in records:
            if rec[2] is not None:
                matched_recs.append(rec[2])

        assert len(matched_recs) == 3
        for rec in matched_recs:
            assert rec['account_id'] == 1 or rec['account_id'] == 3 or rec[
                'account_id'] == 4

    def test_pos_select_many_with_large_predexp(self):
        '''
        Proper call to select_many with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(4),
            as_predexp.integer_equal(),
            as_predexp.string_bin('user_name'),
            as_predexp.string_value('user3'),
            as_predexp.string_equal(),
            as_predexp.integer_var('list_val'),
            as_predexp.integer_value(12),
            as_predexp.integer_less(),
            as_predexp.list_bin('charges'),
            as_predexp.list_iterate_and('list_val'),
            as_predexp.predexp_or(3)
        ]

        matched_recs = []
        records = self.as_connection.select_many(self.keys, ['account_id'],
                                                 {'predexp': predexp})
        for rec in records:
            if rec[2] is not None:
                matched_recs.append(rec[2])

        assert len(matched_recs) == 3
        for rec in matched_recs:
            assert rec['account_id'] == 1 or rec['account_id'] == 3 or rec[
                'account_id'] == 4

    def test_pos_remove_with_predexp(self):
        '''
        Call remove with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(1),
            as_predexp.integer_equal()
        ]
        records = self.as_connection.remove(self.keys[0])

        rec = self.as_connection.exists(self.keys[0])
        assert rec[1] is None

    def test_remove_with_predexp_filtered_out(self):
        '''
        Call remove with predexp in policy with expected failure.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(3),
            as_predexp.integer_equal()
        ]
        with pytest.raises(e.FilteredOut):
            self.as_connection.remove(self.keys[0],
                                      policy={'predexp': predexp})

    def test_remove_bin_with_predexp(self):
        '''
        Call remove_bin with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(1),
            as_predexp.integer_equal()
        ]
        self.as_connection.remove_bin(self.keys[0],
                                      ['account_id', 'user_name'],
                                      policy={'predexp': predexp})

        rec = self.as_connection.get(self.keys[0])
        assert rec[2].get('account_id') is None and rec[2].get(
            'user_name') is None

    def test_remove_bin_with_predexp_filtered_out(self):
        '''
        Call remove_bin with predexp in policy with expected failure.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(4),
            as_predexp.integer_equal()
        ]
        with pytest.raises(e.FilteredOut):
            self.as_connection.remove_bin(self.keys[0],
                                          ['account_id', 'user_name'],
                                          policy={'predexp': predexp})

    def test_put_with_predexp(self):
        '''
        Call put with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(1),
            as_predexp.integer_equal()
        ]
        self.as_connection.put(self.keys[0], {'newkey': 'newval'},
                               policy={'predexp': predexp})

        rec = self.as_connection.get(self.keys[0])
        assert rec[2]['newkey'] == 'newval'

    def test_put_new_record_with_predexp(self):  # should this fail?
        '''
        Call put a new record with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(1),
            as_predexp.integer_equal()
        ]
        key = ("test", "demo", 10)
        self.as_connection.put(key, {'newkey': 'newval'},
                               policy={'predexp': predexp})

        rec = self.as_connection.get(key)
        self.as_connection.remove(key)
        assert rec[2]['newkey'] == 'newval'

    def test_put_with_predexp_filtered_out(self):
        '''
        Call put with predexp in policy with expected failure.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(4),
            as_predexp.integer_equal()
        ]
        with pytest.raises(e.FilteredOut):
            self.as_connection.put(self.keys[0], {'newkey': 'newval'},
                                   policy={'predexp': predexp})

    def test_get_with_predexp(self):
        '''
        Call to get with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(1),
            as_predexp.integer_equal()
        ]
        record = self.as_connection.get(self.keys[0], {'predexp': predexp})

        assert record[2]['account_id'] == 1

    def test_get_with_predexp_filtered_out(self):
        '''
        Call to get with predexp in policy with expected failures.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(3),
            as_predexp.integer_equal()
        ]
        with pytest.raises(e.FilteredOut):
            self.as_connection.get(self.keys[0], {'predexp': predexp})

    def test_select_with_predexp(self):
        '''
        Call to select with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('acct_balance'),
            as_predexp.integer_value(20),
            as_predexp.integer_equal(),
            as_predexp.integer_var('charge'),
            as_predexp.integer_value(20),
            as_predexp.integer_less(),
            as_predexp.list_bin('charges'),
            as_predexp.list_iterate_and('charge'),
            as_predexp.predexp_and(2)
        ]

        result = self.as_connection.select(self.keys[1],
                                           ['account_id', 'acct_balance'],
                                           {'predexp': predexp})
        assert result[2]['account_id'] == 2 and result[2]['acct_balance'] == 20

    def test_select_with_predexp_filtered_out(self):
        '''
        Call to select with predexp in policy with expected failures.
        '''
        predexp = [
            as_predexp.integer_bin('acct_balance'),
            as_predexp.integer_value(20),
            as_predexp.integer_equal(),
            as_predexp.integer_var('charge'),
            as_predexp.integer_value(
                10),  # charge should trigger a filtered_out
            as_predexp.integer_less(),
            as_predexp.list_bin('charges'),
            as_predexp.list_iterate_and('charge'),
            as_predexp.predexp_and(2)
        ]

        with pytest.raises(e.FilteredOut):
            self.as_connection.select(self.keys[1],
                                      ['account_id', 'acct_balance'],
                                      {'predexp': predexp})

    def test_exists_many_with_large_predexp(self):
        '''
        Proper call to exists_many with predexp in policy.
        '''
        predexp = [
            as_predexp.integer_bin('account_id'),
            as_predexp.integer_value(4),
            as_predexp.integer_equal(),
            as_predexp.string_bin('user_name'),
            as_predexp.string_value('user3'),
            as_predexp.string_equal(),
            as_predexp.integer_var('list_val'),
            as_predexp.integer_value(12),
            as_predexp.integer_less(),
            as_predexp.list_bin('charges'),
            as_predexp.list_iterate_and('list_val'),
            as_predexp.predexp_or(3)
        ]

        matched_recs = []
        records = self.as_connection.exists_many(self.keys,
                                                 {'predexp': predexp})
        for rec in records:
            if rec[1] is not None:
                matched_recs.append(rec[1])

        assert len(matched_recs) == 3
    client = aerospike.client(config).connect(options.username, options.password)
except ex.ClientError as e:
    print("Error: {0} [{1}]".format(e.msg, e.code))
    sys.exit(2)

key = (options.namespace, options.set, "list-append")
try:
    client.remove(key)
except ex.RecordError as e:
    pass

try:
    print("\nappend(bin, value[, writeFlags, context])\n")
    # create a new record with one element by upsert
    # by default a newly created list is unordered
    ret = client.operate(key, [listops.list_append("l", 1)])
    key, metadata, bins = client.get(key)
    print("append('l', 1): {}".format(bins["l"]))
    # [1]

    # append the same element with an ADD_UNIQUE and NO_FAIL write flags
    # this should fail gracefully
    policy = {
        "write_flags": aerospike.LIST_WRITE_ADD_UNIQUE | aerospike.LIST_WRITE_NO_FAIL,
        "list_order": aerospike.LIST_UNORDERED,
    }
    ret = client.operate(key, [listops.list_append("l", 1, policy)])
    print("\nappend('l', 1, ADD_UNIQUE|NO_FAIL)")
    print("The number of elements in the list is {}".format(ret[2]["l"]))
    # The number of elements in the list is 1
except ex.ClientError as e:
    print("Error: {0} [{1}]".format(e.msg, e.code))
    sys.exit(2)

key = (options.namespace, options.set, "fooz")
try:
    client.remove(key)
except ex.RecordError as e:
    pass

try:
    wildcard = aerospike.CDTWildcard()
    client.put(key, {"l": [[1, 'a'], [2, 'b'], [4, 'd'], [2, 'bb']]})
    ctx = [cdt_ctx.cdt_ctx_list_value([2, wildcard])]
    _, _, bins = client.operate(key, [
        listops.list_append("l", {'c': 3}, aerospike.MAP_RETURN_NONE, ctx=ctx)
    ])
    print("{}".format(bins["l"]))
    _, _, bins = client.get(key)
    print("{}".format(bins["l"]))
    sys.exit(18)

    # put the same element with the CREATE_ONLY and NO_FAIL write flags
    # this should fail gracefully
    policy = {
        "map_write_flags":
        aerospike.MAP_WRITE_FLAGS_CREATE_ONLY
        | aerospike.MAP_WRITE_FLAGS_NO_FAIL,
    }
    _, _, bins = client.operate(key, [mapops.map_put("l", "a", 11, policy)])
    print(