def test_will_handle_a_really_weird_failure(self, rnd):
            db = ExampleDatabase()

            @given(
                specifier,
                settings=Settings(
                    database=db,
                    max_examples=max_examples,
                    min_satisfying_examples=2,
                    average_list_length=2.0,
                ), random=rnd
            )
            def nope(x):
                s = hashlib.sha1(repr(x).encode(u'utf-8')).digest()
                assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
                if Random(s).randint(0, 1):
                    raise Rejected(u'%r with digest %r' % (
                        x, s
                    ))
            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
        def test_will_handle_a_really_weird_failure(self, s):
            db = ExampleDatabase()

            @given(specifier)
            @Settings(
                settings,
                database=db,
                max_examples=max_examples,
                min_satisfying_examples=2,
            )
            @seed(s)
            def nope(x):
                s = hashlib.sha1(repr(x).encode('utf-8')).digest()
                assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
                if Random(s).randint(0, 1):
                    raise Rejected('%r with digest %r' % (
                        x, s
                    ))
            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
Exemple #3
0
        def test_will_handle_a_really_weird_failure(self, s):
            db = ExampleDatabase()

            @given(specifier)
            @Settings(
                settings,
                database=db,
                max_examples=max_examples,
                min_satisfying_examples=2,
            )
            @seed(s)
            def nope(x):
                s = hashlib.sha1(repr(x).encode('utf-8')).digest()
                assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
                if Random(s).randint(0, 1):
                    raise Rejected('%r with digest %r' % (
                        x, s
                    ))
            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
        def test_will_handle_a_really_weird_failure(self):
            db = ExampleDatabase()

            @given(
                specifier,
                settings=Settings(
                    database=db,
                    max_examples=max_examples,
                    min_satisfying_examples=2,
                    average_list_length=2.0,
                )
            )
            def nope(x):
                s = hashlib.sha1(show(x).encode('utf-8')).digest()
                if Random(s).randint(0, 1):
                    raise Rejected()
            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
Exemple #5
0
        def test_will_handle_a_really_weird_failure(self):
            db = ExampleDatabase()

            @given(
                specifier,
                settings=Settings(
                    database=db,
                    max_examples=max_examples,
                    min_satisfying_examples=2,
                    average_list_length=2.0,
                )
            )
            def nope(x):
                s = hashlib.sha1(show(x).encode('utf-8')).digest()
                if Random(s).randint(0, 1):
                    raise Rejected()
            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
Exemple #6
0
        def test_will_handle_a_really_weird_failure(self, rnd):
            db = ExampleDatabase()

            @given(specifier,
                   settings=Settings(
                       database=db,
                       max_examples=max_examples,
                       min_satisfying_examples=2,
                       average_list_length=2.0,
                   ),
                   random=rnd)
            def nope(x):
                s = hashlib.sha1(repr(x).encode(u'utf-8')).digest()
                assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
                if Random(s).randint(0, 1):
                    raise Rejected(u'%r with digest %r' % (x, s))

            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
Exemple #7
0
def test_deduplicates():
    database = ExampleDatabase()
    storage = database.storage_for(int)
    storage.save(1)
    storage.save(1)
    assert list(storage.fetch()) == [1]
    database.close()
Exemple #8
0
def via_database(spec, strat, template):
    db = ExampleDatabase()
    try:
        s = db.storage_for(strat, strat)
        s.save(template)
        results = list(s.fetch())
        assert len(results) == 1
        return results[0]
    finally:
        db.close()
Exemple #9
0
def via_database(spec, strat, template):
    db = ExampleDatabase()
    try:
        s = db.storage_for(strat, strat)
        s.save(template)
        results = list(s.fetch())
        assert len(results) == 1
        return results[0]
    finally:
        db.close()
Exemple #10
0
 def test_can_round_trip_through_the_database(self, template, rnd):
     empty_db = ExampleDatabase(backend=SQLiteBackend(u':memory:'), )
     try:
         storage = empty_db.storage(u'round trip')
         storage.save(template, strat)
         values = list(storage.fetch(strat))
         assert len(values) == 1
         assert strat.to_basic(template) == strat.to_basic(values[0])
     finally:
         empty_db.close()
 def test_can_round_trip_through_the_database(self, template, rnd):
     empty_db = ExampleDatabase(backend=SQLiteBackend(":memory:"))
     try:
         storage = empty_db.storage("round trip")
         storage.save(template, strat)
         values = list(storage.fetch(strat))
         assert len(values) == 1
         assert strat.to_basic(template) == strat.to_basic(values[0])
     finally:
         empty_db.close()
Exemple #12
0
        def test_will_find_a_failure_from_the_database(self):
            db = ExampleDatabase()

            @given(specifier, settings=Settings(max_examples=10, database=db))
            def nope(x):
                raise Rejected()
            try:
                for i in hrange(3):
                    self.assertRaises(Rejected, nope)  # pragma: no cover
            finally:
                db.close()
        def test_will_find_a_failure_from_the_database(self):
            db = ExampleDatabase()

            @given(specifier, settings=Settings(max_examples=10, database=db))
            def nope(x):
                raise Rejected()
            try:
                for i in hrange(3):
                    self.assertRaises(Rejected, nope)  # pragma: no cover
            finally:
                db.close()
Exemple #14
0
def via_database(spec, strat, template):
    db = ExampleDatabase()
    key = u'via_database'
    try:
        s = db.storage(key)
        s.save(template, strat)
        results = list(s.fetch(strat))
        assert len(results) == 1
        return results[0]
    finally:
        db.close()
Exemple #15
0
 def test_can_round_trip_through_the_database(self, template):
     empty_db = ExampleDatabase(
         backend=SQLiteBackend(':memory:'),
     )
     try:
         storage = empty_db.storage_for(specifier)
         storage.save(template)
         values = list(storage.fetch())
         assert len(values) == 1
         assert strat.to_basic(template) == strat.to_basic(values[0])
     finally:
         empty_db.close()
Exemple #16
0
def run_round_trip(specifier, value, format=None, backend=None):
    if backend is not None:
        backend = backend()
    else:
        backend = SQLiteBackend()
    db = ExampleDatabase(format=format, backend=backend)
    try:
        storage = db.storage(u'round trip')
        storage.save(value, specifier)
        saved = list(storage.fetch(specifier))
        assert len(saved) == 1
        assert specifier.to_basic(saved[0]) == specifier.to_basic(value)
    finally:
        db.close()
Exemple #17
0
def run_round_trip(specifier, value, format=None, backend=None):
    if backend is not None:
        backend = backend()
    else:
        backend = SQLiteBackend()
    db = ExampleDatabase(format=format, backend=backend)
    try:
        storage = db.storage(u'round trip')
        storage.save(value, specifier)
        saved = list(storage.fetch(specifier))
        assert len(saved) == 1
        assert specifier.to_basic(saved[0]) == specifier.to_basic(value)
    finally:
        db.close()
Exemple #18
0
def test_will_find_a_failure_from_the_database():
    db = ExampleDatabase()

    class Rejected(Exception):
        pass

    @given(dav_strategy, settings=Settings(max_examples=10, database=db))
    def nope(x):
        raise Rejected()

    try:
        with pytest.raises(Rejected):
            nope()  # pragma: no branch
    finally:
        db.close()
Exemple #19
0
def test_will_find_a_failure_from_the_database():
    db = ExampleDatabase()

    class Rejected(Exception):
        pass

    @given(dav_strategy, settings=Settings(max_examples=10, database=db))
    def nope(x):
        raise Rejected()

    try:
        with pytest.raises(Rejected):
            nope()  # pragma: no branch
    finally:
        db.close()
Exemple #20
0
def test_can_handle_more_than_max_iterations_in_db():
    """This is checking that if we store a large number of examples in the DB
    and then subsequently reduce max_examples below that count, we a) don't
    error (which is how this bug was found) and b) stop at max_examples rather
    than continuing onwards."""
    db = ExampleDatabase()

    try:
        settings = hs.Settings(database=db, max_examples=10, max_iterations=10)
        seen = []
        first = [True]
        for _ in range(10):
            first[0] = True

            @given(integers(), settings=settings)
            def test_seen(x):
                if x not in seen:
                    if first[0]:
                        first[0] = False
                        seen.append(x)
                if x not in seen:
                    raise ValueError(u'Weird')

            try:
                test_seen()
            except ValueError:
                pass

        assert len(seen) >= 3

        seen = []

        @given(integers(),
               settings=hs.Settings(max_examples=1,
                                    max_iterations=2,
                                    database=db))
        def test_seen(x):
            seen.append(x)
            assume(False)

        with pytest.raises(Unsatisfiable):
            test_seen()
        assert len(seen) == 2
    finally:
        db.close()
Exemple #21
0
def test_can_handle_more_than_max_iterations_in_db():
    """This is checking that if we store a large number of examples in the DB
    and then subsequently reduce max_examples below that count, we a) don't
    error (which is how this bug was found) and b) stop at max_examples rather
    than continuing onwards."""
    db = ExampleDatabase()

    try:
        settings = hs.Settings(database=db, max_examples=10, max_iterations=10)
        seen = []
        first = [True]
        for _ in range(10):
            first[0] = True

            @given(integers(), settings=settings)
            def test_seen(x):
                if x not in seen:
                    if first[0]:
                        first[0] = False
                        seen.append(x)
                if x not in seen:
                    raise ValueError(u'Weird')

            try:
                test_seen()
            except ValueError:
                pass

        assert len(seen) >= 3

        seen = []

        @given(
            integers(), settings=hs.Settings(
                max_examples=1, max_iterations=2, database=db))
        def test_seen(x):
            seen.append(x)
            assume(False)
        with pytest.raises(Unsatisfiable):
            test_seen()
        assert len(seen) == 2
    finally:
        db.close()
Exemple #22
0
def test_can_handle_more_than_max_examples_values_in_db():
    """This is checking that if we store a large number of examples in the DB
    and then subsequently reduce max_examples below that count, we a) don't
    error (which is how this bug was found) and b) stop at max_examples rather
    than continuing onwards."""
    db = ExampleDatabase()

    try:
        settings = hs.settings(database=db, max_examples=10)
        seen = []
        first = [True]
        for _ in range(10):
            first[0] = True

            @given(integers())
            @settings
            def test_seen(x):
                if x not in seen:
                    if first[0]:
                        first[0] = False
                        seen.append(x)
                assert x in seen

            try:
                test_seen()
            except AssertionError:
                pass

        assert len(seen) >= 2

        seen = []

        @given(integers())
        @hs.settings(max_examples=1, database=db)
        def test_seen(x):
            seen.append(x)
        test_seen()
        assert len(seen) == 1
    finally:
        db.close()
def test_can_time_out_when_reading_from_database():
    should_timeout = False
    limit = 0
    examples = []
    db = ExampleDatabase()

    try:

        @given(integers(), settings=hs.Settings(timeout=0.1, database=db))
        def test_run_test(x):
            examples.append(x)
            if should_timeout:
                time.sleep(0.5)
            assert x >= limit

        for i in hrange(10):
            limit = -i
            examples = []
            with pytest.raises(AssertionError):
                test_run_test()

        examples = []

        limit = 0

        with pytest.raises(AssertionError):
            test_run_test()

        limit = min(examples) - 1

        should_timeout = True
        examples = []

        with pytest.raises(Timeout):
            test_run_test()

        assert len(examples) == 1
    finally:
        db.close()
Exemple #24
0
def test_can_time_out_when_reading_from_database():
    should_timeout = False
    limit = 0
    examples = []
    db = ExampleDatabase()

    try:

        @given(integers(), settings=hs.Settings(timeout=0.1, database=db))
        def test_run_test(x):
            examples.append(x)
            if should_timeout:
                time.sleep(0.5)
            assert x >= limit

        for i in hrange(10):
            limit = -i
            examples = []
            with pytest.raises(AssertionError):
                test_run_test()

        examples = []

        limit = 0

        with pytest.raises(AssertionError):
            test_run_test()

        limit = min(examples) - 1

        should_timeout = True
        examples = []

        with pytest.raises(Timeout):
            test_run_test()

        assert len(examples) == 1
    finally:
        db.close()