<commit_before>assert = require('chai').assert
helpers = require '../helpers'
request = require('request')
url = require('url')
_ = require('underscore')
Q = require 'q'
async = require('async')
sinon = require('sinon')

suite('API - Indicator')

Theme = require('../../models/theme').model
Indicator = require('../../models/indicator').model
IndicatorData = require('../../models/indicator_data').model
Page = require('../../models/page').model

test('POST create', (done) ->
  data =
    name: "<NAME>"

  request.post({
    url: helpers.appurl('api/indicators/')
    json: true
    body: data
  },(err, res, body) ->
    id = body._id

    assert.equal res.statusCode, 201

    Indicator
      .findOne(_id: id)
      .exec( (err, indicator) ->
        assert.equal indicator.name, data.name
        done()
      )
  )
)

test("GET show", (done) ->
  helpers.createIndicator( (err, indicator) ->
    request.get({
      url: helpers.appurl("api/indicators/#{indicator.id}")
      json: true
    }, (err, res, body) ->
      assert.equal res.statusCode, 200

      reloadedIndicator = body
      assert.equal reloadedIndicator._id, indicator.id
      assert.equal reloadedIndicator.content, indicator.content

      done()
    )
  )
)

test("GET /indicators/:id/fat returns the indicator with its nested page ", (done) ->
  indicator = null
  nestedPage = new Page()
  toObjectWithNestedPageStub = sinon.stub(Indicator::, 'toObjectWithNestedPage', ->
    Q.fcall(=>
      object = @toObject()
      object.page = nestedPage
      object
    )
  )

  Q.nfcall(
    helpers.createIndicator
  ).then( (createdIndicator) ->
    indicator = createdIndicator

    Q.nfcall(
      request.get, {
        url: helpers.appurl("api/indicators/#{indicator._id}/fat")
        json: true
      }
    )
  ).spread( (res, body) ->

    try
      assert.equal res.statusCode, 200,
        "Expected the query to succeed"

      assert.match res.headers['content-type'], /.*json.*/,
        "Expected the response to be JSON"

      reloadedIndicator = body
      assert.equal reloadedIndicator._id, indicator.id,
        "Expected the query to return the correct indicator"

      assert.property reloadedIndicator, 'page',
        "Expected the page attribute to be populated"

      assert.ok toObjectWithNestedPageStub.calledOnce,
        "Expected indicator.toObjectWithNestedPage to be called"

      assert.equal reloadedIndicator.page._id, nestedPage.id,
        "Expected the page attribute to be the right page"

      done()
    catch err
      done(err)
    finally
      toObjectWithNestedPageStub.restore()

  ).catch( (err) ->
    toObjectWithNestedPageStub.restore()
    done(err)
  )
)

test('GET /api/indicators/ returns all indicators', (done) ->
  async.series([helpers.createIndicator, helpers.createIndicator], (err, indicators) ->
    request.get({
      url: helpers.appurl("api/indicators")
      json: true
    }, (err, res, body) ->
      assert.equal res.statusCode, 200

      indicatorJson = body

      assert.equal indicatorJson.length, indicators.length
      jsonNames = _.map(indicatorJson, (indicator)->
        indicator.name
      )
      indicatorNames = _.map(indicators, (indicator)->
        indicator.name
      )

      assert.deepEqual jsonNames, indicatorNames
      done()
    )
  )
)

test('GET /api/indicators?withData=true only returns indicators with indicator data associated', (done) ->
  async.series([helpers.createIndicator, helpers.createIndicator], (err, indicators) ->
    indicatorWithData = indicators[0]

    helpers.createIndicatorData(
      indicator: indicatorWithData._id
    ).then( ->

      request.get({
        url: helpers.appurl("api/indicators")
        qs: withData: true
        json: true
      }, (err, res, body) ->
        assert.equal res.statusCode, 200

        indicatorJson = body

        try
          assert.lengthOf indicatorJson, 1, "Only the indicator with data is expected"

          assert.strictEqual indicatorJson[0]._id, indicatorWithData._id.toString(),
            "Expected the indicator with data to be returned"
          done()
        catch err
          done(err)
      )

    ).catch(done)
  )
)<commit_msg>use async parallel to speed up test<commit_after>assert = require('chai').assert
helpers = require '../helpers'
request = require('request')
url = require('url')
_ = require('underscore')
Q = require 'q'
async = require('async')
sinon = require('sinon')

suite('API - Indicator')

Theme = require('../../models/theme').model
Indicator = require('../../models/indicator').model
IndicatorData = require('../../models/indicator_data').model
Page = require('../../models/page').model

test('POST create', (done) ->
  data =
    name: "<NAME>"

  request.post({
    url: helpers.appurl('api/indicators/')
    json: true
    body: data
  },(err, res, body) ->
    id = body._id

    assert.equal res.statusCode, 201

    Indicator
      .findOne(_id: id)
      .exec( (err, indicator) ->
        assert.equal indicator.name, data.name
        done()
      )
  )
)

test("GET show", (done) ->
  helpers.createIndicator( (err, indicator) ->
    request.get({
      url: helpers.appurl("api/indicators/#{indicator.id}")
      json: true
    }, (err, res, body) ->
      assert.equal res.statusCode, 200

      reloadedIndicator = body
      assert.equal reloadedIndicator._id, indicator.id
      assert.equal reloadedIndicator.content, indicator.content

      done()
    )
  )
)

test("GET /indicators/:id/fat returns the indicator with its nested page ", (done) ->
  indicator = null
  nestedPage = new Page()
  toObjectWithNestedPageStub = sinon.stub(Indicator::, 'toObjectWithNestedPage', ->
    Q.fcall(=>
      object = @toObject()
      object.page = nestedPage
      object
    )
  )

  Q.nfcall(
    helpers.createIndicator
  ).then( (createdIndicator) ->
    indicator = createdIndicator

    Q.nfcall(
      request.get, {
        url: helpers.appurl("api/indicators/#{indicator._id}/fat")
        json: true
      }
    )
  ).spread( (res, body) ->

    try
      assert.equal res.statusCode, 200,
        "Expected the query to succeed"

      assert.match res.headers['content-type'], /.*json.*/,
        "Expected the response to be JSON"

      reloadedIndicator = body
      assert.equal reloadedIndicator._id, indicator.id,
        "Expected the query to return the correct indicator"

      assert.property reloadedIndicator, 'page',
        "Expected the page attribute to be populated"

      assert.ok toObjectWithNestedPageStub.calledOnce,
        "Expected indicator.toObjectWithNestedPage to be called"

      assert.equal reloadedIndicator.page._id, nestedPage.id,
        "Expected the page attribute to be the right page"

      done()
    catch err
      done(err)
    finally
      toObjectWithNestedPageStub.restore()

  ).catch( (err) ->
    toObjectWithNestedPageStub.restore()
    done(err)
  )
)

test('GET /api/indicators/ returns all indicators', (done) ->
  async.series([helpers.createIndicator, helpers.createIndicator], (err, indicators) ->
    request.get({
      url: helpers.appurl("api/indicators")
      json: true
    }, (err, res, body) ->
      assert.equal res.statusCode, 200

      indicatorJson = body

      assert.equal indicatorJson.length, indicators.length
      jsonNames = _.map(indicatorJson, (indicator)->
        indicator.name
      )
      indicatorNames = _.map(indicators, (indicator)->
        indicator.name
      )

      assert.deepEqual jsonNames, indicatorNames
      done()
    )
  )
)

test('GET /api/indicators?withData=true only returns indicators with indicator data associated', (done) ->
  async.parallel([helpers.createIndicator, helpers.createIndicator], (err, indicators) ->
    indicatorWithData = indicators[0]

    helpers.createIndicatorData(
      indicator: indicatorWithData._id
    ).then( ->

      request.get({
        url: helpers.appurl("api/indicators")
        qs: withData: true
        json: true
      }, (err, res, body) ->
        assert.equal res.statusCode, 200

        indicatorJson = body

        try
          assert.lengthOf indicatorJson, 1, "Only the indicator with data is expected"

          assert.strictEqual indicatorJson[0]._id, indicatorWithData._id.toString(),
            "Expected the indicator with data to be returned"
          done()
        catch err
          done(err)
      )

    ).catch(done)
  )
)
{'fraction_non_alphanumeric': 0.12728568036095939, 'fraction_numerical': 0.004274519116599383, 'mean_word_length': 2.6478995236032916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '6977206', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2533, 'n_words': 594}