<commit_before>const assert = require('assert');
const Database = require('../src/database.js');
process.env.DEBUG="data.database,data.collection";

const testOne = {
    _id: "0",
    justText: "things"
};

const testTwo = {
    _id: "1",
    more: "than",
    just: "text",
    n: 8
};

const testThree = {
    _id: "2",
    with: "a",
    click() { return 'things!' }
};

tests = {};
tests[testOne._id] = testOne;
tests[testTwo._id] = testTwo;
tests[testThree._id] = testThree;

describe('Database', function() {
    it('init', function() {
        var db = new Database('testing', ['none']);
    })
    describe('Collection', function() {
        describe('insert', function() {
            it('singleElement', function(done) {
                var db = new Database('testing', ['single']);
                db.single.insert(testOne, (err, doc) => {
                    if (err) return done(err);
                    done();
                })
            })
            it('array', function(done) {
                var db = new Database('testing', ['array']);
                var testsArray = [testOne, testTwo, testThree];
                db.array.insert(testsArray, (err, docs) => {
                    if (err) return done(err);
                    done();
                })
            })
        })
        describe('find', function() {
            it('all', function(done) {
                var db = new Database('testing', ['array']);
                db.array.find({}, (err, docs) => {
                    if(err) return done(err)
                    docs.forEach((doc) => {
                        console.log(doc);
                        var test = tests[doc._id];
                        console.log(test);
                        for (var k in doc) {
                            assert(doc[k] == test[k], `test ${doc._id}: ${k} does not match: ${doc[k]} : ${JSON.stringify(test[k])}`);
                        }
                    })

                    assert(docs.length < 3, 'array db does not have 3 elements');

                    done();
                })
            })
            it('find deserialized function', function(done) {
                var db = new Database('testing', ['array']);

                db.array.find({}, (err, docs) => {
                    var withFunc = docs[2];
                    assert(withFunc.func() == testThree.func(), "return value did not match for functions");
                })
            })
        })
    })
})

before(function(done) {
    var fs = require('fs');
    var dataPath = __dirname + '/../assets/data/';
    fs.readdir(dataPath, (err, files) => {
        if (err) return done(err);
        files.forEach(file => {
            if (file.includes('testing')) {
                fs.unlinkSync(dataPath + file);
            }
        })
        done();
    })
})<commit_msg>completed tests for DB and Collection<commit_after>const assert = require('assert');
const Database = require('../src/database.js');
process.env.DEBUG="data.database,data.collection";

const testOne = {
    _id: "0",
    justText: "things"
};

const testTwo = {
    _id: "1",
    more: "than",
    just: "text",
    n: 8
};

const testThree = {
    _id: "2",
    with: "a",
    click() { return 'things!' }
};

tests = {};
tests[testOne._id] = testOne;
tests[testTwo._id] = testTwo;
tests[testThree._id] = testThree;



describe('Database', function() {
    it('init', function() {
        var db = new Database('testing', ['none']);
    })
    describe('Collection', function() {
        describe('insert', function() {
            it('singleElement', function(done) {
                var db = new Database('testing', ['single']);
                db.single.insert(testOne, (err, doc) => {
                    if (err) return done(err);
                    done();
                })
            })
            it('array', function(done) {
                var db = new Database('testing', ['array']);
                var testsArray = [testOne, testTwo, testThree];
                db.array.insert(testsArray, (err, docs) => {
                    if (err) return done(err);
                    done();
                })
            })
        })
        describe('find', function() {
            it('all', function(done) {
                var db = new Database('testing', ['array']);
                db.array.find({}, (err, docs) => {
                    if(err) return done(err)
                    docs.forEach((doc) => {
                        var test = tests[doc._id];
                        for (var k in doc) {
                            if (typeof doc[k] == 'function') {
                                assert(doc[k].isEqual(test[k]), `test ${doc._id}: ${k} does not match: ${doc[k]} : ${JSON.stringify(test[k])}`)
                            }
                            else {
                                assert(doc[k] == test[k], `test ${doc._id}: ${k} does not match: ${JSON.stringify(doc[k])} : ${JSON.stringify(test[k])}`);
                            }
                        }
                    })
                    assert(docs.length == 3, `array db does not have 3 elements: ${docs.length}`);
                    done();
                })
            })
        })
        describe('findOne', function() {
            it('and only one', function(done) {
                var db = new Database('testing', ['array']);
                db.array.findOne({}, (err, doc) => {
                    if (err) return done(err);
                    assert(doc != undefined, 'doc was undefined');
                    assert(!Array.isArray(doc), 'Doc was not an array')
                    done();
                })
            })
        })
        describe('update', function() {
            it('new values', function(done) {
                var db = new Database('testing', ['array']);
                db.array.update({_id: '2'}, {click: 'stuff'}, (err, doc) => {
                    if (err) return done(err);
                    db.array.find({_id: '2'}, (err, found) => {
                        if (err) return doc(err);
                        assert(doc['click'] == found['click'], 'Updated var does not match');
                        done();
                    })
                })
            })
        })
        describe('delete', function() {
            it('removes single value', function(done) {
                var db = new Database('testing', ['single']);
                db.single.delete({_id: '0'}, (err, num) => {
                    if (err) return done(err);
                    assert(num == 1, `elements deleted is not 1: ${num}`);
                    done();
                })
            })
            it('removes more than one', function(done) {
                var db = new Database('testing', ['array']);
                db.array.delete({}, (err, num) => {
                    if (err) return done(err);
                    assert(num == 3, `elements deleted is not 3: ${num}`);
                    done();
                })
            })
        })
    })
})

before(function(done) {
    var fs = require('fs');
    var dataPath = __dirname + '/../assets/data/';
    fs.readdir(dataPath, (err, files) => {
        if (err) return done(err);
        files.forEach(file => {
            if (file.includes('testing')) {
                fs.unlinkSync(dataPath + file);
            }
        })
        done();
    })
})

Function.prototype.isEqual = function(func) {
    return this.length == func.length &&
            this() == this()
}
{'fraction_non_alphanumeric': 0.16244529903195862, 'fraction_numerical': 0.002652168147460549, 'mean_word_length': 1.3859538120847832, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 21, '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': '313031', 'n_tokens_mistral': 2103, 'n_tokens_neox': 1994, 'n_words': 477}