<commit_before>// tslint:disable prefer-const max-classes-per-file no-unused-variable no-unnecessary-class
import {
  SQL_MEMORY_DB_SHARED,
  SQL_OPEN_DEFAULT,
  SQL_OPEN_READWRITE,
  SqlConnectionPool,
  SqlDatabase,
  SqlDatabaseSettings
} from '../../';

describe('test SqlConnectionPool', () => {
  // ---------------------------------------------

  it('expect pool to be able to open a database using default settings', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open(SQL_MEMORY_DB_SHARED);
      expect(pool.isOpen()).toBeTruthy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      expect(sqldb1).toBeDefined();
      expect(sqldb1.isOpen()).toBeTruthy();

      await sqldb1.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

    } catch (err) {
      fail(err);
    }
    done();

  });

  it('expect pool to be able to open a database using min < 1', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open(SQL_MEMORY_DB_SHARED, SQL_OPEN_DEFAULT, 0);
      expect(pool.isOpen()).toBeTruthy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      expect(sqldb1).toBeDefined();
      expect(sqldb1.isOpen()).toBeTruthy();

      await sqldb1.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

    } catch (err) {
      fail(err);
    }
    done();

  });


  it('expect pool to be able to open a (file-)database', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open('testsqlite3.db', SQL_OPEN_DEFAULT, 1, 2);
      expect(pool.isOpen()).toBeTruthy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      expect(sqldb1).toBeDefined();
      expect(sqldb1.isOpen()).toBeTruthy();

      // getting second connection
      let sqldb2 = await pool.get(100);
      expect(sqldb2).toBeDefined();
      expect(sqldb2.isOpen()).toBeTruthy();

      // getting third connect should fail
      let sqldb3: SqlDatabase|undefined;
      try {
        sqldb3 = await pool.get(100);
        fail('got 3 connection from pool with max 2 connections');
      } catch (ignore) {
      }
      expect(sqldb3).toBeUndefined();

      // first connection should work
      let ver1 = await sqldb1.getUserVersion();

      ver1 += 3;
      await sqldb1.setUserVersion(ver1);

      // second connection should work
      let ver2 = await sqldb2.getUserVersion();
      expect(ver2).toBe(ver1, 'got wrong user version from connection 2');

      // closing one connection
      await sqldb2.close();
      expect(sqldb2.isOpen()).toBeFalsy('closed connection 2 is open');

      // getting third connect should succeed
      sqldb3 = await pool.get(100);
      expect(sqldb3).toBeDefined();
      expect(sqldb3.isOpen()).toBeTruthy();
      expect(sqldb3 !== sqldb2).toBeTruthy('got same connection instance from pool');

      // third connection should work
      let ver3 = await sqldb3.getUserVersion();
      expect(ver3).toBe(ver1, 'got wrong user version from connection 3');

      await sqldb1.close();
      await sqldb2.close();
      await sqldb3.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

    } catch (err) {
      fail(err);
    }
    done();

  });


  it('expect pool to be closed after failed attempt to open a database', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open('::/.', SQL_OPEN_READWRITE);
      expect(pool.isOpen()).toBeFalsy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      fail('got invalid connection');
    } catch (err) {
    }
    done();

  });


  it('expect getting connection after pool has closed to fail', async (done) => {
    let pool = new SqlConnectionPool();
    try {
      await pool.open(SQL_MEMORY_DB_SHARED, SQL_OPEN_DEFAULT, 0);<commit_msg>test: shared file and memory database pool<commit_after>// tslint:disable prefer-const max-classes-per-file no-unused-variable no-unnecessary-class
import {
  SQL_MEMORY_DB_SHARED,
  SQL_OPEN_DEFAULT,
  SQL_OPEN_READWRITE,
  SqlConnectionPool,
  SqlDatabase,
  SqlDatabaseSettings
} from '../../';

describe('test SqlConnectionPool', () => {
  // ---------------------------------------------

  it('expect pool to be able to open a database using default settings', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open(SQL_MEMORY_DB_SHARED);
      expect(pool.isOpen()).toBeTruthy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      expect(sqldb1).toBeDefined();
      expect(sqldb1.isOpen()).toBeTruthy();

      await sqldb1.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

    } catch (err) {
      fail(err);
    }
    done();

  });

  it('expect pool to be able to open a database using min < 1', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open(SQL_MEMORY_DB_SHARED, SQL_OPEN_DEFAULT, 0);
      expect(pool.isOpen()).toBeTruthy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      expect(sqldb1).toBeDefined();
      expect(sqldb1.isOpen()).toBeTruthy();

      await sqldb1.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

    } catch (err) {
      fail(err);
    }
    done();

  });


  it('expect pool to share a file-database', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      const fileName = 'testsqlite3.db';
      await pool.open(fileName, SQL_OPEN_DEFAULT, 2, 2);
      expect(pool.isOpen()).toBeTruthy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      expect(sqldb1).toBeDefined();
      expect(sqldb1.isOpen()).toBeTruthy();

      // getting second connection
      let sqldb2 = await pool.get(100);
      expect(sqldb2).toBeDefined();
      expect(sqldb2.isOpen()).toBeTruthy();

      // getting third connect should fail
      let sqldb3: SqlDatabase|undefined;
      try {
        sqldb3 = await pool.get(100);
        fail('got 3 connection from pool with max 2 connections');
      } catch (ignore) {
      }
      expect(sqldb3).toBeUndefined();

      // first connection should work
      const ver0 = await sqldb1.getUserVersion();

      const ver1 = ver0 + 3;
      await sqldb1.setUserVersion(ver1);

      // second connection should work
      const ver2 = await sqldb2.getUserVersion();
      expect(ver2).toBe(ver1, 'got wrong user version from connection 2');

      // closing one connection
      await sqldb2.close();
      expect(sqldb2.isOpen()).toBeFalsy('closed connection 2 is open');

      // getting third connect should succeed
      sqldb3 = await pool.get(100);
      expect(sqldb3).toBeDefined();
      expect(sqldb3.isOpen()).toBeTruthy();
      expect(sqldb3 !== sqldb2).toBeTruthy('got same connection instance from pool');

      // third connection should work
      const ver3 = await sqldb3.getUserVersion();
      expect(ver3).toBe(ver1, 'got wrong user version from connection 3');

      await sqldb1.close();
      await sqldb2.close();
      await sqldb3.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

      await pool.open(fileName, SQL_OPEN_DEFAULT, 2, 2);
      expect(pool.isOpen()).toBeTruthy();

      sqldb1 = await pool.get(100);

      const ver4 = await sqldb1.getUserVersion();
      expect(ver4).toBe(ver3, 'user version after reopening pool to file db');

      await sqldb1.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

    } catch (err) {
      fail(err);
    }
    done();

  });


  it('expect pool to share a memory-database', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open(SQL_MEMORY_DB_SHARED, SQL_OPEN_DEFAULT, 2, 2);
      expect(pool.isOpen()).toBeTruthy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      expect(sqldb1).toBeDefined();
      expect(sqldb1.isOpen()).toBeTruthy();

      // getting second connection
      let sqldb2 = await pool.get(100);
      expect(sqldb2).toBeDefined();
      expect(sqldb2.isOpen()).toBeTruthy();

      // getting third connect should fail
      let sqldb3: SqlDatabase|undefined;
      try {
        sqldb3 = await pool.get(100);
        fail('got 3 connection from pool with max 2 connections');
      } catch (ignore) {
      }
      expect(sqldb3).toBeUndefined();

      // first connection should work
      const ver0 = await sqldb1.getUserVersion();

      const ver1 = ver0 + 3;
      await sqldb1.setUserVersion(ver1);

      // second connection should work
      const ver2 = await sqldb2.getUserVersion();
      expect(ver2).toBe(ver1, 'got wrong user version from connection 2');

      // closing one connection
      await sqldb2.close();
      expect(sqldb2.isOpen()).toBeFalsy('closed connection 2 is open');

      // getting third connect should succeed
      sqldb3 = await pool.get(100);
      expect(sqldb3).toBeDefined();
      expect(sqldb3.isOpen()).toBeTruthy();
      expect(sqldb3 !== sqldb2).toBeTruthy('got same connection instance from pool');

      // third connection should work
      const ver3 = await sqldb3.getUserVersion();
      expect(ver3).toBe(ver1, 'got wrong user version from connection 3');

      await sqldb1.close();
      await sqldb2.close();
      await sqldb3.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

      await pool.open(SQL_MEMORY_DB_SHARED, SQL_OPEN_DEFAULT, 2, 2);
      expect(pool.isOpen()).toBeTruthy();

      sqldb1 = await pool.get(100);

      const ver4 = await sqldb1.getUserVersion();
      expect(ver4).toBe(ver1, 'user version after reopening pool connected to memory db');

      await sqldb1.close();
      await pool.close();
      expect(pool.isOpen()).toBeFalsy();

    } catch (err) {
      fail(err);
    }
    done();

  });


  it('expect pool to be closed after failed attempt to open a database', async (done) => {
    try {
      let pool = new SqlConnectionPool();
      await pool.open('::/.', SQL_OPEN_READWRITE);
      expect(pool.isOpen()).toBeFalsy();

      // getting first connection
      let sqldb1 = await pool.get(100);
      fail('got invalid connection');
    } catch (err) {
    }
    done();

  });


  it('expect getting connection after pool has closed to fail', async (done) => {
    let pool = new SqlConnectionPool();
    try {
      await pool.open(SQL_MEMORY_DB_SHARED, SQL_OPEN_DEFAULT, 0);
{'fraction_non_alphanumeric': 0.13925004710759376, 'fraction_numerical': 0.021104201997361974, 'mean_word_length': 2.649020281883809, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, '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': '4223968', 'n_tokens_mistral': 3439, 'n_tokens_neox': 3328, 'n_words': 813}