Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare authored and Johannestegner committed Jun 29, 2020
1 parent 558c0e1 commit 4f293f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
5 changes: 4 additions & 1 deletion personnummer/personnummer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

string_types = str


class PersonnummerException(Exception):
pass


class Personnummer:
def __init__(self, ssn, options=None):
"""
Expand All @@ -24,7 +26,8 @@ def __init__(self, ssn, options=None):
self.parts = self.get_parts(ssn)

if self.valid() is False:
raise PersonnummerException(str(ssn) + ' Not a valid Swedish personal identity number!')
raise PersonnummerException(
str(ssn) + ' Not a valid Swedish personal identity number!')

def format(self, long_format=False):
"""
Expand Down
33 changes: 21 additions & 12 deletions personnummer/tests/test_personnummer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,38 @@
import json
import mock


def get_test_data():
response = urllib.request.urlopen('https://raw.githubusercontent.com/personnummer/meta/master/testdata/list.json')
response = urllib.request.urlopen(
'https://raw.githubusercontent.com/personnummer/meta/master/testdata/list.json')
raw = response.read().decode('utf-8')
return json.loads(raw)


test_data = get_test_data()
availableListFormats = [
'long_format',
'short_format',
'separated_format',
'separated_long',
'long_format',
'short_format',
'separated_format',
'separated_long',
]


class TestPersonnummer(TestCase):
def testPersonnummerList(self):
for item in test_data:
for format in availableListFormats:
self.assertEqual(personnummer.valid(item[format]), item['valid'])
self.assertEqual(personnummer.valid(
item[format]), item['valid'])

def testPersonnummerFormat(self):
for item in test_data:
for format in availableListFormats:
if format != 'short_format' and item['separated_format'].find('+') != -1:
self.assertEqual(personnummer.parse(item[format]).format(), item['separated_format'])
self.assertEqual(personnummer.parse(item[format]).format(True), item['long_format'])
self.assertEqual(personnummer.parse(
item[format]).format(), item['separated_format'])
self.assertEqual(personnummer.parse(
item[format]).format(True), item['long_format'])

def testPersonnummerError(self):
for item in test_data:
Expand All @@ -49,8 +56,10 @@ def testPersonnummerSex(self):
if not item['valid']:
return

self.assertEqual(personnummer.parse(item[format]).isMale(), item['isMale'])
self.assertEqual(personnummer.parse(item[format]).isFemale(), item['isFemale'])
self.assertEqual(personnummer.parse(
item[format]).isMale(), item['isMale'])
self.assertEqual(personnummer.parse(
item[format]).isFemale(), item['isFemale'])

def testPersonnummerAge(self):
for item in test_data:
Expand All @@ -64,8 +73,8 @@ def testPersonnummerAge(self):
if item['type'] == 'con':
day = day - 60

date = datetime(year=year,month=month,day=day)
date = datetime(year=year, month=month, day=day)
p = personnummer.parse(item[format])

with mock.patch('personnummer.personnummer.get_current_datetime',mock.Mock(return_value=date)):
with mock.patch('personnummer.personnummer.get_current_datetime', mock.Mock(return_value=date)):
self.assertEqual(0, p.get_age())

0 comments on commit 4f293f1

Please sign in to comment.