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

sinon = require('sinon')
passportStub = require 'passport-stub'

Indicator = require('../../models/indicator').model
IndicatorData = require('../../models/indicator_data').model

suite('Indicatoration')

test("calling /admin/updateIndicatorData/{indicatorId}
  returns updated indicatorData", (done) ->
  indicator = new Indicator(
    type: "standard"
    indicatorDefinition: {
      fields: []
    }
    indicatorationConfig: {
      source: "esri"
      esriConfig: {
        serverUrl: "http://nrtstest.ead.ae/ka/rest/services"
        serviceName: "NRT_AD_AirQuality"
        featureServer: 1
      }
      range: [
        {minValue: 0.35, message: "Excellent"},
        {minValue: 0.15, message: "Good"},
        {minValue: 0, message: "Bad"}
      ]
    }
  )

  esriResponse = {
    "features": [
      {
        "geometry": {
          "x": 55.342883000000029,
          "y": 24.466660000000047
        },
        "attributes": {
          "periodStart": 1364774400000,
          "value": 0.29999999999999999,
          "station": "Sweihan"
        }
      }
    ]
  }

  requestGetStub = sinon.stub(request, 'get', (options, cb) ->
    cb(null, {body: JSON.stringify(esriResponse)})
  )

  Q.nsend(
    indicator, 'save'
  ).spread( (savedIndicator) ->
    indicator = savedIndicator
    Q.nfcall(
      request.post, {
        url: helpers.appurl("/admin/updateIndicatorData/#{indicator.id}")
      }
    )
  ).spread((res, body) ->
    Q.nsend(
      IndicatorData.findOne(indicator: indicator.id),
      'exec'
    ).then((indicatorData) ->
      try
        assert.equal res.statusCode, 201,
          "Expected response to succeed, but got error:
          #{body}"

        indicatorDataIdMatch = "\"_id\":\"#{indicatorData.id}\""
        indicatorIdMatch = "\"indicator\":\"#{indicator.id}\""
        dataMatch = "\"data\":\\[null\\]"
        assert.match body, new RegExp(".*Successfully updated indicator.*")
        assert.match body, new RegExp(".*#{indicatorDataIdMatch}.*")
        assert.match body, new RegExp(".*#{indicatorIdMatch}.*")
        assert.match body, new RegExp(".*#{dataMatch}.*")

        done()
      catch e
        done(e)
      finally
        requestGetStub.restore()
    )
  ).fail((err)->
    requestGetStub.restore()
    return done(err)
  )
)


test("calling /admin/updateIndicatorData/{indicatorId}
  correctly queries an indicator with a world bank source", (done) ->
  indicator = new Indicator(
    type: "standard"
    shortName: "Forest Area"
    indicatorDefinition: {
      fields: []
    }
    indicatorationConfig: {
      source: "worldBank"
      worldBankConfig: {
        countryCode: 'MU'
        indicatorCode: 'AG.LND.FRST.ZS'
      }
      range: [
        {minValue: 0.35, message: "Excellent"},
        {minValue: 0.15, message: "Good"},
        {minValue: 0, message: "Bad"}
      ]
    }
  )

  worldBankResponse = {
  }

  requestGetStub = sinon.stub(request, 'get', (options, cb) ->
    cb(null, {body: JSON.stringify(worldBankResponse)})
  )

  Q.nsend(
    indicator, 'save'
  ).spread( (savedIndicator) ->
    indicator = savedIndicator
    Q.nfcall(
      request.post, {
        url: helpers.appurl("/admin/updateIndicatorData/#{indicator.id}")
      }
    )
  ).spread((res, body) ->
    Q.nsend(
      IndicatorData.findOne(indicator: indicator.id),
      'exec'
    ).then((indicatorData) ->
      try
        assert.equal res.statusCode, 201,
          "Expected response to succeed, but got error:
          #{body}"

        indicatorDataIdMatch = "\"_id\":\"#{indicatorData.id}\""
        indicatorIdMatch = "\"indicator\":\"#{indicator.id}\""
        dataMatch = "\"data\":\\[null\\]"
        assert.match body, new RegExp(".*Successfully updated indicator.*")
        assert.match body, new RegExp(".*#{indicatorDataIdMatch}.*")
        assert.match body, new RegExp(".*#{indicatorIdMatch}.*")
        assert.match body, new RegExp(".*#{dataMatch}.*")

        done()
      catch e
        done(e)
      finally
        requestGetStub.restore()
    )
  ).fail((err)->
    requestGetStub.restore()
    return done(err)
  )
)
<commit_msg>Add world bank response data to world bank test<commit_after>assert = require('chai').assert
helpers = require '../helpers'
request = require('request')
url = require('url')
Q = require('q')

sinon = require('sinon')
passportStub = require 'passport-stub'

Indicator = require('../../models/indicator').model
IndicatorData = require('../../models/indicator_data').model

suite('Indicatoration')

test("calling /admin/updateIndicatorData/{indicatorId}
  returns updated indicatorData", (done) ->
  indicator = new Indicator(
    type: "standard"
    indicatorDefinition: {
      fields: []
    }
    indicatorationConfig: {
      source: "esri"
      esriConfig: {
        serverUrl: "http://nrtstest.ead.ae/ka/rest/services"
        serviceName: "NRT_AD_AirQuality"
        featureServer: 1
      }
      range: [
        {minValue: 0.35, message: "Excellent"},
        {minValue: 0.15, message: "Good"},
        {minValue: 0, message: "Bad"}
      ]
    }
  )

  esriResponse = {
    "features": [
      {
        "geometry": {
          "x": 55.342883000000029,
          "y": 24.466660000000047
        },
        "attributes": {
          "periodStart": 1364774400000,
          "value": 0.29999999999999999,
          "station": "Sweihan"
        }
      }
    ]
  }

  requestGetStub = sinon.stub(request, 'get', (options, cb) ->
    cb(null, {body: JSON.stringify(esriResponse)})
  )

  Q.nsend(
    indicator, 'save'
  ).spread( (savedIndicator) ->
    indicator = savedIndicator
    Q.nfcall(
      request.post, {
        url: helpers.appurl("/admin/updateIndicatorData/#{indicator.id}")
      }
    )
  ).spread((res, body) ->
    Q.nsend(
      IndicatorData.findOne(indicator: indicator.id),
      'exec'
    ).then((indicatorData) ->
      try
        assert.equal res.statusCode, 201,
          "Expected response to succeed, but got error:
          #{body}"

        indicatorDataIdMatch = "\"_id\":\"#{indicatorData.id}\""
        indicatorIdMatch = "\"indicator\":\"#{indicator.id}\""
        dataMatch = "\"data\":\\[null\\]"
        assert.match body, new RegExp(".*Successfully updated indicator.*")
        assert.match body, new RegExp(".*#{indicatorDataIdMatch}.*")
        assert.match body, new RegExp(".*#{indicatorIdMatch}.*")
        assert.match body, new RegExp(".*#{dataMatch}.*")

        done()
      catch e
        done(e)
      finally
        requestGetStub.restore()
    )
  ).fail((err)->
    requestGetStub.restore()
    return done(err)
  )
)


test("calling /admin/updateIndicatorData/{indicatorId}
  correctly queries an indicator with a world bank source", (done) ->
  indicator = new Indicator(
    type: "standard"
    shortName: "Forest Area"
    indicatorDefinition: {
      fields: []
    }
    indicatorationConfig: {
      source: "worldBank"
      worldBankConfig: {
        countryCode: 'MU'
        indicatorCode: 'AG.LND.FRST.ZS'
      }
      range: [
        {minValue: 0.35, message: "Excellent"},
        {minValue: 0.15, message: "Good"},
        {minValue: 0, message: "Bad"}
      ]
    }
  )

  worldBankResponse = [
    {},
    [
      {
        "indicator": {
          "id": "AG.LND.FRST.ZS",
          "value": "Forest area (% of land area)"
        },
        "country": {
          "id": "MU",
          "value": "Mauritius"
        },
        "value": "17.2512315270936",
        "decimal": "1",
        "date": "1969"
      }
    ]
  ]

  requestGetStub = sinon.stub(request, 'get', (options, cb) ->
    cb(null, {body: JSON.stringify(worldBankResponse)})
  )

  Q.nsend(
    indicator, 'save'
  ).spread( (savedIndicator) ->
    indicator = savedIndicator
    Q.nfcall(
      request.post, {
        url: helpers.appurl("/admin/updateIndicatorData/#{indicator.id}")
      }
    )
  ).spread((res, body) ->
    Q.nsend(
      IndicatorData.findOne(indicator: indicator.id),
      'exec'
    ).then((indicatorData) ->
      try
        assert.equal res.statusCode, 201,
          "Expected response to succeed, but got error:
          #{body}"

        indicatorDataIdMatch = "\"_id\":\"#{indicatorData.id}\""
        indicatorIdMatch = "\"indicator\":\"#{indicator.id}\""
        dataMatch = "\"data\":\\[null\\]"
        assert.match body, new RegExp(".*Successfully updated indicator.*")
        assert.match body, new RegExp(".*#{indicatorDataIdMatch}.*")
        assert.match body, new RegExp(".*#{indicatorIdMatch}.*")
        assert.match body, new RegExp(".*#{dataMatch}.*")

        done()
      catch e
        done(e)
      finally
        requestGetStub.restore()
    )
  ).fail((err)->
    requestGetStub.restore()
    return done(err)
  )
)

{'fraction_non_alphanumeric': 0.16080458478480728, 'fraction_numerical': 0.021575457916619845, 'mean_word_length': 2.322135125046659, 'pattern_counts': {'":': 37, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '6193082', 'n_tokens_mistral': 2987, 'n_tokens_neox': 2900, 'n_words': 546}