return value def deserialize_data(self, data): if isinstance(data, tuple): return list(map(self.deserialize_data, data)) else: return data backend_format_pairs = ( (SQLiteBackend, None), (InMemoryBackend, ObjectFormat()), ) settings = hs.Settings( max_examples=500, average_list_length=3.0, ) def test_errors_if_given_incompatible_format_and_backend(): with pytest.raises(ValueError): ExampleDatabase(backend=InMemoryBackend()) def test_storage_does_not_error_if_the_database_is_invalid(): database = ExampleDatabase() ints = database.storage_for(integers()) database.backend.save(ints.key, '["hi", "there"]') assert list(ints.fetch()) == []
@fails @given(float, float) def test_float_addition_cancels(self, x, y): assert x + (y - x) == y @fails @given(int, name=str) def test_can_be_given_keyword_args(x, name): assume(x > 0) assert len(name) < x @fails_with(Unsatisfiable) @given(int, settings=hs.Settings(timeout=0.1)) def test_slow_test_times_out(x): time.sleep(0.05) # Cheap hack to make test functions which fail on their second invocation calls = [0, 0, 0, 0] timeout_settings = hs.Settings(timeout=0.2) # The following tests exist to test that verifiers start their timeout # from when the test first executes, not from when it is defined. @fails @given(int, settings=timeout_settings) def test_slow_failing_test_1(x):
def test_can_verify_a_non_serializale_type(): verifier = Verifier(settings=hs.Settings(database=ExampleDatabase())) verifier.falsify(lambda x: len(x) > 0, Awkward)
class ParserFuzzTest(testutils.BaseTestCase): @given(st.text(min_size=1), settings=settings.Settings(max_examples=10000)) @example('True') @example(r'"test\t\t\a\\a"') @example(r' "test\t\t\a\\a" ') @example('"(1, 2)"') @example('(1, 2)') @example('(1, 2)') @example('(1, 2) ') @example('a,b,c,d') @example('(a,b,c,d)') @example('[a,b,c,d]') @example('{a,b,c,d}') @example('test:(a,b,c,d)') @example('{test:(a,b,c,d)}') @example('{test:a,b,c,d}') @example('{test:a,b:(c,d)}') # Note: Edit distance may be high for dicts. @example('0,') @example('#') @example('A#00000') # Note: '#'' is treated as a comment. @example('\x80') # Note: Causes UnicodeDecodeError. @example(100 * '[' + '0') # Note: Causes MemoryError. @example('\r\r\r\r1\r\r') def testDefaultParseValueFuzz(self, value): try: result = parser.DefaultParseValue(value) except TypeError: # It's OK to get a TypeError if the string has the null character. if u'\x00' in value: return raise except MemoryError: if len(value) > 100: # This is not what we're testing. return raise try: uvalue = six.u(value) uresult = six.u(result) except UnicodeDecodeError: # This is not what we're testing. return # Check that the parsed value doesn't differ too much from the input. distance = Levenshtein.distance(uresult, uvalue) max_distance = ( 2 + # Quotes or parenthesis can be implicit. sum(c.isspace() for c in value) + value.count('"') + value.count("'") + 3 * (value.count(',') + 1) + # 'a,' can expand to "'a', " 3 * (value.count(':')) + # 'a:' can expand to "'a': " 2 * value.count('\\')) if '#' in value: max_distance += len(value) - value.index('#') if not isinstance(result, six.string_types): max_distance += value.count('0') # Leading 0s are stripped. # Note: We don't check distance for dicts since item order can be changed. if '{' not in value: self.assertLessEqual(distance, max_distance, (distance, max_distance, uvalue, uresult))
def test_a_verifier_saves_any_failing_examples_in_its_database(): database = ExampleDatabase() verifier = Verifier(settings=hs.Settings(database=database)) counterexample = verifier.falsify(lambda x: x > 0, int) saved = list(database.storage_for((int, )).fetch()) assert saved == [counterexample]
def test_a_verifier_can_still_do_its_thing_if_a_saved_example_fails(): database = ExampleDatabase() verifier = Verifier(settings=hs.Settings(database=database)) verifier.falsify(lambda x: x < 11, int) verifier2 = Verifier(settings=hs.Settings(database=database)) verifier2.falsify(lambda x: x < 100, int)
class RateTest(TestCase, SeleniumTestCase): def tearDown(self): self.logout() def test_user_is_not_logged_in___user_cannot_rate(self): self.driver.get(self.live_server_url) with self.assertRaises(NoSuchElementException): self.driver.find_element_by_xpath('//*[@data-score]').click() @given(integers(min_value=1, max_value=5)) def test_click_star___rating_is_set_to_the_star_value(self, value): expected_percentage = 20 * value expected_style = 'width: {}%;'.format(expected_percentage) get_user_model().objects.create_user('user', password='******') self.driver.get(self.live_server_url) self.login('user', 'pass') self.click_score(value) self.wait_for_user_to_equal(value) self.assertEqual( expected_style, str(self.foreground_elem.get_attribute('style')).strip()) self.assertEqual(value, float(self.avg_rating_elem.text)) self.assertEqual(1, int(self.count_elem.text)) @given(integers(min_value=1, max_value=10)) def test_star_rating_range_is_set___rating_range_on_page_is_the_star_rating( self, value): with patch('star_ratings.templatetags.ratings.STAR_RATINGS_RANGE', value): self.driver.get(self.live_server_url) background = self.driver.find_element_by_class_name( 'star-ratings-rating-background') elements = background.find_elements_by_tag_name('li') self.assertEqual(value, len(elements)) # remove the timeout on this test as it can take a while to run on remote browsers and there are no assumptions to # stop it finding examples @given(lists(integers(min_value=1, max_value=5), min_size=2, max_size=10)) def test_multiple_users_rate___average_count_and_user_are_correct( self, scores): for i, score in enumerate(scores): self.driver.get(self.live_server_url) uname = 'user' + str(i) password = '******' + str(i) get_user_model().objects.create_user( username=uname, password=password, ) self.login(uname, password) self.click_score(score) self.wait_for_user_to_equal(score) self.logout() expected_avg = sum(scores) / len(scores) self.assertAlmostEqual(expected_avg, float(self.avg_rating_elem.text), places=2) self.assertEqual(len(scores), int(self.count_elem.text)) @override_settings(STAR_RATINGS_RERATE=True) @given(lists(integers(min_value=1, max_value=5), unique_by=lambda x: x, min_size=2, max_size=2), settings=settings.Settings(max_examples=10)) def test_rerate_is_true___the_user_is_able_to_change_their_rating( self, values): first, second = values get_user_model().objects.create_user('user', password='******') self.driver.get(self.live_server_url) self.login('user', 'pass') self.click_score(first) self.click_score(second) self.wait_for_user_to_equal(second) self.assertEqual(1, int(self.count_elem.text)) @override_settings(STAR_RATINGS_RERATE=False) @given(lists(integers(min_value=1, max_value=5), unique_by=lambda x: x, min_size=2, max_size=2), settings=settings.Settings(max_examples=10)) def test_rerate_is_false___the_user_is_not_able_to_change_their_rating( self, values): first, second = values get_user_model().objects.create_user('user', password='******') self.driver.get(self.live_server_url) self.login('user', 'pass') self.click_score(first) self.wait_for_user_to_equal(first) self.click_score(second) self.assertEqual(1, int(self.count_elem.text)) self.assertEqual(first, int(self.user_rating_elem.text)) # # helper functions # @property def user_rating_elem(self): return self.driver.find_element_by_xpath( '//*[@class="star-ratings-rating-user"]/*[@class="star-ratings-rating-value"]' ) @property def avg_rating_elem(self): return self.driver.find_element_by_xpath( '//*[@class="star-ratings-rating-average"]/*[@class="star-ratings-rating-value"]' ) @property def count_elem(self): return self.driver.find_element_by_xpath( '//*[@class="star-ratings-rating-count"]/*[@class="star-ratings-rating-value"]' ) @property def foreground_elem(self): return self.driver.find_element_by_class_name( 'star-ratings-rating-foreground') def wait_for_user_to_equal(self, value): try: WebDriverWait( self.driver, 30).until(lambda d: int(self.user_rating_elem.text) == value) except TimeoutException: self.assertEqual(value, int(self.user_rating_elem.text)) def click_score(self, score): try: if self.browser_tag and 'android_' in self.browser_tag: self.driver.find_element_by_xpath( '//*[@class="star-ratings-rating-background"]//*[@data-score="{}"]/..' .format(score)).click() else: self.driver.find_element_by_xpath( '//*[@class="star-ratings-rating-background"]//*[@data-score="{}"]' .format(score)).click() except WebDriverException: # if we aren't able to click the background this is most likely because the foreground is getting in the # way so try clicking that instead if self.browser_tag and 'android_' in self.browser_tag: self.driver.find_element_by_xpath( '//*[@class="star-ratings-rating-foreground"]//*[@data-score="{}"]/..' .format(score)).click() else: self.driver.find_element_by_xpath( '//*[@class="star-ratings-rating-foreground"]//*[@data-score="{}"]' .format(score)).click() def login(self, username, password): self.driver.find_element_by_id('login-link').click() self.driver.find_element_by_id('id_username').send_keys(username) self.driver.find_element_by_id('id_password').send_keys(password) self.driver.find_element_by_id('id_submit').click() def logout(self): with self.ignore_implicit_wait(): try: self.driver.find_element_by_id('logout-link').click() except NoSuchElementException: pass
@fails @given(floats(), floats()) def test_float_addition_cancels(self, x, y): assert x + (y - x) == y @fails @given(x=integers(), name=text()) def test_can_be_given_keyword_args(x, name): assume(x > 0) assert len(name) < x @fails_with(Unsatisfiable) @given(integers(), settings=hs.Settings(timeout=0.1)) def test_slow_test_times_out(x): time.sleep(0.05) # Cheap hack to make test functions which fail on their second invocation calls = [0, 0, 0, 0] timeout_settings = hs.Settings(timeout=0.2) # The following tests exist to test that verifiers start their timeout # from when the test first executes, not from when it is defined. @fails @given(integers(), settings=timeout_settings) def test_slow_failing_test_1(x):
class TestSummarize(unittest.TestCase): test_msgs = test_json_msgs summ = SpacyTsSummarizer() summ.set_summarizer(lsa.LsaSummarizer()) @given(lists(elements=sampled_from(test_json_msgs), min_size=3), integers(min_value=1, max_value=20), settings=hs.Settings(timeout=1000)) def test_text_rank_summarization_ds1_days(self, smp_msgs, days): """Generate something for N day interval""" logger.info("Input is %s", smp_msgs) asd = { 'days': days, 'size': 3, 'txt': u'Summary for first {} days:\n'.format(days) } #TestSummarize.summ.set_interval() TestSummarize.summ.set_channel('elasticsearch') sumry = TestSummarize.summ.summarize(smp_msgs, range_spec=asd) logger.debug("Summary is %s", sumry) # Length of summary is at least 1 and no greater than 3 self.assertTrue(len(sumry) >= 1) #self.assertTrue(len(sumry) <= 3) # Length of summary is less than or equal to the original length #self.assertTrue(len(sumry) <= len(smp_msgs)) # Each message in the summary must correspond to a message @given(lists(elements=sampled_from(test_json_msgs_c2), min_size=12), integers(min_value=1, max_value=20), settings=hs.Settings(timeout=1000)) def test_text_rank_summarization_ds2_days(self, smp_msgs, days): """Generate something for N day interval""" logger.info("Input is %s", smp_msgs) asd = { 'days': days, 'size': 3, 'txt': u'Summary for first {} days:\n'.format(days) } #TestSummarize.summ.set_interval(asd) TestSummarize.summ.set_channel('elasticsearch') sumry = TestSummarize.summ.summarize(smp_msgs, range_spec=asd) logger.debug("Summary is %s", sumry) # Length of summary is at least 1 and no greater than 3 self.assertTrue(len(sumry) >= 1) #self.assertTrue(len(sumry) <= 3) # Length of summary is less than or equal to the original length #self.assertTrue(len(sumry) <= len(smp_msgs)) # Each message in the summary must correspond to a message @given(integers(min_value=1, max_value=1000), integers(min_value=1, max_value=20), settings=hs.Settings(timeout=1000)) def test_text_rank_summarization_ds3_days(self, sampsize, days): """Generate something for N day interval""" channel, ssamp = random.choice(test_json_msgs_c3) samp = ssamp[random.randint(1, len(ssamp) - 2):] logger.info("Input is segment is %s", samp) asd = { 'days': days, 'size': 3, 'txt': u'Summary for first {} days:\n'.format(days) } #TestSummarize.summ.set_interval() TestSummarize.summ.set_channel(channel) sumry = TestSummarize.summ.summarize(samp, range_spec=asd) logger.debug("Summary is %s", sumry) # Length of summary is at least 1 and no greater than 3 self.assertTrue(len(sumry) >= 1) #self.assertTrue(len(sumry) <= 3) # Length of summary is less than or equal to the original length #self.assertTrue(len(sumry) <= len(samp)) # Each message in the summary must correspond to a message @given(lists(elements=sampled_from(test_json_msgs), min_size=1), integers(min_value=1, max_value=24), settings=hs.Settings(timeout=1000)) def test_text_rank_summarization_ds1_hours(self, smp_msgs, hours): """Generate something for N hour intervals""" logger.info("Input is %s", smp_msgs) asd = { 'hours': hours, 'size': 3, 'txt': u'Summary for first {} hours:\n'.format(hours) } #TestSummarize.summ.set_interval() TestSummarize.summ.set_channel('elasticsearch') sumry = TestSummarize.summ.summarize(smp_msgs, range_spec=asd) logger.debug("Summary is %s", sumry) # Length of summary is at least 1 and no greater than 3 self.assertTrue(len(sumry) >= 1) #self.assertTrue(len(sumry) <= 3) # Length of summary is less than or equal to the original length #self.assertTrue(len(sumry) <= len(smp_msgs)) # Each message in the summary must correspond to a message @given(lists(elements=sampled_from(test_json_msgs_c2), min_size=1), integers(min_value=1, max_value=24), settings=hs.Settings(timeout=1000)) def test_text_rank_summarization_ds2_hours(self, smp_msgs, hours): """Generate something for N hour intervals""" logger.info("Input is %s", smp_msgs) asd = { 'hours': hours, 'size': 3, 'txt': u'Summary for first {} hours:\n'.format(hours) } #TestSummarize.summ.set_interval() TestSummarize.summ.set_channel('elasticsearch') sumry = TestSummarize.summ.summarize(smp_msgs, range_spec=asd) logger.debug("Summary is %s", sumry) # Length of summary is at least 1 and no greater than 3 self.assertTrue(len(sumry) >= 1) #self.assertTrue(len(sumry) <= 3) # Length of summary is less than or equal to the original length #self.assertTrue(len(sumry) <= len(smp_msgs)) # Each message in the summary must correspond to a message @given(integers(min_value=2, max_value=1000), integers(min_value=1, max_value=24), settings=hs.Settings(timeout=1000)) def test_text_rank_summarization_ds3_hours(self, sampsize, hours): """Generate something for N hour intervals""" channel, ssamp = random.choice(test_json_msgs_c3) samp = ssamp[random.randint(1, len(ssamp) - 2):] TestSummarize.summ.set_channel(channel) logger.info("Input is segment is %s", samp) asd = { 'hours': hours, 'size': 3, 'txt': u'Summary for first {} hours:\n'.format(hours) } sumry = TestSummarize.summ.summarize(samp, range_spec=asd) logger.debug("Summary is %s", sumry) # Length of summary is at least 1 and no greater than 3 self.assertTrue(len(sumry) >= 1)
def test_can_generate_naive_datetime(): minimal(datetimes(allow_naive=True), lambda d: not d.tzinfo) def test_can_generate_non_naive_datetime(): assert minimal(datetimes(allow_naive=True), lambda d: d.tzinfo).tzinfo == pytz.UTC def test_can_generate_non_utc(): minimal(datetimes(), lambda d: assume(d.tzinfo) and d.tzinfo.zone != u'UTC') with hs.Settings(max_examples=1000): @given(datetimes(timezones=[])) def test_naive_datetimes_are_naive(dt): assert not dt.tzinfo @given(datetimes(allow_naive=False)) def test_timezone_aware_datetimes_are_timezone_aware(dt): assert dt.tzinfo def test_restricts_to_allowed_set_of_timezones(): timezones = list(map(pytz.timezone, list(pytz.all_timezones)[:3])) x = minimal(datetimes(timezones=timezones)) assert any(tz.zone == x.tzinfo.zone for tz in timezones)
@fails @given(float, float) def test_float_addition_cancels(self, x, y): assert x + (y - x) == y @fails @given(int, name=str) def test_can_be_given_keyword_args(x, name): assume(x > 0) assert len(name) < x @fails_with(Unsatisfiable) @given(int, verifier_settings=hs.Settings(timeout=0.1)) def test_slow_test_times_out(x): time.sleep(0.05) # Cheap hack to make test functions which fail on their second invocation calls = [0, 0, 0, 0] timeout_settings = hs.Settings(timeout=0.2) # The following tests exist to test that verifiers start their timeout # from when the test first executes, not from when it is defined. @fails @given(int, verifier_settings=timeout_settings) def test_slow_failing_test_1(x):
def test_can_derandomize_on_evalled_functions(): table = StrategyTable() settings = hs.Settings(derandomize=True) v = Verifier(strategy_table=table, settings=settings) assert v.falsify(eval('lambda x: x > 0'), int) == (0, )
class Foo(object): @given(integers(), settings=hs.Settings(max_examples=10)) def this_has_a_unique_name_and_lives_on_a_class(self, x): calls2[0] += 1 assume(False)
def test_does_not_accept_random_if_derandomize(): with pytest.raises(ValueError): @given(int, settings=hs.Settings(derandomize=True), random=Random()) def test_blah(x): pass
(SQLiteBackend, None), (InMemoryBackend, ObjectFormat()), ) @pytest.mark.parametrize( ('descriptor', 'value', 'backend', 'format'), [dv + bf for dv in data_examples for bf in backend_format_pairs]) def test_simple_example_set(descriptor, value, backend, format): run_round_trip(descriptor, value, backend=backend, format=format) @given(DescriptorWithValue, verifier=Verifier( strategy_table=small_table, settings=hs.Settings(max_examples=500), )) def test_can_round_trip_a_single_value_through_the_database(dav): run_round_trip(dav.descriptor, dav.value) def test_errors_if_given_incompatible_format_and_backend(): with pytest.raises(ValueError): ExampleDatabase(backend=InMemoryBackend()) def test_a_verifier_saves_any_failing_examples_in_its_database(): database = ExampleDatabase() verifier = Verifier(settings=hs.Settings(database=database)) counterexample = verifier.falsify(lambda x: x > 0, int) saved = list(database.storage_for((int, )).fetch())
from random import Random from collections import namedtuple import pytest import hypothesis.settings as hs from hypothesis.internal.debug import timeout from hypothesis import strategy from hypothesis.specifiers import integers_from, floats_in_range, \ integers_in_range, just, one_of, sampled_from, streaming from hypothesis.internal.compat import text_type, binary_type from hypothesis.searchstrategy.narytree import NAryTree from tests.common.basic import Bitfields from hypothesis.utils.show import show settings = hs.Settings(max_examples=100, timeout=4) __all__ = ['small_verifier', 'timeout', 'standard_types', 'OrderedPair'] OrderedPair = namedtuple('OrderedPair', ('left', 'right')) @strategy.extend_static(OrderedPair) def ordered_pair_strategy(_, settings): return strategy(int, settings).flatmap( lambda right: strategy(integers_from(0), settings).map( lambda length: OrderedPair(right - length, right))) ConstantList = namedtuple('ConstantList', ('spec', ))
elif isinstance(specifier, dict): children = specifier.values() elif isinstance(specifier, (Just, SampledFrom)): return 1 else: try: children = list(specifier) except TypeError: return 1 return 1 + sum(map(size, children)) MAX_SIZE = 15 settings = hs.Settings( max_examples=100, timeout=4, average_list_length=2.0, database=None, ) verifier = Verifier(settings=settings, ) @timeout(5) @given(Descriptor, Random, verifier=verifier) def test_can_falsify_false_things(desc, random): assume(size(desc) <= MAX_SIZE) verifier.random = random x = verifier.falsify(lambda x: False, desc)[0] assert not list(strategy(desc, settings).simplify(x))