<commit_before>'use strict';

var assert  = require('assert');
var pick    = require('object.pick');
var extend  = require('extend');
var watson  = require('../lib/index');
var nock    = require('nock');
var fs      = require('fs');

describe('document_conversion', function() {

  var noop = function() {};

  // Test params
  var service_options = {
    username: 'batman',
    password: '<PASSWORD>',
    url: 'http://ibm.com:80',
    version: 'v1-experimental'
  };
  var convertPath = '/v1/convert_document';

  var payload = {
    conversion_target: 'ANSWER_UNITS',
    word: {
      heading: {
        fonts: [
          { level: 1, min_size: 24 },
          { level: 2, min_size: 16, max_size: 24 }
        ]
      }
    },
    file: fs.createReadStream(__dirname + '/resources/sampleWORD.docx'),
  };

  before(function() {
    nock.disableNetConnect();
  });

  after(function() {
    nock.cleanAll();
  });

  var servInstance = watson.document_conversion(service_options);

  var missingParameter = function(err) {
    assert.ok((err instanceof Error) && /required parameters/.test(err));
  };

  describe('convert()', function() {

    it('should check no parameters provided', function() {
      servInstance.convert({}, missingParameter);
      servInstance.convert(null, missingParameter);
      servInstance.convert(undefined, missingParameter);
      servInstance.convert(pick(payload,['file']), missingParameter);
      servInstance.convert(pick(payload,['conversion_target']), missingParameter);
    });

    it('should validate conversion_target', function() {
      servInstance.convert({
        file: payload.file,
        conversion_target: 'foo'
      }, missingParameter);
    });

    it('should validate file as Stream', function() {
      servInstance.convert({
        file: 'not a file',
        conversion_target: payload.conversion_target
      }, missingParameter);
    });

    it('should generate a valid payload', function() {
      var req = servInstance.convert(payload, noop);
      assert(req.uri.href.startsWith(service_options.url + convertPath));
      assert.equal(req.method, 'POST');
      assert(req.formData);
    });

    it('should send extra config params', function() {
      var req = servInstance.convert(payload, noop);
      var config = JSON.parse(req.formData.config.value);
      assert(config.word.heading.fonts);
    });

    it('should send the version query param', function() {
      var req = servInstance.convert(payload, noop);
      assert(req.uri.query);
      assert(req.uri.query.indexOf("version=") > -1)
    });

    it('should allow the version query param to be overridden', function() {
      var custServInstance = watson.document_conversion(extend(service_options, { version_date: "2015-11-30"}));
      var req = custServInstance.convert(payload, noop);
      assert(req.uri.query.indexOf("version=2015-11-30" > -1));
    });
  });
});
<commit_msg>[DCS] Fixes a failing test for Node.js 0.12<commit_after>'use strict';

var assert  = require('assert');
var pick    = require('object.pick');
var extend  = require('extend');
var watson  = require('../lib/index');
var nock    = require('nock');
var fs      = require('fs');

describe('document_conversion', function() {

  var noop = function() {};

  // Test params
  var service_options = {
    username: 'batman',
    password: '<PASSWORD>',
    url: 'http://ibm.com:80',
    version: 'v1-experimental'
  };
  var convertPath = '/v1/convert_document';

  var payload = {
    conversion_target: 'ANSWER_UNITS',
    word: {
      heading: {
        fonts: [
          { level: 1, min_size: 24 },
          { level: 2, min_size: 16, max_size: 24 }
        ]
      }
    },
    file: fs.createReadStream(__dirname + '/resources/sampleWORD.docx'),
  };

  before(function() {
    nock.disableNetConnect();
  });

  after(function() {
    nock.cleanAll();
  });

  var servInstance = watson.document_conversion(service_options);

  var missingParameter = function(err) {
    assert.ok((err instanceof Error) && /required parameters/.test(err));
  };

  describe('convert()', function() {

    it('should check no parameters provided', function() {
      servInstance.convert({}, missingParameter);
      servInstance.convert(null, missingParameter);
      servInstance.convert(undefined, missingParameter);
      servInstance.convert(pick(payload,['file']), missingParameter);
      servInstance.convert(pick(payload,['conversion_target']), missingParameter);
    });

    it('should validate conversion_target', function() {
      servInstance.convert({
        file: payload.file,
        conversion_target: 'foo'
      }, missingParameter);
    });

    it('should validate file as Stream', function() {
      servInstance.convert({
        file: 'not a file',
        conversion_target: payload.conversion_target
      }, missingParameter);
    });

    it('should generate a valid payload', function() {
      var req = servInstance.convert(payload, noop);
      var url = service_options.url + convertPath;
      assert.equal(req.uri.href.slice(0, url.length), url);
      assert.equal(req.method, 'POST');
      assert(req.formData);
    });

    it('should send extra config params', function() {
      var req = servInstance.convert(payload, noop);
      var config = JSON.parse(req.formData.config.value);
      assert(config.word.heading.fonts);
    });

    it('should send the version query param', function() {
      var req = servInstance.convert(payload, noop);
      assert(req.uri.query);
      assert(req.uri.query.indexOf("version=") > -1)
    });

    it('should allow the version query param to be overridden', function() {
      var custServInstance = watson.document_conversion(extend(service_options, { version_date: "2015-11-30"}));
      var req = custServInstance.convert(payload, noop);
      assert(req.uri.query.indexOf("version=2015-11-30" > -1));
    });
  });
});

{'fraction_non_alphanumeric': 0.1508860759493671, 'fraction_numerical': 0.01080168776371308, 'mean_word_length': 3.2328571428571427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1555463', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1715, 'n_words': 374}