{"size": 1551, "ext": "js", "max_stars_repo_path": "test/matching-indexes.js", "max_stars_repo_name": "Zangetsu101/hearth", "max_stars_repo_stars_event_min_datetime": "2017-01-09T15:48:16.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-28T20:51:59.000Z", "max_issues_repo_path": "test/matching-indexes.js", "max_issues_repo_name": "Zangetsu101/hearth", "max_issues_repo_issues_event_min_datetime": "2017-01-11T11:23:19.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-03T22:24:23.000Z", "max_forks_repo_path": "test/matching-indexes.js", "max_forks_repo_name": "SafeSurgerySA/hearth", "max_forks_repo_forks_event_min_datetime": "2018-01-15T18:09:54.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-21T08:09:04.000Z"}
{"max_stars_count": 27, "max_issues_count": 44, "max_forks_count": 14, "avg_line_length": 24.234375, "max_line_length": 91, "alphanum_fraction": 0.5944551902}
 /**
 * Copyright (c) 2017-present, Jembi Health Systems NPC.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

'use strict'

process.env.NODE_ENV = 'test'
require('../lib/init')

const tap = require('tap')
const async = require('neo-async')

const mongo = require('../lib/mongo')()
const matchingIndexes = require('../scripts/matching-indexes')(mongo)
const resourceConfig = require('../config/matching').resourceConfig

const matchingIndexesTest = (t, test) => {
  mongo.getDB((err, db) => {
    t.error(err)

    matchingIndexes.createMatchingIndexes((err) => {
      t.error(err)

      test(db, () => {
        db.dropDatabase((err) => {
          t.error(err)

          mongo.closeDB(() => {
            t.error(err)
            t.end()
          })
        })
      })
    })
  })
}

tap.test('should ensure indexes are created', (t) => {
  matchingIndexesTest(t, (db, done) => {
    const tasks = []
    Object.keys(resourceConfig).forEach((collection) => {
      const c = db.collection(collection)

      Object.keys(resourceConfig[collection].matchingProperties).forEach((path) => {
        tasks.push((callback) => c.indexExists(`${path}_1`, callback))
        tasks.push((callback) => c.indexExists(`_transforms.matching.${path}_1`, callback))
      })
    })

    async.parallel(tasks, (err, results) => {
      t.error(err)

      results.forEach((result) => {
        t.true(result)
      })
      done()
    })
  })
})
