<commit_before>import os
import sqlite3
from collections import Counter
from contextlib import closing

from hypothesis import settings, assume, note, event
from hypothesis.stateful import (
    RuleBasedStateMachine,
    rule,
    initialize,
    invariant,
    precondition,
)
from hypothesis.strategies import data, integers, booleans, text, sampled_from

from siebenapp.autolink import ToggleAutoLink
from siebenapp.domain import (
    EdgeType,
    HoldSelect,
    ToggleClose,
    Delete,
    ToggleLink,
    Add,
    Select,
    Insert,
    Rename,
    Command,
)
from siebenapp.filter_view import FilterBy
from siebenapp.goaltree import Goals
from siebenapp.layers import all_layers
from siebenapp.open_view import ToggleOpenView
from siebenapp.switchable_view import ToggleSwitchableView
from siebenapp.system import run_migrations, save_updates
from siebenapp.zoom import ToggleZoom

settings.register_profile("ci", settings(max_examples=1000))
settings.register_profile("dev", settings(max_examples=200))
settings.load_profile(os.getenv("HYPOTHESIS_PROFILE", "dev"))


class GoaltreeRandomWalk(RuleBasedStateMachine):
    db_is_ready = False

    def __init__(self):
        super().__init__()
        self.goaltree = all_layers(Goals("Root"))
        self.database = sqlite3.connect(":memory:")

    @initialize()
    def open_db_connection(self):
        run_migrations(self.database)
        self.db_is_ready = True

    def teardown(self):
        self.database.close()

    def _accept_all(self, *commands: Command) -> None:
        for command in commands:
            note(str(command))
        self.goaltree.accept_all(*commands)

    def _accept(self, command: Command) -> None:
        note(str(command))
        self.goaltree.accept(command)

    #
    # Modifiers
    #

    @rule()
    def add_goal(self):
        event("add")
        self._accept(Add("a"))

    @rule()
    def delete_goal(self):
        event("delete")
        self._accept(Delete())

    @rule(d=data())
    def select_random_goal(self, d):
        event("select")
        random_goal = d.draw(
            integers(min_value=1, max_value=max(self.goaltree.q().keys()))
        )
        assume(random_goal in self.goaltree.q())
        event("valid select")
        self._accept(Select(random_goal))
        # Any valid goal must be selectable
        assert self.goaltree.q("select")[random_goal]["select"] == "select"

    @rule()
    def hold_selection(self):
        event("hold")
        self._accept(HoldSelect())

    @rule(d=data())
    # Ignore trivial trees (without any subgoal)
    @precondition(lambda self: len(self.goaltree.q()) > 1)
    def insert(self, d):
        event("insert")
        goal_keys = sorted(list(self.goaltree.q().keys()))
        selection = self.goaltree.settings("selection")
        goal_keys.remove(selection)
        random_goal = d.draw(sampled_from(goal_keys))
        self._accept_all(HoldSelect(), Select(random_goal), Insert("i"))

    @rule(b=booleans(), d=data())
    # Ignore trivial trees (without any subgoal)
    @precondition(lambda self: len(self.goaltree.q()) > 1)
    def toggle_link(self, b, d):
        event("toggle link")
        goal_keys = sorted(list(k for k in self.goaltree.q().keys() if k > 0))
        assume(len(goal_keys) > 1)
        selection = d.draw(sampled_from(goal_keys))
        goal_keys.remove(selection)
        prev_selection = d.draw(sampled_from(goal_keys))
        event("valid toggle link")<commit_msg>Fix: respect possible zoom during insert in proprty tests #181<commit_after>import os
import sqlite3
from collections import Counter
from contextlib import closing

from hypothesis import settings, assume, note, event
from hypothesis.stateful import (
    RuleBasedStateMachine,
    rule,
    initialize,
    invariant,
    precondition,
)
from hypothesis.strategies import data, integers, booleans, text, sampled_from

from siebenapp.autolink import ToggleAutoLink
from siebenapp.domain import (
    EdgeType,
    HoldSelect,
    ToggleClose,
    Delete,
    ToggleLink,
    Add,
    Select,
    Insert,
    Rename,
    Command,
)
from siebenapp.filter_view import FilterBy
from siebenapp.goaltree import Goals
from siebenapp.layers import all_layers
from siebenapp.open_view import ToggleOpenView
from siebenapp.switchable_view import ToggleSwitchableView
from siebenapp.system import run_migrations, save_updates
from siebenapp.zoom import ToggleZoom

settings.register_profile("ci", settings(max_examples=1000))
settings.register_profile("dev", settings(max_examples=200))
settings.load_profile(os.getenv("HYPOTHESIS_PROFILE", "dev"))


class GoaltreeRandomWalk(RuleBasedStateMachine):
    db_is_ready = False

    def __init__(self):
        super().__init__()
        self.goaltree = all_layers(Goals("Root"))
        self.database = sqlite3.connect(":memory:")

    @initialize()
    def open_db_connection(self):
        run_migrations(self.database)
        self.db_is_ready = True

    def teardown(self):
        self.database.close()

    def _accept_all(self, *commands: Command) -> None:
        for command in commands:
            note(str(command))
        self.goaltree.accept_all(*commands)

    def _accept(self, command: Command) -> None:
        note(str(command))
        self.goaltree.accept(command)

    #
    # Modifiers
    #

    @rule()
    def add_goal(self):
        event("add")
        self._accept(Add("a"))

    @rule()
    def delete_goal(self):
        event("delete")
        self._accept(Delete())

    @rule(d=data())
    def select_random_goal(self, d):
        event("select")
        random_goal = d.draw(
            integers(min_value=1, max_value=max(self.goaltree.q().keys()))
        )
        assume(random_goal in self.goaltree.q())
        event("valid select")
        self._accept(Select(random_goal))
        # Any valid goal must be selectable
        assert self.goaltree.q("select")[random_goal]["select"] == "select"

    @rule()
    def hold_selection(self):
        event("hold")
        self._accept(HoldSelect())

    @rule(d=data())
    # Ignore trivial trees (without any subgoal)
    @precondition(lambda self: len(self.goaltree.q()) > 1)
    def insert(self, d):
        event("insert")
        candidates = sorted(
            list(
                goal_id
                for goal_id, attrs in self.goaltree.q("select").items()
                if attrs["select"] != "select"
            )
        )
        random_goal = d.draw(sampled_from(candidates))
        self._accept_all(HoldSelect(), Select(random_goal), Insert("i"))

    @rule(b=booleans(), d=data())
    # Ignore trivial trees (without any subgoal)
    @precondition(lambda self: len(self.goaltree.q()) > 1)
    def toggle_link(self, b, d):
        event("toggle link")
        goal_keys = sorted(list(k for k in self.goaltree.q().keys() if k > 0))
        assume(len(goal_keys) > 1)
        selection = d.draw(sampled_from(goal_keys))
        goal_keys.remove(selection)
        prev_selection = d.draw(sampled_from(goal_keys))
        event("valid toggle link")
{'fraction_non_alphanumeric': 0.11434628975265018, 'fraction_numerical': 0.004381625441696113, 'mean_word_length': 3.1380116959064326, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 15, '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': '1192721', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2164, 'n_words': 469}