<commit_before>'use strict';
var path = require('path');
var fs = require('fs-extra');
var _ = require('lodash');
var joi = require('joi');
var joiJsonSchema = require('joi-to-json-schema');
var jj = joiJsonSchema;

var chai = require('chai');
chai.use(require('chai-json-schema'));

var MongoClient = require('mongodb').MongoClient;

var assert = chai.assert;

var url = 'mongodb://localhost:27017/test';


// init
var rootPath = path.resolve(__dirname + '/../../../..');
var tmpPath = rootPath + '/tests/tmp/Tingodb';

var logger = require('log4js').getLogger();

var ModelMongodb = require(rootPath + '/src/Model/Mongodb');

var db;

class TestModel extends ModelMongodb {
    constructor(db) {
        super(db);
        this._collectionName = 'test';
    }
}

var model;

describe('Model/Mongodb', function() {

    beforeEach(function(done) {

        if (db) {
            db.close();
        }

        MongoClient.connect(url, function(err, _db) {

            if (err) {
                return done(err);
            }

            db = _db;

            db.collection('test').drop(function (err) {

                if (err && err.message !== 'ns not found') {
                    return done(err);
                }

                model = new TestModel(db);

                model.init();

                done();
            });

        });

    });

    describe('#insertOne', function() {

        it('insert success', function(done) {

            model.insertOne({
                    id: 1,
                    name: 'test'
                })
                .then((item) => {

                    assert.jsonSchema(item, jj(joi.object().keys({
                        _id: joi.number().required().valid(1),
                        id: joi.number().required().valid(1),
                        name: joi.string().required().valid('test')
                    })));

                    done();
                })
                .catch((error) => {
                    done(error);
                });

        });

        it('reject error on duplicate id', function(done) {
            var data = {
                id: 1,
                name: 'test'
            };

            model.insertOne(data)
                .then((item) => {
                    return model.insertOne(data);
                })
                .catch((error) => {
                    assert.equal(error.message, 'record with id = 1 already exists');
                    assert.equal(error.code, model.Error.CODES.ALREADY_EXISTS);
                    done();
                });
        });

    });

    describe('#findOneAndUpdate', function () {

        it('update success', function (done) {
            var data = {
                id: 1,
                name: 'test'
            };

            model.insertOne(data)
                .then((item) => {
                    return model.findOneAndUpdate(
                        {id: 1},
                        {$set: {name: '100'}}
                    );<commit_msg>test: Model/Mongodb already exists test<commit_after>'use strict';
var path = require('path');
var fs = require('fs-extra');
var _ = require('lodash');
var joi = require('joi');
var joiJsonSchema = require('joi-to-json-schema');
var jj = joiJsonSchema;

var chai = require('chai');
chai.use(require('chai-json-schema'));

var MongoClient = require('mongodb').MongoClient;

var assert = chai.assert;

var url = 'mongodb://localhost:27017/test';


// init
var rootPath = path.resolve(__dirname + '/../../../..');
var tmpPath = rootPath + '/tests/tmp/Tingodb';

var logger = require('log4js').getLogger();

var ModelMongodb = require(rootPath + '/src/Model/Mongodb');

var db;

class TestModel extends ModelMongodb {
    constructor(db) {
        super(db);
        this._collectionName = 'test';
    }
}

var model;

describe('Model/Mongodb', function() {

    beforeEach(function(done) {

        if (db) {
            db.close();
        }

        MongoClient.connect(url, function(err, _db) {

            if (err) {
                return done(err);
            }

            db = _db;

            db.collection('test').drop(function (err) {

                if (err && err.message !== 'ns not found') {
                    return done(err);
                }

                model = new TestModel(db);

                model.init();

                done();
            });

        });

    });

    describe('#insertOne', function() {

        it('insert success', function(done) {

            model.insertOne({
                    id: 1,
                    name: 'test'
                })
                .then((item) => {

                    assert.jsonSchema(item, jj(joi.object().keys({
                        _id: joi.number().required().valid(1),
                        id: joi.number().required().valid(1),
                        name: joi.string().required().valid('test')
                    })));

                    done();
                })
                .catch((error) => {
                    done(error);
                });

        });

        it('reject error on duplicate id', function(done) {
            var data = {
                id: 1,
                name: 'test'
            };

            model.insertOne(data)
                .then((item) => {
                    return model.insertOne(data);
                })
                .catch((error) => {
                    assert.equal(error.message, 'document already exists');
                    assert.equal(error.code, model.Error.CODES.ALREADY_EXISTS);
                    done();
                });
        });

    });

    describe('#findOneAndUpdate', function () {

        it('update success', function (done) {
            var data = {
                id: 1,
                name: 'test'
            };

            model.insertOne(data)
                .then((item) => {
                    return model.findOneAndUpdate(
                        {id: 1},
                        {$set: {name: '100'}}
                    );
{'fraction_non_alphanumeric': 0.14722405816259088, 'fraction_numerical': 0.005122273628552544, 'mean_word_length': 1.305026656511805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5195129', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1599, 'n_words': 319}