-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support of error parser for expense report export (#113)
- Loading branch information
1 parent
8a14ba5
commit bafb51a
Showing
10 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
error_reference = { | ||
"expense_report": | ||
{ | ||
'category_reference_error': {'regex': r"An error occured in a upsert request: Invalid category reference key \d+ for entity \d+", 'keys': ['expense_category', 'employee']}, | ||
'account_reference_error': {'regex': r"An error occured in a upsert request: Invalid account reference key \d+ for subsidiary \d+", 'keys': ['account', 'subsidiary']}, | ||
'project_reference_error': {'regex': r"An error occured in a upsert request: Invalid customer reference key \d+ for entity \d+", 'keys': ['customer', 'employee']}, | ||
'location_reference_error': {'regex': r"An error occured in a upsert request: Invalid location reference key \d+ for subsidiary \d+", 'keys': ['location', 'subsidiary']}, | ||
'department_reference_error': {'regex':r"An error occured in a upsert request: Invalid department reference key \d+ for subsidiary \d+" , 'keys': ['department', 'subsidiary']}, | ||
'currency_reference_error': {'regex': r"An error occured in a upsert request: Invalid currency reference key \d+ for subsidiary \d+", 'keys': ['currency', 'subsidiary']} | ||
} | ||
} | ||
|
||
list_of_dicts = [ | ||
['expense_category', 'employee'], ['account', 'subsidiary'], | ||
['customer', 'employee'], ['location', 'subsidiary'], | ||
['department', 'subsidiary'], ['currency', 'subsidiary'] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import re | ||
from .errors import error_reference | ||
|
||
|
||
def replace_numbers(string , replacement1, replacement2, number1, number2): | ||
replaced_string = re.sub(r'\b({}|{})\b'.format(number1, number2), lambda m: replacement1 if m.group() == number1 else replacement2, string) | ||
return replaced_string | ||
|
||
|
||
def convert_to_camelcase(word): | ||
return ''.join(word.title().split('_')) | ||
|
||
|
||
def export_error_matcher(string, export_type): | ||
for _, error_data in error_reference[export_type].items(): | ||
if re.match(error_data['regex'], string): | ||
numbers = re.findall(r'\d+', string) | ||
return {key: int(number) for key, number in zip(error_data['keys'], numbers)} | ||
|
||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from .helpers import replace_numbers, convert_to_camelcase | ||
from .errors import list_of_dicts | ||
|
||
class ErrorParser(): | ||
|
||
def __init__(self, get_instance): | ||
self.get_instance = get_instance | ||
|
||
def get_entity_values(self, error_dict): | ||
|
||
entity_keys = list(error_dict) | ||
object_1 = self.get_instance(convert_to_camelcase(entity_keys[0]), error_dict[entity_keys[0]]) | ||
object_2 = self.get_instance(convert_to_camelcase(entity_keys[1]), error_dict[entity_keys[1]]) | ||
|
||
if object_1 and object_2: | ||
if entity_keys[1] == 'employee': | ||
object_2 = object_2['email'] if object_2['email'] else object_2['firstName'] + " " + object_2['lastName'] | ||
return object_1['name'], object_2 | ||
|
||
if entity_keys[0] == 'account': | ||
object_1 = object_1['acctName'] | ||
return object_1, object_2['name'] | ||
|
||
return object_1['name'], object_2['name'] | ||
|
||
|
||
def export_error_parser(self, error_dict, message): | ||
|
||
parsed_message = message | ||
if list(error_dict) in list_of_dicts: | ||
object_1, object_2 = self.get_entity_values(error_dict) | ||
entity_keys = list(error_dict) | ||
parsed_message = replace_numbers(message, object_1, object_2, error_dict[entity_keys[0]], error_dict[entity_keys[1]]) | ||
|
||
return parsed_message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
setuptools.setup( | ||
name='netsuitesdk', | ||
version='2.17.1', | ||
version='2.18.0', | ||
author='Siva Narayanan', | ||
author_email='[email protected]', | ||
description='Python SDK for accessing the NetSuite SOAP webservice', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pytest | ||
from netsuitesdk.errors.helpers import replace_numbers, convert_to_camelcase, export_error_matcher | ||
|
||
errors = [ | ||
('An error occured in a upsert request: Invalid category reference key 1 for entity 12', {'expense_category': 1, 'employee': 12}), | ||
('An error occured in a upsert request: Invalid account reference key 1 for subsidiary 12', {'account': 1, 'subsidiary': 12}), | ||
('An error occured in a upsert request: Invalid customer reference key 1 for entity 12', {'customer': 1, 'employee': 12}), | ||
('An error occured in a upsert request: Invalid location reference key 1 for subsidiary 12', {'location': 1, 'subsidiary': 12}), | ||
('An error occured in a upsert request: Invalid department reference key 1 for subsidiary 12', {'department': 1, 'subsidiary': 12}), | ||
('An error occured in a upsert request: Invalid currency reference key 1 for subsidiary 12', {'currency': 1, 'subsidiary': 12}) | ||
] | ||
|
||
def test_replace_number(): | ||
final_string = "An error occured in a upsert request: Invalid category reference key Travel for entity John Doe" | ||
replaced_string = replace_numbers('An error occured in a upsert request: Invalid category reference key 1 for entity 2', 'Travel', 'John Doe', '1', '2') | ||
assert final_string == replaced_string | ||
|
||
|
||
def test_convert_to_camelcase(): | ||
entity_type = 'ExpenseCategory' | ||
result = convert_to_camelcase('expense_category') | ||
assert entity_type == result | ||
|
||
entity_type = 'Currency' | ||
result = convert_to_camelcase('currency') | ||
assert entity_type == result | ||
|
||
|
||
@pytest.mark.parametrize("input, output", errors) | ||
def test_export_error_matcher(input, output): | ||
|
||
result = export_error_matcher(input, 'expense_report') | ||
assert result == output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
from netsuitesdk.errors.parser import ErrorParser | ||
|
||
|
||
def test_export_error_parser(mocker, ns): | ||
|
||
mocker.patch( | ||
'netsuitesdk.errors.parser.ErrorParser.get_entity_values', | ||
return_value={'Travel', 'John Doe'} | ||
) | ||
|
||
parser = ErrorParser(ns) | ||
result = parser.export_error_parser({'expense_category': '1', 'employee': '12'}, 'An error occured in a upsert request: Invalid category reference key 1 for entity 12') | ||
assert result == "An error occured in a upsert request: Invalid category reference key Travel for entity John Doe" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters