Skip to content

Commit

Permalink
add project import related test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 committed Jul 18, 2024
1 parent 37445f9 commit bfcc8b0
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/test_mappings/test_imports/test_modules/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,44 @@ def add_project_mappings():
detail='Sage 300 Project - Platform APIs, Id - 10081',
active=True
)
DestinationAttribute.objects.create(
workspace_id=workspace_id,
attribute_type='JOB',
display_name='CRE Platform',
value='CRE Platform',
destination_id='10065',
detail='Sage 300 Project - CRE Platform, Id - 10065',
active=True,
code='123'
)
DestinationAttribute.objects.create(
workspace_id=workspace_id,
attribute_type='JOB',
display_name='Integrations CRE',
value='Integrations CRE',
destination_id='10082',
detail='Sage 300 Project - Integrations CRE, Id - 10082',
active=True,
code='123'
)
ExpenseAttribute.objects.create(
workspace_id=workspace_id,
attribute_type='PROJECT',
display_name='CRE Platform',
value='123 CRE Platform',
source_id='10065',
detail='Sage 300 Project - 123 CRE Platform, Id - 10065',
active=True
)
ExpenseAttribute.objects.create(
workspace_id=workspace_id,
attribute_type='PROJECT',
display_name='Integrations CRE',
value='123 Integrations CRE',
source_id='10082',
detail='Sage 300 Project - 123 Integrations CRE, Id - 10082',
active=True
)


@pytest.fixture()
Expand Down
14 changes: 14 additions & 0 deletions tests/test_mappings/test_imports/test_modules/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8188,4 +8188,18 @@
"offset": 0,
}
],
"create_fyle_project_payload_with_code_create_new_case":[
{
'name': '123 CRE Platform',
'code': '10065',
'description': 'Sage 300 Project - 123 CRE Platform, Id - 10065',
'is_enabled': True
},
{
'name': '123 Integrations CRE',
'code': '10082',
'description': 'Sage 300 Project - 123 Integrations CRE, Id - 10082',
'is_enabled': True
}
]
}
49 changes: 49 additions & 0 deletions tests/test_mappings/test_imports/test_modules/test_projects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from apps.mappings.imports.modules.projects import Project
from apps.workspaces.models import ImportSetting
from fyle_accounting_mappings.models import DestinationAttribute
from .fixtures import data

Expand Down Expand Up @@ -57,3 +58,51 @@ def test_construct_fyle_payload(api_client, test_connection, mocker, create_temp
)

assert fyle_payload == data['create_fyle_project_payload_create_disable_case2']


def test_get_existing_fyle_attributes(db, create_temp_workspace, add_project_mappings, add_import_settings):
project = Project(1, 'PROJECT', None)

paginated_destination_attributes = DestinationAttribute.objects.filter(workspace_id=1, attribute_type='JOB')
paginated_destination_attributes_without_duplicates = project.remove_duplicate_attributes(paginated_destination_attributes)
paginated_destination_attribute_values = [attribute.value for attribute in paginated_destination_attributes_without_duplicates]
existing_fyle_attributes_map = project.get_existing_fyle_attributes(paginated_destination_attribute_values)

assert existing_fyle_attributes_map == {}

# with code prepending
project.use_code_in_naming = True
paginated_destination_attributes_without_duplicates = project.remove_duplicate_attributes(paginated_destination_attributes)
paginated_destination_attribute_values = [attribute.value for attribute in paginated_destination_attributes_without_duplicates]
existing_fyle_attributes_map = project.get_existing_fyle_attributes(paginated_destination_attribute_values)

assert existing_fyle_attributes_map == {'123 cre platform': '10065', '123 integrations cre': '10082'}


def test_construct_fyle_payload_with_code(db, create_temp_workspace, add_project_mappings, add_cost_category, add_import_settings):
project = Project(1, 'PROJECT', None, True)
project.use_code_in_naming = True

paginated_destination_attributes = DestinationAttribute.objects.filter(workspace_id=1, attribute_type='JOB')
paginated_destination_attributes_without_duplicates = project.remove_duplicate_attributes(paginated_destination_attributes)
paginated_destination_attribute_values = [attribute.value for attribute in paginated_destination_attributes_without_duplicates]
existing_fyle_attributes_map = project.get_existing_fyle_attributes(paginated_destination_attribute_values)

# already exists
fyle_payload = project.construct_fyle_payload(
paginated_destination_attributes,
existing_fyle_attributes_map,
True
)

assert fyle_payload == []

# create new case
existing_fyle_attributes_map = {}
fyle_payload = project.construct_fyle_payload(
paginated_destination_attributes,
existing_fyle_attributes_map,
True
)

assert fyle_payload == data["create_fyle_project_payload_with_code_create_new_case"]
109 changes: 109 additions & 0 deletions tests/test_sage300/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,112 @@ def test_sync_cost_codes(

result = sage_connector.sync_cost_codes(cost_code_import_log)
assert result == []


def test_bulk_create_or_update_destination_attributes_with_code(db, create_temp_workspace):
workspace_id = 1

# Dummy data for testing
attributes = [
{
'attribute_type': 'COST_CODE',
'display_name': 'Display 1',
'value': 'Value 1',
'destination_id': 'DestID1',
'detail': {'info': 'Detail 1'},
'active': True,
'code': 'Code1'
},
{
'attribute_type': 'COST_CODE',
'display_name': 'Display 2',
'value': 'Value 2',
'destination_id': 'DestID2',
'detail': {'info': 'Detail 2'},
'active': False,
'code': 'Code2'
}
]

# Call the function to test
DestinationAttribute.bulk_create_or_update_destination_attributes(
attributes=attributes,
attribute_type='COST_CODE',
workspace_id=workspace_id,
update=True
)

# Verify creation
assert DestinationAttribute.objects.filter(destination_id='DestID1').exists()
assert DestinationAttribute.objects.filter(destination_id='DestID2').exists()

# Update data
attributes[0]['value'] = 'Updated Value 1'
attributes[1]['value'] = 'Updated Value 2'
attributes[0]['code'] = 'Updated Code1'
attributes[1]['code'] = 'Updated Code2'
DestinationAttribute.bulk_create_or_update_destination_attributes(
attributes=attributes,
attribute_type='COST_CODE',
workspace_id=workspace_id,
update=True
)

# Verify update
destination_attributes = DestinationAttribute.objects.filter(destination_id__in=['DestID1', 'DestID2'])
destination_attributes_dict = {attr.destination_id: attr for attr in destination_attributes}

assert destination_attributes_dict['DestID1'].value == 'Updated Value 1'
assert destination_attributes_dict['DestID2'].value == 'Updated Value 2'
assert destination_attributes_dict['DestID1'].code == 'Updated Code1'
assert destination_attributes_dict['DestID2'].code == 'Updated Code2'


def test_bulk_create_or_update_destination_attributes_without_code(db, create_temp_workspace):
workspace_id = 1

# Dummy data for testing
attributes = [
{
'attribute_type': 'COST_CODE',
'display_name': 'Display 1',
'value': 'Value 1',
'destination_id': 'DestID1',
'detail': {'info': 'Detail 1'},
'active': True,
},
{
'attribute_type': 'COST_CODE',
'display_name': 'Display 2',
'value': 'Value 2',
'destination_id': 'DestID2',
'detail': {'info': 'Detail 2'},
'active': False,
}
]

# Call the function to test
DestinationAttribute.bulk_create_or_update_destination_attributes(
attributes=attributes,
attribute_type='COST_CODE',
workspace_id=workspace_id,
update=True
)

# Verify creation
assert DestinationAttribute.objects.filter(destination_id='DestID1').exists()
assert DestinationAttribute.objects.filter(destination_id='DestID2').exists()

# Update data
attributes[0]['value'] = 'Updated Value 1'
attributes[1]['value'] = 'Updated Value 2'
DestinationAttribute.bulk_create_or_update_destination_attributes(
attributes=attributes,
attribute_type='COST_CODE',
workspace_id=workspace_id,
update=True
)

# Verify update
assert DestinationAttribute.objects.get(destination_id='DestID1').value == 'Updated Value 1'
assert DestinationAttribute.objects.get(destination_id='DestID2').value == 'Updated Value 2'

0 comments on commit bfcc8b0

Please sign in to comment.