Exemplo n.º 1
0
def test_can_falsify_types_without_default_productions():
    strategies = StrategyTable()
    strategies.define_specification_for(
        Bar, lambda s, d: BarStrategy(
            s.strategy(descriptors.integers_in_range(0, 100))))

    with pytest.raises(MissingSpecification):
        StrategyTable.default().strategy(Bar)

    verifier = Verifier(strategy_table=strategies)
    assert verifier.falsify(
        lambda x: False,
        Bar,
    )[0] == Bar()
    assert verifier.falsify(lambda x: x.size() < 3, Bar)[0] == Bar(Bar(Bar()))
Exemplo n.º 2
0
def test_can_grow_the_set_of_available_parameters_if_doing_badly():
    runs = 10
    number_grown = 0
    number_grown_large = 0
    for _ in hrange(runs):
        source = ExampleSource(
            random=random.Random(),
            strategy=StrategyTable.default().strategy(int),
            storage=None,
            min_parameters=1,
        )
        i = 0
        for example in source:
            if example < 0:
                source.mark_bad()
            i += 1
            if i >= 100:
                break
        if len(source.parameters) > 1:
            number_grown += 1
        if len(source.parameters) > 10:
            number_grown_large += 1

    assert number_grown >= 0.5 * runs
    assert number_grown_large <= 0.5 * runs
Exemplo n.º 3
0
def test_example_source_needs_random():
    with pytest.raises(ValueError):
        ExampleSource(
            random=None,
            strategy=StrategyTable.default().strategy(int),
            storage=None,
        )
Exemplo n.º 4
0
def test_errors_if_you_mark_bad_before_fetching():
    storage = None
    if hs.default.database is not None:
        storage = hs.default.database.storage_for(int)
    source = ExampleSource(
        random=random.Random(),
        strategy=StrategyTable.default().strategy(int),
        storage=storage,
    )
    with pytest.raises(ValueError):
        source.mark_bad()
Exemplo n.º 5
0
def test_tries_each_parameter_at_least_min_index_times():
    source = ExampleSource(random=random.Random(),
                           strategy=StrategyTable.default().strategy(int),
                           storage=None,
                           min_tries=5)
    i = 0
    for x in source:
        i += 1
        if i > 500:
            break
        if i % 2:
            source.mark_bad()
    # The last index may not have been fully populated
    assert all(c >= 5 for c in source.counts[:-1])
Exemplo n.º 6
0
def test_negative_is_not_too_far_off_mean():
    source = ExampleSource(
        random=random.Random(),
        strategy=StrategyTable.default().strategy(int),
        storage=None,
    )
    positive = 0
    i = 0
    for example in source:
        if example >= 0:
            positive += 1
        i += 1
        if i >= N_EXAMPLES:
            break
    assert 0.3 <= float(positive) / N_EXAMPLES <= 0.7
Exemplo n.º 7
0
def test_marking_negative_avoids_similar_examples():
    source = ExampleSource(
        random=random.Random(),
        strategy=StrategyTable.default().strategy(int),
        storage=None,
    )
    positive = 0
    i = 0
    for example in source:
        if example >= 0:
            positive += 1
        else:
            source.mark_bad()
        i += 1
        if i >= N_EXAMPLES:
            break
    assert float(positive) / N_EXAMPLES >= 0.8
    def run_test():
        if condition is None:
            _condition = lambda x: True
            condition_string = ''
        else:
            _condition = condition
            condition_string = strip_lambda(
                reflection.get_pretty_function_description(condition))

        count = 0
        successful_runs = 0
        strategy = StrategyTable.default().strategy(descriptor)
        for _ in hrange(MAX_RUNS):
            pv = strategy.parameter.draw(random)
            x = strategy.produce(random, pv)
            if not _condition(x):
                continue
            successful_runs += 1
            if predicate(x):
                count += 1
        if successful_runs < MIN_RUNS:
            raise ConditionTooHard(
                ('Unable to find enough examples satisfying predicate %s '
                 'only found %d but required at least %d for validity') %
                (condition_string, successful_runs, MIN_RUNS))

        result = Result(
            count,
            successful_runs,
            q,
            predicate,
            condition_string,
        )

        p = cumulative_binomial_probability(successful_runs, q, count)
        run_test.test_result = result
        # The test passes if we fail to reject the null hypothesis that
        # the probability is at least q
        if p < REQUIRED_P:
            result.failed = True
            raise HypothesisFalsified(result.description() + ' rejected')
Exemplo n.º 9
0
def test_minimizes_to_empty(desc):
    x = falsify(always_false, desc)[0]
    s = StrategyTable.default().strategy(desc)
    assert not list(s.simplify(x))
Exemplo n.º 10
0
 def __init__(self, strategy_table=None):
     super(ConverterTable, self).__init__()
     self.strategy_table = strategy_table or StrategyTable.default()
Exemplo n.º 11
0
                steps=steps,
                init_args=init_args,
                init_kwargs=init_kwargs
            )
        return TestRun(self.descriptor, steps)

    def unpack(self, x):
        steps = x.steps
        if self.requires_init:
            return ((x.init_args, x.init_kwargs), steps)
        else:
            return steps

    def simplify(self, x):
        pruned = x.prune()
        if pruned:
            yield pruned

        for y in super(StatefulStrategy, self).simplify(x):
            yield y

StrategyTable.default().define_specification_for_classes(
    define_stateful_strategy,
    subclasses_of=StatefulTest
)

ConverterTable.default().define_specification_for_classes(
    StatefulConverter,
    subclasses_of=StatefulTest
)
Exemplo n.º 12
0
class Awkward(str):
    pass


ConverterTable.default().mark_not_serializeable(Awkward)


class AwkwardStrategy(SearchStrategy):
    descriptor = Awkward
    parameter = params.CompositeParameter()

    def produce(self, random, pv):
        return Awkward()


StrategyTable.default().define_specification_for(
    Awkward, lambda s, d: AwkwardStrategy())


def test_can_verify_a_non_serializale_type():
    verifier = Verifier(settings=hs.Settings(database=ExampleDatabase()))
    verifier.falsify(lambda x: len(x) > 0, Awkward)


def test_verifier_deduplicates_on_coming_out_of_the_database():
    db = ExampleDatabase()
    storage = db.storage_for((frozenset({int}), ))
    db.backend.save(storage.key, '[1, 2, 3]')
    db.backend.save(storage.key, '[3, 2, 1]')
    counter = Counter()
    calls = []
    good = frozenset({1, 2, 3})
Exemplo n.º 13
0
def load():
    StrategyTable.default().define_specification_for(
        datetime, lambda s, d: DatetimeStrategy())