<commit_before>var expect = require('expect');
var _ = require('lodash');
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var subject = require('../../../backend/lib/openfisca/test');
var tmp = require('tmp');

var details = {
    name: 'Ideal name',
    description: 'Thorough description',
    output: {
        rsa: 545.48,
    },
    absolute_error_margin: 0.1,
};

var currentPeriod = '2018-01';
var situation = {
    dateDeValeur: new Date(currentPeriod),
    famille: {},
    foyer_fiscal: {},
    individus: [{
        id: 'id',
        date_naissance: new Date('1989-01-01'),
        role: 'demandeur',
        specificSituations: []
    }],
    menage: {
        personne_de_reference: ['id'],
        statut_occupation_logement: 'sans_domicile'
    },
};

describe('openfisca generateTest', function() {
    var result = subject.generateTest(details, situation);

    it('does not add rsa_non_calculable', function() {
        expect(typeof result.input.familles._.rsa_non_calculable[currentPeriod]).toBe('undefined');
    });
});


function run_cmd(cmd, args) {
    return new Promise(function(resolve, reject) {
        var spawn = require('child_process').spawn;
        var child = spawn(cmd, args);
        var respErr = "";
        var respOut = "";

        child.stdout.on('data', function (buffer) { respOut += buffer.toString(); });
        child.stderr.on('data', function (buffer) { respErr += buffer.toString(); });
        child.on('exit', function(code) {
            var result = {
                stdout: respOut,
                stderr: respErr
            };
            if (code) {
                result.error = 'Exit code was ' + code;
                reject(result);
            } else {
                resolve(result);
            }
        });
        child.on('error', function(err) { reject({ error: err }); });
    });
}

function runOpenFiscaTest(yaml, extension) {
    var tmpobj = tmp.fileSync();
    return fs.writeFileAsync(tmpobj.fd, yaml, 'utf8')
        .then(function() {
            var args = extension ? [tmpobj.name, '--extensions', extension] : [tmpobj.name];

            return run_cmd('openfisca-run-test', args);
        });
}

describe('openfisca generateYAMLTest', function() {
    var result = subject.generateYAMLTest(details, situation);

    it('generates a non empty string', function() {
        expect(result).toBeTruthy();
    });

    it('contains provided output', function() {
        expect(result).toContain('rsa: 545.48');
    });

    function validateYAMLRun(payload, extension) {
        return runOpenFiscaTest(payload, extension)
            .catch(function(failure) {
                console.log(payload);
                expect(failure).toBeFalsy();
            })
            .then(function(result) {
                expect(result.stderr).toMatch(/\nOK\n$/);
            });
    }

    if (process.env.VIRTUAL_ENV) {
        describe('generates processable YAML files', function() {
            it('passes OpenFisca test without extension', function() {
                var info = Object.assign({}, details);
                var yamlContent = subject.generateYAMLTest(info, situation);

                return validateYAMLRun(yamlContent);
            });

            Object.keys(subject.EXTENSION_VARIABLES).forEach(function(extensionName) {
                it('passes OpenFisca test with ' + extensionName  + ' extension', function() {
                    var info = Object.assign({ extension: extensionName }, details);
                    var yamlContent = subject.generateYAMLTest(info, situation);

                    var variableListRegex = _.values(subject.EXTENSION_VARIABLES[extensionName]).map(function(variableList) { return variableList.join('|'); }).join('|');
                    expect(yamlContent).toMatch(new RegExp(variableListRegex));

                    return validateYAMLRun(yamlContent, extensionName.replace(/-/g, '_'));<commit_msg>Adapte les tests YAML OpenFisca à pytest<commit_after>var expect = require('expect');
var _ = require('lodash');
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var subject = require('../../../backend/lib/openfisca/test');
var tmp = require('tmp');

var details = {
    name: 'Ideal name',
    description: 'Thorough description',
    output: {
        rsa: 545.48,
    },
    absolute_error_margin: 0.1,
};

var currentPeriod = '2018-01';
var situation = {
    dateDeValeur: new Date(currentPeriod),
    famille: {},
    foyer_fiscal: {},
    individus: [{
        id: 'id',
        date_naissance: new Date('1989-01-01'),
        role: 'demandeur',
        specificSituations: []
    }],
    menage: {
        personne_de_reference: ['id'],
        statut_occupation_logement: 'sans_domicile'
    },
};

describe('openfisca generateTest', function() {
    var result = subject.generateTest(details, situation);

    it('does not add rsa_non_calculable', function() {
        expect(typeof result.input.familles._.rsa_non_calculable[currentPeriod]).toBe('undefined');
    });
});


function run_cmd(cmd, args) {
    return new Promise(function(resolve, reject) {
        var spawn = require('child_process').spawn;
        var child = spawn(cmd, args);
        var respErr = "";
        var respOut = "";

        child.stdout.on('data', function (buffer) { respOut += buffer.toString(); });
        child.stderr.on('data', function (buffer) { respErr += buffer.toString(); });
        child.on('exit', function(code) {
            var result = {
                stdout: respOut,
                stderr: respErr
            };
            if (code) {
                result.error = 'Exit code was ' + code;
                reject(result);
            } else {
                resolve(result);
            }
        });
        child.on('error', function(err) { reject({ error: err }); });
    });
}

function runOpenFiscaTest(yaml, extension) {
    var tmpobj = tmp.fileSync({postfix: '.yaml'});
    return fs.writeFileAsync(tmpobj.fd, yaml, 'utf8')
        .then(function() {
            var args = extension ? [tmpobj.name, '--extensions', extension] : [tmpobj.name];

            return run_cmd('openfisca-run-test', args);
        });
}

describe('openfisca generateYAMLTest', function() {
    var result = subject.generateYAMLTest(details, situation);

    it('generates a non empty string', function() {
        expect(result).toBeTruthy();
    });

    it('contains provided output', function() {
        expect(result).toContain('rsa: 545.48');
    });

    function validateYAMLRun(payload, extension) {
        return runOpenFiscaTest(payload, extension)
            .catch(function(failure) {
                console.log(payload);
                expect(failure).toBeFalsy();
            })
            .then(function(result) {
                expect(result.stdout).toMatch(/passed in/);
                expect(result.stdout).not.toMatch(/= ERRORS =/);
            });
    }

    if (process.env.VIRTUAL_ENV) {
        describe('generates processable YAML files', function() {
            it('passes OpenFisca test without extension', function() {
                var info = Object.assign({}, details);
                var yamlContent = subject.generateYAMLTest(info, situation);

                return validateYAMLRun(yamlContent);
            });

            Object.keys(subject.EXTENSION_VARIABLES).forEach(function(extensionName) {
                it('passes OpenFisca test with ' + extensionName  + ' extension', function() {
                    var info = Object.assign({ extension: extensionName }, details);
                    var yamlContent = subject.generateYAMLTest(info, situation);

                    var variableListRegex = _.values(subject.EXTENSION_VARIABLES[extensionName]).map(function(variableList) { return variableList.join('|'); }).join('|');
                    expect(yamlContent).toMatch(new RegExp(variableListRegex));

                    return validateYAMLRun(yamlContent, extensionName.replace(/-/g, '_'));
{'fraction_non_alphanumeric': 0.14177623418506574, 'fraction_numerical': 0.006698089804018854, 'mean_word_length': 2.4078613693998308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3285927', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2256, 'n_words': 456}