From 56b8d9def0f4c0c4cb767d8842a42ea12bab9519 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Fri, 6 Nov 2020 22:23:47 +0100 Subject: [PATCH 1/6] add validator functions and tests --- validators/test_validators.py | 37 +++++++++++++++++++++++++++++++++++ validators/validators.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 validators/test_validators.py create mode 100644 validators/validators.py diff --git a/validators/test_validators.py b/validators/test_validators.py new file mode 100644 index 0000000..b959892 --- /dev/null +++ b/validators/test_validators.py @@ -0,0 +1,37 @@ +from validators import mandatory, empty, iso_date, valid_iban, valid_bic + + +def test_mandatory(): + assert mandatory("Foo") == True + assert mandatory("Foo Bar") == True + assert mandatory("1970-01-01") == True + assert mandatory("") == False + + +def test_empty(): + assert empty("") == True + assert empty(" ") == False + assert empty("Foo") == False + assert empty("1970-01-01") == False + + +def test_iso_date(): + assert iso_date("1970-01-01") == True + assert iso_date("1970-1-1") == True + assert iso_date("70-01-01") == False + assert iso_date("1970/01/01") == False + assert iso_date("1.1.1970") == False + assert iso_date("01.01.1970") == False + + +def test_valid_iban(): + assert valid_iban("DE89 3704 0044 0532 0130 00") == True + assert valid_iban("DX89 3704 0044 0532 0130 00") == False + assert valid_iban("DE99 3704 0044 0532 0130 00") == False + + +def test_valid_bic(): + assert valid_bic("PBNKDEFFXXX") == True + assert valid_bic("PBNKDXFFXXX") == False + assert valid_bic("PBNKDXFFXXXX") == False + assert valid_bic("PBN1DXFFXXX") == False diff --git a/validators/validators.py b/validators/validators.py new file mode 100644 index 0000000..aadab3e --- /dev/null +++ b/validators/validators.py @@ -0,0 +1,34 @@ +import datetime +from schwifty import IBAN, BIC + + +def mandatory(field: str) -> bool: + return bool(field) + + +def empty(field: str) -> bool: + return not bool(field) + + +def iso_date(field: str) -> bool: + try: + datetime.datetime.strptime(field, "%Y-%m-%d") + return True + except ValueError: + return False + + +def valid_iban(field: str) -> bool: + try: + IBAN(field) + return True + except ValueError: + return False + + +def valid_bic(field: str) -> bool: + try: + BIC(field) + return True + except ValueError: + return False -- 2.45.2 From a41573bffb0ea0499fb12fe3d2e7019af509d37b Mon Sep 17 00:00:00 2001 From: Stefan Haun Date: Sat, 7 Nov 2020 13:19:55 +0100 Subject: [PATCH 2/6] Add more test cases for validators --- validators/test_validators.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/validators/test_validators.py b/validators/test_validators.py index b959892..16d3a9a 100644 --- a/validators/test_validators.py +++ b/validators/test_validators.py @@ -6,9 +6,11 @@ def test_mandatory(): assert mandatory("Foo Bar") == True assert mandatory("1970-01-01") == True assert mandatory("") == False + assert mandatory(None) == False def test_empty(): + assert empty(None) == True assert empty("") == True assert empty(" ") == False assert empty("Foo") == False @@ -22,12 +24,18 @@ def test_iso_date(): assert iso_date("1970/01/01") == False assert iso_date("1.1.1970") == False assert iso_date("01.01.1970") == False + assert iso_date("") == False + assert iso_date(None) == False def test_valid_iban(): assert valid_iban("DE89 3704 0044 0532 0130 00") == True + assert valid_iban("DE89370400440532013000") == True assert valid_iban("DX89 3704 0044 0532 0130 00") == False assert valid_iban("DE99 3704 0044 0532 0130 00") == False + assert valid_iban("") == False + assert valid_iban(None) == False + assert valid_iban("DE89 3704 0044 0532 0130 00".split()) == False def test_valid_bic(): @@ -35,3 +43,8 @@ def test_valid_bic(): assert valid_bic("PBNKDXFFXXX") == False assert valid_bic("PBNKDXFFXXXX") == False assert valid_bic("PBN1DXFFXXX") == False + assert valid_bic("") == False + assert valid_bic(None) == False + assert valid_bic("PBNKDEFFXXX".split()) == False + + -- 2.45.2 From 1c46e5b925b5593b16105dd58b1646a9e3d4d16f Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 7 Nov 2020 19:21:13 +0100 Subject: [PATCH 3/6] add schwifty to requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 757931f..8318237 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ tornado==6.0.4 isodate==0.6.0 pytest==5.4.1 +schwifty==2020.9.0 -- 2.45.2 From c859a735eead481b21b88a8a40a60cb21f528497 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 7 Nov 2020 19:48:17 +0100 Subject: [PATCH 4/6] fix validators for additional test cases --- validators/validators.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/validators/validators.py b/validators/validators.py index aadab3e..9ff8f47 100644 --- a/validators/validators.py +++ b/validators/validators.py @@ -11,6 +11,8 @@ def empty(field: str) -> bool: def iso_date(field: str) -> bool: + if not field: + return False try: datetime.datetime.strptime(field, "%Y-%m-%d") return True @@ -19,6 +21,10 @@ def iso_date(field: str) -> bool: def valid_iban(field: str) -> bool: + if not field: + return False + if not type(field) == str: + return False try: IBAN(field) return True @@ -27,6 +33,10 @@ def valid_iban(field: str) -> bool: def valid_bic(field: str) -> bool: + if not field: + return False + if not type(field) == str: + return False try: BIC(field) return True -- 2.45.2 From 652d1445a83e8817b3ccbd08dbb7bcd82464d890 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 7 Nov 2020 19:53:47 +0100 Subject: [PATCH 5/6] add function to validate money fields --- validators/test_validators.py | 11 ++++++++++- validators/validators.py | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/validators/test_validators.py b/validators/test_validators.py index 16d3a9a..3094007 100644 --- a/validators/test_validators.py +++ b/validators/test_validators.py @@ -1,4 +1,4 @@ -from validators import mandatory, empty, iso_date, valid_iban, valid_bic +from validators import mandatory, empty, iso_date, valid_iban, valid_bic, valid_money_amount def test_mandatory(): @@ -48,3 +48,12 @@ def test_valid_bic(): assert valid_bic("PBNKDEFFXXX".split()) == False +def test_valid_money_amount(): + assert valid_money_amount(None) == False + assert valid_money_amount("") == False + assert valid_money_amount("0,00") == False + assert valid_money_amount("13,37") == False + assert valid_money_amount("0") == True + assert valid_money_amount("0.00") == True + assert valid_money_amount("13") == True + assert valid_money_amount("13.37") == True \ No newline at end of file diff --git a/validators/validators.py b/validators/validators.py index 9ff8f47..f0f33cc 100644 --- a/validators/validators.py +++ b/validators/validators.py @@ -42,3 +42,12 @@ def valid_bic(field: str) -> bool: return True except ValueError: return False + +def valid_money_amount(field: str) -> bool: + if not field: + return False + try: + float(field) + return True + except (ValueError, TypeError): + return False \ No newline at end of file -- 2.45.2 From 5612109024df62b729f04ab8091860b1260bc9ce Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sat, 7 Nov 2020 20:00:12 +0100 Subject: [PATCH 6/6] formatting --- validators/validators.py | 1 + 1 file changed, 1 insertion(+) diff --git a/validators/validators.py b/validators/validators.py index f0f33cc..8ce199d 100644 --- a/validators/validators.py +++ b/validators/validators.py @@ -43,6 +43,7 @@ def valid_bic(field: str) -> bool: except ValueError: return False + def valid_money_amount(field: str) -> bool: if not field: return False -- 2.45.2