Beispiel #1
0
def test_change_eppyname():
    """pytest for changing eppyname"""
    # make the epmunch
    dct = dict(a=dict(aa=dict(z=-1, y=-2), bb=dict(z=1, y=2)))
    dctstr = json.dumps(dct)
    fhandle = StringIO(dctstr)
    amunch = readepj.readepjjson(fhandle)
    aa = amunch.a.aa
    aa["eppyname"] = "cc"
    # aa.eppyname = 'cc'
    assert aa == amunch.a.cc
    cc = amunch.a.cc
    # cc['eppyname'] = 'dd'
    cc.eppyname = "dd"
    assert cc == amunch.a.dd
    amunch.a.dd.z = 55
    dct = dict(a=dict(dd=dict(z=55, y=-2), bb=dict(z=1, y=2)))
    dctstr = json.dumps(dct)
    fhandle = StringIO(dctstr)
    bmunch = readepj.readepjjson(fhandle)
    # assert amunch == bmunch

    # remove eppykeys beflore comparison.
    # otherwise it go into a recursive loop because of 'eppy_epobjects'
    amunchd = amunch.toDict()
    amunchd = Munch.fromDict(amunchd)
    removeeppykeys(amunchd)
    bmunchd = bmunch.toDict()
    bmunchd = Munch.fromDict(bmunchd)
    removeeppykeys(bmunchd)

    assert amunchd == bmunchd
Beispiel #2
0
def simplemunch(request):
    """simple epmuch for tests"""
    dct = dict(a=dict(aa=dict(z=-1, y=-2)))
    dctstr = json.dumps(dct)
    fhandle = StringIO(dctstr)
    amunch = readepj.readepjjson(fhandle)
    # print('\n-----------------')
    # print('fixturename : %s' % request.fixturename)
    # print('scope       : %s' % request.scope)
    # print('function    : %s' % request.function.__name__)
    # print('cls         : %s' % request.cls)
    # print('module      : %s' % request.module.__name__)
    # print('fspath      : %s' % request.fspath)
    # print('-----------------')

    request.cls.amunch = amunch
    # see this link for more on how request works
    # https://docs.pytest.org/en/stable/fixture.html#request-context
    # still not clear to me right now
    #
    # You can pick up amunch in the test as self.amunch

    # print('\n-----------------')
    # print('fixturename : %s' % request.fixturename)
    # print('scope       : %s' % request.scope)
    # print('function    : %s' % request.function.__name__)
    # print('cls         : %s' % request.cls)
    # print('module      : %s' % request.module.__name__)
    # print('fspath      : %s' % request.fspath)
    # print('-----------------')
    yield
Beispiel #3
0
def test_readepjjson():
    """py.test for readepjjson"""
    dct = dict(
        a=dict(aa=dict(z=-1, y=-2), bb=dict(zz=-11, yy=-22)),
        b=dict(cc=dict(ab=12, bc=23), dd=dict(cd=34, de=45)),
    )
    dctstr = json.dumps(dct)
    fhandle = StringIO(dctstr)
    result = readepj.readepjjson(fhandle)
    assert isinstance(result, EPMunch)
Beispiel #4
0
def test_removeeppykeys():
    """py.test for removeeppykeys"""
    dct = dict(
        a=dict(aa=dict(z=-1, y=-2), bb=dict(zz=-11, yy=-22)),
        b=dict(cc=dict(ab=12, bc=23), dd=dict(cd=34, de=45)),
    )
    dctstr = json.dumps(dct)
    # test for rkeys=None
    fhandle = StringIO(dctstr)
    epmunch = readepj.readepjjson(fhandle)
    readepj.removeeppykeys(epmunch)
    epobject = epmunch.a.aa
    assert "eppykey" not in epobject
    assert "eppyname" not in epobject
    # test for rkeys=['eppykey']
    fhandle = StringIO(dctstr)
    epmunch = readepj.readepjjson(fhandle)
    readepj.removeeppykeys(epmunch, rkeys=["eppykey"])
    epobject = epmunch.a.aa
    assert "eppykey" not in epobject
    assert "eppyname" in epobject
Beispiel #5
0
def test_addeppykeys():
    """py.test for addeppykeys"""
    dct = dict(
        a=dict(aa=dict(z=-1, y=-2), bb=dict(zz=-11, yy=-22)),
        b=dict(cc=dict(ab=12, bc=23), dd=dict(cd=34, de=45)),
    )
    dctstr = json.dumps(dct)
    fhandle = StringIO(dctstr)
    epmunch = readepj.readepjjson(fhandle)
    epobject = epmunch.a.aa
    assert "eppykey" in epobject
    assert "eppyname" in epobject
Beispiel #6
0
def test_delete():
    """py.test for EPMunch.delete"""
    # make the epmunch
    dct = dict(a=dict(aa=dict(z=-1, y=-2), bb=dict(z=1, y=2)))
    dctstr = json.dumps(dct)
    fhandle = StringIO(dctstr)
    amunch = readepj.readepjjson(fhandle)
    amunch.a.aa.delete()
    assert list(amunch.a.bb.eppy_epobjects.keys()) == ["bb"]
    # test when it not an epobject
    with pytest.raises(epMunch.NotEPObject):
        amunch.a.delete()
    amunch["a"]["bb"].delete()  # test when bb is not an attribute
    assert list(amunch.a.keys()) == []
Beispiel #7
0
def test_printmunch_withprint(capsys):
    """py.test for printmunch with print"""
    (dct, indent, index, expected) = (
        dict(a=dict(aa=dict(z=-1, y=-2))),
        0,
        None,
        "\n".join([
            "",
            "a                                                !-  EP_KEY         # use .eppykey",
            "            aa                                   !-  EPJOBJECT_NAME # use .eppyname",  # noqa: E501
            "            -1                                   !-  z",
            "            -2                                   !-  y",
        ]),
    )
    dctstr = json.dumps(dct)
    fhandle = StringIO(dctstr)
    amunch = readepj.readepjjson(fhandle)
    epMunch.printmunch(amunch, indent, index)
    captured = capsys.readouterr()
    assert captured.out == expected + "\n"
Beispiel #8
0
    def read(self):
        """read the epj file"""
        self.epj = readepjjson(self.epjname)
        self.epobjects = {
            key: [val1 for val1 in val.values()]
            for key, val in self.epj.items()
        }
        # TODO: the above line should get the epobjects from the schema
        # This should happen whenever the schema is read.
        # in case the schema reading happens far in the future
        # eppy3000 should partilly work even without the schema
        # -

        # insert epj into the epjobject
        # this allows the epjobject to access all other objects in the epj
        for epobjects in self.epobjects.values():
            for epobject in epobjects:
                epobject["eppy_epj"] = self.epj
        if self.epschemaname:
            for key in self.epobjects.keys():
                for epobject in self.epobjects[key]:
                    epobject[
                        "eppy_objepschema"] = self.epschema.epschemaobjects[
                            key]
Beispiel #9
0
def test_printmunch():
    """py.test for printmunch"""
    data = (
        (
            dict(a=dict(aa=dict(z=-1, y=-2))),
            0,
            None,
            [
                "",
                "a                                                !-  EP_KEY         # use .eppykey",
                "            aa                                   !-  EPJOBJECT_NAME # use .eppyname",
                "            -1                                   !-  z",
                "            -2                                   !-  y",
            ],
        ),  # dct, indent, index, expected
        (
            dict(a=dict(aa=dict(z=-1, y=[dict(zz=1), dict(zz=2)]))),
            0,
            None,
            [
                "",
                "a                                                !-  EP_KEY         # use .eppykey",
                "            aa                                   !-  EPJOBJECT_NAME # use .eppyname",
                "            -1                                   !-  z",
                "                                                 !-  y",
                "                1                                    !-  zz #1",
                "                2                                    !-  zz #2",
            ],
        ),  # dct, indent, index, expected
        (
            dict(a=dict(aa=dict(z=-1, y=-2))),
            0,
            3,
            [
                "",
                "a                                                !-  EP_KEY         # use .eppykey",
                "            aa                                   !-  EPJOBJECT_NAME # use .eppyname",
                "            -1                                   !-  z #3",
                "            -2                                   !-  y #3",
            ],
        ),  # dct, indent, index, expected
        (
            dict(a=dict(aa=dict(z=-1, y=-2))),
            0,
            None,
            [
                "",
                "a                                                !-  EP_KEY         # use .eppykey",
                "            aa                                   !-  EPJOBJECT_NAME # use .eppyname",
                "            -1                                   !-  z",
                "            -2                                   !-  y",
            ],
        ),  # dct, indent, index, expected
    )  # noqa: E501
    for dct, indent, index, expected in data:
        dctstr = json.dumps(dct)
        fhandle = StringIO(dctstr)
        amunch = readepj.readepjjson(fhandle)
        result = []
        epMunch.printmunch(amunch, indent, index, result.append)
        assert result == expected
Beispiel #10
0
 def setup(self):
     dct = dict(a=dict(aa=dict(z=-1, y=-2)))
     dctstr = json.dumps(dct)
     fhandle = StringIO(dctstr)
     self.amunch = readepj.readepjjson(fhandle)