Пример #1
0
def test_load_more_than_ten(distribution):
    """Test that a family with more than 10 subtypes can be created from a
    cache."""

    family = Family(distribution)
    for _ in range(11):
        family.add_subtype()

    family.save(".testcache")

    pickled = Family.load(distribution, root=".testcache")

    assert isinstance(pickled, Family)
    assert pickled.distribution is distribution
    assert pickled.subtype_id == 11
    assert list(pickled.subtypes.keys()) == list(range(11))

    for subtype_id, subtype in pickled.subtypes.items():
        assert issubclass(subtype, distribution)
        assert subtype.__init__ is distribution.__init__
        assert subtype.sample is distribution.sample
        assert subtype.subtype_id == subtype_id
        assert subtype.family is pickled

    for fpart, ppart in zip(family.random_state.get_state(),
                            pickled.random_state.get_state()):
        try:
            assert all(fpart == ppart)
        except TypeError:
            assert fpart == ppart

    os.system("rm -r .testcache")
Пример #2
0
def test_load(distribution):
    """ Test that a family can be created from a cache. """

    family = Family(distribution)
    family.add_subtype()
    subtype = family.subtypes[0]
    family.save(".testcache")

    pickled = Family.load(distribution, root=".testcache")
    pickled_subtype = pickled.subtypes[0]

    assert isinstance(pickled, Family)
    assert pickled.distribution is distribution
    assert pickled.subtype_id == 1
    assert pickled.subtypes == {0: pickled_subtype}

    assert issubclass(pickled_subtype, distribution)
    assert pickled_subtype.__name__ == subtype.__name__
    assert pickled_subtype.name == subtype.name
    assert pickled_subtype.dtype == subtype.dtype
    assert pickled_subtype.subtype_id == 0
    assert pickled_subtype.family is pickled

    assert pickled_subtype.hard_limits == subtype.hard_limits
    assert pickled_subtype.param_limits == subtype.param_limits
    assert pickled_subtype.__init__ is subtype.__init__
    assert pickled_subtype.__repr__ is subtype.__repr__
    assert pickled_subtype.sample is subtype.sample

    for fpart, ppart in zip(family.random_state.get_state(),
                            pickled.random_state.get_state()):
        try:
            assert all(fpart == ppart)
        except TypeError:
            assert fpart == ppart

    os.system("rm -r .testcache")