<commit_before>"use strict";

var i18n = require('../src/i18n');
var assert = require('assert');

describe("internationalization", function() {
  var json = {
    name: {
      en: 'Chris',
      ru: 'Крис'
    }
  };

  it("converts translated documents to popolo", function() {
    assert.equal(i18n(json, 'en').name, 'Chris');
    assert.equal(i18n(json, 'ru').name, 'Крис');
  });

  it("uses the default language when there is no match", function() {
    assert.equal(i18n(json, 'es', 'en').name, 'Chris');
    assert.equal(i18n(json, 'es', 'ru').name, 'Крис');
  });

  it("returns an empty string when no languages match", function() {
    assert.equal(i18n(json, 'es', 'de').name, '');
  });
});
<commit_msg>Add tests for translations in the API<commit_after>"use strict";

var i18n = require('../src/i18n');
var assert = require('assert');
var fixtures = require('pow-mongodb-fixtures');
var defaults = require('./defaults');
var supertest = require('supertest');
var popitApp = require('../test-server-app');

describe("internationalization", function() {
  var json = {
    name: {
      en: 'Chris',
      ru: 'Крис'
    }
  };

  it("converts translated documents to popolo", function() {
    assert.equal(i18n(json, 'en').name, 'Chris');
    assert.equal(i18n(json, 'ru').name, 'Крис');
  });

  it("uses the default language when there is no match", function() {
    assert.equal(i18n(json, 'es', 'en').name, 'Chris');
    assert.equal(i18n(json, 'es', 'ru').name, 'Крис');
  });

  it("returns an empty string when no languages match", function() {
    assert.equal(i18n(json, 'es', 'de').name, '');
  });

  describe("translating documents in the API", function() {
    var fixture = fixtures.connect(defaults.databaseName);
    var request = supertest(popitApp);
    beforeEach(function(done) {
      fixture.clearAllAndLoad({
        persons: [
          { _id: 'fred-bloggs', name: { en: '<NAME>', ru: '<NAME>' } }
        ]
      }, done);
    });

    it("returns the document in the requested language", function(done) {
      request.get('/api/persons/fred-bloggs')
      .set('Accept-Language', 'en')
      .expect(200)
      .end(function(err, res) {
        assert.ifError(err);
        assert.equal(res.body.result.name, '<NAME>');
        done();
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.1953125, 'fraction_numerical': 0.013454861111111112, 'mean_word_length': 3.349056603773585, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '5122580', 'n_tokens_mistral': 798, 'n_tokens_neox': 762, 'n_words': 177}