def test_kennitala_random(): """Tests random kennitala""" kt_no = Kennitala.random() kennitala = Kennitala(kt_no) assert kennitala.validate(), kt_no birth_date = kennitala.get_birth_date() assert date(1900, 1, 1) <= birth_date <= date.today()
def test_kennitala_birth_date(info): """Validates birth date extraction""" kt_no, birth_date = info kennitala = Kennitala(kt_no) assert kennitala.get_birth_date() == birth_date # same for static version assert Kennitala.to_date(kt_no) == birth_date
def test_kennitala_is_person_fails(kt_no): """Tests kennitala type detection""" kennitala = Kennitala(kt_no) first_digit = int(kt_no[0]) personal = (0 <= first_digit <= 3) with pytest.raises(Kennitala.Invalid): fail = kennitala.is_person()
def test_kennitala_is_person(kt_no): """Tests kennitala type detection""" kennitala = Kennitala(kt_no) first_digit = int(kt_no[0]) personal = (0 <= first_digit <= 3) assert kennitala.is_person() == personal assert Kennitala.is_personal(kt_no) == personal
def test_kennitala_is_person(kt_no): """Tests kennitala type detection""" kennitala = Kennitala(kt_no) first_digit = int(kt_no[0]) personal = 0 <= first_digit <= 3 assert kennitala.is_person() == personal assert Kennitala.is_personal(kt_no) == personal
def test_fa_faedingardag(self): kt0 = Kennitala('0209724249') kt1 = Kennitala('1709715079') kt2 = Kennitala('2111972859') str_kt3 = '30119344' vartala = Kennitala(str_kt3).fa_vartolu() kt3 = Kennitala(str_kt3 + str(vartala) + '8') kt4 = Kennitala('4807042310') kt5 = Kennitala('7103042110') format = '%Y%m%d' #YYYYmmdd dt0 = time.strptime('19720902', format) dt1 = time.strptime('19710917', format) dt2 = time.strptime('19971121', format) dt3 = time.strptime('18931130', format) dt4 = time.strptime('20040708', format) dt5 = time.strptime('20040331', format) #self.assertEqual(dt0, kt0.fa_faedingardag(), 'kt0 ekki rétt') self.assertEqual(dt1, kt1.fa_faedingardag(), 'kt1 ekki rétt') self.assertEqual(dt2, kt2.fa_faedingardag(), 'kt2 ekki rétt') self.assertEqual(dt3, kt3.fa_faedingardag(), 'kt3 ekki rétt') self.assertEqual(dt4, kt4.fa_faedingardag(), 'kt4 ekki rétt') self.assertEqual(dt5, kt5.fa_faedingardag(), 'kt5 ekki rétt')
def test_kennitala_birth_date_raises(kt_no): """Tests birth date extraction fails for invalid kennitala""" kennitala = Kennitala(kt_no) with pytest.raises(Kennitala.Invalid): birth_date = kennitala.get_birth_date() # same for static version with pytest.raises(Kennitala.Invalid): birth_date = Kennitala.to_date(kt_no)
def test_kennitala_generate(idx, person): """Test bunch of generated kennitalas""" today = date.today() kt_no = Kennitala.generate(today, person) kennitala = Kennitala(kt_no) assert kennitala.validate(), kt_no assert kennitala.get_birth_date() == today assert kennitala.is_person() == person
def test_kennitala_with_dash(kt_no): """Test kennitala only_digits""" kennitala = Kennitala(kt_no) dashed = kennitala.with_dash() assert len(dashed) == 11 assert len([x for x in dashed if x.isdigit()]) == 10 assert dashed.count("-") == 1 assert dashed[6] == "-"
def test_kennitala_with_dash(kt_no): """Test kennitala only_digits""" kennitala = Kennitala(kt_no) dashed = kennitala.with_dash() assert len(dashed) == 11 assert len([x for x in dashed if x.isdigit()]) == 10 assert dashed.count('-') == 1 assert dashed[6] == '-'
def _validate_ssn(self, key): ssn = self.cleaned_data.get(key) kt = Kennitala(ssn) if kt.validate(): if self.person: if kt.is_personal(ssn): return True else: if not kt.is_personal(ssn): return True self.errors[key] = ['Kennitala ekki rétt slegin inn'] return False
def aldursbil(kennitala): '''Takes kennitala, returns aldursbil (age level)''' # INPUT: kennitala/ssn. preferably a string like DDMMYYXXXX # where DD is date, MM month, YY year, rest we don't care about. # OUTPUT: aldursbil. "bil 1", "bil 2", "bil 3" # where bil 1 means was born in jan-may, 2 means apr-aug, 3 means sep-dec. kt = Kennitala(kennitala) if not kt.validate(): # we have an error return "villa" else: month = int(kennitala[2:4]) third_of_year = int(max(0, month - 1) / 4) # rounds down third_to_aldursbil = {0: 'bil 1', 1: 'bil 2', 2: 'bil 3'} return third_to_aldursbil[third_of_year]
def get_random_person(self, gender=None, year=None, parent=None, address=None): """Get random person as a dict Keyword Arguments: gender {str} -- Gender of person. If None a random gender is selected. (default: {None}) year {int} -- Birth year of person. If None a random year is selected. (default: {None}) parent {dict} -- Parent to derive last name from. If None a random last name is selected. (default: {None}) address {dict} -- An address to associate person with. If None a random address is selected. (default: {None}) Returns: dict -- A dict with attributes for person {gender, firstname, lastname, ssn} """ if year is not None: # Select random date in given year start_date = datetime.date(year, 1, 1).toordinal() end_date = datetime.date(year, 12, 31).toordinal() random_date = datetime.date.fromordinal( random.randint(start_date, end_date)) ssn = Kennitala.generate(random_date) else: ssn = Kennitala.random() if gender is None: gender = random.choice(self.genders) if address is None: address = self.address_generator.get_random_address() return { 'gender': gender, 'firstname': self.get_first_name(gender), 'lastname': self.get_last_name(gender, parent=parent), 'ssn': ssn, 'address': address }
def test_kennitala_ok(kt_no): """Tests few real kennitala numbers""" kennitala = Kennitala(kt_no) assert kennitala.validate(), kt_no # test static version assert Kennitala.is_valid(kt_no), kt_no
def test_kennitala_only_digits(kt_no): """Test kennitala only_digits""" kennitala = Kennitala(kt_no) digits = kennitala.only_digits() assert len(digits) == 10 assert all(x.isdigit() for x in digits)
def test_kennitala_only_digits_raises(kt_no): """Tests Kennitala.only_digits raises on invalid input""" kennitala = Kennitala(kt_no) with pytest.raises(Kennitala.Invalid): kennitala.only_digits()
def test_kennitala_only_digits_raises(kt_no): """Tests Kennitala.only_digits raises on invalid input""" kennitala = Kennitala(kt_no) with pytest.raises(Kennitala.Invalid): digits = kennitala.only_digits()
def test_kennitala_with_dash_raises(kt_no): """Tests Kennitala.with_dash raises on invalid input""" kennitala = Kennitala(kt_no) with pytest.raises(Kennitala.Invalid): dashed = kennitala.with_dash()
def test_kennitala_with_dash_raises(kt_no): """Tests Kennitala.with_dash raises on invalid input""" kennitala = Kennitala(kt_no) with pytest.raises(Kennitala.Invalid): kennitala.with_dash()
def test_kennitala_random_same_day(): """Tests random kennitala""" y2k = date(2000, 1, 1) kt_no = Kennitala.random(y2k, y2k) assert Kennitala.is_valid(kt_no) assert Kennitala.to_date(kt_no) == y2k
def test_kennitala_is_valid_none(): """Tests that None is not valid kennitala""" kennitala = Kennitala(None) assert not kennitala.validate() assert not Kennitala.is_valid(None)
def test_str_ok(kt_no): kennitala = Kennitala(kt_no) assert str(kennitala) == kt_no
def test_kennitala_is_person_fails(kt_no): """Tests kennitala type detection""" kennitala = Kennitala(kt_no) with pytest.raises(Kennitala.Invalid): kennitala.is_person()
def test_kennitala_fail(kt_no): """Makes sure invalid kennitala do not validate""" kennitala = Kennitala(kt_no) assert not kennitala.validate(), kt_no # same with static version assert not Kennitala.is_valid(kt_no), kt_no
def kennitala_get(year): start_date = datetime.date(year, 1, 1).toordinal() end_date = datetime.date(year, 12, 31).toordinal() random_date = datetime.date.fromordinal(random.randint(start_date, end_date)) kt = Kennitala('') return kt.generate(random_date)
def test_fa_vartolu(self): kt0 = Kennitala('1709715079') kt1 = Kennitala('5901690809') self.assertEqual(kt0.fa_vartolu(), 7, 'kt0 ekki rétt vartala') self.assertEqual(kt1.fa_vartolu(), 0, 'kt1 ekki rétt vartala')
def test_str_inv(kt_no): kennitala = Kennitala(kt_no) assert str(kennitala) == "Invalid kennitala"
def test_tegund_kennitolu(self): kt_eins = Kennitala('2222222229') kt_fyr = Kennitala('6809780429') self.assertEqual('EINST', kt_eins.tegund_kennitolu()) self.assertEqual('FYRIRT', kt_fyr.tegund_kennitolu())
def test_kennitala_random_invalid_input(): """Tests that random kennitala fails on invalid input""" y2k = date(2000, 1, 1) today = date.today() with pytest.raises(ValueError): Kennitala.random(today, y2k)
def test_kennitala_random_invalid_input(): """Tests that random kennitala fails on invalid input""" y2k = date(2000, 1, 1) today = date.today() with pytest.raises(ValueError): kt_no = Kennitala.random(today, y2k)