<commit_before>/**
 * Copyright (c) 2002-2018 "Neo4j,"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import neo4j from '../../src/v1';
import sharedNeo4j from '../internal/shared-neo4j';

/**
 * The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
 * Formatting of code here is important. Lines are rendered in manual and language guide web page as is.
 *
 * DO NOT add tests to this file that are not for that exact purpose.
 * DO NOT modify these tests without ensuring they remain consistent with the equivalent examples in other drivers
 */
describe('examples', () => {

  const neo4jV1 = neo4j;

  let driverGlobal;
  let console;
  let originalTimeout;

  let testResultPromise;
  let resolveTestResultPromise;

  const user = sharedNeo4j.username;
  const password = <PASSWORD>;
  const uri = 'bolt://localhost:7687';

  beforeAll(() => {
    originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;

    driverGlobal = neo4j.driver(uri, sharedNeo4j.authToken);
  });

  beforeEach(done => {

    testResultPromise = new Promise((resolve, reject) => {
      resolveTestResultPromise = resolve;
    });

    // Override console.log, to assert on stdout output
    console = {log: resolveTestResultPromise};

    const session = driverGlobal.session();
    session.run('MATCH (n) DETACH DELETE n').then(() => {
      session.close(() => {
        done();
      });
    });
  });

  afterAll(() => {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    driverGlobal.close();
  });

  it('autocommit transaction example', done => {
    const driver = driverGlobal;

    // tag::autocommit-transaction[]
    function addPerson(name) {
      const session = driver.session();
      return session.run('CREATE (a:Person {name: $name})', {name: name}).then(result => {
        session.close();
        return result;
      });
    }

    // end::autocommit-transaction[]

    addPerson('Alice').then(() => {
      const session = driver.session();
      session.run('MATCH (a:Person {name: $name}) RETURN count(a) AS result', {name: 'Alice'}).then(result => {
        session.close(() => {
          expect(result.records[0].get('result').toInt()).toEqual(1);
          done();
        });
      });
    });
  });

  it('basic auth example', done => {
    // tag::basic-auth[]
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
    // end::basic-auth[]

    driver.onCompleted = () => {
      driver.close();
      done();
    };
  });

  it('config connection pool example', done => {
    // tag::config-connection-pool[]
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
      {
        maxConnectionLifetime: 30*60*60,
        maxConnectionPoolSize: 50,
        connectionAcquisitionTimeout: 2*60
      }
    );
    // end::config-connection-pool[]

    driver.onCompleted = () => {
      driver.close();
      done();
    };
  });

  it('config load balancing example', done => {
    // tag::config-load-balancing-strategy[]
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
      {
        loadBalancingStrategy: "least_connected"
      }
    );
    // end::config-load-balancing-strategy[]

    driver.onCompleted = () => {
      driver.close();
      done();
    };
  });

  it('config max retry time example', done => {
    // tag::config-max-retry-time[]
    const maxRetryTimeMs = 15 * 1000; // 15 seconds
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
      {
        maxTransactionRetryTime: maxRetryTimeMs<commit_msg>Update timeouts in config example<commit_after>/**
 * Copyright (c) 2002-2018 "Neo4j,"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import neo4j from '../../src/v1';
import sharedNeo4j from '../internal/shared-neo4j';

/**
 * The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
 * Formatting of code here is important. Lines are rendered in manual and language guide web page as is.
 *
 * DO NOT add tests to this file that are not for that exact purpose.
 * DO NOT modify these tests without ensuring they remain consistent with the equivalent examples in other drivers
 */
describe('examples', () => {

  const neo4jV1 = neo4j;

  let driverGlobal;
  let console;
  let originalTimeout;

  let testResultPromise;
  let resolveTestResultPromise;

  const user = sharedNeo4j.username;
  const password = <PASSWORD>;
  const uri = 'bolt://localhost:7687';

  beforeAll(() => {
    originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;

    driverGlobal = neo4j.driver(uri, sharedNeo4j.authToken);
  });

  beforeEach(done => {

    testResultPromise = new Promise((resolve, reject) => {
      resolveTestResultPromise = resolve;
    });

    // Override console.log, to assert on stdout output
    console = {log: resolveTestResultPromise};

    const session = driverGlobal.session();
    session.run('MATCH (n) DETACH DELETE n').then(() => {
      session.close(() => {
        done();
      });
    });
  });

  afterAll(() => {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    driverGlobal.close();
  });

  it('autocommit transaction example', done => {
    const driver = driverGlobal;

    // tag::autocommit-transaction[]
    function addPerson(name) {
      const session = driver.session();
      return session.run('CREATE (a:Person {name: $name})', {name: name}).then(result => {
        session.close();
        return result;
      });
    }

    // end::autocommit-transaction[]

    addPerson('Alice').then(() => {
      const session = driver.session();
      session.run('MATCH (a:Person {name: $name}) RETURN count(a) AS result', {name: 'Alice'}).then(result => {
        session.close(() => {
          expect(result.records[0].get('result').toInt()).toEqual(1);
          done();
        });
      });
    });
  });

  it('basic auth example', done => {
    // tag::basic-auth[]
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
    // end::basic-auth[]

    driver.onCompleted = () => {
      driver.close();
      done();
    };
  });

  it('config connection pool example', done => {
    // tag::config-connection-pool[]
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
      {
        maxConnectionLifetime: 3 * 60 * 60 * 1000, // 3 hours
        maxConnectionPoolSize: 50,
        connectionAcquisitionTimeout: 2 * 60 * 1000 // 120 seconds
      }
    );
    // end::config-connection-pool[]

    driver.onCompleted = () => {
      driver.close();
      done();
    };
  });

  it('config load balancing example', done => {
    // tag::config-load-balancing-strategy[]
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
      {
        loadBalancingStrategy: "least_connected"
      }
    );
    // end::config-load-balancing-strategy[]

    driver.onCompleted = () => {
      driver.close();
      done();
    };
  });

  it('config max retry time example', done => {
    // tag::config-max-retry-time[]
    const maxRetryTimeMs = 15 * 1000; // 15 seconds
    const driver = neo4j.driver(uri, neo4j.auth.basic(user, password),
      {
        maxTransactionRetryTime: maxRetryTimeMs
{'fraction_non_alphanumeric': 0.1367662690445258, 'fraction_numerical': 0.016416676508798866, 'mean_word_length': 2.9294663573085846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1518844', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2453, 'n_words': 698}