def test_check(): for employee_type, test_parameters in test_case_by_employee_type.iteritems(): period = test_parameters.get("period", default_period) parent1 = dict( birth = datetime.date(periods.period(period).start.year - 40, 1, 1), ) parent1.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period = period, parent1 = parent1, ).new_simulation(debug = True) for variable, amounts in test_parameters['output_variables'].iteritems(): if isinstance(amounts, dict): for period_str, amount in amounts.iteritems(): output = simulation.calculate(variable, period = periods.period(period_str)) variable_message = "{} at {}".format(variable, period_str) yield assert_variable, variable_message, employee_type, amount, output else: output = simulation.calculate(variable) variable_message = variable amount = amounts yield assert_variable, variable_message, employee_type, amount, output
def test_decomposition(print_decomposition=False): simulation = tax_benefit_system.new_scenario().init_single_entity( period="2013-01", parent1=dict( effectif_entreprise=3000, exposition_accident=3, code_postal_entreprise="75001", ratio_alternants=.025, salaire_de_base={"2013": 12 * 3000}, taille_entreprise=3, categorie_salarie=0, ), menage=dict(zone_apl=1, ), ).new_simulation() xml_file_path = os.path.join(tax_benefit_system.DECOMP_DIR, "fiche_de_paie_decomposition.xml") decomposition_json = decompositions.get_decomposition_json( tax_benefit_system, xml_file_path=xml_file_path) simulations = [simulation] response = decompositions.calculate(simulations, decomposition_json) if print_decomposition: print unicode( json.dumps(response, encoding='utf-8', ensure_ascii=False, indent=2))
def check_sal(type_sal, year = 2014): period = periods.period("{}-01".format(year)) single_entity_kwargs = dict( axes = [dict(count = 101, max = 2000, min = 40, name = 'salaire_de_base')], # TODO: min = 0 period = period, parent1 = dict( birth = datetime.date(year - 40, 1, 1), type_sal = type_sal, ), ) simulation = tax_benefit_system.new_scenario().init_single_entity( **single_entity_kwargs ).new_simulation(debug = False) brut = simulation.get_holder('salaire_de_base').array smic_horaire = simulation.legislation_at(period.start).cotsoc.gen.smic_h_b smic_mensuel = smic_horaire * 35 * 52 / 12 brut = simulation.get_holder('salaire_de_base').array simulation.get_or_new_holder('contrat_de_travail').array = brut < smic_mensuel # temps plein ou temps partiel simulation.get_or_new_holder('heures_remunerees_volume').array = brut // smic_horaire # temps plein ou partiel imposable = simulation.calculate('salaire_imposable') inversion_reform = inversion_revenus.build_reform(tax_benefit_system) inverse_simulation = inversion_reform.new_scenario().init_single_entity( **single_entity_kwargs).new_simulation(debug = True) inverse_simulation.get_holder('salaire_de_base').delete_arrays() inverse_simulation.get_or_new_holder('salaire_imposable_pour_inversion').array = imposable.copy() inverse_simulation.get_or_new_holder('contrat_de_travail').array = brut < smic_mensuel # temps plein ou partiel inverse_simulation.get_or_new_holder('heures_remunerees_volume').array = ( (brut // smic_horaire) * (brut < smic_mensuel) ) new_brut = inverse_simulation.calculate('salaire_de_base') assert_near(new_brut, brut, absolute_error_margin = 1)
def test_decomposition(print_decomposition=False): from openfisca_core.decompositions import calculate, get_decomposition_json import json import os simulation = tax_benefit_system.new_scenario().init_single_entity( period="2013-01", parent1=dict( effectif_entreprise=3000, exposition_accident=3, localisation_entreprise="75001", ratio_alternants=.025, salaire_de_base={"2013": 12 * 3000}, taille_entreprise=3, type_sal=0, ), menage=dict(zone_apl=1, ), ).new_simulation(debug=True) xml_file_path = os.path.join(tax_benefit_system.DECOMP_DIR, "fiche_de_paie_decomposition.xml") decomposition_json = get_decomposition_json(xml_file_path, tax_benefit_system) response = calculate(simulation, decomposition_json) if print_decomposition: print unicode( json.dumps(response, encoding='utf-8', ensure_ascii=False, indent=2))
def test_check(): for employee_type, test_parameters in test_case_by_employee_type.iteritems( ): period = test_parameters.get("period", default_period) parent1 = dict(birth=datetime.date( periods.period(period).start.year - 40, 1, 1), ) parent1.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period=period, parent1=parent1, ).new_simulation(debug=True) for variable, amounts in test_parameters['output_variables'].iteritems( ): if isinstance(amounts, dict): for period_str, amount in amounts.iteritems(): output = simulation.calculate( variable, period=periods.period(period_str)) variable_message = "{} at {}".format(variable, period_str) yield assert_variable, variable_message, employee_type, amount, output else: output = simulation.calculate(variable) variable_message = variable amount = amounts yield assert_variable, variable_message, employee_type, amount, output
def net_plot(revenu, count = 11, max_revenu = 5000, min_revenu = 0): year = 2014 period = periods.period("{}-01".format(year)) if revenu == 'chomage': brut_name = 'chobrut' net_name = 'chonet' elif revenu == 'retraite': brut_name = 'rstbrut' net_name = 'rstnet' elif revenu == 'salaire': brut_name = 'salaire_de_base' net_name = 'salaire_net' else: return single_entity_kwargs = dict( axes = [[ dict(count = count, max = max_revenu, min = min_revenu, name = brut_name) ]], period = period, parent1 = dict( birth = datetime.date(year - 40, 1, 1), ), ) simulation = tax_benefit_system.new_scenario().init_single_entity( **single_entity_kwargs).new_simulation(debug = True) smic_horaire = simulation.legislation_at(period.start).cotsoc.gen.smic_h_b smic_mensuel = smic_horaire * 35 * 52 / 12 brut = simulation.get_holder(brut_name).array simulation.get_or_new_holder('contrat_de_travail').array = brut < smic_mensuel # temps plein ou temps partiel simulation.get_or_new_holder('heures_remunerees_volume').array = brut // smic_horaire # temps plein ou temps partiel net = simulation.calculate(net_name) inversion_reform = inversion_revenus.build_reform(tax_benefit_system) inverse_simulation = inversion_reform.new_scenario().init_single_entity( **single_entity_kwargs).new_simulation(debug = True) inverse_simulation.get_holder(brut_name).delete_arrays() inverse_simulation.get_or_new_holder(net_name).array = net.copy() inverse_simulation.get_or_new_holder('contrat_de_travail').array = brut < smic_mensuel # temps plein ou temps partiel inverse_simulation.get_or_new_holder('heures_remunerees_volume').array = ( (brut // smic_horaire) * (brut < smic_mensuel) ) print inverse_simulation.get_or_new_holder('contrat_de_travail').array print inverse_simulation.get_or_new_holder('heures_remunerees_volume').array new_brut = inverse_simulation.calculate(brut_name) pyplot.subplot(2, 1, 1) pyplot.plot(brut, net, 'ro', label = "direct") pyplot.plot(new_brut, net, 'db', label = "inversed") pyplot.legend() pyplot.subplot(2, 1, 2) pyplot.plot(brut, new_brut - brut, 'r-') pyplot.show() assert_near(new_brut, brut, absolute_error_margin = 1)
def test_cho(year = 2014): simulation = tax_benefit_system.new_scenario().init_single_entity( axes = [dict(count = 11, max = 24000, min = 0, name = 'chobrut')], period = year, parent1 = dict( birth = datetime.date(year - 40, 1, 1), ), ).new_simulation(debug = True) brut = simulation.get_holder('chobrut').array imposable = simulation.calculate('cho') inversion_reform = inversion_revenus.build_reform(tax_benefit_system) inverse_simulation = inversion_reform.new_scenario().init_single_entity( axes = [dict(count = 11, max = 24000, min = 0, name = 'chobrut')], period = year, parent1 = dict( birth = datetime.date(year - 40, 1, 1), ), ).new_simulation(debug = True) inverse_simulation.get_holder('chobrut').delete_arrays() inverse_simulation.get_or_new_holder('choi').array = imposable.copy() new_brut = inverse_simulation.calculate('chobrut') assert_near(new_brut, brut, error_margin = 1)
def test_decomposition(print_decomposition = False): simulation = tax_benefit_system.new_scenario().init_single_entity( period = "2013-01", parent1 = dict( effectif_entreprise = 3000, exposition_accident = 3, code_postal_entreprise = "75001", ratio_alternants = .025, salaire_de_base = {"2013": 12 * 3000}, taille_entreprise = 3, type_sal = 0, ), menage = dict( zone_apl = 1, ), ).new_simulation(debug = True) xml_file_path = os.path.join( tax_benefit_system.DECOMP_DIR, "fiche_de_paie_decomposition.xml" ) decomposition_json = decompositions.get_decomposition_json(tax_benefit_system, xml_file_path = xml_file_path) simulations = [simulation] response = decompositions.calculate(simulations, decomposition_json) if print_decomposition: print unicode( json.dumps(response, encoding = 'utf-8', ensure_ascii = False, indent = 2) )
def make_couple_with_child_scenario(nombre_enfants=0, year=None, tax_benefit_system=tax_benefit_system, axes_variable='salaire_de_base', ax_variable_max=300000, count=5000): enfant = [dict(birth=date(2005, 1, 1), )] enfants = enfant * nombre_enfants scenario = tax_benefit_system.new_scenario().init_single_entity( axes=[[ dict( count=count, min=0, max=ax_variable_max, name=axes_variable, period=year - 2, ), dict( count=count, min=0, max=ax_variable_max, name=axes_variable, period=year - 1, ), dict( count=count, min=0, max=ax_variable_max, name=axes_variable, period=year, ), ]], period=year, parent1=dict( birth=date(1980, 1, 1), statmarit=5, #pacsés ppe_du_sa=1610, ), parent2=dict( birth=date(1980, 1, 1), statmarit=5, ), enfants=enfants, menage=dict( loyer=1000, statut_occupation=4, ), ) return scenario
def test_check(): for test_parameters in iter_scenarios(): test_name = test_parameters["name"] period = test_parameters["period"] parent1 = dict( birth = datetime.date(periods.period(period).start.year - 40, 1, 1), ) parent1.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period = period, parent1 = parent1, ).new_simulation(debug = True) for variable_name, expected_value in test_parameters['output_variables'].iteritems(): yield check_variable, simulation, variable_name, test_name, expected_value
def simple_check(tests): for test_parameters in tests: name = test_parameters["name"] period = test_parameters["period"] parent1 = dict( birth = datetime.date(periods.period(period).start.year - 40, 1, 1), ) parent1.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period = period, parent1 = parent1, ).new_simulation(debug = True) for variable, monthly_amount in test_parameters['output_variables'].iteritems(): check_variable(simulation, variable, name, monthly_amount)
def test_rst(year=2014): simulation = tax_benefit_system.new_scenario().init_single_entity( axes=[dict(count=11, max=24000, min=0, name='rstbrut')], period=year, parent1=dict(birth=datetime.date(year - 40, 1, 1), ), ).new_simulation(debug=True) brut = simulation.get_holder('rstbrut').array imposable = simulation.calculate('rst') inverse_simulation = simulation.clone(debug=True) inverse_simulation.get_holder('rstbrut').delete_arrays() inverse_simulation.get_or_new_holder('rsti').array = imposable.copy() new_brut = inverse_simulation.calculate('rstbrut') assert_near(new_brut, brut, error_margin=1)
def test_check(): for test_parameters in iter_scenarios(): test_name = test_parameters["name"] period = test_parameters["period"] parent1 = dict(birth=datetime.date( periods.period(period).start.year - 40, 1, 1), ) parent1.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period=period, parent1=parent1, ).new_simulation(debug=True) for variable_name, expected_value in test_parameters[ 'output_variables'].iteritems(): yield check_variable, simulation, variable_name, test_name, expected_value
def simple_check(tests): for test_parameters in tests: name = test_parameters["name"] period = test_parameters["period"] parent1 = dict(birth=datetime.date( periods.period(period).start.year - 40, 1, 1), ) parent1.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period=period, parent1=parent1, ).new_simulation(debug=True) for variable, monthly_amount in test_parameters[ 'output_variables'].iteritems(): check_variable(simulation, variable, name, monthly_amount)
def test_1(): simulation = tax_benefit_system.new_scenario().init_single_entity( period="2013", parent1=dict( effectif_entreprise=3000, exposition_accident=3, localisation_entreprise="75001", ratio_alternants=.025, salaire_de_base={"2011:3": 12 * 3000}, taille_entreprise=3, type_sal=0, ), menage=dict(zone_apl=1, ), ).new_simulation(debug=True) simulation.calculate("revdisp")
def brut_plot(revenu, count = 11, max_revenu = 5000, min_revenu = 0): year = 2014 period = periods.period("{}-01".format(year)) if revenu == 'chomage': brut_name = 'chobrut' imposable_name = 'cho' inversible_name = 'choi' elif revenu == 'retraite': brut_name = 'rstbrut' imposable_name = 'rst' inversible_name = 'rsti' elif revenu == 'salaire': brut_name = 'salaire_de_base' imposable_name = 'sal' inversible_name = 'sali' else: return single_entity_kwargs = dict( axes = [dict(count = count, max = max_revenu, min = min_revenu, name = brut_name)], period = period, parent1 = dict( birth = datetime.date(year - 40, 1, 1), ), ) simulation = tax_benefit_system.new_scenario().init_single_entity( **single_entity_kwargs).new_simulation(debug = True) brut = simulation.get_holder(brut_name).array imposable = simulation.calculate(imposable_name) inversion_reform = inversion_revenus.build_reform(tax_benefit_system) inverse_simulation = inversion_reform.new_scenario().init_single_entity( **single_entity_kwargs).new_simulation(debug = True) inverse_simulation.get_holder(brut_name).delete_arrays() inverse_simulation.get_or_new_holder(inversible_name).array = imposable.copy() new_brut = inverse_simulation.calculate(brut_name) pyplot.subplot(2, 1, 1) pyplot.plot(brut, imposable, 'ro', label = "direct") pyplot.plot(new_brut, imposable, 'db', label = "inversed") pyplot.legend() pyplot.subplot(2, 1, 2) pyplot.plot(brut, new_brut - brut, 'r-') pyplot.show() assert_near(new_brut, brut, absolute_error_margin = 1)
def make_couple_with_child_scenario(nombre_enfants = 0, year = None, tax_benefit_system = tax_benefit_system, axes_variable = 'salaire_de_base', ax_variable_max = 300000, count = 5000): enfant = [dict( birth = date(2005, 1, 1), )] enfants = enfant * nombre_enfants scenario = tax_benefit_system.new_scenario().init_single_entity( axes = [[ dict( count = count, min = 0, max = ax_variable_max, name = axes_variable, period = year-2, ), dict( count = count, min = 0, max = ax_variable_max, name = axes_variable, period = year-1, ), dict( count = count, min = 0, max = ax_variable_max, name = axes_variable, period = year, ), ]], period = year, parent1 = dict( birth = date(1980, 1, 1), statmarit = 5, #pacsés ppe_du_sa = 1610, ), parent2 = dict( birth = date(1980, 1, 1), statmarit = 5, ), enfants = enfants, menage = dict( loyer = 1000, statut_occupation = 4, ), ) return scenario
def test_1(): simulation = tax_benefit_system.new_scenario().init_single_entity( period = "2013-12", parent1 = dict( effectif_entreprise = 3000, exposition_accident = 3, localisation_entreprise = "75001", ratio_alternants = .025, salaire_de_base = {"2013-01:12": 12 * 3000}, taille_entreprise = 3, type_sal = 1, ), menage = dict( zone_apl = 1, ), ).new_simulation(debug = True) simulation.calculate("agff_tranche_a_employe", period = "2013-12")
def test_check(): for test_parameters in tests: name = test_parameters["name"] period = test_parameters["period"] parent1 = dict(birth=datetime.date( periods.period(period).start.year - 40, 1, 1), ) foyer_fiscal = dict() foyer_fiscal.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period=period, parent1=parent1, foyer_fiscal=foyer_fiscal, ).new_simulation(debug=True) for variable, monthly_amount in test_parameters[ 'output_variables'].iteritems(): output = simulation.calculate(variable) yield assert_variable, variable, name, monthly_amount, output
def check_sal(type_sal, year = 2014): simulation = tax_benefit_system.new_scenario().init_single_entity( axes = [dict(count = 11, max = 24000, min = 0, name = 'salbrut')], period = year, parent1 = dict( birth = datetime.date(year - 40, 1, 1), type_sal = type_sal, ), ).new_simulation(debug = False) brut = simulation.get_holder('salbrut').array imposable = simulation.calculate('sal') inverse_simulation = simulation.clone(debug = True) inverse_simulation.get_holder('salbrut').delete_arrays() inverse_simulation.get_or_new_holder('sali').array = imposable.copy() new_brut = inverse_simulation.calculate('salbrut') assert_near(new_brut, brut, error_margin = 1)
def test_check(): for test_parameters in tests: name = test_parameters["name"] period = test_parameters["period"] parent1 = dict( birth = datetime.date(periods.period(period).start.year - 40, 1, 1), ) foyer_fiscal = dict() foyer_fiscal.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period = period, parent1 = parent1, foyer_fiscal = foyer_fiscal, ).new_simulation(debug = True) for variable, monthly_amount in test_parameters['output_variables'].iteritems(): output = simulation.calculate_add(variable) yield assert_variable, variable, name, monthly_amount, output
def simple_check(tests): for test_parameters in tests: name = test_parameters["name"] period = test_parameters["period"] parent1 = dict( birth = datetime.date(periods.period(period).start.year - 40, 1, 1), ) parent1.update(test_parameters['input_variables']) simulation = tax_benefit_system.new_scenario().init_single_entity( period = period, parent1 = parent1, ).new_simulation(debug = True) for variable_name, expected_value in test_parameters['output_variables'].iteritems(): if isinstance(expected_value, dict): for item_period, item_value in expected_value.iteritems(): check_variable(simulation, variable_name, test_name, item_period, item_value) else: check_variable(simulation, variable_name, test_name, period, expected_value)
def test_rst(year = 2014): period = periods.period("{}-01".format(year)) single_entity_kwargs = dict( axes = [dict(count = 101, max = 2000, min = 0, name = 'rstbrut')], period = period, parent1 = dict( birth = datetime.date(year - 40, 1, 1), ), ) simulation = tax_benefit_system.new_scenario().init_single_entity( **single_entity_kwargs ).new_simulation(debug = True) brut = simulation.get_holder('rstbrut').array imposable = simulation.calculate('rst') inversion_reform = inversion_revenus.build_reform(tax_benefit_system) inverse_simulation = inversion_reform.new_scenario().init_single_entity( **single_entity_kwargs).new_simulation(debug = True) inverse_simulation.get_holder('rstbrut').delete_arrays() inverse_simulation.get_or_new_holder('rsti').array = imposable.copy() new_brut = inverse_simulation.calculate('rstbrut') assert_near(new_brut, brut, absolute_error_margin = 1)
if json_dumped_file: with io.open(json_dumped_file, 'w', encoding='utf-8') as f: f.write( unicode( json.dumps(scenario_json, ensure_ascii=False, encoding='utf-8', indent=2))) if __name__ == '__main__': from openfisca_france.tests.base import tax_benefit_system period = "2014-12" parent1 = dict( date_naissance=datetime.date( periods.period(period).start.year - 40, 1, 1), salbrut={"2014-12": 1445.38}, ) scenario = tax_benefit_system.new_scenario().init_single_entity( period=period, parent1=parent1, ) variables = ["salaire_super_brut"] trace( scenario, variables, api_url=u"http://127.0.0.1:2000", json_dumped_file="test_smicard.json", )
url = trace_base_url + "?" + urllib.urlencode({ "simulation": json.dumps(simulation_json), "api_url": api_url, }) browser = webbrowser.get(browser_name) browser.open_new_tab(url) if json_dumped_file: with io.open(json_dumped_file, 'w', encoding='utf-8') as f: f.write(unicode(json.dumps(scenario_json, ensure_ascii=False, encoding='utf-8', indent = 2))) if __name__ == '__main__': from openfisca_france.tests.base import tax_benefit_system period = "2014-12" parent1 = dict( birth = datetime.date(periods.period(period).start.year - 40, 1, 1), salbrut = {"2014-12": 1445.38}, ) scenario = tax_benefit_system.new_scenario().init_single_entity( period = period, parent1 = parent1, ) variables = ["salaire_super_brut"] trace( scenario, variables, api_url = u"http://127.0.0.1:2000", json_dumped_file = "test_smicard.json", )