def test_reference_object():
    '''
    get a reference to an object,then retrieve the object by reference
    '''
    a = 1
    refs = References()
    r1 = refs.reference(a)
    obj = refs.retrieve(r1)
    assert obj is a

    r2 = refs.reference(a)
    assert r2 == r1
Exemple #2
0
def test_reference_object():
    '''
    get a reference to an object,then retrieve the object by reference
    '''
    a = 1
    refs = References()
    r1 = refs.reference(a)
    obj = refs.retrieve(r1)
    assert obj is a

    r2 = refs.reference(a)
    assert r2 == r1
Exemple #3
0
def test_exceptions():
    a = 1
    refs = References()
    refs.reference(a, 'a')
    refs.reference(a, 'a')  # should not do anything
    assert refs.retrieve('a') is a

    with pytest.raises(ValueError):
        refs.reference(a, 'b')

    with pytest.raises(ValueError):
        refs.reference(2, 'a')
def test_gnome_obj_reference():
    '''
    create two equal but different objects and make sure a new reference is
    created for each
    '''
    objs = [constant_wind_mover(0, 0) for _i in range(2)]
    assert objs[0] is not objs[1]

    refs = References()
    r_objs = [refs.reference(item) for item in objs]
    assert len(r_objs) == len(objs)
    assert r_objs[0] != r_objs[1]

    for ix, ref in enumerate(r_objs):
        assert refs.retrieve(ref) is objs[ix]
        assert objs[ix] in refs  # double check __contains__

    unknown = constant_wind_mover(0, 0)
    assert unknown not in refs  # check __contains__
def test_exceptions():
    a = 1
    refs = References()
    refs.reference(a, 'a')
    refs.reference(a, 'a')  # should not do anything
    assert refs.retrieve('a') is a

    with pytest.raises(ValueError):
        refs.reference(a, 'b')

    with pytest.raises(ValueError):
        refs.reference(2, 'a')
def test_gnome_obj_reference():
    '''
    create two equal but different objects and make sure a new reference is
    created for each
    '''
    objs = [constant_wind_mover(0, 0) for _i in range(2)]
    assert objs[0] is not objs[1]

    refs = References()
    r_objs = [refs.reference(item) for item in objs]
    assert len(r_objs) == len(objs)
    assert r_objs[0] != r_objs[1]

    for ix, ref in enumerate(r_objs):
        assert refs.retrieve(ref) is objs[ix]
        assert objs[ix] in refs   # double check __contains__

    unknown = constant_wind_mover(0, 0)
    assert unknown not in refs  # check __contains__
Exemple #7
0
def test_save_load(save_ref, saveloc_):
    """
    tests and illustrates the functionality of save/load for
    WindMover
    """
    wind = Wind(filename=file_)
    wm = WindMover(wind)
    wm_fname = 'WindMover_save_test.json'
    refs = None
    if save_ref:
        w_fname = 'Wind.json'
        refs = References()
        refs.reference(wind, w_fname)
        wind.save(saveloc_, refs, w_fname)

    wm.save(saveloc_, references=refs, name=wm_fname)

    l_refs = References()
    obj = load(os.path.join(saveloc_, wm_fname), l_refs)
    assert (obj == wm and obj is not wm)
    assert (obj.wind == wind and obj.wind is not wind)
    shutil.rmtree(saveloc_)  # clean-up
Exemple #8
0
def test_save_load(save_ref, saveloc_):
    """
    tests and illustrates the functionality of save/load for
    WindMover
    """
    wind = Wind(filename=file_)
    wm = WindMover(wind)
    wm_fname = 'WindMover_save_test.json'
    refs = None
    if save_ref:
        w_fname = 'Wind.json'
        refs = References()
        refs.reference(wind, w_fname)
        wind.save(saveloc_, refs, w_fname)

    wm.save(saveloc_, references=refs, name=wm_fname)

    l_refs = References()
    obj = load(os.path.join(saveloc_, wm_fname), l_refs)
    assert (obj == wm and obj is not wm)
    assert (obj.wind == wind and obj.wind is not wind)
    shutil.rmtree(saveloc_)  # clean-up