Skip to content

Commit

Permalink
new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anishfyle committed Jul 29, 2024
1 parent 799d804 commit 8727cfe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
14 changes: 6 additions & 8 deletions apps/mappings/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def sync_custom_field(self, source_type: str, field_mapping: FieldMapping, sync_
'type': 'eq.SELECT',
'is_enabled': 'eq.true'
}
custom_fields = self.platform.v1beta.admin.expense_custom_fields.list_all(query)
custom_fields = self.platform.v1beta.admin.expense_fields.list_all(query)
query = QBDMapping.objects.filter(workspace_id=self.workspace_id, attribute_type=source_type)
existing_source_attributes = query.values_list('value', flat=True)

Expand Down Expand Up @@ -96,7 +96,6 @@ def sync_custom_field(self, source_type: str, field_mapping: FieldMapping, sync_
if source_attributes:
QBDMapping.update_or_create_mapping_objects(source_attributes, self.workspace_id)


def sync_projects(self, source_type: str):
"""
Sync PROJECT as Item
Expand All @@ -117,18 +116,17 @@ def sync_projects(self, source_type: str):
if project['name'] not in existing_projects:
source_attributes.append({
'attribute_type': source_type,
'source_value': project['name'],
'value': project['name'],
'source_id': project['id']
})


if source_attributes:
QBDMapping.update_or_create_mapping_objects(source_attributes, self.workspace_id)


def sync_cost_center(self, source_type: str):
"""
Sync PROJECT as Item
Sync COST CENTER as Item
"""

query = {
Expand All @@ -141,11 +139,11 @@ def sync_cost_center(self, source_type: str):

for cost_centers in cost_center_generator:
for cost_center in cost_centers.get('data'):
if cost_center['name'] not in existing_cost_centers:
if cost_center not in existing_cost_centers:
source_attributes.append({
'attribute_type': source_type,
'source_value': cost_center['name'],
'source_id': cost_center['id']
'value': cost_center,
'source_id': cost_center
})


Expand Down
41 changes: 41 additions & 0 deletions tests/test_mapping/test_connector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from apps.mappings.connector import PlatformConnector
from apps.mappings.models import QBDMapping
from apps.workspaces.models import FieldMapping
from tests.conftest import add_field_mappings
from .fixtures import fixture


Expand All @@ -21,3 +23,42 @@ def test_sync_corporate_card(create_temp_workspace,
for i, _ in enumerate(qbd_mappings):
assert qbd_mappings[i].source_value == fixture['get_qbd_ccc_mapping']['results'][i]['source_value']

@pytest.mark.django_db(databases=['default'], transaction=True)
def test_sync_projects(create_temp_workspace, add_fyle_credentials, mocker):
workspace_id = 1
source_type = 'PROJECT'
mock_response = [
{'data': [
{'id': 1, 'name': 'Project 1', 'sub_project': 'Sub Project 1'},
{'id': 2, 'name': 'Project 2', 'sub_project': 'Sub Project 1'}
]}
]
mocker.patch(
'fyle.platform.apis.v1beta.admin.projects.list_all',
return_value=mock_response
)
qbd_connection = PlatformConnector(workspace_id=workspace_id)
qbd_connection.sync_projects(source_type)
qbd_mappings = QBDMapping.objects.filter(workspace_id=workspace_id, attribute_type=source_type)
assert len(qbd_mappings) == 2
mapping = qbd_mappings.first()
assert mapping.source_value == 'Project 1 / Sub Project 1'

@pytest.mark.django_db(databases=['default'], transaction=True)
def test_sync_cost_center(create_temp_workspace, add_fyle_credentials, mocker):
workspace_id = 1
source_type = 'COST_CENTER'
mock_response = [
{'data': ['Cost Center 1', 'Cost Center 2']}
]
mocker.patch(
'fyle.platform.apis.v1beta.admin.cost_centers.list_all',
return_value=mock_response
)
qbd_connection = PlatformConnector(workspace_id=workspace_id)
qbd_connection.sync_cost_center(source_type)
qbd_mappings = QBDMapping.objects.filter(workspace_id=workspace_id, attribute_type=source_type)
assert len(qbd_mappings) == len(mock_response[0]['data'])
for i, mapping in enumerate(qbd_mappings):
cost_center = mock_response[0]['data'][i]
assert mapping.source_value == cost_center

0 comments on commit 8727cfe

Please sign in to comment.