<commit_before>/** External modules */
import Seneca from 'seneca';
import Sequelize from 'sequelize';
import { expect } from 'chai';
import Faker from 'faker';
import _ from 'lodash';

/** Internal modules */
import XDb from '.';

const testTable = 'test_x_db';

const models = [
	{
		name: testTable, schema: {
			username: {
				type: Sequelize.STRING(128),
				unique: true,
				validate: {
					is: /^[a-zA-Z0-9._]+$/i,
					len: [3, 128]
				}
			},
			email: {
				type: Sequelize.STRING(255),
				unique: true,
				validate: {
					isEmail: true,
				}
			},
			first_name: {
				type: Sequelize.STRING(60),
				allowNull: true,
				defaultValue: 'Lorem',
				validate: {
					is: /^[a-zA-Z0-9._ ]+$/i,
					len: [0, 60]
				}
			},
			last_name: {
				type: Sequelize.STRING(60),
				allowNull: true,
				defaultValue: 'Ipsum',
				validate: {
					is: /^[a-zA-Z0-9._ ]+$/i,
					len: [0, 60]
				}
			},
		}
	}
]

describe('XDb', function () {
	const TestApp = fn => {
		return Seneca({
			log: 'test'
		})
			.test(fn)
			.use(XDb, { models, options: { logging: false } })
	}
	let db, app, rowId;

	before((done) => {
		app = TestApp(done);
		app.ready(function () {
			db = this.XDb$;
			return done();
		})
	})

	it('Insert data', function (done) {
		const data = {
			model: testTable,
			attributes: {
				username: Faker.internet.userName(),
				email: Faker.internet.email(),
				first_name: Faker.name.findName(),
				last_name: Faker.name.lastName(),
			}
		}
		app.act('x_db:create', data, function (err, result = {}) {
			const { data$ } = result;

			expect(err).to.be.not.exist;
			expect(data$).to.be.an('object');
			expect(data$.id).to.be.an('number');
			expect(data$.username).to.equal(data.attributes.username);

			rowId = data$.id;
			return done();
		})
	});

	it('Find all data', function (done) {
		const data = {
			model: testTable,
		}
		app.act('x_db:find_all', data, function (err, result = {}) {
			const { data$, _meta$ } = result;

			expect(err).to.be.not.exist;
			expect(data$).to.be.an('array');
			expect(data$.length).to.equal(1);
			expect(_meta$).to.be.exist;
			expect(_meta$.count).to.be.exist;
			expect(_meta$.count).to.equal(1);
			return done();
		})
	});

	it('Find data by id', function (done) {
		const data = {
			model: testTable,
			id: rowId
		}
		app.act('x_db:find_by_id', data, function (err, result = {}) {
			const { data$ } = result;

			expect(err).to.be.not.exist;
			expect(data$).to.be.an('object');
			expect(data$.id).to.be.an('number');
			expect(data$.id).to.equal(rowId);
			expect(_.size(data$)).to.equal(1);
			return done();
		})
	});

	it('Update data by id', function (done) {
		const data = {
			model: testTable,
			id: rowId,
			attributes: {
				username: Faker.internet.userName(),
			}
		}
		app.act('x_db:update', data, function (err, result = {}) {
			const { data$ } = result;

			expect(err).to.be.not.exist;
			expect(data$).to.be.an('object');
			expect(data$.id).to.be.an('number');
			expect(data$.id).to.equal(data.id);
			expect(data$.username).to.equal(data.attributes.username);

			rowId = data$.id;
			return done();
		})
	});

	it('Delete data by id', function (done) {
		const data = {
			model: testTable,
			id: rowId,
		}
		app.act('x_db:delete_by_id', data, function (err, result = {}) {
			const { data$ } = result;

			expect(err).to.be.not.exist;
			expect(data$).to.be.an('number');
			expect(data$).to.equal(rowId);

			rowId = data$.id;
			return done();
		})
	});

	after(function afterTestXDb(done) {
		db.model(`kryptstorm_${testTable}`).drop().then(() => done()).catch(err => done(err));
	});
});<commit_msg>Add more test for x_db<commit_after>/** External modules */
import Seneca from 'seneca';
import Sequelize from 'sequelize';
import { expect } from 'chai';
import Faker from 'faker';
import Bluebird from 'bluebird';

/** Internal modules */
import XDb from '.';

const testTable = 'test_x_db';

const models = [
	{
		name: testTable,
		schema: {
			username: {
				type: Sequelize.STRING(128),
				unique: true,
				validate: {
					is: /^[a-zA-Z0-9._]+$/i,
					len: [3, 128]
				}
			},
			email: {
				type: Sequelize.STRING(255),
				unique: true,
				validate: {
					isEmail: true,
				}
			},
			first_name: {
				type: Sequelize.STRING(60),
				allowNull: true,
				defaultValue: 'Lorem',
				validate: {
					is: /^[a-zA-Z0-9._ ]+$/i,
					len: [0, 60]
				}
			},
			last_name: {
				type: Sequelize.STRING(60),
				allowNull: true,
				defaultValue: 'Ipsum',
				validate: {
					is: /^[a-zA-Z0-9._ ]+$/i,
					len: [0, 60]
				}
			},
		}
	}
]


describe('XDb', function () {
	const TestApp = fn => {
		return Seneca({
			log: 'test'
		})
			.test(fn)
			.use(XDb, { models, options: { logging: false } })
	}
	let act, db, app, row;

	before((done) => {
		app = TestApp(done);
		act = Bluebird.promisify(app.act, { context: app });

		app.ready(function () {
			db = this.XDb$;
			return done();
		})
	})

	it('Insert data', function (done) {
		const payload$ = {
			model: testTable,
			attributes: {
				username: Faker.internet.userName(),
				email: Faker.internet.email(),
				first_name: Faker.name.findName(),
				last_name: Faker.name.lastName(),
			},
			returnFields: ['id', 'username', 'email', 'first_name', 'last_name']
		}
		act('x_db:create', payload$)
			.then(({ errorCode$ = 'ERROR_NONE', data$ }) => {

				expect(errorCode$).to.be.equal('ERROR_NONE');

				expect(data$).to.be.an('object');
				expect(data$.username).to.be.exist;
				expect(data$.username).to.be.equal(payload$.attributes.username);

				row = data$;
				return done();
			})
			.catch(err => done(err));
	});

	it('Find all data', function (done) {
		const payload$ = {
			model: testTable,
			returnFields: ['id', 'username', 'email', 'first_name', 'last_name']
		}
		act('x_db:find_all', payload$)
			.then(({ errorCode$ = 'ERROR_NONE', data$, _meta$ }) => {

				expect(errorCode$).to.be.equal('ERROR_NONE');

				expect(data$).to.be.an('array');
				expect(_meta$).to.be.exist;
				expect(_meta$.count).to.be.exist;
				expect(_meta$.count).to.be.an('number');
				return done();
			})
			.catch(err => done(err));
	});

	it('Find data by id', function (done) {
		const payload$ = {
			model: testTable,
			id: row.id,
			returnFields: ['id', 'username', 'email', 'first_name', 'last_name']
		}

		act('x_db:find_by_id', payload$)
			.then(({ errorCode$ = 'ERROR_NONE', data$, _meta$ }) => {

				expect(errorCode$).to.be.equal('ERROR_NONE');

				expect(data$).to.be.an('object');
				expect(data$.id).to.be.equal(row.id);
				expect(_meta$).to.be.exist;
				expect(_meta$.count).to.be.exist;
				expect(_meta$.count).to.be.an('number');
				return done();
			})
			.catch(err => done(err));
	});

	it('Update data by id', function (done) {
		const payload$ = {
			model: testTable,
			id: row.id,
			attributes: {
				first_name: Faker.name.firstName(),
				last_name: Faker.name.lastName(),
			},
			returnFields: ['id', 'username', 'email', 'first_name', 'last_name']
		}
		act('x_db:update', payload$)
			.then(({ errorCode$ = 'ERROR_NONE', data$, _meta$ }) => {

				expect(errorCode$).to.be.equal('ERROR_NONE');

				expect(data$).to.be.an('object');
				expect(data$.id).to.be.equal(payload$.id);
				expect(data$.first_name).to.be.equal(payload$.attributes.first_name);
				expect(data$.last_name).to.be.equal(payload$.attributes.last_name);
				expect(_meta$).to.be.exist;
				expect(_meta$.count).to.be.exist;
				expect(_meta$.count).to.be.an('number');
				return done();
			})
			.catch(err => done(err));
	});

	it('Delete data by id', function (done) {
		const data = {
			model: testTable,
			id: row.id,
			returnFields: ['id', 'username', 'email', 'first_name', 'last_name']
		}
		act('x_db:delete_by_id', data)
			.then(({ errorCode$ = 'ERROR_NONE', data$, _meta$ }) => {

				expect(errorCode$).to.be.equal('ERROR_NONE');

				expect(data$).to.be.an('object');
				expect(data$.id).to.be.equal(row.id);
				expect(_meta$).to.be.exist;
				expect(_meta$.count).to.be.exist;
				expect(_meta$.count).to.be.an('number');
				return done();
			})
			.catch(err => done(err));
	});

	it('Validate unique', function (done) {
		const data = {
			model: testTable,
			field: 'username',
			value: row.username
		}

		act('x_db:validate, scenario:unique', data)
			.then(({ errorCode$ = 'ERROR_NONE' }) => {

				expect(errorCode$).to.be.equal('ERROR_VALIDATION_FAILED');
				return done();
			})
			.catch(err => done(err));
	});

	after(function afterTestXDb(done) {
		db.model(`kryptstorm_${testTable}`).drop().then(() => done()).catch(err => done(err));
	});
});
{'fraction_non_alphanumeric': 0.2064118945289813, 'fraction_numerical': 0.0063886630270647, 'mean_word_length': 3.115678776290631, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '7373906', 'n_tokens_mistral': 3884, 'n_tokens_neox': 3218, 'n_words': 588}