示例#1
0
def test_get_memory_usage(tax_benefit_system):
    simulation = SimulationBuilder().build_from_entities(
        tax_benefit_system, single)
    simulation.calculate('disposable_income', '2017-01')
    memory_usage = simulation.get_memory_usage(variables=['salary'])
    assert (memory_usage['total_nb_bytes'] > 0)
    assert (len(memory_usage['by_variable']) == 1)
示例#2
0
def test_get_memory_usage(tax_benefit_system):
    test_case = deepcopy(situation_examples.single)
    test_case["persons"]["Alicia"]["salary"] = {"2017-01": 0}
    simulation = SimulationBuilder().build_from_dict(tax_benefit_system, test_case)
    simulation.calculate('disposable_income', '2017-01')
    memory_usage = simulation.person.get_memory_usage(variables = ['salary'])
    assert(memory_usage['total_nb_bytes'] > 0)
    assert(len(memory_usage['by_variable']) == 1)
示例#3
0
def test_simulation(tax_benefit_system):
    input_yaml = """
        salary:
            2016-10: 12000
    """

    simulation = SimulationBuilder().build_from_dict(
        tax_benefit_system, test_runner.yaml.safe_load(input_yaml))

    assert simulation.get_array("salary", "2016-10") == 12000
    simulation.calculate("income_tax", "2016-10")
    simulation.calculate("total_taxes", "2016-10")
示例#4
0
def test_vectorial_input(tax_benefit_system):
    input_yaml = """
        salary:
            2016-10: [12000, 20000]
    """

    simulation = SimulationBuilder().build_from_dict(
        tax_benefit_system, test_runner.yaml.safe_load(input_yaml))

    tools.assert_near(simulation.get_array("salary", "2016-10"),
                      [12000, 20000])
    simulation.calculate("income_tax", "2016-10")
    simulation.calculate("total_taxes", "2016-10")
示例#5
0
def test_dump(tax_benefit_system):
    directory = tempfile.mkdtemp(prefix = "openfisca_")
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, situation_examples.couple)
    calculated_value = simulation.calculate('disposable_income', '2018-01')
    simulation_dumper.dump_simulation(simulation, directory)

    simulation_2 = simulation_dumper.restore_simulation(directory, tax_benefit_system)

    # Check entities structure have been restored

    testing.assert_array_equal(simulation.person.ids, simulation_2.person.ids)
    testing.assert_array_equal(simulation.person.count, simulation_2.person.count)
    testing.assert_array_equal(simulation.household.ids, simulation_2.household.ids)
    testing.assert_array_equal(simulation.household.count, simulation_2.household.count)
    testing.assert_array_equal(simulation.household.members_position, simulation_2.household.members_position)
    testing.assert_array_equal(simulation.household.members_entity_id, simulation_2.household.members_entity_id)
    testing.assert_array_equal(simulation.household.members_role, simulation_2.household.members_role)

    # Check calculated values are in cache

    disposable_income_holder = simulation_2.person.get_holder('disposable_income')
    cached_value = disposable_income_holder.get_array('2018-01')
    assert cached_value is not None
    testing.assert_array_equal(cached_value, calculated_value)

    shutil.rmtree(directory)
示例#6
0
def test_calculate_full_tracer(tax_benefit_system):
    simulation = SimulationBuilder().build_default_simulation(
        tax_benefit_system)
    simulation.trace = True
    simulation.calculate('income_tax', '2017-01')

    income_tax_node = simulation.tracer.trees[0]
    assert income_tax_node.name == 'income_tax'
    assert str(income_tax_node.period) == '2017-01'
    assert income_tax_node.value == 0

    salary_node = income_tax_node.children[0]
    assert salary_node.name == 'salary'
    assert str(salary_node.period) == '2017-01'
    assert salary_node.parameters == []

    assert len(income_tax_node.parameters) == 1
    assert income_tax_node.parameters[0].name == 'taxes.income_tax_rate'
    assert income_tax_node.parameters[0].period == '2017-01-01'
    assert income_tax_node.parameters[0].value == 0.15
示例#7
0
def test_group_encapsulation():
    """Projects a calculation to all members of an entity.

    When a household contains more than one family
    Variables can be defined for the the household
    And calculations are projected to all the member families.

    """
    from openfisca_core.taxbenefitsystems import TaxBenefitSystem
    from openfisca_core.entities import build_entity
    from openfisca_core.periods import ETERNITY

    person_entity = build_entity(
        key="person",
        plural="people",
        label="A person",
        is_person=True,
    )
    family_entity = build_entity(
        key="family",
        plural="families",
        label="A family (all members in the same household)",
        containing_entities=["household"],
        roles=[{
            "key": "member",
            "plural": "members",
            "label": "Member",
        }])
    household_entity = build_entity(
        key="household",
        plural="households",
        label="A household, containing one or more families",
        roles=[{
            "key": "member",
            "plural": "members",
            "label": "Member",
        }])

    entities = [person_entity, family_entity, household_entity]

    system = TaxBenefitSystem(entities)

    class household_level_variable(Variable):
        value_type = int
        entity = household_entity
        definition_period = ETERNITY

    class projected_family_level_variable(Variable):
        value_type = int
        entity = family_entity
        definition_period = ETERNITY

        def formula(family, period):
            return family.household("household_level_variable", period)

    system.add_variables(household_level_variable,
                         projected_family_level_variable)

    simulation = SimulationBuilder().build_from_dict(
        system, {
            "people": {
                "person1": {},
                "person2": {},
                "person3": {}
            },
            "families": {
                "family1": {
                    "members": ["person1", "person2"]
                },
                "family2": {
                    "members": ["person3"]
                },
            },
            "households": {
                "household1": {
                    "members": ["person1", "person2", "person3"],
                    "household_level_variable": {
                        "eternity": 5
                    }
                }
            }
        })

    assert (simulation.calculate("projected_family_level_variable",
                                 "2021-01-01") == 5).all()