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

from collections import OrderedDict

from pytest import raises, fixture, approx

from openfisca_core.simulation_builder import SimulationBuilder, Simulation
from openfisca_core.tools import assert_near
from openfisca_core.tools.test_runner import yaml
from openfisca_core.entities import PersonEntity
from openfisca_core.variables import Variable
from openfisca_country_template.entities import Household
from openfisca_country_template.situation_examples import couple
from openfisca_core.errors import SituationParsingError
from openfisca_core.periods import ETERNITY
from openfisca_core.indexed_enums import Enum as OFEnum
from enum import Enum

from .test_countries import tax_benefit_system


@fixture
def simulation_builder():
    return SimulationBuilder()


@fixture
def enum_variable():

    class TestEnum(Variable):
        definition_period = ETERNITY
        value_type = OFEnum
        dtype = 'O'
        default_value = '0'
        is_neutralized = False
        set_input = None
        possible_values = Enum('foo', 'bar')
        name = "enum"

        def __init__(self):
            pass

    return TestEnum()


@fixture
def persons():

    class TestVariable(Variable):
        definition_period = ETERNITY
        value_type = float
        dtype = 'O'
        default_value = '0'
        is_neutralized = False
        set_input = None

        def __init__(self):
            pass

    class TestPersonEntity(PersonEntity):
        def __init__(self):
            super().__init__(None)
            self.plural = "persons"

        def get_variable(self, variable_name):
            result = TestVariable()
            result.name = variable_name
            return result

        def check_variable_defined_for_entity(self, variable_name):
            return True

    return TestPersonEntity()


def test_build_default_simulation(simulation_builder):
    one_person_simulation = simulation_builder.build_default_simulation(tax_benefit_system, 1)
    assert one_person_simulation.persons.count == 1
    assert one_person_simulation.household.count == 1
    assert one_person_simulation.household.members_entity_id == [0]
    assert one_person_simulation.household.members_role == Household.FIRST_PARENT

    several_persons_simulation = simulation_builder.build_default_simulation(tax_benefit_system, 4)
    assert several_persons_simulation.persons.count == 4
    assert several_persons_simulation.household.count == 4
    assert (several_persons_simulation.household.members_entity_id == [0, 1, 2, 3]).all()
    assert (several_persons_simulation.household.members_role == Household.FIRST_PARENT).all()


def test_explicit_singular_entities(simulation_builder):
    assert simulation_builder.explicit_singular_entities(
        tax_benefit_system,
        {'persons': {'Javier': {}}, 'household': {'parents': ['Javier']}}
        ) == {'persons': {'Javier': {}}, 'households': {'household': {'parents': ['Javier']}}}


def test_hydrate_person_entity(simulation_builder, persons):
    persons_json = OrderedDict([('Alicia', {'salary': {}}), ('Javier', {})])  # We need an OrderedDict in Python 2
    simulation_builder.hydrate_entity(persons, persons_json)
    assert persons.count == 2
    assert persons.ids == ['Alicia', 'Javier']


def test_hydrate_person_entity_with_variables(simulation_builder, persons):
    persons_json = OrderedDict([('Alicia', {'salary': {'2018-11': 3000}}), ('Javier', {})])  # We need an OrderedDict in Python 2
    simulation_builder.hydrate_entity(persons, persons_json)
    assert_near(persons.get_holder('salary').get_array('2018-11'), [3000, 0])


def test_set_entity_input_values(simulation_builder, persons):
    persons_json = OrderedDict([('Alicia', {'salary': {'2018-11': 3000}}), ('Javier', {})])  # We need an OrderedDict in Python 2
    simulation_builder.hydrate_entity(persons, persons_json)
    assert_near(simulation_builder.get_input('salary', '2018-11'), [3000, 0])


def test_add_variable_value(simulation_builder, persons):
    instance_index = 0
    persons.count = 1
    salary = persons.get_variable('salary')
    simulation_builder.add_variable_value(persons, salary, instance_index, 'Alicia', '2018-11', 3000)
    input_array = simulation_builder.get_input('salary', '2018-11')
    assert input_array[instance_index] == approx(3000)


def test_add_variable_value_as_expression(simulation_builder, persons):
    instance_index = 0
    persons.count = 1
    salary = persons.get_variable('salary')
    simulation_builder.add_variable_value(persons, salary, instance_index, 'Alicia', '2018-11', '3 * 1000')
    input_array = simulation_builder.get_input('salary', '2018-11')
    assert input_array[instance_index] == approx(3000)


def test_fail_on_ill_formed_expression(simulation_builder, persons):
    instance_index = 0
    persons.count = 1
    salary = persons.get_variable('salary')
    with raises(SituationParsingError) as excinfo:
        simulation_builder.add_variable_value(persons, salary, instance_index, 'Alicia', '2018-11', '2 * / 1000')
    assert excinfo.value.error == {'persons': {'Alicia': {'salary': {'2018-11': "I couldn't understand '2 * / 1000' as a value for 'salary'"}}}}


def test_add_unknown_enum_variable_value(simulation_builder, persons, enum_variable):
    instance_index = 0
    persons.count = 1
    with raises(SituationParsingError):<commit_msg>Add test for integer overflow<commit_after># -*- coding: utf-8 -*-

from collections import OrderedDict

from pytest import raises, fixture, approx

from openfisca_core.simulation_builder import SimulationBuilder, Simulation
from openfisca_core.tools import assert_near
from openfisca_core.tools.test_runner import yaml
from openfisca_core.entities import PersonEntity
from openfisca_core.variables import Variable
from openfisca_country_template.entities import Household
from openfisca_country_template.situation_examples import couple
from openfisca_core.errors import SituationParsingError
from openfisca_core.periods import ETERNITY
from openfisca_core.indexed_enums import Enum as OFEnum
from enum import Enum

from .test_countries import tax_benefit_system


@fixture
def simulation_builder():
    return SimulationBuilder()


@fixture
def int_variable(persons):

    class intvar(Variable):
        definition_period = ETERNITY
        value_type = int
        entity = persons.__class__

        def __init__(self):
            super().__init__()

    return intvar()


@fixture
def enum_variable():

    class TestEnum(Variable):
        definition_period = ETERNITY
        value_type = OFEnum
        dtype = 'O'
        default_value = '0'
        is_neutralized = False
        set_input = None
        possible_values = Enum('foo', 'bar')
        name = "enum"

        def __init__(self):
            pass

    return TestEnum()


@fixture
def persons():

    class TestVariable(Variable):
        definition_period = ETERNITY
        value_type = float
        dtype = 'O'
        default_value = '0'
        is_neutralized = False
        set_input = None

        def __init__(self):
            pass

    class TestPersonEntity(PersonEntity):
        def __init__(self):
            super().__init__(None)
            self.plural = "persons"

        def get_variable(self, variable_name):
            result = TestVariable()
            result.name = variable_name
            return result

        def check_variable_defined_for_entity(self, variable_name):
            return True

    return TestPersonEntity()


def test_build_default_simulation(simulation_builder):
    one_person_simulation = simulation_builder.build_default_simulation(tax_benefit_system, 1)
    assert one_person_simulation.persons.count == 1
    assert one_person_simulation.household.count == 1
    assert one_person_simulation.household.members_entity_id == [0]
    assert one_person_simulation.household.members_role == Household.FIRST_PARENT

    several_persons_simulation = simulation_builder.build_default_simulation(tax_benefit_system, 4)
    assert several_persons_simulation.persons.count == 4
    assert several_persons_simulation.household.count == 4
    assert (several_persons_simulation.household.members_entity_id == [0, 1, 2, 3]).all()
    assert (several_persons_simulation.household.members_role == Household.FIRST_PARENT).all()


def test_explicit_singular_entities(simulation_builder):
    assert simulation_builder.explicit_singular_entities(
        tax_benefit_system,
        {'persons': {'Javier': {}}, 'household': {'parents': ['Javier']}}
        ) == {'persons': {'Javier': {}}, 'households': {'household': {'parents': ['Javier']}}}


def test_hydrate_person_entity(simulation_builder, persons):
    persons_json = OrderedDict([('Alicia', {'salary': {}}), ('Javier', {})])  # We need an OrderedDict in Python 2
    simulation_builder.hydrate_entity(persons, persons_json)
    assert persons.count == 2
    assert persons.ids == ['Alicia', 'Javier']


def test_hydrate_person_entity_with_variables(simulation_builder, persons):
    persons_json = OrderedDict([('Alicia', {'salary': {'2018-11': 3000}}), ('Javier', {})])  # We need an OrderedDict in Python 2
    simulation_builder.hydrate_entity(persons, persons_json)
    assert_near(persons.get_holder('salary').get_array('2018-11'), [3000, 0])


def test_set_entity_input_values(simulation_builder, persons):
    persons_json = OrderedDict([('Alicia', {'salary': {'2018-11': 3000}}), ('Javier', {})])  # We need an OrderedDict in Python 2
    simulation_builder.hydrate_entity(persons, persons_json)
    assert_near(simulation_builder.get_input('salary', '2018-11'), [3000, 0])


def test_add_variable_value(simulation_builder, persons):
    instance_index = 0
    persons.count = 1
    salary = persons.get_variable('salary')
    simulation_builder.add_variable_value(persons, salary, instance_index, 'Alicia', '2018-11', 3000)
    input_array = simulation_builder.get_input('salary', '2018-11')
    assert input_array[instance_index] == approx(3000)


def test_add_variable_value_as_expression(simulation_builder, persons):
    instance_index = 0
    persons.count = 1
    salary = persons.get_variable('salary')
    simulation_builder.add_variable_value(persons, salary, instance_index, 'Alicia', '2018-11', '3 * 1000')
    input_array = simulation_builder.get_input('salary', '2018-11')
    assert input_array[instance_index] == approx(3000)


def test_fail_on_ill_formed_expression(simulation_builder, persons):
    instance_index = 0
    persons.count = 1
    salary = persons.get_variable('salary')
    with raises(SituationParsingError) as excinfo:
        simulation_builder.add_variable_value(persons, salary, instance_index, 'Alicia', '2018-11', '2 * / 1000')
    assert excinfo.value.error == {'persons': {'Alicia': {'salary': {'2018-11': "I couldn't understand '2 * / 1000' as a value for 'salary'"}}}}


def test_fail_on_integer_overflow(simulation_builder, persons, int_variable):
    instance_index = 0
    persons.count = 1
    with raises(SituationParsingError) as excinfo:
        simulation_builder.add_variable_value(persons, int_variable, instance_index, 'Alicia', '2018-11', 9223372036854775808)
    assert excinfo.value.error == {'persons': {'Alicia': {'intvar': {'2018-11': "Can't deal with value: '9223372036854775808', it's too large for type 'integer'."}}}}


def test_add_unknown_enum_variable_value(simulation_builder, persons, enum_variable):
    instance_index = 0
    persons.count = 1
    with raises(SituationParsingError):
{'fraction_non_alphanumeric': 0.10473880273758988, 'fraction_numerical': 0.027202633630771897, 'mean_word_length': 4.379310344827586, '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': '234258', 'n_tokens_mistral': 3806, 'n_tokens_neox': 3517, 'n_words': 744}