def test_none_returned_error(): errfunc = dedent("""\ def return_none(x, y): return None""") space = mx.new_model(name="ErrModel").new_space(name="ErrSpace") cells = space.new_cells(formula=errfunc) cells.allow_none = False with SuppressFormulaError(): with pytest.raises(NoneReturnedError) as errinfo: cells(1, 3) errmsg = "ErrModel.ErrSpace.return_none(x=1, y=3)" assert errinfo.value.args[0] == errmsg with pytest.raises(mx.core.system.FormulaError) as errinfo: cells(1, 3) errmsg = dedent("""\ Error raised during formula execution modelx.core.errors.NoneReturnedError: ErrModel.ErrSpace.return_none(x=1, y=3) Formula traceback: 0: ErrModel.ErrSpace.return_none(x=1, y=3) Formula source: def return_none(x, y): return None """) assert errinfo.value.args[0] == errmsg
def test_delete_global(refspace): assert refspace.foo() == 3 del refspace.model.baz with SuppressFormulaError(): with pytest.raises(NameError): refspace.foo()
def test_del_attrref(): """ m-----SpaceA-----SpaceB---x | +----foo +--SpaceC(SpaceA) """ m = mx.new_model() A = m.new_space("SpaceA") B = A.new_space("SpaceB") B.x = 3 def foo(): return SpaceB.x A.new_cells(formula=foo) C = m.new_space("SpaceC", bases=A) assert A.foo() == 3 assert C.foo() == 3 del B.x with SuppressFormulaError(): with pytest.raises(AttributeError): A.foo() with pytest.raises(AttributeError): C.foo()
def test_delete_ref(refspace): del refspace.bar with SuppressFormulaError(): with pytest.raises(NameError): refspace.foo()
def test_setitem_in_formula_invalid_assignment_error(setitemsample): def invalid_in_formula_assignment(x): invalid_in_formula_assignment[x + 1] = 3 * x setitemsample.new_cells(formula=invalid_in_formula_assignment) with SuppressFormulaError(): with pytest.raises(KeyError): setitemsample.invalid_in_formula_assignment[3]
def test_setitem_in_formula_duplicate_assignment_error(setitemsample): def duplicate_assignment(x): duplicate_assignment[x] = 4 * x return 4 * x setitemsample.new_cells(formula=duplicate_assignment) with SuppressFormulaError(): with pytest.raises(ValueError): setitemsample.duplicate_assignment[4]
def test_delattr_ref(testmodel): s = testmodel.new_space() s.x = 3 assert s.x == 3 del s.x with SuppressFormulaError(): with pytest.raises(AttributeError): s.x
def test_trace_cleanup_value_error(): @mx.defcells def foo(i): import datetime return foo(i - 1).replace(month=foo(i - 1).month + 1) if i > 0 else datetime.date(2016, 1, 1) with SuppressFormulaError(): with pytest.raises(ValueError): foo(20) assert foo._impl.check_sanity() assert len(foo) == 12
def test_global_ref_delattr(): model, space = new_model(), new_space() @defcells def func1(x): return min(n, x) model.n = 2 del model.n with SuppressFormulaError(): with pytest.raises(NameError): func1(4)
def test_maxout_recursion(): m, s = mx.new_model(), mx.new_space() @mx.defcells def foo(x): if x == 0: return 0 else: return foo(x-1) + 1 with SuppressFormulaError(maxdepth): with pytest.raises(DeepReferenceError): foo(maxdepth+1)
def test_trace_cleanup_type_error(): @mx.defcells def foo(i): if i > 0: return foo(i - 1) + (1 if i < 2 else "error") else: return 0 with SuppressFormulaError(): with pytest.raises(TypeError): foo(2) assert foo._impl.check_sanity() assert len(foo) == 2
def test_get_too_few_args(cells_signatures): space = cells_signatures with SuppressFormulaError(): with pytest.raises(TypeError): space.single_param() with pytest.raises(TypeError): space.mult_params(1) with pytest.raises(TypeError): space.mult_params[1]
def test_with_sapce_allow_none_false(model_param, space_param, cells_param, op): model, space = new_model(), new_space() cells = space.new_cells(formula="def test1(x): return None") model.allow_none = model_param space.allow_none = space_param cells.allow_none = cells_param with SuppressFormulaError(): with pytest.raises(NoneReturnedError): if op == "get": assert cells[1] == None else: cells[1] = None
def test_zerodiv(): zerodiv = dedent("""\ def zerodiv(x): if x == 3: return x / 0 else: return zerodiv(x + 1)""") space = mx.new_model().new_space(name="ZeroDiv") cells = space.new_cells(formula=zerodiv) with SuppressFormulaError(): with pytest.raises(ZeroDivisionError): cells(0)
def test_delattr_cells(testmodel): space = new_space() @defcells def foo(x): return 2 * x foo(3) del space.foo with SuppressFormulaError(): with pytest.raises(AttributeError): space.foo(3) with pytest.raises(RuntimeError): foo(3)
def test_new_cells_from_series(sample_model, sample_series): space = sample_model.new_space() series, name, param = sample_series if not any(series.index.names) and not param: with SuppressFormulaError(): with pytest.raises(ValueError): space.new_cells_from_pandas(series, cells=name, param=param) else: cells = space.new_cells_from_pandas(series, cells=name, param=param) assert cells.series.equals(series) if name: assert cells.name == name if param: assert cells.parameters == tuple(param)
def test_deep_reference_error(): from modelx.core import mxsys last_maxdepth = mxsys.callstack.maxdepth try: set_recursion(3) errfunc = dedent("""\ def erronerous(x, y): return erronerous(x + 1, y - 1)""") space = new_model(name="ErrModel").new_space(name="ErrSpace") cells = space.new_cells(formula=errfunc) with SuppressFormulaError(): with pytest.raises(DeepReferenceError) as errinfo: cells(1, 3) assert errinfo.value.args[0] == "Formula chain exceeded the 3 limit" with pytest.raises(mx.core.system.FormulaError) as errinfo: cells(1, 3) errmsg = dedent("""\ Error raised during formula execution modelx.core.errors.DeepReferenceError: Formula chain exceeded the 3 limit Formula traceback: 0: ErrModel.ErrSpace.erronerous(x=1, y=3), line 2 1: ErrModel.ErrSpace.erronerous(x=2, y=2), line 2 2: ErrModel.ErrSpace.erronerous(x=3, y=1), line 2 3: ErrModel.ErrSpace.erronerous(x=4, y=0), line 2 Formula source: def erronerous(x, y): return erronerous(x + 1, y - 1) """) finally: set_recursion(last_maxdepth) assert errinfo.value.args[0] == errmsg
def test_delattr_cells(testmodel): """ A---foo(x) """ space = new_space("A") @defcells def foo(x): return 2 * x foo(3) del space.foo assert not foo._is_valid() with SuppressFormulaError(): with pytest.raises(AttributeError): space.foo(3) with pytest.raises(DeletedObjectError): foo(3)
def test_get_too_many_args(cells_signatures): space = cells_signatures with SuppressFormulaError(): with pytest.raises(TypeError): space.no_param(1) with pytest.raises(TypeError): space.no_param[1] with pytest.raises(TypeError): space.single_param(1, 2) with pytest.raises(TypeError): space.single_param[1, 2] with pytest.raises(TypeError): space.single_param[(1, 2)] # Intepreted as [1, 2]
def test_del_global_attrref(): """ m-----SpaceA-----foo +--SpaceB +--s2 +--x """ m = mx.new_model() s = mx.new_space("SpaceA") @mx.defcells def foo(i): return s2.x s2 = mx.new_space(name="SpaceB", formula=lambda a: None) s.s2 = s2 m.x = 3 assert foo(3) == 3 del m.x with SuppressFormulaError(): with pytest.raises(AttributeError): foo(3)