def test_calculate_with_trace(): simulation = SimulationBuilder().build_default_simulation(tax_benefit_system) simulation.trace = True simulation.calculate('income_tax', '2017-01') salary_trace = simulation.tracer.trace['salary<2017-01>'] assert salary_trace['parameters'] == {} income_tax_trace = simulation.tracer.trace['income_tax<2017-01>'] assert income_tax_trace['parameters']['taxes.income_tax_rate<2017-01-01>'] == 0.15 # Trace parameters called with indirect access simulation.calculate('housing_tax', '2017') housing_tax_trace = simulation.tracer.trace['housing_tax<2017>'] assert 'taxes.housing_tax<2017-01-01>' not in housing_tax_trace['parameters'] assert housing_tax_trace['parameters']['taxes.housing_tax.rate<2017-01-01>'] == 10 assert housing_tax_trace['parameters']['taxes.housing_tax.minimal_amount<2017-01-01>'] == 200
def trace(tax_benefit_system, input_data): simulation = SimulationBuilder().build_from_entities(tax_benefit_system, input_data) simulation.trace = True requested_calculations = [] requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter = lambda t: t is None, yielded = True) for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split('/') requested_calculations.append(f"{variable_name}<{str(period)}>") simulation.calculate(variable_name, period) trace = simulation.tracer.get_serialized_flat_trace() return { "trace": trace, "entitiesDescription": simulation.describe_entities(), "requestedCalculations": requested_calculations }
def test_calculate_full_tracer(): 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
def trace(tax_benefit_system, input_data): simulation = SimulationBuilder().build_from_entities(tax_benefit_system, input_data) simulation.trace = True requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter = lambda t: t is None, yielded = True) for computation in requested_computations: path = computation[0] entity_plural, entity_id, variable_name, period = path.split('/') simulation.calculate(variable_name, period) trace = deepcopy(simulation.tracer.trace) for _vector_key, vector_trace in trace.items(): value = vector_trace['value'].tolist() if isinstance(vector_trace['value'], EnumArray): value = [item.name for item in vector_trace['value'].decode()] if isinstance(value[0], bytes): value = [str(item) for item in value] vector_trace['value'] = value return { "trace": trace, "entitiesDescription": simulation.describe_entities(), "requestedCalculations": list(simulation.tracer.requested_calculations) }