<commit_before># coding: utf-8
from mikado import Goals
from hypothesis import given, note, assume, example
from hypothesis.strategies import integers, lists, tuples, sampled_from, composite, choices


USER_ACTIONS = {
    'add': lambda g, x: g.add('a'),
    'delete': lambda g, x: g.delete(),
    'hold_select': lambda g, x: g.hold_select(),
    'insert': lambda g, x: g.insert('i'),
    'rename': lambda g, x: g.rename('r'),
    'select': lambda g, x: g.select(x),
    'toggle_close': lambda g, x: g.toggle_close(),
    'toggle_link': lambda g, x: g.toggle_link(),
}


@composite
def user_actions(draw, min_size=0, skip=[]):
    actions = draw(lists(tuples(
        sampled_from(k for k in USER_ACTIONS.keys() if k not in skip),
        integers(0, 9)
    ), min_size=min_size))
    return actions


def pretty_print(actions):
    result = []
    for name, int_val in actions:
        if name == 'select':
            result.append('%s %d' % (name, int_val))
        elif name in ['add', 'insert']:
            result.append('%s foo' % name)
        else:
            result.append(name)
    return result


def build_from(actions):
    note(pretty_print(actions))
    g = Goals('Root')
    for name, int_val in actions:
        USER_ACTIONS[name](g, int_val)
    return g


@given(user_actions())
def test_there_is_always_at_least_one_goal(actions):
    g = build_from(actions)
    assert g.all()


@given(user_actions())
def test_there_is_always_one_selected_goal(actions):
    g = build_from(actions)
    assert len([1 for k, v in g.all(keys='select').items() if v]) == 1


@given(user_actions(min_size=15,skip=['rename']), user_actions(min_size=1, skip=['select']), choices())
def test_any_goal_may_be_selected(all_actions, non_select_actions, choice):
    g = build_from(all_actions + non_select_actions)
    rnd_goal = choice(list(g.all().keys()))
    for i in str(rnd_goal):
        g.select(int(i))
    assert g.all(keys='select')[rnd_goal] == True


@given(user_actions(skip=['rename']))
def test_all_goals_must_be_connected_to_the_root(actions):
    g = build_from(actions)
    edges = g.all(keys='edge')
    queue, visited = [k for k, v in g.all().items() if v == 'Root'], set()
    while queue:
        goal = queue.pop()
        queue.extend(g for g in edges[goal] if g not in visited)
        visited.add(goal)
    assert visited == set(edges.keys())


@given(user_actions(skip=['rename']))
def test_all_open_goals_must_be_connected_to_the_root_via_other_open_goals(actions):
    g = build_from(actions)
    open_goals = set(k for k, v in g.all(keys='open').items() if v)
    edges = {k: v for k, v in g.all(keys='edge').items() if k in open_goals}
    assume(edges)
    queue, visited = [k for k, v in g.all().items() if v == 'Root'], set()
    while queue:
        goal = queue.pop()
        queue.extend(g for g in edges[goal] if g not in visited and g in open_goals)<commit_msg>Improve Hypothesis tests efficiency<commit_after># coding: utf-8
from mikado import Goals
from hypothesis import given, note, assume, example
from hypothesis.strategies import integers, lists, sampled_from, composite, choices, streaming


USER_ACTIONS = {
    'add': lambda g, x: g.add('a'),
    'delete': lambda g, x: g.delete(),
    'hold_select': lambda g, x: g.hold_select(),
    'insert': lambda g, x: g.insert('i'),
    'rename': lambda g, x: g.rename('r'),
    'select': lambda g, x: g.select(x),
    'toggle_close': lambda g, x: g.toggle_close(),
    'toggle_link': lambda g, x: g.toggle_link(),
}


@composite
def user_actions(draw, min_size=0, skip=[]):
    return draw(lists(sampled_from(k for k in USER_ACTIONS.keys() if k not in skip),
                      min_size=min_size))


def build_from(actions, ints):
    ints_index = 0
    g = Goals('Root')
    for name in actions:
        int_val = 0
        if name == 'select':
            int_val = ints[ints_index]
            ints_index += 1
        USER_ACTIONS[name](g, int_val)
    return g


@given(user_actions(), streaming(integers(0, 9)))
def test_there_is_always_at_least_one_goal(actions, ints):
    g = build_from(actions, ints)
    assert g.all()


@given(user_actions(), streaming(integers(0, 9)))
def test_there_is_always_one_selected_goal(actions, ints):
    g = build_from(actions, ints)
    assert len([1 for k, v in g.all(keys='select').items() if v]) == 1


@given(user_actions(min_size=15,skip=['rename']),
       user_actions(min_size=1, skip=['select']),
       streaming(integers(0, 9)), choices())
def test_any_goal_may_be_selected(all_actions, non_select_actions, ints, choice):
    g = build_from(all_actions + non_select_actions, ints)
    rnd_goal = choice(list(g.all().keys()))
    for i in str(rnd_goal):
        g.select(int(i))
    assert g.all(keys='select')[rnd_goal] == True


@given(user_actions(skip=['rename']), streaming(integers(0, 9)))
def test_all_goals_must_be_connected_to_the_root(actions, ints):
    g = build_from(actions, ints)
    edges = g.all(keys='edge')
    queue, visited = [k for k, v in g.all().items() if v == 'Root'], set()
    while queue:
        goal = queue.pop()
        queue.extend(g for g in edges[goal] if g not in visited)
        visited.add(goal)
    assert visited == set(edges.keys())


@given(user_actions(skip=['rename']), streaming(integers(0, 9)))
def test_all_open_goals_must_be_connected_to_the_root_via_other_open_goals(actions, ints):
    g = build_from(actions, ints)
    open_goals = set(k for k, v in g.all(keys='open').items() if v)
    edges = {k: v for k, v in g.all(keys='edge').items() if k in open_goals}
    assume(edges)
    queue, visited = [k for k, v in g.all().items() if v == 'Root'], set()
    while queue:
        goal = queue.pop()
        queue.extend(g for g in edges[goal] if g not in visited and g in open_goals)
{'fraction_non_alphanumeric': 0.14646814404432132, 'fraction_numerical': 0.005020775623268698, 'mean_word_length': 3.7005695687550855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '209044', 'n_tokens_mistral': 2096, 'n_tokens_neox': 2036, 'n_words': 528}