<commit_before># -*- coding: utf-8 -*-


import json

from nose.tools import assert_equal, assert_in, assert_not_in
from webob import Request

from . import common


def setup_module(module):
    common.get_or_load_app()


def test_simulate_without_body():
    req = Request.blank('/api/1/simulate', headers = (('Content-Type', 'application/json'),), method = 'POST')
    res = req.get_response(common.app)
    assert_equal(res.status_code, 400)


def test_simulate_with_invalid_body():
    req = Request.blank(
        '/api/1/simulate',
        body = 'XXX',
        headers = (('Content-Type', 'application/json'),),
        method = 'POST',
        )
    res = req.get_response(common.app)
    assert_equal(res.status_code, 400)


def test_simulate_with_test_case():
    test_case = {
        'scenarios': [
            {
                'test_case': {
                    'familles': [
                        {
                            'parents': ['ind0', 'ind1'],
                            },
                        ],
                    'foyers_fiscaux': [
                        {
                            'declarants': ['ind0', 'ind1'],
                            },
                        ],
                    'individus': [
                        {'id': 'ind0', 'salaire_imposable': 15000},
                        {'id': 'ind1'},
                        ],
                    'menages': [
                        {
                            'conjoint': 'ind1',
                            'personne_de_reference': 'ind0',
                            },
                        ],
                    },
                'period': '2013',
                },
            ],
        }
    req = Request.blank(
        '/api/1/simulate',
        body = json.dumps(test_case),
        headers = (('Content-Type', 'application/json'),),
        method = 'POST',
        )
    res = req.get_response(common.app)
    assert_equal(res.status_code, 200, res.body)
    res_body_json = json.loads(res.body)
    assert_not_in('error', res_body_json)
    assert_in('value', res_body_json)


def test_simulate_with_wrong_input_variable_period():
    test_case = {
        'scenarios': [
            {
                'test_case': {
                    'familles': [
                        {
                            'parents': ['ind0', 'ind1'],
                            },
                        ],
                    'foyers_fiscaux': [
                        {
                            'declarants': ['ind0', 'ind1'],
                            # rfr is defined for a year, its value can't be set for a month
                            'rfr': {"2013-01": 15000},
                            },
                        ],
                    'individus': [
                        {'id': 'ind0'},
                        {'id': 'ind1'},
                        ],
                    'menages': [
                        {
                            'conjoint': 'ind1',
                            'personne_de_reference': 'ind0',
                            },
                        ],
                    },
                'period': '2013',
                },
            ],
        }
    req = Request.blank(
        '/api/1/simulate',
        body = json.dumps(test_case),
        headers = (('Content-Type', 'application/json'),),
        method = 'POST',
        )
    res = req.get_response(common.app)
    assert_equal(res.status_code, 400, res.body)
    res_body_json = json.loads(res.body)
    assert_in(u'ValueError: Unable to set a value for variable', res_body_json['error']['message'], res.body)<commit_msg>Add test<commit_after># -*- coding: utf-8 -*-


import json

from nose.tools import assert_equal, assert_in, assert_not_in
from webob import Request

from . import common


def setup_module(module):
    common.get_or_load_app()


def test_simulate_without_body():
    req = Request.blank('/api/1/simulate', headers = (('Content-Type', 'application/json'),), method = 'POST')
    res = req.get_response(common.app)
    assert_equal(res.status_code, 400)


def test_simulate_with_invalid_body():
    req = Request.blank(
        '/api/1/simulate',
        body = 'XXX',
        headers = (('Content-Type', 'application/json'),),
        method = 'POST',
        )
    res = req.get_response(common.app)
    assert_equal(res.status_code, 400)


def test_simulate_with_test_case():
    test_case = {
        'scenarios': [
            {
                'test_case': {
                    'familles': [
                        {
                            'parents': ['ind0', 'ind1'],
                            },
                        ],
                    'foyers_fiscaux': [
                        {
                            'declarants': ['ind0', 'ind1'],
                            },
                        ],
                    'individus': [
                        {'id': 'ind0', 'salaire_imposable': 15000},
                        {'id': 'ind1'},
                        ],
                    'menages': [
                        {
                            'conjoint': 'ind1',
                            'personne_de_reference': 'ind0',
                            },
                        ],
                    },
                'period': '2013',
                },
            ],
        }
    req = Request.blank(
        '/api/1/simulate',
        body = json.dumps(test_case),
        headers = (('Content-Type', 'application/json'),),
        method = 'POST',
        )
    res = req.get_response(common.app)
    assert_equal(res.status_code, 200, res.body)
    res_body_json = json.loads(res.body)
    assert_not_in('error', res_body_json)
    assert_in('value', res_body_json)


def test_simulate_with_wrong_input_variable_period():
    test_case = {
        'scenarios': [
            {
                'test_case': {
                    'familles': [
                        {
                            'parents': ['ind0', 'ind1'],
                            },
                        ],
                    'foyers_fiscaux': [
                        {
                            'declarants': ['ind0', 'ind1'],
                            # rfr is defined for a year, its value can't be set for a month
                            'rfr': {"2013-01": 15000},
                            },
                        ],
                    'individus': [
                        {'id': 'ind0'},
                        {'id': 'ind1'},
                        ],
                    'menages': [
                        {
                            'conjoint': 'ind1',
                            'personne_de_reference': 'ind0',
                            },
                        ],
                    },
                'period': '2013',
                },
            ],
        }
    req = Request.blank(
        '/api/1/simulate',
        body = json.dumps(test_case),
        headers = (('Content-Type', 'application/json'),),
        method = 'POST',
        )
    res = req.get_response(common.app)
    assert_equal(res.status_code, 400, res.body)
    res_body_json = json.loads(res.body)
    assert_in(u'ValueError: Unable to set a value for variable', res_body_json['error']['message'], res.body)


def test_simulate_with_wrong_period():
    test_case = {
        'scenarios': [
            {
                'test_case': {
                    'familles': [
                        {
                            'parents': ['ind0'],
                            },
                        ],
                    'foyers_fiscaux': [
                        {
                            'declarants': ['ind0'],
                            },
                        ],
                    'individus': [
                        {'id': 'ind0'},
                        ],
                    'menages': [
                        {
                            'personne_de_reference': 'ind0',
                            },
                        ],
                    },
                'period': '2015-01',
                },
            ],
        }
    req = Request.blank(
        '/api/1/simulate',
        body = json.dumps(test_case),
        headers = (('Content-Type', 'application/json'),),
        method = 'POST',
        )
    res = req.get_response(common.app)
    assert_equal(res.status_code, 400, res.body)
    res_body_json = json.loads(res.body)
    assert_in(u'ValueError: Unable to compute variable', res_body_json['error']['message'], res.body)
{'fraction_non_alphanumeric': 0.12683609232921428, 'fraction_numerical': 0.014921893215201678, 'mean_word_length': 1.0271739130434783, 'pattern_counts': {'":': 2, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '919285', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2247, 'n_words': 377}