Skip to content

Commit

Permalink
fix vendor case check (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 committed Aug 22, 2024
1 parent 715f85b commit 50c216b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
4 changes: 1 addition & 3 deletions apps/sage_intacct/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,7 @@ def get_or_create_vendor(self, vendor_name: str, email: str = None, create: bool
:param create: False to just Get and True to Get or Create if not exists
:return: Vendor
"""
vendor_name = self.sanitize_vendor_name(vendor_name)
vendor_from_db = DestinationAttribute.objects.filter(workspace_id=self.workspace_id, attribute_type='VENDOR', value=vendor_name, active=True).first()

vendor_from_db = DestinationAttribute.objects.filter(workspace_id=self.workspace_id, attribute_type='VENDOR', value__iexact=vendor_name.lower(), active=True).first()
if vendor_from_db:
return vendor_from_db

Expand Down
46 changes: 39 additions & 7 deletions tests/test_sageintacct/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,11 @@ def test_get_expense_link(mocker, db, create_journal_entry):

def test_get_or_create_vendor(mocker, db):
workspace_id = 1
mocker.patch(
get_call_mock = mocker.patch(
'sageintacctsdk.apis.Vendors.get',
return_value={'vendor': data['get_vendors'], '@totalcount': 2}
)
mocker.patch(
post_call_mock = mocker.patch(
'sageintacctsdk.apis.Vendors.post',
return_value=data['post_vendors']
)
Expand Down Expand Up @@ -728,15 +728,47 @@ def test_get_or_create_vendor(mocker, db):
assert vendor.id == new_vendor.id
assert vendor.value == 'Already existing vendor in DB'

mocker.patch(
'sageintacctsdk.apis.Vendors.get',
return_value={'VENDOR': data['get_vendors'], '@totalcount': 2}
)
get_call_mock.return_value = {'VENDOR': data['get_vendors'], '@totalcount': 2}

vendor = sage_intacct_connection.get_or_create_vendor('Non existing vendor in DB', '[email protected]', False)

assert vendor.value == 'Ashwin'

# case insensitive search in db
vendor_from_db = DestinationAttribute.objects.filter(workspace_id=workspace_id, attribute_type='VENDOR').first()
vendor_from_db.value = 'already Existing VENDOR iN Db UsE aLl CaSeS'
vendor_from_db.active = True
vendor_from_db.save()

assert DestinationAttribute.objects.filter(workspace_id=workspace_id, attribute_type='VENDOR', value='already Existing VENDOR iN Db UsE aLl CaSeS').exists() is True
vendor_to_create = 'Already eXisting VeNdor In dB UsE aLl caSES'
vendor = sage_intacct_connection.get_or_create_vendor(vendor_to_create, create=True)

assert vendor.value == vendor_from_db.value
assert vendor.id == vendor_from_db.id

# case insensitive not found in db -> search in intacct and found
data['get_vendors'][0]['NAME'] = 'Non existing vendor in DB use all cases'

get_call_mock.return_value = {'VENDOR': data['get_vendors'], '@totalcount': 2}

vendor = sage_intacct_connection.get_or_create_vendor('non exiSting VENDOR iN Db UsE aLl CaSeS', create=True)

assert vendor.value == 'Non existing vendor in DB use all cases'
assert DestinationAttribute.objects.filter(workspace_id=workspace_id, attribute_type='VENDOR', value='Non existing vendor in DB use all cases').exists() is True

# case insensitive not found in db -> search in intacct and not found -> create new in intacct
get_call_mock.return_value = {}

new_post_vendors_data = data['post_vendors']
new_post_vendors_data['data']['vendor']['NAME'] = 'non exiSting VENDOR iN intacct UsE aLl CaSeS'
new_post_vendors_data['data']['vendor']['VENDORID'] = 'non exiSting VENDOR iN intacct UsE aLl CaSeS'

post_call_mock.return_value = new_post_vendors_data

vendor = sage_intacct_connection.get_or_create_vendor('non exiSting VENDOR iN intacct UsE aLl CaSeS', create=True)
assert vendor.destination_id == 'non exiSting VENDOR iN intacct UsE aLl CaSeS'


def test_get_or_create_employee(mocker, db):
workspace_id = 1
Expand All @@ -761,7 +793,7 @@ def test_get_or_create_employee(mocker, db):
new_employee_count = DestinationAttribute.objects.filter(workspace_id=workspace_id, attribute_type='EMPLOYEE').count()
assert new_employee_count == 55


def test_sanitize_vendor_name(db):
workspace_id = 1
sage_intacct_credentials = SageIntacctCredential.objects.get(workspace_id=workspace_id)
Expand Down

0 comments on commit 50c216b

Please sign in to comment.