From 8727cfe8586c344bfe5ca0ed2d4112875c699759 Mon Sep 17 00:00:00 2001 From: anishfyle Date: Mon, 29 Jul 2024 20:12:15 +0530 Subject: [PATCH] new unit tests --- apps/mappings/connector.py | 14 ++++------ tests/test_mapping/test_connector.py | 41 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/apps/mappings/connector.py b/apps/mappings/connector.py index 1cbd26b..399459e 100644 --- a/apps/mappings/connector.py +++ b/apps/mappings/connector.py @@ -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) @@ -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 @@ -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 = { @@ -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 }) diff --git a/tests/test_mapping/test_connector.py b/tests/test_mapping/test_connector.py index 365823a..3cdd223 100644 --- a/tests/test_mapping/test_connector.py +++ b/tests/test_mapping/test_connector.py @@ -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 @@ -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 \ No newline at end of file