<commit_before>import * as should from 'should';
import * as _ from 'lodash';
import { Logger } from '../lib/logger';
import { Database } from 'arangojs';
import * as chassis from '../lib';
import { DatabaseProvider } from '../lib/database';
const config = chassis.config;
const database = chassis.database;

let db: DatabaseProvider;

/* global describe context it beforeEach */

const providers = [
  {
    name: 'arango',
    init: async function init(): Promise<DatabaseProvider> {
      await config.load(process.cwd() + '/test');
      const cfg = await config.get();
      const logger = new Logger(cfg.get('logger'));
      return database.get(cfg.get('database:arango'), logger);
    },
    drop: async function drop(): Promise<any> {
      await config.load(process.cwd() + '/test');
      const cfg = await config.get();

      const dbHost: string = cfg.get('database:arango:host');
      const dbPort: string = cfg.get('database:arango:port');
      const dbName: string = cfg.get('database:arango:database');

      const db = new Database('http://' + dbHost + ':' + dbPort);
      await db.dropDatabase(dbName);
    },
    custom: function customTests() {
      describe('testing custom queries', () => {
        it('should register a custom query', () => {
          const script = 'return "Hello World"';
          db.registerCustomQuery('helloWorld', script, 'query');
          const queries = db.listCustomQueries();
          should.exist(queries);
          queries.should.have.length(1);
          should.exist(queries[0][0]);
          queries[0][0].should.equal('helloWorld');

          should.exist(queries[0][1]);
          should.exist(queries[0][1].code);
          queries[0][1].code.should.equal(script);
          should.exist(queries[0][1].type);
          queries[0][1].type.should.equal('query');
        });
        it('should unregister a custom query', async () => {
          const script = 'return "Hello World";';
          db.registerCustomQuery('helloWorld', script, 'query');
          let functions = db.listCustomQueries();
          should.exist(functions);
          functions.should.have.length(1);

          db.unregisterCustomQuery('helloWorld');
          functions = db.listCustomQueries();
          should.exist(functions);
          functions.should.have.length(0);
        });
        it('should execute a custom query', async () => {
          const script = `return "Hello World"`;
          await db.registerCustomQuery('helloWorld', script, 'query');
          const result = await db.find('test', {}, {
            custom_query: 'helloWorld'
          });
          should.exist(result);
          result.should.have.length(1);
          result[0].should.equal('Hello World');
        });
        it('should execute a custom query with custom parameters', async () => {
          const script = `return @param`;
          await db.registerCustomQuery('helloWorld', script, 'query');
          const result = await db.find('test', {}, {
            custom_query: 'helloWorld',
            custom_arguments: {
              param: 'Hello World'
            }
          });
          should.exist(result);
          result.should.have.length(1);
          result[0].should.equal('Hello World');
        });
        it('should execute a custom query which accesses the database', async () => {
          const script = `for t in test return t`;
          await db.registerCustomQuery('script', script, 'query');
          const result = await db.find('test', {}, {
            custom_query: 'script'
          });
          should.exist(result);
          result.should.have.length(6);
        });
        it('should execute a custom filter within a `find` query', async () => {
          const script = `node.id == @param`;
          await db.registerCustomQuery('script', script, 'filter');
          const result = await db.find('test', {}, {
            custom_query: 'script',
            custom_arguments: {
              param: '/test/sort0'
            }
          });

          should.exist(result);
          result.should.have.length(1);

          should.exist(result[0].id);
          result[0].id.should.equal('/test/sort0');
          should.exist(result[0].include);
          result[0].include.should.equal(true);
          should.exist(result[0].value);
          result[0].value.should.equal('c');
        });
      });
    }
  },
  {
    name: 'nedb',
    init: async function init(): Promise<DatabaseProvider> {
      await config.load(process.cwd() + '/test');
      const cfg = await config.get();
      const logger = new Logger(cfg.get('logger'));
      return database.get(cfg.get('database:nedb'), logger);
    },
    drop: async function drop(): Promise<any> { },
    custom: () => { return () => { }; }
  }
];
providers.forEach((providerCfg) => {
  describe(`with database provider ${providerCfg.name}`, () => {
    testProvider(providerCfg);
  });
});

function testProvider(providerCfg) {
  const collection = 'test';<commit_msg>Up tests for applying custom filters<commit_after>import * as should from 'should';
import * as _ from 'lodash';
import { Logger } from '../lib/logger';
import { Database } from 'arangojs';
import * as chassis from '../lib';
import { DatabaseProvider } from '../lib/database';
const config = chassis.config;
const database = chassis.database;

let db: DatabaseProvider;

/* global describe context it beforeEach */

const providers = [
  {
    name: 'arango',
    init: async function init(): Promise<DatabaseProvider> {
      await config.load(process.cwd() + '/test');
      const cfg = await config.get();
      const logger = new Logger(cfg.get('logger'));
      return database.get(cfg.get('database:arango'), logger);
    },
    drop: async function drop(): Promise<any> {
      await config.load(process.cwd() + '/test');
      const cfg = await config.get();

      const dbHost: string = cfg.get('database:arango:host');
      const dbPort: string = cfg.get('database:arango:port');
      const dbName: string = cfg.get('database:arango:database');

      const db = new Database('http://' + dbHost + ':' + dbPort);
      await db.dropDatabase(dbName);
    },
    custom: function customTests() {
      describe('testing custom queries', () => {
        it('should register a custom query', () => {
          const script = 'return "Hello World"';
          db.registerCustomQuery('helloWorld', script, 'query');
          const queries = db.listCustomQueries();
          should.exist(queries);
          queries.should.have.length(1);
          should.exist(queries[0][0]);
          queries[0][0].should.equal('helloWorld');

          should.exist(queries[0][1]);
          should.exist(queries[0][1].code);
          queries[0][1].code.should.equal(script);
          should.exist(queries[0][1].type);
          queries[0][1].type.should.equal('query');
        });
        it('should unregister a custom query', async () => {
          const script = 'return "Hello World";';
          db.registerCustomQuery('helloWorld', script, 'query');
          let functions = db.listCustomQueries();
          should.exist(functions);
          functions.should.have.length(1);

          db.unregisterCustomQuery('helloWorld');
          functions = db.listCustomQueries();
          should.exist(functions);
          functions.should.have.length(0);
        });
        it('should execute a custom query', async () => {
          const script = `return "Hello World"`;
          await db.registerCustomQuery('helloWorld', script, 'query');
          const result = await db.find('test', {}, {
            custom_query: 'helloWorld'
          });
          should.exist(result);
          result.should.have.length(1);
          result[0].should.equal('Hello World');
        });
        it('should execute a custom query with custom parameters', async () => {
          const script = `return @param`;
          await db.registerCustomQuery('helloWorld', script, 'query');
          const result = await db.find('test', {}, {
            custom_query: 'helloWorld',
            custom_arguments: {
              param: 'Hello World'
            }
          });
          should.exist(result);
          result.should.have.length(1);
          result[0].should.equal('Hello World');
        });
        it('should execute a custom query which accesses the database', async () => {
          const script = `for t in test return t`;
          await db.registerCustomQuery('script', script, 'query');
          const result = await db.find('test', {}, {
            custom_query: 'script'
          });
          should.exist(result);
          result.should.have.length(6);
        });
        it('should apply a custom filter within a `find` query', async () => {
          const script = `node.id == @param`;
          await db.registerCustomQuery('script', script, 'filter');
          const result = await db.find('test', {}, {
            custom_query: 'script',
            custom_arguments: {
              param: '/test/sort0'
            }
          });

          should.exist(result);
          result.should.have.length(1);

          should.exist(result[0].id);
          result[0].id.should.equal('/test/sort0');
          should.exist(result[0].include);
          result[0].include.should.equal(true);
          should.exist(result[0].value);
          result[0].value.should.equal('c');
        });
        it('should combine a custom filter with normal filters', async () => {
          const script = `node.value != @param`;
          await db.registerCustomQuery('script', script, 'filter');
          const result = await db.find('test', {
            include: {
              $eq: true
            }
          }, {
              custom_query: 'script',
              custom_arguments: {
                param: 'a'
              }
            });

          should.exist(result);
          result.should.have.length(2);

          const sorted = _.sortBy(result, ['id']);

          should.exist(sorted[0].id);
          sorted[0].id.should.equal('/test/sort0');
          should.exist(sorted[0].include);
          sorted[0].include.should.equal(true);
          should.exist(sorted[0].value);
          sorted[0].value.should.equal('c');

          should.exist(sorted[1].id);
          sorted[1].id.should.equal('/test/sort4');
          should.exist(sorted[1].include);
          sorted[1].include.should.equal(true);
          should.exist(sorted[1].value);
          sorted[1].value.should.equal('b');
        });
      });
    }
  },
  {
    name: 'nedb',
    init: async function init(): Promise<DatabaseProvider> {
      await config.load(process.cwd() + '/test');
      const cfg = await config.get();
      const logger = new Logger(cfg.get('logger'));
      return database.get(cfg.get('database:nedb'), logger);
    },
    drop: async function drop(): Promise<any> { },
    custom: () => { return () => { }; }
  }
];
providers.forEach((providerCfg) => {
  describe(`with database provider ${providerCfg.name}`, () => {
    testProvider(providerCfg);
  });
});

function testProvider(providerCfg) {
  const collection = 'test';
{'fraction_non_alphanumeric': 0.1567751161959242, 'fraction_numerical': 0.0068823739721129784, 'mean_word_length': 2.3015638831513723, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 34, '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': '198138', 'n_tokens_mistral': 3073, 'n_tokens_neox': 3074, 'n_words': 671}