<commit_before>var riakpbc = require('../index');
var chai = require('chai');
chai.Assertion.includeStack = true; // defaults to false
var expect = chai.expect;
var inspect = require('eyespect').inspector();
var q = require('q');
var client = riakpbc.createClient();
var sinon = require('sinon');
var async = require('async');
var savedKeys = {};

describe('Client test', function () {

    this.slow('.2s');

    after(function (done) {
        async.each(Object.keys(savedKeys), deleteKey, done);
    });

    it('setClientId', function (done) {
        client.setClientId({
            client_id: 'testrunner'
        }, function (err, reply) {
            expect(err).to.not.exist;
            done();
        });
    });

    it('getClientId', function (done) {
        client.getClientId(function (err, reply) {
            expect(err).to.not.exist;
            expect(reply.client_id).to.equal('testrunner');
            done();
        });
    });

    it('ping', function (done) {
        client.ping(function (err, reply) {
            expect(err).to.not.exist;
            done();
        });
    });

    it('getServerInfo', function (done) {
        client.getServerInfo(function (err, reply) {
            expect(err).to.not.exist;
            expect(reply.node).to.exist;
            expect(reply.server_version).to.exist;
            done();
        });
    });

    it('put', function (done) {
        var bucket = 'test';
        var valueObject = {
            test: 'data'
        };
        var indexes = [];
        indexes.push({
            key: 'test_bin',
            value: 'test'
        });

        var value = JSON.stringify(valueObject);
        var content = {
            value: value,
            content_type: 'application/json',
            indexes: indexes
        };

        var key = 'test';
        savedKeys[key] = bucket;

        var opts = {
            bucket: bucket,
            key: key,
            content: content,
            return_body: true
        };
        client.put(opts, function (err, reply) {
            expect(err).to.not.exist;
            var value = reply.content[0].value;
            expect(value).to.exist;
            expect(value).to.eql(valueObject);
            done();
        });
    });

    it('putVclock', function (done) {
        var key = 'test-vclock';
        var bucket = 'test';
        var value = '{"test":"data"}';
        var options = {
            bucket: bucket,
            key: key,
            content: {
                value: value,
                content_type: 'application/json'
            },
            return_body: true
        };
        savedKeys[key] = bucket;
        client.put(options, function (err, reply) {
            expect(err).to.not.exist;
            var options = {
                bucket: 'test',
                key: 'test-vclock',
                content: {
                    value: '{"test":"data"}',
                    content_type: 'application/json'
                },
                return_body: true
            };
            options.vclock = reply.vclock;
            client.put(options, function (err) {
                expect(err).to.not.exist;
                done();
            });
        });
    });

    describe('Secondary Index', function () {

        var bucket = 'test_secondary_index';
        var value = '{"test":"data"}';
        var keyValue = 'test_put_key';
        savedKeys[keyValue] = bucket;
        var binaryIndexKey = 'test_put_bin';
        var binaryIndexValue = 'value1';
        var integerIndexKey = 'test_put_int';
        var integerIndexValue = 20;


        it('putIndex', function (done) {
            var integerIndex = {
                key: integerIndexKey,
                value: integerIndexValue
            };
            var binaryIndex = {
                key: binaryIndexKey,
                value: binaryIndexValue
            };
            var indexes = [binaryIndex, integerIndex];
            var options = {
                bucket: bucket,
                key: keyValue,
                content: {
                    value: value,
                    content_type: 'application/json',
                    indexes: indexes
                },
                return_body: true
            };
            client.put(options, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                var replyIndexes = reply.content[0].indexes;
                expect(replyIndexes.length).to.eql(indexes.length);
                done();
            });
        });

        it('getIndex for single value', function (done) {
            var opts = {
                bucket: bucket,
                index: binaryIndexKey,
                qtype: 0,
                key: binaryIndexValue
            };
            client.getIndex(opts, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                expect(reply).to.have.ownProperty('keys');
                expect(reply.keys).to.be.an('array');
                expect(reply.keys[0]).to.equal(keyValue);
                done();
            });
        });

        it('getIndex for binary range', function (done) {
            var opts = {
                bucket: bucket,
                index: binaryIndexKey,
                qtype: 1,
                range_min: '!',
                range_max: '~'
            };
            client.getIndex(opts, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                expect(reply).to.have.ownProperty('keys');
                expect(reply.keys).to.be.an('array');
                expect(reply.keys[0]).to.equal(keyValue);
                done();
            });
        });

        it('getIndex for integer range', function (done) {
            var opts = {
                bucket: bucket,
                index: integerIndexKey,
                qtype: 1,
                range_min: -99999,
                range_max: 99999
            };
            client.getIndex(opts, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                expect(reply).to.have.ownProperty('keys');<commit_msg>Add: test for streaming secondary index query.<commit_after>var riakpbc = require('../index');
var chai = require('chai');
chai.Assertion.includeStack = true; // defaults to false
var expect = chai.expect;
var inspect = require('eyespect').inspector();
var q = require('q');
var client = riakpbc.createClient();
var sinon = require('sinon');
var async = require('async');
var savedKeys = {};

describe('Client test', function () {

    this.slow('.2s');

    after(function (done) {
        async.each(Object.keys(savedKeys), deleteKey, done);
    });

    it('setClientId', function (done) {
        client.setClientId({
            client_id: 'testrunner'
        }, function (err, reply) {
            expect(err).to.not.exist;
            done();
        });
    });

    it('getClientId', function (done) {
        client.getClientId(function (err, reply) {
            expect(err).to.not.exist;
            expect(reply.client_id).to.equal('testrunner');
            done();
        });
    });

    it('ping', function (done) {
        client.ping(function (err, reply) {
            expect(err).to.not.exist;
            done();
        });
    });

    it('getServerInfo', function (done) {
        client.getServerInfo(function (err, reply) {
            expect(err).to.not.exist;
            expect(reply.node).to.exist;
            expect(reply.server_version).to.exist;
            done();
        });
    });

    it('put', function (done) {
        var bucket = 'test';
        var valueObject = {
            test: 'data'
        };
        var indexes = [];
        indexes.push({
            key: 'test_bin',
            value: 'test'
        });

        var value = JSON.stringify(valueObject);
        var content = {
            value: value,
            content_type: 'application/json',
            indexes: indexes
        };

        var key = 'test';
        savedKeys[key] = bucket;

        var opts = {
            bucket: bucket,
            key: key,
            content: content,
            return_body: true
        };
        client.put(opts, function (err, reply) {
            expect(err).to.not.exist;
            var value = reply.content[0].value;
            expect(value).to.exist;
            expect(value).to.eql(valueObject);
            done();
        });
    });

    it('putVclock', function (done) {
        var key = 'test-vclock';
        var bucket = 'test';
        var value = '{"test":"data"}';
        var options = {
            bucket: bucket,
            key: key,
            content: {
                value: value,
                content_type: 'application/json'
            },
            return_body: true
        };
        savedKeys[key] = bucket;
        client.put(options, function (err, reply) {
            expect(err).to.not.exist;
            var options = {
                bucket: 'test',
                key: 'test-vclock',
                content: {
                    value: '{"test":"data"}',
                    content_type: 'application/json'
                },
                return_body: true
            };
            options.vclock = reply.vclock;
            client.put(options, function (err) {
                expect(err).to.not.exist;
                done();
            });
        });
    });

    describe('Secondary Index', function () {

        var bucket = 'test_secondary_index';
        var value = '{"test":"data"}';
        var keyValue = 'test_put_key';
        savedKeys[keyValue] = bucket;
        var binaryIndexKey = 'test_put_bin';
        var binaryIndexValue = 'value1';
        var integerIndexKey = 'test_put_int';
        var integerIndexValue = 20;


        it('putIndex', function (done) {
            var integerIndex = {
                key: integerIndexKey,
                value: integerIndexValue
            };
            var binaryIndex = {
                key: binaryIndexKey,
                value: binaryIndexValue
            };
            var indexes = [binaryIndex, integerIndex];
            var options = {
                bucket: bucket,
                key: keyValue,
                content: {
                    value: value,
                    content_type: 'application/json',
                    indexes: indexes
                },
                return_body: true
            };
            client.put(options, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                var replyIndexes = reply.content[0].indexes;
                expect(replyIndexes.length).to.eql(indexes.length);
                done();
            });
        });

        it('getIndex for single value', function (done) {
            var opts = {
                bucket: bucket,
                index: binaryIndexKey,
                qtype: 0,
                key: binaryIndexValue
            };
            client.getIndex(opts, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                expect(reply).to.have.ownProperty('keys');
                expect(reply.keys).to.be.an('array');
                expect(reply.keys[0]).to.equal(keyValue);
                done();
            });
        });

        it('getIndex for binary range', function (done) {
            var opts = {
                bucket: bucket,
                index: binaryIndexKey,
                qtype: 1,
                range_min: '!',
                range_max: '~'
            };
            client.getIndex(opts, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                expect(reply).to.have.ownProperty('keys');
                expect(reply.keys).to.be.an('array');
                expect(reply.keys[0]).to.equal(keyValue);
                done();
            });
        });

        it('getIndex streaming for binary range', function (done) {
            var opts = {
                bucket: bucket,
                index: binaryIndexKey,
                qtype: 1,
                range_min: '!',
                range_max: '~'
            };
            var streaming = true;
            var readStream = client.getIndex(opts, streaming);
            var dataHandlerSpy = sinon.spy(dataHandler);
            readStream.on('data', dataHandlerSpy);
            readStream.on('end', endHandler);

            function dataHandler(reply) {
                expect(reply).to.exist;
                expect(reply).to.have.ownProperty('keys');
                expect(reply.keys).to.be.an('array');
                expect(reply.keys[0]).to.equal(keyValue);
            }

            function endHandler() {
                expect(dataHandlerSpy.callCount).to.be.above(0);
                done();
            }
        });

        it('getIndex for integer range', function (done) {
            var opts = {
                bucket: bucket,
                index: integerIndexKey,
                qtype: 1,
                range_min: -99999,
                range_max: 99999
            };
            client.getIndex(opts, function (err, reply) {
                expect(err).to.not.exist;
                expect(reply).to.exist;
                expect(reply).to.have.ownProperty('keys');
{'fraction_non_alphanumeric': 0.12781012751529447, 'fraction_numerical': 0.0033168718213311714, 'mean_word_length': 1.3596521739130434, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6772728', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3529, 'n_words': 710}