Esempio n. 1
0
class GameRules(RuleBasedStateMachine):
    state = initial_state

    @srulep(choice=choices())
    def move(self, state, choice):
        unlocked_exits = [
            exit_name for (exit_name, (req_key, dest)) in
            state.location.exits.items() if req_key is None
        ]
        direc = choice(unlocked_exits)
        st = move(state, direc)
        assert st.location_name == state.location.exits[direc][1]
        return st

    @srulep(
        precond=lambda self: len(self.state.location.items) > 0,
        choice=choices()
    )
    def take(self, state, choice):
        thing_name = choice(state.location.items.keys())
        st = take(state, thing_name)
        assert thing_name in (t.name for t in st.inventory)
        assert thing_name not in st.location.items
        return st

    @srulep(
        precond=lambda self: len(eligible_locked_exits(self.state)) > 0,
        choice=choices()
    )
    def move_through_locked_door(self, state, choice):
        direc = choice(eligible_locked_exits(state))
        st = move(state, direc)
        assert st.location_name == state.location.exits[direc][1]
        return st
Esempio n. 2
0
class NamespaceListTests(TestCase):
    """
    Tests for ``NamespaceList``.
    """
    @given(collection=namespacelists(), choose=choices())
    def test_remove(self, collection, choose):
        """
        ``NamespaceList.remove`` creates a new ``NamespaceList`` which does not
        have the given item.
        """
        assume(len(collection.items) > 0)
        item = choose(collection.items)
        removed = collection.remove(item)
        self.assertThat(removed.items, Not(Contains(item)))

    @given(collection=namespacelists(), choose=choices())
    def test_item_by_name(self, collection, choose):
        """
        ``NamespaceList.item_by_name`` returns the ``Namespace`` with the matching
        name.
        """
        assume(len(collection.items) > 0)
        for item in collection.items:
            self.expectThat(collection.item_by_name(item.metadata.name),
                            Is(item))

        item = choose(collection.items)
        collection = collection.remove(item)
        self.expectThat(
            lambda: collection.item_by_name(item.metadata.name),
            raises(KeyError(item.metadata.name)),
        )
def test_fails_to_draw_from_empty_sequence():
    @given(st.choices())
    def test(choice):
        choice([])

    with pytest.raises(IndexError):
        test()
def test_stability():
    @given(
        st.lists(st.text(min_size=1, max_size=1), unique=True, min_size=5),
        st.choices(),
    )
    @settings(
        database=ExampleDatabase(),
    )
    def test_choose_and_then_fail(ls, choice):
        for _ in hrange(100):
            choice(ls)
        assert False

    # Run once first for easier debugging
    with raises(AssertionError):
        test_choose_and_then_fail()

    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out1 = o.getvalue()
    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out2 = o.getvalue()
    assert out1 == out2
    assert 'Choice #100:' in out1
Esempio n. 5
0
def test_fails_to_draw_from_empty_sequence():
    @given(st.choices())
    def test(choice):
        choice([])

    with pytest.raises(IndexError):
        test()
def test_stability():
    @given(
        st.lists(st.integers(0, 1000), unique=True, min_size=5),
        st.choices(),
    )
    @settings(
        database=ExampleDatabase(),
        max_shrinks=10**6,
        timeout=unlimited,
    )
    def test_choose_and_then_fail(ls, choice):
        for _ in hrange(100):
            choice(ls)
        assert False

    # Run once first for easier debugging
    with raises(AssertionError):
        test_choose_and_then_fail()

    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out1 = o.getvalue()
    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out2 = o.getvalue()
    assert out1 == out2
    assert 'Choice #100:' in out1
Esempio n. 7
0
def test_exhaustion():
    @given(st.lists(st.text(), min_size=10), st.choices())
    def test(ls, choice):
        while ls:
            s = choice(ls)
            assert s in ls
            ls.remove(s)
    test()
Esempio n. 8
0
def test_exhaustion():
    @given(st.lists(st.text(), min_size=10), st.choices())
    def test(ls, choice):
        while ls:
            l = choice(ls)
            assert l in ls
            ls.remove(l)
    test()
 def delete(self, item, choice=st.choices()):
     """
     Remove an item from the sequences.
     """
     assume(item.current_list)
     i = choice(range(len(item.current_list)))
     del item.current_list[i]
     del item.current_evolver[i]
Esempio n. 10
0
class NamespaceListTests(TestCase):
    """
    Tests for ``NamespaceList``.
    """
    @settings(suppress_health_check=[HealthCheck.exception_in_generation])
    @given(collection=namespacelists(), choose=choices())
    def test_remove(self, collection, choose):
        """
        ``NamespaceList.remove`` creates a new ``NamespaceList`` which does not
        have the given item.
        """
        assume(len(collection.items) > 0)
        item = choose(collection.items)
        removed = collection.remove(item)
        self.assertThat(removed.items, Not(Contains(item)))

    @given(collection=namespacelists(), choose=choices())
    def test_item_by_name(self, collection, choose):
        """
        ``NamespaceList.item_by_name`` returns the ``Namespace`` with the matching
        name.
        """
        assume(len(collection.items) > 0)
        for item in collection.items:
            self.expectThat(collection.item_by_name(item.metadata.name),
                            Is(item))

        item = choose(collection.items)
        collection = collection.remove(item)
        self.expectThat(
            lambda: collection.item_by_name(item.metadata.name),
            raises(KeyError(item.metadata.name)),
        )

    @given(collection=namespacelists(), choose=choices())
    def test_no_duplicates(self, collection, choose):
        assume(len(collection.items) > 0)
        self.expectThat(
            lambda: collection.add(choose(collection.items)),
            raises_exception(InvariantException),
        )

    @given(collection=namespacelists())
    def test_constant_attributes(self, collection):
        self.expectThat(collection.kind, Equals(u"NamespaceList"))
        self.expectThat(collection.apiVersion, Equals(u"v1"))
Esempio n. 11
0
 def delete(self, item, choice=st.choices()):
     """
     Remove an item from the sequences.
     """
     assume(item.current_list)
     i = choice(range(len(item.current_list)))
     del item.current_list[i]
     del item.current_evolver[i]
Esempio n. 12
0
def test_stability():
    @given(
        st.lists(st.text(max_size=1), unique=True, min_size=5),
        st.choices(),
    )
    @settings(database=ExampleDatabase())
    def test_choose_and_then_fail(ls, choice):
        for _ in hrange(100):
            choice(ls)
        assert False

    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out1 = o.getvalue()
    with capture_out() as o:
        with raises(AssertionError):
            test_choose_and_then_fail()
    out2 = o.getvalue()
    assert out1 == out2
    assert 'Choice #100:' in out1
Esempio n. 13
0
class ApplyTests(TestCase):
    """
    Tests for `txapply`.
    """
    @given(anything=any_value())
    def test_identity(self, anything):
        """
        ``txapply(identity, deferred)`` is equivalent to ``deferred``.
        """
        d = txapply(identity, succeed(anything))
        self.assertThat(d, succeeded(Is(anything)))

    @given(f=unary_functions, x=integers())
    def test_unary_function(self, f, x):
        """
        ``txapply(f, d)`` is equivalent to ``d.addCallback(f)``.
        """
        d = txapply(f, succeed(x))
        self.assertThat(d, succeeded(Equals(f(x))))

    @given(f=binary_functions, x=integers(), y=integers())
    def test_binary_function(self, f, x, y):
        """
        The function given to ``txapply`` is called with the values from the
        Deferreds it is given.

        ``txapply(f, d1, d2)`` will return a Deferred with the result of
        ``f(x, y)``, where ``x`` is the result of ``d1`` and ``y`` is the
        result of ``d2``.
        """
        assume(f not in (operator.div, operator.mod) or y != 0)
        d = txapply(f, succeed(x), succeed(y))
        self.assertThat(d, succeeded(Equals(f(x, y))))

    @given(args=arguments())
    def test_positional_arguments(self, args):
        """
        The function given to ``txapply`` is called with the values from the
        Deferreds it is given.
        """
        deferred_args = map(succeed, args)
        d = txapply(lambda *a: a, *deferred_args)
        self.assertThat(d, succeeded(Equals(tuple(args))))

    @given(kwargs=keyword_arguments())
    def test_keyword_arguments(self, kwargs):
        """
        The function given to ``txapply`` is called with keyword arguments from
        the values of the Deferreds it is given as keyword arguments.

        That is, keyword arguments are passed through.

        ``txapply(f, foo=d1, bar=d2)`` is equivalent to ``f(foo=x, bar=y)``
        where ``x`` is the result of ``d1`` and ``y`` is the result of ``d2``.

        """
        deferred_kwargs = {
            key: succeed(value)
            for key, value in kwargs.items()
        }
        d = txapply(dict, **deferred_kwargs)
        self.assertThat(d, succeeded(Equals(kwargs)))

    @given(args=arguments(), kwargs=keyword_arguments())
    def test_combination_arguments(self, args, kwargs):
        """
        If ``txapply`` is given a combination of positional and keyword
        arguments, these are passed through to the function.
        """
        deferred_args = map(succeed, args)
        deferred_kwargs = {
            key: succeed(value)
            for key, value in kwargs.items()
        }

        def capture(*a, **kw):
            return a, kw

        d = txapply(capture, *deferred_args, **deferred_kwargs)
        self.assertThat(d, succeeded(Equals((tuple(args), kwargs))))

    @given(args=arguments(), kwargs=keyword_arguments())
    def test_combination_arguments_deferred_function(self, args, kwargs):
        """
        If ``txapply`` is called with a function ``f`` that itself returns a
        ``Deferred`` then the result of that ``Deferred`` is the result of
        calling ``f`` with the results of all of the ``Deferred`` objects
        passed to ``txapply``.
        """
        deferred_args = map(succeed, args)
        deferred_kwargs = {
            key: succeed(value)
            for key, value in kwargs.items()
        }

        def capture(*a, **kw):
            return succeed((a, kw))

        d = txapply(capture, *deferred_args, **deferred_kwargs)
        self.assertThat(d, succeeded(Equals((tuple(args), kwargs))))

    @given(args=arguments(min_size=1),
           exception=exceptions(),
           choice=choices())
    def test_exception_in_args(self, args, exception, choice):
        """
        If one of the arguments is a failing Deferred, then "reraise" that
        failing Deferred.
        """
        i = choice(range(len(args)))
        deferred_args = map(succeed, args)
        deferred_args[i] = maybeDeferred(throw, exception)
        d = txapply(lambda *a: a, *deferred_args)
        self.assertThat(
            d,
            failed(
                AfterPreprocessing(lambda failure: failure.value,
                                   Equals(exception))))
Esempio n. 14
0
from mygrad.tensor_base import Tensor
from mygrad.nnet.layers import RecurrentUnit, dense
from mygrad.nnet.activations import tanh
from mygrad.math import add_sequence

import hypothesis.strategies as st
from hypothesis import given
import hypothesis.extra.numpy as hnp

import numpy as np


@given(st.data(), st.choices())
def test_recurrent(data, choice):
    X = data.draw(
        hnp.arrays(shape=hnp.array_shapes(max_side=3, min_dims=3, max_dims=3),
                   dtype=float,
                   elements=st.floats(-10, 10)))
    T, N, C = X.shape
    D = choice(list(range(1, 5)))

    s0 = data.draw(
        hnp.arrays(shape=(N, D), dtype=float, elements=st.floats(0.0, 0.0)))

    W = data.draw(
        hnp.arrays(shape=(D, D), dtype=float, elements=st.floats(-10.0, 10.0)))

    U = data.draw(
        hnp.arrays(shape=(C, D), dtype=float, elements=st.floats(-10.0, 10.0)))

    V = data.draw(
Esempio n. 15
0
 def steps(self):
     result = tuples(just('extend'), lists(integers()))
     if self.pool:
         result |= tuples(just('choose'), choices())
     return result
Esempio n. 16
0
        fragments2 = copy.copy(fragments)
        head = p.shift(fragments, default)
        assert [head] + fragments == fragments2


@given(text(), booleans(), text(min_size=1))
@example("/foo", True, "0")
def test_destination(filepath, preserve_paths, outdir):
    dest = p.destination(filepath,
                         preserve_paths=preserve_paths,
                         outdir=outdir)
    assert dest.startswith(outdir)
    assert dest.endswith(".html")


@given(choices(), text())
def test_parse(choice, source):
    l = get_language(choice)
    parsed = p.parse(source, l)
    for s in parsed:
        assert {"code_text", "docs_text"} == set(s.keys())


def test_skip_coding_directive():
    source = "# -*- coding: utf-8 -*-\n" + FOO_FUNCTION
    parsed = p.parse(source, PYTHON)
    for section in parsed:
        assert "coding" not in section['code_text']


def test_multi_line_leading_spaces():
Esempio n. 17
0
class GridRouterStateMachine(RuleBasedStateMachine):
    def __init__(self, case):
        super(GridRouterStateMachine, self).__init__()

        self.case = case

        self.network = MemoryReactor()
        self.clock = Clock()
        self.reactor = FakeReactor(self.network, self.clock)
        self.kubernetes = memory_kubernetes()
        self.client = self.case.successResultOf(
            self.kubernetes.versioned_client())
        self.model = self.client.model

        self.deploy_config = NullDeploymentConfiguration()
        # Set a few dummy values that we know create_deployment requires.
        self.deploy_config.kubernetes_namespace = u"testing"
        self.deploy_config.introducer_image = u"example-invalid/tahoe-introducer"
        self.deploy_config.storageserver_image = u"example-invalid/tahoe-storageserver"

        self.used_tubs = set()
        self.pods = {}
        # Keep deployments alive so they can provide a unique identifier for
        # pod naming.
        self.deployments = []

        self.interval = 1.0
        options = Options()
        self.case.patch(
            options,
            "get_kubernetes_service",
            lambda reactor: self.kubernetes,
        )

        options.parseOptions([
            b"--interval",
            u"{}".format(self.interval).encode("ascii"),
            b"--kubernetes-namespace",
            self.deploy_config.kubernetes_namespace.encode("ascii"),
            b"--k8s-service-account",
            b"--kubernetes",
            b"http://127.0.0.1:1234/",
        ])
        self.service = makeService(options, self.reactor)

    @rule()
    def start(self):
        """
        The ``GridRouter`` service starts up.
        """
        assume(not self.service.running)
        self.service.privilegedStartService()
        self.service.startService()
        self.case.addCleanup(self.service.stopService)

    @rule(
        ip=ipv4_addresses(),
        storage_pem=node_pems(),
        storage_port=port_numbers(),
        intro_pem=node_pems(),
        intro_port=port_numbers(),
    )
    def create_pod(self, ip, storage_pem, storage_port, intro_pem, intro_port):
        """
        A new customer grid pod shows up, as would happen if a new user just
        signed up and got provisioned.
        """
        assume(storage_pem != intro_pem
               and Tub(storage_pem).getTubID() not in self.used_tubs
               and Tub(intro_pem).getTubID() not in self.used_tubs)
        details = SubscriptionDetails(
            bucketname=u"foo",
            # Set the node secrets.  From these, tub identifiers can be
            # derived.
            oldsecrets={
                # Storage server.
                u"server_node_pem": storage_pem,
                u"introducer_node_pem": intro_pem,
            },
            customer_email=u"foo",
            customer_pgpinfo=u"foo",
            product_id=u"foo",
            customer_id=u"foo",
            subscription_id=u"foo",
            introducer_port_number=intro_port,
            storage_port_number=storage_port,
        )
        deployment = create_deployment(
            self.deploy_config,
            details,
            self.model,
        )
        self.deployments.append(deployment)
        pod = derive_pod(self.model, deployment, ip)
        self.case.successResultOf(self.client.create(pod))

        self.pods[pod] = (ip, storage_pem, storage_port, intro_pem, intro_port)
        self.used_tubs.update({
            Tub(storage_pem).getTubID(),
            Tub(intro_pem).getTubID(),
        })

    @rule(choose=choices())
    def remove_pod(self, choose):
        """
        An existing customer grid pod goes away, as would happen if a user
        cancelled their subscription.
        """
        assume(0 < len(self.pods))
        pod, values = choose(sorted(self.pods.items()))
        _, storage_pem, _, intro_pem, _ = values
        del self.pods[pod]
        self.used_tubs.difference_update({
            Tub(storage_pem).getTubID(),
            Tub(intro_pem).getTubID(),
        })
        self.case.successResultOf(self.client.delete(pod))

    @rule()
    def check(self):
        """
        Examine the routing configuration of the ``GridRouter`` and fail if it
        diverges from what's expected given the pods which currently exist.
        """
        assume(self.service.running)

        # Advance the clock to make sure the router has had a chance to look
        # at the current state.
        self.clock.advance(self.interval)

        # GridRouter ought to have a mapping from the active subscription tub
        # identifiers to the internal addresses that own those tubs.
        mapping = self.service.route_mapping()

        expected = {}
        for pod, values in self.pods.iteritems():
            (ip, storage_pem, storage_port, intro_pem, intro_port) = values
            expected[Tub(storage_pem).getTubID()] = (ip, storage_port)
            expected[Tub(intro_pem).getTubID()] = (ip, intro_port)

        self.case.assertThat(
            mapping,
            AfterPreprocessing(
                lambda m: {
                    tub_id: address
                    for (tub_id, (pod, address)) in m.iteritems()
                },
                Equals(expected),
            ),
        )
Esempio n. 18
0
class TaggedUnionInvariantTests(TestCase):
    """
    Tests for ``TaggedUnionInvariant``.
    """
    @given(
        args=ALGEBRAIC_TYPE_ARGUMENTS_STRATEGY, )
    @example(args={'state': States.ALLOWED, 'extra': False})
    @example(args={'state': States.WITH_ATTRIBUTE, 'extra': True, 'one': True})
    @example(
        args={
            'state': States.WITH_TWO_ATTRIBUTES,
            'extra': True,
            'one': True,
            'two': False
        })
    def test_valid_strategy(self, args):
        """
        When a valid dictionary of attributes is provided, an
        instance of ``AlgebraicType`` is provided.
        """
        self.assertIsInstance(AlgebraicType(**args), AlgebraicType)

    @given(args=ALGEBRAIC_TYPE_ARGUMENTS_STRATEGY,
           choice=st.choices(),
           extra_value=st.booleans())
    @example(
        args={
            'state': States.ALLOWED,
            'extra': False
        },
        # The argument to add.
        choice=lambda _: 'one',
        extra_value=True,
    )
    @example(
        args={
            'state': States.WITH_ATTRIBUTE,
            'extra': False,
            'one': True
        },
        # The argument to add.
        choice=lambda _: 'two',
        extra_value=True,
    )
    def test_extra_attributes(self, args, choice, extra_value):
        """
        When an extra attribute that isn't allowed in a given state
        is provided, ``InvariantException`` is raised.

        :param choice: A choice function
        :param extra_value: A value to provide to the exta attribute
        """
        state = args['state']
        invariant = AlgebraicType.__invariant__
        extra_attributes = (invariant._all_attributes -
                            invariant.attributes_for_tag[state])
        assume(extra_attributes)
        extra_attribute = choice(sorted(extra_attributes))

        # Add the extra attribute to the arguments.
        args[extra_attribute] = extra_value

        exc = self.assertRaises(InvariantException, AlgebraicType, **args)
        self.assertIn(
            "can't be specified in state",
            exc.invariant_errors[0],
        )

    @given(
        args=ALGEBRAIC_TYPE_ARGUMENTS_STRATEGY,
        choice=st.choices(),
    )
    @example(
        args={
            'state': States.WITH_ATTRIBUTE,
            'extra': False,
            'one': True
        },
        # The argument to remove
        choice=lambda _: 'one',
    )
    @example(
        args={
            'state': States.WITH_TWO_ATTRIBUTES,
            'extra': False,
            'one': True,
            'two': False
        },
        # The argument to remove
        choice=lambda _: 'one',
    )
    @example(
        args={
            'state': States.WITH_TWO_ATTRIBUTES,
            'extra': False,
            'one': True,
            'two': False
        },
        # The argument to remove
        choice=lambda _: 'two',
    )
    def test_missing_attributes(self, args, choice):
        """
        When an attribute required in a given state isn't provided,
        ``InvariantException`` is raised.

        :param args: A valid dict of attributes for ``AlgebraicType``.
        :param choice: A choice function
        """
        state = args['state']
        invariant = AlgebraicType.__invariant__
        # The required attributes of the current state.
        required_attributes = invariant.attributes_for_tag[state]
        assume(required_attributes)
        removed_attribute = choice(sorted(required_attributes))

        # Remove a required attribute.
        del args[removed_attribute]

        exc = self.assertRaises(InvariantException, AlgebraicType, **args)
        self.assertIn(
            'must be specified in state',
            exc.invariant_errors[0],
        )

    @given(
        state=st.sampled_from(
            pset(States.iterconstants()) -
            AlgebraicType.__invariant__._allowed_tags),
        extra_value=st.booleans(),
    )
    @example(
        state=States.DISALLOWED,
        extra_value=False,
    )
    def test_invalid_states(self, state, extra_value):
        """
        When constructed with a state that isn't allowed,
        `InvariantException` is raised.

        :param state: A state which isn't a valid state for
            ``AlgebraicType``.
        """
        args = {'state': state, 'extra': extra_value}

        exc = self.assertRaises(InvariantException, AlgebraicType, **args)

        self.assertIn(
            'can only be in states',
            exc.invariant_errors[0],
        )
Esempio n. 19
0
def test_can_use_a_choice_function_after_find():
    c = find(st.choices(), lambda c: True)
    ls = [1, 2, 3]
    assert c(ls) in ls
Esempio n. 20
0
class MALTest(TestCase):
    @staticmethod
    def read_fixture(filename):
        with open(os.path.join(settings.TEST_DATA_DIR, filename), 'r') as f:
            return f.read()

    def setUp(self):
        self.mal = MALClient('test_client', 'test_client')
        self.search_fixture = self.read_fixture('code_geass_mal_search.xml')
        self.list_fixture = self.read_fixture('raitobezarius_mal.xml')
        self.user = User.objects.create(username='******')

    @given(choice=st.choices(), query=st.text())
    @responses.activate
    def test_mal_client_exceptions(self, choice, query):
        work_type = MALWorks(choice(list(map(lambda x: x.value, MALWorks))))
        catch_all = re.compile('https?://myanimelist\.net/api/.*')
        for status_code in [400, 401, 403, 500, 502]:
            with self.subTest(
                    "Testing {} status code through MAL API search wrapper".
                    format(status_code),
                    status_code=status_code):
                responses.add(responses.GET, catch_all, status=status_code)
                with self.assertRaisesRegex(
                        RuntimeError,
                        r'(Invalid MAL credentials!)|(MAL request failure!)'):
                    self.mal.search_works(work_type, query)

    @responses.activate
    def test_mal_search_one_work(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/api/.*/search.xml\?q=.*'),
            body=self.search_fixture,
            status=200,
            content_type='application/xml')
        anime_query = 'code geass'
        result = self.mal.search_work(MALWorks.animes, anime_query)
        self.assertEqual(result.work_type, MALWorks.animes)
        self.assertEqual(len(responses.calls), 1)

        # FIXME: rather clunky, we should move this into another test.
        # We should be able to be test-data-agnostic.
        self.assertNotEqual(result.start_date, None)
        self.assertEqual(result.synonyms, [])
        self.assertEqual(result.nb_episodes, 25)
        self.assertNotEqual(result.poster, None)
        self.assertEqual(result.title, 'Code Geass: Hangyaku no Lelouch')
        self.assertEqual(result.english_title,
                         'Code Geass: Lelouch of the Rebellion')
        self.assertNotEqual(result.source_url, None)
        self.assertNotEqual(result.mal_id, None)

    @responses.activate
    def test_mal_list_works_from_a_user(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/malappinfo.php\?.*'),
            body=self.list_fixture,
            status=200,
            content_type='application/xml')
        results = list(
            self.mal.list_works_from_a_user(MALWorks.animes, 'raitobezarius'))

        self.assertNotEqual(len(results), 0)
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_mal_malformed_xml(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/malappinfo.php\?.*'),
            body='<xml><myinfo>42</myinfo><anime></anime></xml>',
            status=200,
            content_type='application/xml')

        from mangaki.utils.mal import logger as mal_logger
        with self.assertLogs(logger=mal_logger, level='ERROR'):
            results = list(
                self.mal.list_works_from_a_user(MALWorks.animes,
                                                'raitobezarius'))
            self.assertEqual(len(results), 0)

    @patch('mangaki.utils.mal.import_mal')
    @patch('redis.StrictRedis', autospec=True, create=True)
    def test_mal_task_cleanup(self, strict_redis, import_mal_operation):
        tasks.import_mal.push_request(id=1)

        with self.subTest(
                'When the import succeeds, there is no background task anymore, nor Redis task details.'
        ):
            tasks.import_mal.run('RaitoBezarius', self.user.username)
            r = strict_redis.return_value
            self.assertTrue(r.delete.called)
            self.assertFalse(self.user.background_tasks.exists())

        with self.subTest(
                'When the import fails, there is no background task anymore, nor Redis task details.'
        ):
            import_mal_operation.side_effect = Exception('Boom !')
            with self.assertRaises(Exception):
                tasks.import_mal.run('RaitoBezarius', self.user.username)

            r = strict_redis.return_value
            self.assertTrue(r.delete.called)
            self.assertFalse(self.user.background_tasks.exists())

    @patch('redis.StrictRedis', autospec=True, create=True)
    @patch('mangaki.utils.mal.import_mal')
    def test_mal_task_multiple_start(self, import_mal_operation, strict_redis):
        bg_task, created = UserBackgroundTask.objects.get_or_create(
            owner=self.user, tag=tasks.MAL_IMPORT_TAG)

        self.assertTrue(created)

        tasks.import_mal('RaitoBezarius', self.user.username)
        r = strict_redis.return_value
        bg_task.delete()

        self.assertFalse(r.set.called)
        import_mal_operation.assert_not_called()
Esempio n. 21
0
import hypothesis.strategies as st
from hypothesis import find, given
from hypothesis.errors import InvalidArgument


def test_exhaustion():
    @given(st.lists(st.text(), min_size=10), st.choices())
    def test(ls, choice):
        while ls:
            l = choice(ls)
            assert l in ls
            ls.remove(l)
    test()


@given(st.choices(), st.choices())
def test_choice_is_shared(choice1, choice2):
    assert choice1 is choice2


def test_cannot_use_choices_within_find():
    with pytest.raises(InvalidArgument):
        find(st.choices(), lambda c: True)


def test_fails_to_draw_from_empty_sequence():
    @given(st.choices())
    def test(choice):
        choice([])

    with pytest.raises(IndexError):
Esempio n. 22
0
def test_can_use_a_choice_function_after_find():
    c = find(st.choices(), lambda c: True)
    ls = [1, 2, 3]
    assert c(ls) in ls
Esempio n. 23
0
class MALTest(TestCase):
    @staticmethod
    def read_fixture(filename):
        with open(os.path.join(settings.TEST_DATA_DIR, filename), 'r') as f:
            return f.read()

    def setUp(self):
        self.mal = MALClient('test_client', 'test_client')
        self.search_fixture = self.read_fixture('mal/code_geass_search.xml')
        self.list_fixture = self.read_fixture('mal/raitobezarius_mal.xml')

        self.steins_gate_xml = ET.fromstring(
            self.read_fixture('mal/steins_gate_entry.xml'))
        self.steins_gate_zero_xml = ET.fromstring(
            self.read_fixture('mal/steins_gate_zero_entry.xml'))
        self.steins_gate_movie_xml = ET.fromstring(
            self.read_fixture('mal/steins_gate_movie_entry.xml'))
        self.darling_in_the_franxx_xml = ET.fromstring(
            self.read_fixture('mal/darling_in_the_franxx_entry.xml'))

        self.user, _ = User.objects.get_or_create(username='******')

    @given(choice=st.choices(), query=st.text())
    @responses.activate
    def test_mal_client_exceptions(self, choice, query):
        work_type = MALWorks(choice(list(map(lambda x: x.value, MALWorks))))
        catch_all = re.compile('https?://myanimelist\.net/api/.*')
        for status_code in [400, 401, 403, 500, 502]:
            with self.subTest(
                    "Testing {} status code through MAL API search wrapper".
                    format(status_code),
                    status_code=status_code):
                responses.add(responses.GET, catch_all, status=status_code)
                with self.assertRaisesRegex(
                        RuntimeError,
                        r'(Invalid MAL credentials!)|(MAL request failure!)'):
                    self.mal.search_works(work_type, query)

    @responses.activate
    def test_mal_search_one_work(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/api/.*/search.xml\?q=.*'),
            body=self.search_fixture,
            status=200,
            content_type='application/xml')
        anime_query = 'code geass'
        result = self.mal.search_work(MALWorks.animes, anime_query)
        self.assertEqual(result.work_type, MALWorks.animes)
        self.assertEqual(len(responses.calls), 1)

        # FIXME: rather clunky, we should move this into another test.
        # We should be able to be test-data-agnostic.
        self.assertNotEqual(result.start_date, None)
        self.assertEqual(result.synonyms, [])
        self.assertEqual(result.nb_episodes, 25)
        self.assertNotEqual(result.poster, None)
        self.assertEqual(result.title, 'Code Geass: Hangyaku no Lelouch')
        self.assertEqual(result.english_title,
                         'Code Geass: Lelouch of the Rebellion')
        self.assertNotEqual(result.source_url, None)
        self.assertNotEqual(result.mal_id, None)

    @responses.activate
    def test_mal_list_works_from_a_user(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/malappinfo.php\?.*'),
            body=self.list_fixture,
            status=200,
            content_type='application/xml')
        results = list(
            self.mal.list_works_from_a_user(MALWorks.animes, 'raitobezarius'))

        self.assertNotEqual(len(results), 0)
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_mal_malformed_xml(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/malappinfo.php\?.*'),
            body='<xml><myinfo>42</myinfo><anime></anime></xml>',
            status=200,
            content_type='application/xml')

        from mangaki.utils.mal import logger as mal_logger
        with self.assertLogs(logger=mal_logger, level='ERROR'):
            results = list(
                self.mal.list_works_from_a_user(MALWorks.animes,
                                                'raitobezarius'))
            self.assertEqual(len(results), 0)

    @patch('mangaki.utils.mal.client', autospec=True, create=True)
    @given(st.randoms())
    def test_mal_duplication(self, client_mock, rand):
        from mangaki.utils.mal import import_mal
        # prepare list of animes
        steins_gate_entry = MALEntry(self.steins_gate_xml, MALWorks.animes)
        darling_entry = MALEntry(self.darling_in_the_franxx_xml,
                                 MALWorks.animes)
        steins_gate_movie_entry = MALEntry(self.steins_gate_movie_xml,
                                           MALWorks.animes)
        steins_gate_zero_entry = MALEntry(self.steins_gate_zero_xml,
                                          MALWorks.animes)

        mal_user_works = [
            MALUserWork(steins_gate_entry.title, steins_gate_entry.synonyms,
                        'mal_something', str(steins_gate_entry.mal_id), 10, 2),
            MALUserWork(darling_entry.title, darling_entry.synonyms,
                        'zero_two', str(steins_gate_entry.mal_id), 10, 1),
            MALUserWork(steins_gate_movie_entry.title,
                        steins_gate_movie_entry.synonyms, 'non_canon',
                        str(steins_gate_movie_entry.mal_id), 5, 2),
            MALUserWork(steins_gate_zero_entry.title,
                        steins_gate_zero_entry.synonyms,
                        'brain_science_institute',
                        str(steins_gate_zero_entry.mal_id), 10, 1)
        ]

        search_results = {
            steins_gate_entry.title: [
                steins_gate_movie_entry, steins_gate_entry,
                steins_gate_zero_entry
            ],
            darling_entry.title: [darling_entry],
            steins_gate_zero_entry.title:
            [steins_gate_zero_entry, steins_gate_movie_entry],
            steins_gate_movie_entry.title: [steins_gate_movie_entry]
        }

        # Here, we shuffle lists. using Hypothesis' controlled Random instance.
        rand.shuffle(search_results[steins_gate_entry.title])
        rand.shuffle(search_results[steins_gate_zero_entry.title])

        client_mock.list_works_from_a_user.return_value = (
            item for item in mal_user_works)
        client_mock.search_works.side_effect = lambda _, query: search_results.get(
            query, [])

        import_mal(self.user.username, self.user.username)
        n_works = Work.objects.count()
        expected = len(mal_user_works)

        # Assumption: all users' works were imported.
        self.assertEqual(n_works, expected)

        # Kill the WorkTitle. Remove evidences.
        WorkTitle.objects.all().delete()

        for _ in range(3):
            # Reset mocks.
            client_mock.list_works_from_a_user.return_value = (
                item for item in mal_user_works)
            client_mock.search_works.side_effect = lambda _, query: search_results.get(
                query, [])

            import_mal(self.user.username, self.user.username)

        # Assumption: no duplicates.
        self.assertEqual(n_works, Work.objects.count())

    @patch('mangaki.utils.mal.import_mal')
    @patch('redis.StrictRedis', autospec=True, create=True)
    def test_mal_task_cleanup(self, strict_redis, import_mal_operation):
        tasks.import_mal.push_request(id=1)

        with self.subTest(
                'When the import succeeds, there is no background task anymore, nor Redis task details.'
        ):
            tasks.import_mal.run('RaitoBezarius', self.user.username)
            r = strict_redis.return_value
            self.assertTrue(r.delete.called)
            self.assertFalse(self.user.background_tasks.exists())

        with self.subTest(
                'When the import fails, there is no background task anymore, nor Redis task details.'
        ):
            import_mal_operation.side_effect = Exception('Boom !')
            with self.assertRaises(Exception):
                tasks.import_mal.run('RaitoBezarius', self.user.username)

            r = strict_redis.return_value
            self.assertTrue(r.delete.called)
            self.assertFalse(self.user.background_tasks.exists())

    @patch('redis.StrictRedis', autospec=True, create=True)
    @patch('mangaki.utils.mal.import_mal')
    def test_mal_task_multiple_start(self, import_mal_operation, strict_redis):
        bg_task, created = UserBackgroundTask.objects.get_or_create(
            owner=self.user, tag=tasks.MAL_IMPORT_TAG)

        self.assertTrue(created)

        tasks.import_mal('RaitoBezarius', self.user.username)
        r = strict_redis.return_value
        bg_task.delete()

        self.assertFalse(r.set.called)
        import_mal_operation.assert_not_called()
Esempio n. 24
0
        return "hi"

    class Foo(object):
        pass

    class Bar(object):
        pass

    converter.register_structure_hook_func(can_handle, handle)

    assert converter.structure(10, Foo) == "hi"
    with raises(ValueError):
        converter.structure(10, Bar)


@given(choices(), enums_of_primitives())
def test_structuring_enums(converter, choice, enum):
    # type: (Converter, Any, Any) -> None
    """Test structuring enums by their values."""
    val = choice(list(enum))

    assert converter.structure(val.value, enum) == val


def test_structuring_unsupported(converter):
    # type: (Converter) -> None
    """Loading unsupported classes should throw."""
    with raises(ValueError):
        converter.structure(1, Converter)
    with raises(ValueError):
        converter.structure(1, Union[int, unicode])
Esempio n. 25
0
def test_cannot_use_choices_within_find():
    with pytest.raises(InvalidArgument):
        find(st.choices(), lambda c: True)
Esempio n. 26
0
def test_choice_is_shared():
    @given(st.choices(), st.choices())
    def test(choice1, choice2):
        assert choice1 is choice2
    test()
Esempio n. 27
0
from hypothesis.database import ExampleDatabase
from hypothesis.internal.compat import hrange


def test_exhaustion():
    @given(st.lists(st.text(), min_size=10), st.choices())
    def test(ls, choice):
        while ls:
            l = choice(ls)
            assert l in ls
            ls.remove(l)

    test()


@given(st.choices(), st.choices())
def test_choice_is_shared(choice1, choice2):
    assert choice1 is choice2


def test_stability():
    @given(
        st.lists(st.text(max_size=1), unique=True, min_size=5),
        st.choices(),
    )
    @settings(database=ExampleDatabase())
    def test_choose_and_then_fail(ls, choice):
        for _ in hrange(100):
            choice(ls)
        assert False
Esempio n. 28
0
class MALTest(TestCase):
    @staticmethod
    def read_fixture(filename):
        with open(os.path.join(settings.TEST_DATA_DIR, filename), 'r') as f:
            return f.read()

    def setUp(self):
        self.mal = MALClient('test_client', 'test_client')
        self.search_fixture = self.read_fixture('code_geass_mal_search.xml')
        self.list_fixture = self.read_fixture('raitobezarius_mal.xml')

    @given(choice=st.choices(), query=st.text())
    @responses.activate
    def test_mal_client_exceptions(self, choice, query):
        work_type = MALWorks(choice(list(map(lambda x: x.value, MALWorks))))
        catch_all = re.compile('https?://myanimelist\.net/api/.*')
        for status_code in [400, 401, 403, 500, 502]:
            with self.subTest(
                    "Testing {} status code through MAL API search wrapper".
                    format(status_code),
                    status_code=status_code):
                responses.add(responses.GET, catch_all, status=status_code)
                with self.assertRaisesRegex(
                        RuntimeError,
                        r'(Invalid MAL credentials!)|(MAL request failure!)'):
                    self.mal.search_works(work_type, query)

    @responses.activate
    def test_mal_search_one_work(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/api/.*/search.xml\?q=.*'),
            body=self.search_fixture,
            status=200,
            content_type='application/xml')
        anime_query = 'code geass'
        result = self.mal.search_work(MALWorks.animes, anime_query)
        self.assertEqual(result.work_type, MALWorks.animes)
        self.assertEqual(len(responses.calls), 1)

        # FIXME: rather clunky, we should move this into another test.
        # We should be able to be test-data-agnostic.
        self.assertNotEqual(result.start_date, None)
        self.assertEqual(result.synonyms, [])
        self.assertEqual(result.nb_episodes, 25)
        self.assertNotEqual(result.poster, None)
        self.assertEqual(result.title, 'Code Geass: Hangyaku no Lelouch')
        self.assertEqual(result.english_title,
                         'Code Geass: Lelouch of the Rebellion')
        self.assertNotEqual(result.source_url, None)
        self.assertNotEqual(result.mal_id, None)

    @responses.activate
    def test_mal_list_works_from_a_user(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/malappinfo.php\?.*'),
            body=self.list_fixture,
            status=200,
            content_type='application/xml')
        results = list(
            self.mal.list_works_from_a_user(MALWorks.animes, 'raitobezarius'))

        self.assertNotEqual(len(results), 0)
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_mal_malformed_xml(self):
        responses.add(
            responses.GET,
            re.compile('https?://myanimelist\.net/malappinfo.php\?.*'),
            body='<xml><myinfo>42</myinfo><anime></anime></xml>',
            status=200,
            content_type='application/xml')

        with self.assertLogs(level='ERROR'):
            results = list(
                self.mal.list_works_from_a_user(MALWorks.animes,
                                                'raitobezarius'))
            self.assertEqual(len(results), 0)
Esempio n. 29
0
def test_choice_is_shared():
    @given(st.choices(), st.choices())
    def test(choice1, choice2):
        assert choice1 is choice2

    test()
Esempio n. 30
0
    assert type(dumped) is type(seq)


@given(dicts_of_primitives, unstruct_strats)
def test_mapping_unstructure(converter, map_and_type, dump_strat):
    # type: (Converter, Any, UnstructureStrategy) -> None
    """Dumping a mapping of primitives is a simple copy operation."""
    converter.unstruct_strat = dump_strat
    mapping = map_and_type[0]
    dumped = converter.unstructure(mapping)
    assert dumped == mapping
    assert dumped is not mapping
    assert type(dumped) is type(mapping)


@given(enums_of_primitives(), unstruct_strats, choices())
def test_enum_unstructure(converter, enum, dump_strat, choice):
    # type: (Converter, EnumMeta, UnstructureStrategy) -> None
    """Dumping enums of primitives converts them to their primitives."""
    converter.unstruct_strat = dump_strat

    member = choice(list(enum.__members__.values()))

    assert converter.unstructure(member) == member.value


@given(nested_classes)
def test_attrs_asdict_unstructure(converter, nested_class):
    # type: (Converter, Type) -> None
    """Our dumping should be identical to `attrs`."""
    instance = nested_class[0]()
Esempio n. 31
0
def test_choice_raises_index_error_on_empty():
    c = find(st.choices(), lambda c: True)
    with raises(IndexError):
        c([])
Esempio n. 32
0
def test_cannot_use_choices_within_find():
    with pytest.raises(InvalidArgument):
        find(st.choices(), lambda c: True)
Esempio n. 33
0
 def steps(self):
     result = tuples(just('extend'), lists(integers()))
     if self.pool:
         result |= tuples(just('choose'), choices())
     return result
Esempio n. 34
0
class PVectorEvolverBuilder(RuleBasedStateMachine):
    """
    Build a list and matching pvector evolver step-by-step.

    In each step in the state machine we do same operation on a list and
    on a pvector evolver, and then when we're done we compare the two.
    """
    sequences = Bundle("evolver_sequences")

    @rule(target=sequences, start=PVectorAndLists)
    def initial_value(self, start):
        """
        Some initial values generated by a hypothesis strategy.
        """
        l, pv = start
        return EvolverItem(original_list=l,
                           original_pvector=pv,
                           current_list=l[:],
                           current_evolver=pv.evolver())

    @rule(item=sequences)
    def append(self, item):
        """
        Append an item to the pair of sequences.
        """
        obj = TestObject()
        item.current_list.append(obj)
        item.current_evolver.append(obj)

    @rule(start=sequences, end=sequences)
    def extend(self, start, end):
        """
        Extend a pair of sequences with another pair of sequences.
        """
        # compare() has O(N**2) behavior, so don't want too-large lists:
        assume(len(start.current_list) + len(end.current_list) < 50)
        start.current_evolver.extend(end.current_list)
        start.current_list.extend(end.current_list)

    @rule(item=sequences, choice=st.choices())
    def delete(self, item, choice):
        """
        Remove an item from the sequences.
        """
        assume(item.current_list)
        i = choice(range(len(item.current_list)))
        del item.current_list[i]
        del item.current_evolver[i]

    @rule(item=sequences, choice=st.choices())
    def setitem(self, item, choice):
        """
        Overwrite an item in the sequence using ``__setitem__``.
        """
        assume(item.current_list)
        i = choice(range(len(item.current_list)))
        obj = TestObject()
        item.current_list[i] = obj
        item.current_evolver[i] = obj

    @rule(item=sequences, choice=st.choices())
    def set(self, item, choice):
        """
        Overwrite an item in the sequence using ``set``.
        """
        assume(item.current_list)
        i = choice(range(len(item.current_list)))
        obj = TestObject()
        item.current_list[i] = obj
        item.current_evolver.set(i, obj)

    @rule(item=sequences)
    def compare(self, item):
        """
        The list and pvector evolver must match.
        """
        item.current_evolver.is_dirty()
        # compare() has O(N**2) behavior, so don't want too-large lists:
        assume(len(item.current_list) < 50)
        # original object unmodified
        assert item.original_list == item.original_pvector
        # evolver matches:
        for i in range(len(item.current_evolver)):
            assert item.current_list[i] == item.current_evolver[i]
        # persistent version matches
        assert_equal(item.current_list, item.current_evolver.persistent())
        # original object still unmodified
        assert item.original_list == item.original_pvector
Esempio n. 35
0
        assert p.shift(fragments, default) == default
    else:
        fragments2 = copy.copy(fragments)
        head = p.shift(fragments, default)
        assert [head] + fragments == fragments2


@given(text(), booleans(), text(min_size=1))
@example("/foo", True, "0")
def test_destination(filepath, preserve_paths, outdir):
    dest = p.destination(filepath, preserve_paths=preserve_paths, outdir=outdir)
    assert dest.startswith(outdir)
    assert dest.endswith(".html")


@given(choices(), text())
def test_parse(choice, source):
    l = get_language(choice)
    parsed = p.parse(source, l)
    for s in parsed:
        assert {"code_text", "docs_text"} == set(s.keys())


def test_skip_coding_directive():
    source = "# -*- coding: utf-8 -*-\n" + FOO_FUNCTION
    parsed = p.parse(source, PYTHON)
    for section in parsed:
        assert "coding" not in section['code_text']


def test_multi_line_leading_spaces():
Esempio n. 36
0
        # [-3, -4]

        # stride = [1, 2]
        # pad = [0, 0]
        x = np.arange(1, 13).reshape(1, 1, 3, 4)
        k = -1 * np.arange(1, 5).reshape(1, 1, 2, 2)

        o = conv2d(Tensor(x), k, [1, 2], 0, memory_constrained=mem_constr)

        out = np.array([[[[-44., -64.], [-84., -104.]]]])
        assert isinstance(
            o, Tensor) and not o.constant and not o.scalar_only and np.all(
                o.data == out)


@given(st.data(), st.booleans(), st.choices(), st.choices(), st.choices())
def test_conv2d(data, mem, choice_1, choice_2, choice_3):

    f = choice_1([1, 2, 3])
    c = choice_2([1, 2])

    #w, pad, stride
    ws, pad, stride = choice_3([(1, 0, 4), (1, 0, 1), (3, 1, 2), (5, 0, 1)])

    dat = data.draw(
        hnp.arrays(shape=(2, c, 5, 5), dtype=float, elements=st.floats(1,
                                                                       100)))

    w_dat = data.draw(
        hnp.arrays(shape=(f, c, ws, ws),
                   dtype=float,
Esempio n. 37
0
class PVectorBuilder(RuleBasedStateMachine):
    """
    Build a list and matching pvector step-by-step.

    In each step in the state machine we do same operation on a list and
    on a pvector, and then when we're done we compare the two.
    """
    sequences = Bundle("sequences")

    @rule(target=sequences, start=PVectorAndLists)
    def initial_value(self, start):
        """
        Some initial values generated by a hypothesis strategy.
        """
        return start

    @rule(target=sequences, former=sequences)
    @verify_inputs_unmodified
    def append(self, former):
        """
        Append an item to the pair of sequences.
        """
        l, pv = former
        obj = TestObject()
        l2 = l[:]
        l2.append(obj)
        return l2, pv.append(obj)

    @rule(target=sequences, start=sequences, end=sequences)
    @verify_inputs_unmodified
    def extend(self, start, end):
        """
        Extend a pair of sequences with another pair of sequences.
        """
        l, pv = start
        l2, pv2 = end
        # compare() has O(N**2) behavior, so don't want too-large lists:
        assume(len(l) + len(l2) < 50)
        l3 = l[:]
        l3.extend(l2)
        return l3, pv.extend(pv2)

    @rule(target=sequences, former=sequences, choice=st.choices())
    @verify_inputs_unmodified
    def remove(self, former, choice):
        """
        Remove an item from the sequences.
        """
        l, pv = former
        assume(l)
        l2 = l[:]
        i = choice(range(len(l)))
        del l2[i]
        return l2, pv.delete(i)

    @rule(target=sequences, former=sequences, choice=st.choices())
    @verify_inputs_unmodified
    def set(self, former, choice):
        """
        Overwrite an item in the sequence.
        """
        l, pv = former
        assume(l)
        l2 = l[:]
        i = choice(range(len(l)))
        obj = TestObject()
        l2[i] = obj
        return l2, pv.set(i, obj)

    @rule(target=sequences, former=sequences, choice=st.choices())
    @verify_inputs_unmodified
    def transform_set(self, former, choice):
        """
        Transform the sequence by setting value.
        """
        l, pv = former
        assume(l)
        l2 = l[:]
        i = choice(range(len(l)))
        obj = TestObject()
        l2[i] = obj
        return l2, pv.transform([i], obj)

    @rule(target=sequences, former=sequences, choice=st.choices())
    @verify_inputs_unmodified
    def transform_discard(self, former, choice):
        """
        Transform the sequence by discarding a value.
        """
        l, pv = former
        assume(l)
        l2 = l[:]
        i = choice(range(len(l)))
        del l2[i]
        return l2, pv.transform([i], discard)

    @rule(target=sequences, former=sequences, choice=st.choices())
    @verify_inputs_unmodified
    def subset(self, former, choice):
        """
        A subset of the previous sequence.
        """
        l, pv = former
        assume(l)
        i = choice(range(len(l)))
        j = choice(range(len(l)))
        return l[i:j], pv[i:j]

    @rule(pair=sequences)
    @verify_inputs_unmodified
    def compare(self, pair):
        """
        The list and pvector must match.
        """
        l, pv = pair
        # compare() has O(N**2) behavior, so don't want too-large lists:
        assume(len(l) < 50)
        assert_equal(l, pv)
Esempio n. 38
0
 def steps(self):
     return choices()
Esempio n. 39
0
 def steps(self):
     return choices()
Esempio n. 40
0
def test_choice_raises_index_error_on_empty():
    c = find(st.choices(), lambda c: True)
    with raises(IndexError):
        c([])
Esempio n. 41
0
 def execute_step(self, choices):
     choices([1, 2, 3])
Esempio n. 42
0
def test_bad_examples_found_by_hypothesis(actions, ints):
    def repeat_choices(l):
        def inner(gs):
            assert l, 'There are no more selects expected'
            next_result = l.pop(0)
            assert next_result in gs, 'Goal with id %d should be selected, but existing goals are %s' % (
                next_result, gs)
            return next_result

        return inner

    g = build_from(actions, repeat_choices(ints), show_notes=False)
    assert g.verify()


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


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


@given(user_actions(min_size=15, skip=['rename']),
       user_actions(min_size=1, skip=['select']), choices(), choices())
Esempio n. 43
0
 def execute_step(self, choices):
     choices([1, 2, 3])
Esempio n. 44
0
    @precondition(lambda self: self.count > 0)
    def return_buf(self, rnd):  # Ignore PyLintBear (W0613)
        """Return a buffer."""
        random.shuffle(self.buffers)
        buf = self.buffers.pop()
        assert buf.used == 1
        lib.test_ch_bf_return(self.pool, buf)
        assert buf.used == 0
        self.count -= 1


TestPool = PoolMachine.TestCase


@given(
    st.choices(),
    st.binary(16, 16, 16),
    st.integers(1025, 2**16),
    st.binary(16, 16, 16),
    st.integers(1025, 2**16),
    st.booleans()
)
def test_ch_cn_conn_dict(choice, address1, port1, address2, port2, force_eq):
    """Test if sglib and the connection comperator behaves as expected."""
    inet1          = choice((0, 1))
    inet2          = choice((0, 1))
    if not inet1:
        address1 = address1[:4]
    if not inet2:
        address2 = address2[:4]
    if force_eq: