Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bool_funcs(formula, result):
eval_ctx = ExcelFormula.build_eval_context(lambda x: None, None)
assert eval_ctx(ExcelFormula(formula)) == result
def test_if_args_error():
eval_context = ExcelFormula.build_eval_context(lambda x: 1, lambda x: 1)
assert 0 == eval_context(ExcelFormula('=if(1,0)'))
assert VALUE_ERROR == eval_context(ExcelFormula('=if(#VALUE!,1)'))
assert VALUE_ERROR == eval_context(ExcelFormula('=if(#VALUE!,1,0)'))
assert VALUE_ERROR == eval_context(ExcelFormula('=if(1,#VALUE!,0)'))
assert VALUE_ERROR == eval_context(ExcelFormula('=if(0,1,#VALUE!)'))
def test_value_error(formula, result):
eval_ctx = ExcelFormula.build_eval_context(
lambda x: VALUE_ERROR, lambda x: [[1, 1], [1, VALUE_ERROR]],
logging.getLogger('pycel_x'))
assert eval_ctx(ExcelFormula(formula)) == result
def test_error_logging(caplog):
eval_ctx = ExcelFormula.build_eval_context(
lambda x: DIV0, lambda x: [[1, 1], [1, DIV0]],
logging.getLogger('pycel_x'))
caplog.set_level(logging.INFO)
assert 3 == eval_ctx(ExcelFormula('=iferror(1/0,3)'))
assert 1 == len(caplog.records)
assert "INFO" == caplog.records[0].levelname
assert "1 Div 0" in caplog.records[0].message
assert DIV0 == eval_ctx(ExcelFormula('=1/0'))
assert 2 == len(caplog.records)
assert "WARNING" == caplog.records[1].levelname
message = """return PYTHON_AST_OPERATORS[op](left_op, right_op)
ZeroDivisionError: division by zero
Eval: 1 / 0
def test_build_eval_context(result, formula):
eval_context = ExcelFormula.build_eval_context(lambda x: 1, lambda x: 1)
assert eval_context(ExcelFormula(formula)) == pytest.approx(result)
def empty_eval_context():
return ExcelFormula.build_eval_context(
None, None, logging.getLogger('pycel_x'))
def test_numerics_type_coercion(formula, result):
eval_ctx = ExcelFormula.build_eval_context(lambda x: 3.0, None)
assert eval_ctx(ExcelFormula(formula)) == result
def test_math_wrap():
eval_context = ExcelFormula.build_eval_context(
lambda x: None, lambda x: DIV0)
assert 1 == eval_context(ExcelFormula('=1 + sin(A1)'))
assert DIV0 == eval_context(ExcelFormula('=1 + sin(A1:B1)'))
assert 1 == eval_context(ExcelFormula('=1 + abs(A1)'))
assert DIV0 == eval_context(ExcelFormula('=1 + abs(A1:B1)'))
def eval(self):
if self._eval is None:
eval_ctx = ExcelFormula.build_eval_context(
self._evaluate, self._evaluate_range,
self.log, plugins=self._plugin_modules)
if self.cycles:
def _eval(cell, cse_array_address=None):
cell.start_calcs()
return eval_ctx(
cell.formula, cse_array_address=cse_array_address)
else:
def _eval(cell, cse_array_address=None):
return eval_ctx(
cell.formula, cse_array_address=cse_array_address)
self._eval = _eval