Ejemplo n.º 1
0
def test_argpack_codepack_execution(default_os_env):
    code1 = Code(add2)
    code2 = Code(mul2)
    code1 >> code2
    code2.receive('b') << code1
    codepack = CodePack(id='argpack_test', code=code1, subscribe=code2)
    argpack = codepack.make_argpack()
    snapshot = codepack.to_snapshot(argpack=argpack)
    assert isinstance(snapshot.argpack, dict)
    assert snapshot.argpack['add2']['a'] is None
    assert snapshot.argpack['add2']['b'] is None
    assert snapshot.argpack['mul2']['a'] is None
    assert 'b' not in snapshot.argpack['mul2']
    snapshot2 = CodePackSnapshot.from_dict(snapshot.to_dict())
    assert isinstance(snapshot2.argpack, dict)
    assert snapshot2.argpack['add2']['a'] is None
    assert snapshot2.argpack['add2']['b'] is None
    assert snapshot2.argpack['mul2']['a'] is None
    assert 'b' not in snapshot2.argpack['mul2']
    argpack['add2'](a=3, b=2)
    argpack['mul2'](a=2)
    snapshot3 = codepack.to_snapshot(argpack=argpack)
    assert snapshot3.argpack['add2']['a'] == 3
    assert snapshot3.argpack['add2']['b'] == 2
    assert snapshot3.argpack['mul2']['a'] == 2
    snapshot4 = CodePackSnapshot.from_dict(snapshot3.to_dict())
    assert snapshot4.argpack['add2']['a'] == 3
    assert snapshot4.argpack['add2']['b'] == 2
    assert snapshot4.argpack['mul2']['a'] == 2
    codepack2 = CodePack.from_snapshot(snapshot4)
    assert codepack2(argpack=snapshot4.argpack) == 10
Ejemplo n.º 2
0
def test_codepack_to_snapshot_and_from_snapshot(default_os_env):
    c1 = Code(add3)
    c2 = Code(mul2)
    c3 = Code(combination)
    c4 = Code(linear)
    c5 = Code(print_x)

    c1 >> c3
    c2 >> c3
    c3 >> [c4, c5]

    c3.receive('c') << c1
    c4.receive('c') << c3
    c5.receive('x') << c3

    cp1 = CodePack(id='test_codepack', code=c1, subscribe=c4)

    argpack = cp1.make_argpack()
    argpack['add3']['a'] = 1
    argpack['add3']['b'] = 2
    argpack['add3']['c'] = 3
    argpack['mul2']['a'] = 2
    argpack['mul2']['b'] = 1
    argpack['combination']['a'] = 2
    argpack['combination']['b'] = 5
    argpack['linear']['b'] = 7
    argpack['linear']['a'] = 5

    ret = None
    with pytest.raises(TypeError):
        ret = cp1(argpack)

    assert ret is None
    assert cp1.get_state() == 'ERROR'
    snapshot1 = cp1.to_snapshot(argpack=argpack)
    assert snapshot1.owner is None
    snapshot1['owner'] = 'codepack'
    assert snapshot1.owner == 'codepack'
    cp2 = CodePack.from_snapshot(snapshot1)
    assert cp2.owner == 'codepack'
    assert cp1.id == cp2.id
    assert cp1.serial_number == cp2.serial_number
    assert cp1.get_state() == cp2.get_state()
    cp1_source = cp1.get_source()
    cp2_source = cp2.get_source()
    assert cp1_source.keys() == cp2_source.keys()
    for id in cp1_source.keys():
        assert cp1_source[id].strip() == cp2_source[id].strip()
    # assert cp1.get_structure() == cp2.get_structure()
    assert cp1.subscribe == cp2.subscribe
    assert set(cp1.codes.keys()) == set(cp2.codes.keys())
    assert cp1.owner is None
    assert cp2.owner == 'codepack'
    for code_id in cp1.codes.keys():
        code1 = cp1.codes[code_id]
        code2 = cp2.codes[code_id]
        assert code1.serial_number == code2.serial_number
        assert code1.parents.keys() == code2.parents.keys()
        for id in code1.parents.keys():
            assert code1.parents[id].serial_number == code2.parents[id].serial_number
        assert code1.children.keys() == code2.children.keys()
        for id in code1.children.keys():
            assert code1.children[id].serial_number == code2.children[id].serial_number
        assert code1.get_state() == code2.get_state()
        assert code1.dependency.keys() == code2.dependency.keys()
        for serial_number in code1.dependency.keys():
            assert code1.dependency[serial_number].id == code2.dependency[serial_number].id
            assert code1.dependency[serial_number].serial_number == code2.dependency[serial_number].serial_number
            assert code1.dependency[serial_number].arg == code2.dependency[serial_number].arg