Skip to content

Commit

Permalink
Change the ErrorCode to enum type
Browse files Browse the repository at this point in the history
Renamed two enums values
  • Loading branch information
sabariramc committed Nov 19, 2020
1 parent dbc95d0 commit 39f741c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
18 changes: 9 additions & 9 deletions fieldtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_mandatory_error(self):
self.assertEqual(e.exception.field_name, 'request_id')
self.assertEqual(e.exception.message, 'request_id is a mandatory field')

def test_type_error_1(self):
def test_type_error_int(self):
with self.assertRaises(FieldTypeError) as e:
class_instance.test(
{'request_id': str(uuid4()),
Expand All @@ -123,7 +123,7 @@ def test_type_error_1(self):
self.assertEqual(f'pageNo should be of type {int}', e.exception.message)
self.assertEqual({"expectedType": f'{int}'}, e.exception.error_data)

def test_type_error_2(self):
def test_type_error_int(self):
with self.assertRaises(FieldTypeError) as e:
class_instance.test(
{'request_id': str(uuid4()),
Expand All @@ -138,7 +138,7 @@ def test_type_error_2(self):
self.assertEqual(f'id_list[1] should be of type {int}', e.exception.message)
self.assertEqual({"expectedType": f'{int}'}, e.exception.error_data)

def test_type_error_3(self):
def test_type_error_datetime(self):
with self.assertRaises(FieldTypeError) as e:
class_instance.test(
{'request_id': str(uuid4()),
Expand All @@ -156,7 +156,7 @@ def test_type_error_3(self):
self.assertEqual({'expectedType': '{"type": "<class \'datetime.date\'>", "format": "%Y-%m-%d"}'},
e.exception.error_data)

def test_type_error_4(self):
def test_type_error_bool(self):
with self.assertRaises(FieldTypeError) as e:
class_instance.test(
{'request_id': str(uuid4()),
Expand All @@ -172,7 +172,7 @@ def test_type_error_4(self):
self.assertEqual("location_check should be of type <class 'bool'>", e.exception.message)
self.assertEqual({'expectedType': "<class 'bool'>"}, e.exception.error_data)

def test_value_error_1(self):
def test_value_error_value_list(self):
with self.assertRaises(FieldValueError) as e:
class_instance.test(
{'request_id': str(uuid4()),
Expand All @@ -187,7 +187,7 @@ def test_value_error_1(self):
self.assertEqual(f'id_list[1] should be one of these - [0, 1, 2, 3]', e.exception.message)
self.assertEqual({"allowedValue": [0, 1, 2, 3]}, e.exception.error_data)

def test_value_error_2(self):
def test_value_error_min_value(self):
with self.assertRaises(FieldValueError) as e:
class_instance.test(
{'request_id': str(uuid4()),
Expand All @@ -196,12 +196,12 @@ def test_value_error_2(self):
"first_name": "sabari"
, "phone_number": "8884233317"
}}]})
self.assertEqual(ErrorCode.FIELD_MIN_RANGE_EXCEEDED, e.exception.error_code)
self.assertEqual(ErrorCode.FIELD_MIN_RANGE_VIOLATED, e.exception.error_code)
self.assertEqual('pageNo', e.exception.field_name)
self.assertEqual('pageNo should be greater than or equal to 1', e.exception.message)
self.assertEqual({'minValue': 1}, e.exception.error_data)

def test_value_error_3(self):
def test_value_error_max_value(self):
with self.assertRaises(FieldValueError) as e:
class_instance.test(
{'request_id': str(uuid4()),
Expand All @@ -210,7 +210,7 @@ def test_value_error_3(self):
"first_name": "sabari"
, "phone_number": "8884233317"
}}]})
self.assertEqual(ErrorCode.FIELD_MAX_RANGE_EXCEEDED, e.exception.error_code)
self.assertEqual(ErrorCode.FIELD_MAX_RANGE_VIOLATED, e.exception.error_code)
self.assertEqual('pageNo', e.exception.field_name)
self.assertEqual('pageNo should be lesser than or equal to 10', e.exception.message)
self.assertEqual({'maxValue': 10}, e.exception.error_data)
Expand Down
8 changes: 5 additions & 3 deletions funcargpreprocessor/errorcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
Date : 15-Oct-2020
"""

from enum import Enum

class ErrorCode:

class ErrorCode(Enum):
MISSING_MANDATORY_FIELD = 1000
ERRONEOUS_FIELD_TYPE = 1100
UN_RECOGNIZED_FIELD = 1200
FIELD_VALUE_ERROR = 1300
FIELD_MIN_RANGE_EXCEEDED = 1310
FIELD_MIN_RANGE_VIOLATED = 1310
FIELD_MIN_LENGTH_VIOLATED = 1311
FIELD_MAX_RANGE_EXCEEDED = 1315
FIELD_MAX_RANGE_VIOLATED = 1315
FIELD_MAX_LENGTH_VIOLATED = 1316
FIELD_VALUE_NOT_IN_ALLOWED_LIST = 1320
FIELD_REGEX_VALIDATION_FAILED = 1330
4 changes: 2 additions & 2 deletions funcargpreprocessor/funcargpreprocesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ def check_constraint(value, key, min_val=None, max_val=None, value_list=None, re
:return:
"""
if min_val and value < min_val:
raise FieldValueError(ErrorCode.FIELD_MIN_RANGE_EXCEEDED, key,
raise FieldValueError(ErrorCode.FIELD_MIN_RANGE_VIOLATED, key,
f"{key} should be greater than or equal to {min_val}",
{"minValue": min_val})
if max_val and value > max_val:
raise FieldValueError(ErrorCode.FIELD_MAX_RANGE_EXCEEDED, key,
raise FieldValueError(ErrorCode.FIELD_MAX_RANGE_VIOLATED, key,
f"{key} should be lesser than or equal to {max_val}",
{"maxValue": max_val})
if value_list and value not in value_list:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
long_description = fh.read()

setup(name='funcargpreprocessor',
version='0.8.1',
version='0.9.0',
python_requires='>=3.6',
description='Parser for function arguments',
url='https://github.com/sabariramc/funcargpreprocessor',
Expand Down

0 comments on commit 39f741c

Please sign in to comment.