예제 #1
0
def test_restore_model_close_old(savetestmodel, name, newname):
    """Test restore_model API with/without name args."""

    model, file = savetestmodel
    model.close()
    newmodel = mx.restore_model(file, name)
    assert newmodel.name == newname
예제 #2
0
def test_new_pandas(tmp_path, pdobj, meth, is_relative, save_meth, filetype):

    p = getattr(mx, meth)()
    parent_name = p.name

    path = "files/testpandas.xlsx" if is_relative else (tmp_path /
                                                        "testpandas.xlsx")

    p.new_pandas(name="pdref", path=path, data=pdobj, filetype=filetype)

    if save_meth == "backup":
        datapath = tmp_path / "data"
        datapath.mkdir()
        getattr(p.model, save_meth)(tmp_path / "model", datapath=datapath)

        p.model.close()
        m2 = mx.restore_model(tmp_path / "model", datapath=datapath)

    else:
        getattr(p.model, save_meth)(tmp_path / "model")
        p.model.close()
        m2 = mx.read_model(tmp_path / "model")

    p2 = m2 if meth == "new_model" else m2.spaces[parent_name]

    if isinstance(pdobj, pd.DataFrame):
        pd.testing.assert_frame_equal(getattr(p2, "pdref")(), pdobj)
        pd.testing.assert_frame_equal(getattr(p2, "pdref").value, pdobj)
    elif isinstance(pdobj, pd.Series):
        pd.testing.assert_series_equal(getattr(p2, "pdref")(), pdobj)
        pd.testing.assert_series_equal(getattr(p2, "pdref").value, pdobj)

    m2.close()
예제 #3
0
def test_new_excel_range(tmp_path, meth, is_relative, edit_value, save_meth):
    """Check data new_excel_range

    Check data before and after value edit before after saving and reloading
    """

    p = getattr(mx, meth)()
    parent_name = p.name

    for kwargs in testargs:

        kwargs = kwargs.copy()

        kwargs["path"] = ("files/testexcel.xlsx"
                          if is_relative else tmp_path / "testexcel.xlsx")
        kwargs["sheet"] = "TestTables"
        kwargs["loadpath"] = XL_TESTDATA
        expected = kwargs.pop("expected").copy()
        p.new_excel_range(**kwargs)

        xlr = getattr(p, kwargs["name"])

        if edit_value:
            for key, val in xlr.items():
                xlr[key] = 2 * val

            for key, val in expected.items():
                expected[key] = 2 * val

        assert xlr == expected

    if save_meth == "backup":
        datapath = tmp_path / "data"
        datapath.mkdir()
        getattr(p.model, save_meth)(tmp_path / "model", datapath=datapath)

        p.model.close()
        m2 = mx.restore_model(tmp_path / "model", datapath=datapath)

    else:
        getattr(p.model, save_meth)(tmp_path / "model")
        p.model.close()
        m2 = mx.read_model(tmp_path / "model")

    p2 = m2 if meth == "new_model" else m2.spaces[parent_name]

    for kwargs in testargs:

        name = kwargs["name"]
        expected = kwargs["expected"].copy()

        if edit_value:
            expected = {key: 2 * val for key, val in expected.items()}

        assert getattr(p2, name) == expected

    m2.close()
예제 #4
0
def unpickled_model(request, derived_sample, tmpdir_factory):

    model = derived_sample
    if request.param:
        file = str(tmpdir_factory.mktemp("data").join("testmodel.mx"))
        model.save(file)
        model.close()
        model = mx.restore_model(file)

    yield model
    model.close()
예제 #5
0
def refspace(request, refmodel, tmpdir_factory):

    model = refmodel
    if request.param:
        file = str(tmpdir_factory.mktemp("data").join("refmodel.mx"))
        model.save(file)
        model.close()
        model = mx.restore_model(file)

    yield model.SpaceA
    model.close()
예제 #6
0
def test_save_again(tmpdir_factory):

    m, s = mx.new_model(), mx.new_space()

    @mx.defcells
    def a():
        return 1
    file = str(tmpdir_factory.mktemp("data").join("test_save_again.mx"))
    m.save(file)
    m2 = mx.restore_model(file)
    m2.save(file)
예제 #7
0
def sample_dynamic_model(request, build_sample_dynamic_model, tmpdir_factory):

    model = build_sample_dynamic_model
    if request.param:
        file = str(tmpdir_factory.mktemp("data").join(model.name + ".mx"))
        model.save(file)
        model.close()
        model = mx.restore_model(file)

    yield model
    model.close()
예제 #8
0
def test_new_module(tmp_path, parent, save_meth, module):

    if parent == "model":
        p = mx.new_model(name="Parent")
    else:
        p = mx.new_model().new_space(name="Parent")

    p.new_module(name="Foo", path="Parent/Foo", module=module)
    p.Bar = p.Foo

    assert p.Foo.modbar(2) == 4

    getattr(p.model, save_meth)(tmp_path / "model")
    p.model.close()

    if save_meth == "backup":
        m2 = mx.restore_model(tmp_path / "model")
    else:
        m2 = mx.read_model(tmp_path / "model")

    p2 = m2 if parent == "model" else m2.spaces["Parent"]

    assert p2.Foo.modbar(2) == 4
    assert p2.Bar is p2.Foo

    # Check saving again
    # https://github.com/fumitoh/modelx/issues/45
    getattr(p2.model, save_meth)(tmp_path / "model")
    m2.close()

    if save_meth == "backup":
        m3 = mx.restore_model(tmp_path / "model")
    else:
        m3 = mx.read_model(tmp_path / "model")

    p3 = m3 if parent == "model" else m3.spaces["Parent"]

    assert p3.Foo.modbar(2) == 4
    assert p3.Bar is p3.Foo
예제 #9
0
def test_restore_model_leave_old(savetestmodel, name, newname, renamed):
    """Test restore_model API with/without name args when old model exists.

    Args:
        name: Name passed to restore_model
        newname: Name the new model should have
        renamed: True if the old model is renamed
    """
    model, file = savetestmodel
    oldname = model.name
    newmodel = mx.restore_model(file, name)
    assert newmodel.name == newname
    assert model.name[: len(oldname)] == oldname
    assert renamed != (len(model.name) == len(oldname))
예제 #10
0
def test_new_pandas(tmp_path, pdobj, meth, is_relative, save_meth, filetype,
                    expose_data):

    p = getattr(mx, meth)()
    parent_name = p.name

    path = "files/testpandas.xlsx" if is_relative else (tmp_path /
                                                        "testpandas.xlsx")

    p.new_pandas(name="pdref",
                 path=path,
                 data=pdobj,
                 filetype=filetype,
                 expose_data=expose_data)

    if expose_data:
        assert isinstance(p.pdref._mx_dataclient, BaseDataClient)

    if save_meth == "backup":
        getattr(p.model, save_meth)(tmp_path / "model")

        p.model.close()
        m2 = mx.restore_model(tmp_path / "model")

    else:
        getattr(p.model, save_meth)(tmp_path / "model")
        p.model.close()
        m2 = mx.read_model(tmp_path / "model")

    p2 = m2 if meth == "new_model" else m2.spaces[parent_name]

    if expose_data:
        if isinstance(pdobj, pd.DataFrame):
            pd.testing.assert_frame_equal(p2.pdref, pdobj)
            assert isinstance(p2.pdref._mx_dataclient, BaseDataClient)
        elif isinstance(pdobj, pd.Series):
            pd.testing.assert_series_equal(getattr(p2, "pdref"), pdobj)
            assert isinstance(p2.pdref._mx_dataclient, BaseDataClient)
    else:
        if isinstance(pdobj, pd.DataFrame):
            pd.testing.assert_frame_equal(p2.pdref(), pdobj)
            pd.testing.assert_frame_equal(getattr(p2, "pdref").value, pdobj)
        elif isinstance(pdobj, pd.Series):
            pd.testing.assert_series_equal(p2.pdref(), pdobj)
            pd.testing.assert_series_equal(getattr(p2, "pdref").value, pdobj)

    m2.close()
예제 #11
0
def test_null_object(tmp_path):
    """
        m---A---B
            +---b <- B
            |
            C(A)
    """
    m = mx.new_model()
    A = m.new_space('A')
    B = A.new_space('B')
    A.b = B
    C = m.new_space('C', bases=A)

    del A.B
    assert not A.b._is_valid()
    assert not C.b._is_valid()

    m.backup(tmp_path / "model")
    m2 = mx.restore_model(tmp_path / "model")

    assert not m2.A.b._is_valid()
    assert not m2.C.b._is_valid()

    testutil.compare_model(m, m2)