<commit_before>/* jshint undef: true, strict:false, trailing:false, unused:false */
/* global require, exports, console, process, module, describe, it, expect, jasmine*/

/**
 *  Simple test runner for INDX ---
 *   (c) 2014 - <NAME>, University of Southampton 
 * 
 *  This is an INDX runner that teests some random things
  */

var nodeindx = require('../../lib/services/nodejs/nodeindx'),
    u = nodeindx.utils,
    _ = require('underscore'),
    jQuery = require('jquery'),
    tests = require('../common'),
    test_box = 'c';

jasmine.getEnv().defaultTimeoutInterval = 50000;

describe('indx basic connect', function() { 
    // 
    it('tests must be invoked with host, user, and pass', function() { 
        var args = tests.get_args();
        expect(args).toBeDefined();
    });

    it('connected and login', function(done) { 
        tests.connect().then(function() { done(); });
    });
});

describe('creation of boxes', function() { 
    var bname = 'testbox' + u.guid(5), created;
    it('creates boxes', function(done) { 
        tests.connect().then(function(store) { 
            store.createBox(bname).then(function() { 
                console.log('sucess creating box ');
                store.getBoxList().then(function(bL) {
                    expect(bL.indexOf(name) >= 0).toBe(true);
                    created = true;
                    done();
                }).fail(function(e) { console.error('error listing boxes' , e); });
            }).fail(function(e) { 
                console.error('error creating box', e); 
                expect(true).toBe(false);
                done();
            });
        });
    });
    it('deletes boxes', function(done) {    
        if (created) {
            tests.connect().then(function(store) {
                store.deleteBox(bname).then(function() {
                    store.getBoxList().then(function(bL) {
                        expect(bL.indexOf(name) >= 0).toBe(false);
                        done();
                    }).fail(function(e) { console.error('error listing boxes' , e); });
                }).fail(function(e) { console.error(' error deleting box ', e); });
            });
        } else { done(); }
    }); 
});

describe('creation of an object', function() { 
    var oid = 'testobj' + u.guid(5), 
        key = 'testkey' + u.guid(12),
        val = u.guid(239),
        part2;

    // create it
    it('creates an object', function(done) { 
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(oid).then(function(obj) { 
                    obj.set(key,val);
                    obj.save().then(function() { 
                        done();
                    });
                });
            });
        });
    });
    // set some vals
    it('checks if it created it', function(done) {
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(oid).then(function(obj) {
                    expect(obj.get(key) !== undefined).toBe(true);
                    expect(obj.get(key).length).toBe(1);
                    expect(obj.get(key)[0]).toBe(val);
                    done();
                })
            });
        });
    });
    // delete it
    it('deletes the object', function(done) {
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(oid).then(function(o) {
                    console.log('destroying object ', oid);
                    o.destroy().then(function(x) {
                        console.log('object destroyed.. ') ;
                        done();
                    }).fail(function(e) { console.error('error deleting ', e); });
                })
            });
        });
    });
    // check deleted
    it('deleted the object successfully', function(done) {
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                var ids = box.getObjIDs();
                expect(ids.indexOf(oid)).toBe(-1);
                done();
            });
        });
    });
});




describe('object star stress test', function() { 
    var N = 4, 
        ids = u.range(N).map(function(x) { return 'star-stress-' + u.guid(5); });
    it('creates the objects', function(done) { 
        console.log(' creates the objects  ', ids.length);
        var os = {};
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(ids).then(function(objs) {
                    objs.map(function(v) { os[v.id] = v; });
                    var saved =  objs.map(function(o) {
                        var allbutme = _(os).omit(o.id);
                        expect(_(allbutme).size()).toBe(N-1);

                        
                        o.set(allbutme);
                        console.log('setting allbutme ', allbutme);

                        //  o.set('all', _(objs).without(o));
                        return o.save();
                    }); 
                    u.when(saved).then(function() { 
                        done();
                    });
                });
            });
        });
    });
    it('loads and tests the objects', function(done) { 
        console.log('load and test the objects ------------- ');
        var os = {};        
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                console.log('getting ids ------------------------------------- !', ids.length);
                box.getObj(ids[0]).then(function(objs) {
                    console.log('got ids ...  ', objs.length);
                    try {
                        // first make sure we got all of the objects back
                        expect(objs.length).toBe(N);
                        // second, let's build up an array
                        objs.map(function(v) { os[v.id] = v; });
                        objs.map(function(o){ 
                            var keys = o.keys();
                            console.log('keys > ', keys);
                            // expect(keys.length).toBe(N); // 'all' + 'each of the ids'
                            // keys.map(function(k) { 
                            //     expect(o.get(k)).toBeDefined();
                            //     expect(o.get(k).length).toBe(1);
                            //     expect(o.get(k)[0]).toBe(os[k]);
                            // });
                            // expect(o.get('all')).toBeDefined();
                            // expect(o.get('all').length).toBe(N);
                            // expect(o.get('all').difference(objs).length).toBe(0);
                        });
                        done();
                    } catch(e) { 
                        console.error(e); 
                        done();
                    }
                });
            });<commit_msg>getObj works beautifully and added spec<commit_after>/* jshint undef: true, strict:false, trailing:false, unused:false */
/* global require, exports, console, process, module, describe, it, expect, jasmine*/

/**
 *  Simple test runner for INDX ---
 *   (c) 2014 - <NAME>, University of Southampton 
 * 
 *  This is an INDX runner that teests some random things
  */

var nodeindx = require('../../lib/services/nodejs/nodeindx'),
    u = nodeindx.utils,
    _ = require('underscore'),
    jQuery = require('jquery'),
    tests = require('../common'),
    test_box = 'c';

jasmine.getEnv().defaultTimeoutInterval = 50000;

describe('indx basic connect', function() { 
    // 
    it('tests must be invoked with host, user, and pass', function() { 
        var args = tests.get_args();
        expect(args).toBeDefined();
    });

    it('connected and login', function(done) { 
        tests.connect().then(function() { done(); });
    });
});

describe('creation of boxes', function() { 
    var bname = 'testbox' + u.guid(5), created;
    it('creates boxes', function(done) { 
        tests.connect().then(function(store) { 
            store.createBox(bname).then(function() { 
                console.log('sucess creating box ');
                store.getBoxList().then(function(bL) {
                    expect(bL.indexOf(name) >= 0).toBe(true);
                    created = true;
                    done();
                }).fail(function(e) { console.error('error listing boxes' , e); });
            }).fail(function(e) { 
                console.error('error creating box', e); 
                expect(true).toBe(false);
                done();
            });
        });
    });
    it('deletes boxes', function(done) {    
        if (created) {
            tests.connect().then(function(store) {
                store.deleteBox(bname).then(function() {
                    store.getBoxList().then(function(bL) {
                        expect(bL.indexOf(name) >= 0).toBe(false);
                        done();
                    }).fail(function(e) { console.error('error listing boxes' , e); });
                }).fail(function(e) { console.error(' error deleting box ', e); });
            });
        } else { done(); }
    }); 
});

describe('creation of an object', function() { 
    var oid = 'testobj' + u.guid(5), 
        key = 'testkey' + u.guid(12),
        val = u.guid(239),
        part2;

    // create it
    it('creates an object', function(done) { 
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(oid).then(function(obj) { 
                    obj.set(key,val);
                    obj.save().then(function() { 
                        done();
                    });
                });
            });
        });
    });
    // set some vals
    it('checks if it created it', function(done) {
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(oid).then(function(obj) {
                    expect(obj.get(key) !== undefined).toBe(true);
                    expect(obj.get(key).length).toBe(1);
                    expect(obj.get(key)[0]).toBe(val);
                    done();
                })
            });
        });
    });
    // delete it
    it('deletes the object', function(done) {
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(oid).then(function(o) {
                    console.log('destroying object ', oid);
                    o.destroy().then(function(x) {
                        console.log('object destroyed.. ') ;
                        done();
                    }).fail(function(e) { console.error('error deleting ', e); });
                })
            });
        });
    });
    // check deleted
    it('deleted the object successfully', function(done) {
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                var ids = box.getObjIDs();
                expect(ids.indexOf(oid)).toBe(-1);
                done();
            });
        });
    });
});




describe('object star stress test', function() { 
    var N = 4, 
        ids = u.range(N).map(function(x) { return 'star-stress-' + u.guid(5); });
    it('creates the objects', function(done) { 
        console.log(' creates the objects  ', ids.length);
        var os = {};
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                box.getObj(ids).then(function(objs) {
                    objs.map(function(v) { os[v.id] = v; });
                    var saved =  objs.map(function(o) {
                        var allbutme = _(os).omit(o.id);
                        expect(_(allbutme).size()).toBe(N-1);

                        
                        o.set(allbutme);
                        console.log('setting allbutme ', allbutme);
                        o.set('all', objs);
                        return o.save();
                    }); 
                    u.when(saved).then(function() { 
                        done();
                    });
                });
            });
        });
    });
    it('loads and tests the objects', function(done) { 
        console.log('load and test the objects ------------- ');
        var os = {};        
        tests.connect().then(function(store) { 
            store.getBox(test_box).then(function(box) { 
                console.log('getting ids ------------------------------------- !', ids.length);
                box.getObj(ids).then(function(objs) {
                    console.log('got ids ...  ', objs.length);
                    try {
                        // first make sure we got all of the objects back
                        expect(objs.length).toBe(N);
                        // second, let's build up an array
                        objs.map(function(v) { os[v.id] = v; });
                        objs.map(function(o){ 
                            var keys = _(ids).without(o.id);
                            console.log(o.id, ' - keys > ', keys);
                            // expect(keys.length).toBe(N); // 'all' + 'each of the ids'
                            keys.map(function(k) { 
                                 expect(o.get(k)).toBeDefined();
                                 expect(o.get(k).length).toBe(1);
                                 expect(o.get(k)[0]).toBe(os[k]);
                            });
                            expect(o.get('all')).toBeDefined();
                            expect(o.get('all').length).toBe(N);
                            expect(_(o.get('all')).difference(objs).length).toBe(0);
                        });
                        done();
                    } catch(e) { 
                        console.error(e); 
                        done();
                    }
                });
            });
{'fraction_non_alphanumeric': 0.16102956817698363, 'fraction_numerical': 0.004041693256753882, 'mean_word_length': 1.3049517895080895, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5979940', 'n_tokens_mistral': 3972, 'n_tokens_neox': 3677, 'n_words': 658}