<commit_before>/* global before describe it beforeEach after */
/* eslint-disable global-require */
/**
 * Main test script to run tests
 * */
process.env.NODE_ENV = 'test';
const async = require('async');
const nock = require('nock');
const assert = require('assert');
const supertest = require('supertest');
const pg = require('pg');
const fs = require('fs');
const cassandraDriver = require('cassandra-driver');
const config = require('../config');
const redis = require('../store/redis');
// const utility = require('../util/utility');
const detailsApi = require('./data/details_api.json');
const summariesApi = require('./data/summaries_api.json');
const historyApi = require('./data/history_api.json');
const heroesApi = require('./data/heroes_api.json');
const leaguesApi = require('./data/leagues_api.json');
const retrieverPlayer = require('./data/retriever_player.json');
const detailsApiPro = require('./data/details_api_pro.json');

const initPostgresHost = `postgres://postgres:postgres@${config.INIT_POSTGRES_HOST}/postgres`;
const initCassandraHost = config.INIT_CASSANDRA_HOST;
const pool = new pg.Pool({
  connectionString: initPostgresHost,
});
// these are loaded later, as the database needs to be created when these are required
let db;
let cassandra;
let app;
let queries;
let buildMatch;
// fake api responses
nock('http://api.steampowered.com')
  // fake 500 error
  .get('/IDOTA2Match_570/GetMatchDetails/V001/')
  .query(true)
  .reply(500, {})
  // fake match details
  .get('/IDOTA2Match_570/GetMatchDetails/V001/')
  .query(true)
  .times(10)
  .reply(200, detailsApi)
  // fake player summaries
  .get('/ISteamUser/GetPlayerSummaries/v0002/')
  .query(true)
  .reply(200, summariesApi)
  // fake full history
  .get('/IDOTA2Match_570/GetMatchHistory/V001/')
  .query(true)
  .reply(200, historyApi)
  // fake heroes list
  .get('/IEconDOTA2_570/GetHeroes/v0001/')
  .query(true)
  .reply(200, heroesApi)
  // fake leagues
  .get('/IDOTA2Match_570/GetLeagueListing/v0001/')
  .query(true)
  .reply(200, leaguesApi);
// fake mmr response
nock(`http://${config.RETRIEVER_HOST}`)
  .get('/?account_id=88367253')
  .reply(200, retrieverPlayer);
before(function setup(done) {
  this.timeout(60000);
  async.series([
    function initPostgres(cb) {
      pool.connect((err, client) => {
        if (err) {
          return cb(err);
        }
        return async.series([
          function drop(cb) {
            console.log('drop postgres test database');
            client.query('DROP DATABASE IF EXISTS yasp_test', cb);
          },
          function create(cb) {
            console.log('create postgres test database');
            client.query('CREATE DATABASE yasp_test', cb);
          },
          function tables(cb) {
            db = require('../store/db');
            console.log('connecting to test database and creating tables');
            const query = fs.readFileSync('./sql/create_tables.sql', 'utf8');
            db.raw(query).asCallback(cb);
          },
          function setup(cb) {
            // populate the DB with this leagueid so we insert a pro match
            db.raw('INSERT INTO leagues(leagueid, tier) VALUES(5399, \'professional\')').asCallback(cb);
          },
        ], cb);
      });
    },
    function initCassandra(cb) {
      const client = new cassandraDriver.Client({
        contactPoints: [initCassandraHost],
      });
      async.series([function drop(cb) {
        console.log('drop cassandra test keyspace');
        client.execute('DROP KEYSPACE IF EXISTS yasp_test', cb);
      },
      function create(cb) {
        console.log('create cassandra test keyspace');
        client.execute('CREATE KEYSPACE yasp_test WITH REPLICATION = { \'class\': \'NetworkTopologyStrategy\', \'datacenter1\': 1 };', cb);
      },
      function tables(cb) {<commit_msg>use pg to create tables<commit_after>/* global before describe it beforeEach after */
/* eslint-disable global-require */
/**
 * Main test script to run tests
 * */
process.env.NODE_ENV = 'test';
const async = require('async');
const nock = require('nock');
const assert = require('assert');
const supertest = require('supertest');
const pg = require('pg');
const fs = require('fs');
const cassandraDriver = require('cassandra-driver');
const config = require('../config');
const redis = require('../store/redis');
// const utility = require('../util/utility');
const detailsApi = require('./data/details_api.json');
const summariesApi = require('./data/summaries_api.json');
const historyApi = require('./data/history_api.json');
const heroesApi = require('./data/heroes_api.json');
const leaguesApi = require('./data/leagues_api.json');
const retrieverPlayer = require('./data/retriever_player.json');
const detailsApiPro = require('./data/details_api_pro.json');

const initPostgresHost = `postgres://postgres:postgres@${config.INIT_POSTGRES_HOST}/postgres`;
const initCassandraHost = config.INIT_CASSANDRA_HOST;
const pool = new pg.Pool({
  connectionString: initPostgresHost,
});
// these are loaded later, as the database needs to be created when these are required
let db;
let cassandra;
let app;
let queries;
let buildMatch;
// fake api responses
nock('http://api.steampowered.com')
  // fake 500 error
  .get('/IDOTA2Match_570/GetMatchDetails/V001/')
  .query(true)
  .reply(500, {})
  // fake match details
  .get('/IDOTA2Match_570/GetMatchDetails/V001/')
  .query(true)
  .times(10)
  .reply(200, detailsApi)
  // fake player summaries
  .get('/ISteamUser/GetPlayerSummaries/v0002/')
  .query(true)
  .reply(200, summariesApi)
  // fake full history
  .get('/IDOTA2Match_570/GetMatchHistory/V001/')
  .query(true)
  .reply(200, historyApi)
  // fake heroes list
  .get('/IEconDOTA2_570/GetHeroes/v0001/')
  .query(true)
  .reply(200, heroesApi)
  // fake leagues
  .get('/IDOTA2Match_570/GetLeagueListing/v0001/')
  .query(true)
  .reply(200, leaguesApi);
// fake mmr response
nock(`http://${config.RETRIEVER_HOST}`)
  .get('/?account_id=88367253')
  .reply(200, retrieverPlayer);
before(function setup(done) {
  this.timeout(60000);
  async.series([
    function initPostgres(cb) {
      pool.connect((err, client) => {
        if (err) {
          return cb(err);
        }
        return async.series([
          function drop(cb) {
            console.log('drop postgres test database');
            client.query('DROP DATABASE IF EXISTS yasp_test', cb);
          },
          function create(cb) {
            console.log('create postgres test database');
            client.query('CREATE DATABASE yasp_test', cb);
          },
          function tables(cb) {
            console.log('create postgres test tables');
            const query = fs.readFileSync('./sql/create_tables.sql', 'utf8');
            client.query(query, cb);
          },
          function setup(cb) {
            db = require('../store/db');
            console.log('insert postgres test data');
            // populate the DB with this leagueid so we insert a pro match
            db.raw('INSERT INTO leagues(leagueid, tier) VALUES(5399, \'professional\')').asCallback(cb);
          },
        ], cb);
      });
    },
    function initCassandra(cb) {
      const client = new cassandraDriver.Client({
        contactPoints: [initCassandraHost],
      });
      async.series([function drop(cb) {
        console.log('drop cassandra test keyspace');
        client.execute('DROP KEYSPACE IF EXISTS yasp_test', cb);
      },
      function create(cb) {
        console.log('create cassandra test keyspace');
        client.execute('CREATE KEYSPACE yasp_test WITH REPLICATION = { \'class\': \'NetworkTopologyStrategy\', \'datacenter1\': 1 };', cb);
      },
      function tables(cb) {
{'fraction_non_alphanumeric': 0.14961752884740048, 'fraction_numerical': 0.022559315441462467, 'mean_word_length': 3.1674770394381415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6089212', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2356, 'n_words': 542}