def test_year_bounds_are_respected_in_deserialization(): s = dates() r = Random(1) template = s.draw_template(r, s.draw_parameter(r)) year = s.reify(template).year basic = s.to_basic(template) above = dates(min_year=year + 1) below = dates(max_year=year - 1) with pytest.raises(BadData): above.from_basic(basic) with pytest.raises(BadData): below.from_basic(basic)
def test_add_comment_500(self, comment, event_id): self.user = hyp_models(User, last_login=dates(), date_joined=dates(), email=just('')).example() self.event = hyp_models(Event, date=dates()).example() self.client.force_login(self.user, backend=settings.AUTHENTICATION_BACKENDS[0]) response = self.client.post(reverse('EventShiftSchedule:comment'), { 'value': comment, 'event_id': event_id }) assert response.status_code != 500 self.user.delete()
def test_year_bounds_are_respected_in_deserialization(): s = dates() r = Random(1) while True: try: template = s.draw_template(r, s.draw_parameter(r)) year = s.reify(template).year break except UnsatisfiedAssumption: pass basic = s.to_basic(template) above = dates(min_year=year + 1) below = dates(max_year=year - 1) with pytest.raises(UnsatisfiedAssumption): above.reify(above.from_basic(basic)) with pytest.raises(UnsatisfiedAssumption): below.reify(below.from_basic(basic))
def hypothesis_strategy(field_type): if isinstance(field_type, ForeignKeyField): return none() if isinstance(field_type, CharField) or isinstance(field_type, TextField): return text() if isinstance(field_type, DateField): return dates() if isinstance(field_type, BooleanField): return booleans() if isinstance(field_type, IntegerField): return integers(min_value=-(2**63 - 1), max_value=2**63 - 1) if isinstance(field_type, DecimalField): return decimals(min_value=-(2**63 - 1), max_value=2**63 - 1)
def test_min_year_is_respected(): assert minimal(dates(min_year=2003)).year == 2003
def test_max_year_is_respected(): assert minimal(dates(max_year=1998)).year == 1998
def test_can_find_before_the_year_2000(): assert minimal(dates(), lambda x: x.year < 2000).year == 1999
def test_can_find_each_month(): for i in hrange(1, 12): minimal(dates(), lambda x: x.month == i)
def test_can_find_each_month(): for month in hrange(1, 13): find_any(dates(), lambda x: x.month == month)
def test_can_find_after_the_year_2000(): assert minimal(dates(), lambda x: x.year > 2000).year == 2001
class TestAttrConversions(object): def verify_c_type(self, pointer, leng): """ Verifies that (pointer, leng) is a proper c type :param pointer: pointer to c data :param leng: length of c data """ assert isinstance(pointer, c_void_p) assert isinstance(leng, (integer_types, c_ulong)) def create_ck_attr(self, pointer, leng): """ Given (pointer, leng) creates a c attribute. :param pointer: pointer to c data :param leng: length of c data :return: c attribute """ c_attr = CK_ATTRIBUTE(CKA_CLASS, pointer, leng) return c_attr def reverse_case(self, pointer, leng, func): """ Perform the reverse operation of the given function on (pointer, leng) :param pointer: c pointer :param leng: data length :param func: function type :return: python type """ c_attr = self.create_ck_attr(pointer, leng) return func(c_attr, reverse=True) def force_fail(self, val, func, error): """ run val through func, assert that 'error' is raised :param val: data :param func: function :param error: expected error """ with pytest.raises(error): pointer, leng = func(val) @given(integers(min_value=0, max_value=MAX_INT)) def test_to_long(self, int_val): """ to_long() with param: :param int_val: random integer >= 0 """ pointer, leng = to_long(int_val) self.verify_c_type(pointer, leng) # C type is unsigned integer. Assert result is positive. assert cast(pointer, POINTER(c_ulong)).contents.value >= 0 py_long = self.reverse_case(pointer, leng, to_long) assert int_val == py_long @given(integers(max_value=-1)) def test_to_long_neg_overflow(self, int_val): """ test_to_long() with param: :param int_val: random negative int. Conversion will result in data loss. """ pointer, leng = to_long(int_val) self.verify_c_type(pointer, leng) py_long = self.reverse_case(pointer, leng, to_long) LOG.debug("to_long() data loss: %s => %s", int_val, py_long) assert int_val != py_long @given(one_of(floats(), text())) def test_to_long_fail(self, fail_val): """ to_long() with incompatible params: :param fail_val: random data of known incompatible types (floats, text) """ self.force_fail(fail_val, to_long, TypeError) @given(booleans()) def test_to_bool(self, bool_val): """ to_bool() with param: :param bool_val: random boolean """ pointer, leng = to_bool(bool_val) self.verify_c_type(pointer, leng) py_bool = self.reverse_case(pointer, leng, to_bool) assert bool_val == py_bool @given(integers(min_value=-100, max_value=100)) def test_to_bool_int(self, int_val): """ to_bool() with param: :param int_val: random int """ pointer, leng = to_bool(int_val) self.verify_c_type(pointer, leng) py_bool = self.reverse_case(pointer, leng, to_bool) assert bool(int_val) == py_bool @given(one_of(floats(), text())) def test_to_bool_fail(self, fail_val): """ to_bool() with incompatible param: :param fail_val: data of known incompatible type (floats, text) """ self.force_fail(fail_val, to_bool, TypeError) @given(text(alphabet=letters)) def test_to_char_array_string(self, txt_val): """ to_char_array() with param: :param txt_val: random string """ pointer, leng = to_char_array(txt_val) self.verify_c_type(pointer, leng) py_txt = self.reverse_case(pointer, leng, to_char_array) assert b(txt_val) == py_txt @given( lists(elements=text(alphabet=letters, min_size=1, max_size=1), min_size=1)) def test_to_char_array_list(self, list_val): """ to_char_array() testing with param: :param list_val: random list of ascii strings """ pointer, leng = to_char_array(list_val) self.verify_c_type(pointer, leng) py_txt = self.reverse_case(pointer, leng, to_char_array) assert b("".join(list_val)) == py_txt def test_to_char_array_fail_obj(self): """ Trigger TypeError in to_char_array() with object as paramater. """ self.force_fail(object(), to_char_array, TypeError) @given(dates(min_year=1900)) def test_to_ck_date_string(self, date_val): """ to_ck_date() with param: :param date_val: random date to be converted to date-string """ date_string = date_val.strftime("%Y%m%d") pointer, leng = to_ck_date(date_string) self.verify_c_type(pointer, leng) py_date = self.reverse_case(pointer, leng, to_ck_date) assert b(date_string) == py_date @given(dates(min_year=1900)) def test_to_ck_date_dict(self, date_val): """ to_ck_date() with param: :param date_val: random date to be converted to a dictionary. """ date_dict = { 'year': date_val.year, 'month': date_val.month, 'day': date_val.day } pointer, leng = to_ck_date(date_dict) self.verify_c_type(pointer, leng) py_date = self.reverse_case(pointer, leng, to_ck_date) assert b(date_val.strftime("%Y%m%d")) == py_date @given(dates(min_year=1900)) def test_to_ck_date(self, date_val): """ to_ck_date() with param: :param date_val: random date, kept as date object """ pointer, leng = to_ck_date(date_val) self.verify_c_type(pointer, leng) py_date = self.reverse_case(pointer, leng, to_ck_date) assert b(date_val.strftime("%Y%m%d")) == py_date def test_to_ck_date_fail_obj(self): """ Trigger TypeError in to_ck_date() with object as paramater. """ self.force_fail(object(), to_ck_date, TypeError) @given(lists(elements=integers(min_value=0, max_value=255), min_size=1)) def test_to_byte_array(self, list_val): """ to_byte_array() with param: :param list_val: list of ints in range (0-255), convert to bytearray """ b_array = bytearray(list_val) pointer, leng = to_byte_array(b_array) self.verify_c_type(pointer, leng) py_bytes = self.reverse_case(pointer, leng, to_byte_array) assert py_bytes == hexlify(b_array) @given(integers(min_value=0)) def test_to_byte_array_int(self, int_val): """ to_byte_array() with param: :param int_val: random positive integer """ pointer, leng = to_byte_array(int_val) self.verify_c_type(pointer, leng) py_bytes = self.reverse_case(pointer, leng, to_byte_array) assert int(py_bytes, 16) == int_val @given(integers(max_value=-1)) def test_to_byte_array_int_neg_overflow(self, int_val): """ to_byte_array() with param: :param int_val: random int value. Will result in data loss """ pointer, leng = to_byte_array(int_val) self.verify_c_type(pointer, leng) py_bytes = self.reverse_case(pointer, leng, to_byte_array) LOG.debug("to_byte_array() data loss: %s => %s", b(hex(int_val)), py_bytes) assert int(py_bytes, 16) != int_val @given(lists(elements=integers(min_value=0, max_value=255))) def test_to_byte_array_list(self, list_val): """ to_byte_array() with param: :param list_val: randomly list of postive integers (within byte range). """ pointer, leng = to_byte_array(list_val) self.verify_c_type(pointer, leng) py_bytes = self.reverse_case(pointer, leng, to_byte_array) # Create list from returned byte-string py_list = [] for i in range(0, len(py_bytes), 2): py_list.append(int(py_bytes[i:i + 2], 16)) assert py_list == list_val @given(lists(elements=integers(min_value=256), min_size=1)) def test_to_byte_array_list_fail_big(self, list_val): """ to_byte_array() with incompatible param: :param list_val: random list of integers > 256 -ValueError """ with pytest.raises(ValueError): pointer, leng = to_byte_array(list_val) @given(lists(elements=integers(max_value=-1), min_size=1)) def test_to_byte_array_list_fail_neg(self, list_val): """ to_byte_array() with incompatible param: :param list_val: random list of negative integers. -ValueError """ with pytest.raises(ValueError): pointer, leng = to_byte_array(list_val) def test_to_byte_array_fail_obj(self): """ to_byte_array() with object param. -TypeError """ self.force_fail(object(), to_byte_array, TypeError) @given(text(alphabet="ghijklmnopqrstuvwxyz", min_size=1)) def test_to_byte_array_fail_str(self, txt_val): """ to_byte_array() with incompatible param: :param txt_val: random text -TypeError """ self.force_fail(txt_val, to_byte_array, ValueError) @given(integers(min_value=0)) def test_to_byte_array_hexstring(self, int_val): """ to_byte_array() with param: :param int_val: random integer to be converted to hex string. """ hex_string = hex(int_val).replace("0x", "").replace("L", "") pointer, leng = to_byte_array(hex_string) self.verify_c_type(pointer, leng) py_bytes = self.reverse_case(pointer, leng, to_byte_array) assert int(py_bytes, 16) == int(hex_string, 16) @given( dictionaries(keys=integers(min_value=1, max_value=MAX_INT), dict_class=Attributes, values=booleans())) def test_to_sub_attributes(self, test_dic): """ to_sub_attributes() with param :param test_dic: random dictionary of bools """ mock_xform_dict = defaultdict(lambda: to_bool) mock_xform_dict.update({key: to_bool for key in KEY_TRANSFORMS}) with mock.patch('pycryptoki.attributes.KEY_TRANSFORMS', new=mock_xform_dict): pointer, leng = to_sub_attributes(test_dic) self.verify_c_type(pointer, leng) @given(integers()) def test_to_sub_attributes_fail(self, int_val): """ to_sub_attributes() with incompatible param: :param int_val: random integer """ self.force_fail(int_val, to_sub_attributes, TypeError) @given(lists(elements=integers(min_value=0, max_value=255), min_size=1)) def test_c_byte_array_to_string(self, list_val): b_array = bytearray(list_val) c_b_array = (CK_BYTE * len(b_array))(*b_array) str_result = convert_c_ubyte_array_to_string(c_b_array) assert str_result == hexlify(b_array)
# contribution. # # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import from tests.common.debug import minimal from hypothesis.strategytests import strategy_test_suite from hypothesis.extra.datetime import dates from hypothesis.internal.compat import hrange TestStandardDescriptorFeatures1 = strategy_test_suite(dates()) def test_can_find_after_the_year_2000(): assert minimal(dates(), lambda x: x.year > 2000).year == 2001 def test_can_find_before_the_year_2000(): assert minimal(dates(), lambda x: x.year < 2000).year == 1999 def test_can_find_each_month(): for i in hrange(1, 12): minimal(dates(), lambda x: x.month == i)
@hy.given(st_user(), st_msg()) def test_quote_add(user, msg): inp = ' {} {}'.format(user, msg) run(notes.quote, 'del' + inp) assert run(notes.quote, 'add' + inp) == lexicon.quote.saved @hy.given(st_user(), st_msg()) def test_quote_add_already_exists(user, msg): inp = ' {} {}'.format(user, msg) run(notes.quote, 'add' + inp) assert run(notes.quote, 'add' + inp) == lexicon.quote.already_exists @hy.given(hydt.dates(), st_user(), st_msg()) def test_quote_add_date(date, user, msg): run(notes.quote, 'del {} {}'.format(user, msg)) assert run(notes.quote, 'add {} {} {}'.format( date.isoformat(), user, msg)) == lexicon.quote.saved @hy.settings(suppress_health_check=[hy.HealthCheck.random_module]) def test_quote_get_random(): quote = run(notes.quote, '') assert quote == lexicon.quote.get assert quote.text @hy.given(st.integers().filter(lambda x: x > 0)) def test_quote_get_index(index):
sample_referral.save() assert sample_referral == Person.get(Person.last_name == sample_person_one.last_name).referrals.get() def test_communication_by_querying_person(sample_person_one, sample_communication): sample_person_one.save() sample_communication.Person = Person.get(Person.first_name == sample_person_one.first_name, Person.last_name == sample_person_one.last_name) sample_communication.save() assert sample_communication == Person.get(Person.last_name == sample_person_one.last_name).communications.get() @given(text(), text(), text(), text(), dates(), text(), text()) def test_random_person(db_fixture, hlast_name, hfirst_name, hcellphone, hemail, hbirthdate, haddress_current, haddress_mailing): with db_fixture.atomic() as txn: db_fixture.create_table(Person, True) Person.create(last_name=hlast_name, first_name=hfirst_name, cellphone=hcellphone, email=hemail, birthdate=hbirthdate, address_current=haddress_current, address_mailing=haddress_mailing, is_realtor=False) assert hfirst_name == Person.get(Person.last_name == hlast_name).first_name txn.rollback()
def test_can_find_each_month(): for month in hrange(1, 13): dates().filter(lambda x: x.month == month).example()
def date_strategy(registry, type, context=None): return hsd.dates()
def test_can_draw_times_in_the_final_year(): last_year = dates(min_year=MAXYEAR) r = Random(1) for _ in hrange(1000): last_year.reify(last_year.draw_and_produce(r))
# # END HEADER from __future__ import division, print_function, absolute_import from random import Random import pytest from hypothesis.errors import UnsatisfiedAssumption from hypothesis.strategytests import strategy_test_suite from hypothesis.extra.datetime import dates from hypothesis.internal.debug import minimal from hypothesis.internal.compat import hrange TestStandardDescriptorFeatures1 = strategy_test_suite(dates()) def test_can_find_after_the_year_2000(): assert minimal(dates(), lambda x: x.year > 2000).year == 2001 def test_can_find_before_the_year_2000(): assert minimal(dates(), lambda x: x.year < 2000).year == 1999 def test_can_find_each_month(): for i in hrange(1, 12): minimal(dates(), lambda x: x.month == i)
import datetime from unittest import mock # 3rd party import pytest from hypothesis import given, settings import hypothesis.extra.datetime as st_dt from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker # local from employee_insights.models import Base from employee_insights.serializer import CsvSerializer, date_of_birth_to_age, age_to_date_of_birth from tests.strategies import employee_databases @pytest.mark.skip() @given(st_dt.dates(), st_dt.datetimes()) def test_convert_age_date_of_birth(date_of_birth, timestamp): """ Verify that convert between age and date of birth round trips correctly, currently skipped because I know this doesn't work correctly. :param date_of_birth: The date of birth to use for calculation :param timestamp: The current timestamp to use for calculation """ age = date_of_birth_to_age(date_of_birth, timestamp) date_of_birth2 = age_to_date_of_birth(age, timestamp) assert date_of_birth2 == date_of_birth @pytest.mark.skip() @settings(max_examples=50)