Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Logs for Deps Import #186

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
f6df1ec
Delete purchase_invoice, line_items on failed exports from hh2, type-…
Hrishabh17 May 21, 2024
372c628
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 May 21, 2024
d0ad337
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 May 24, 2024
7825b9e
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 May 27, 2024
b79c74c
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 May 27, 2024
97779c6
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 May 28, 2024
601e3cb
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 May 28, 2024
fb78432
Refactor deps schedule to run post Job import
Hrishabh17 Jun 3, 2024
57a4ed4
Fyle Card <> Vendor Mapping setup
Hrishabh17 Jun 3, 2024
19dfa7d
Added script to add mapping_settings
Hrishabh17 Jun 3, 2024
151e397
Fix post release script
Hrishabh17 Jun 4, 2024
ffbe1f5
Projects and Deps fields disable v1
Hrishabh17 Jun 4, 2024
372e5ab
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 Jun 4, 2024
737c79d
Remove dep setting trigger, add logger
Hrishabh17 Jun 4, 2024
da072df
Merge branch 'refactor-deps-import' into vendor-card-mapping
Hrishabh17 Jun 4, 2024
e2f4cef
Modified script, added additional test case
Hrishabh17 Jun 4, 2024
5bc9075
lint fix
Hrishabh17 Jun 4, 2024
c6a4697
Remove mock object
Hrishabh17 Jun 4, 2024
f667558
Merge branch 'refactor-deps-import' into vendor-card-mapping
Hrishabh17 Jun 4, 2024
1c2e3c1
Add details while logging
Hrishabh17 Jun 4, 2024
0e98a8a
Merge branch 'refactor-deps-import' into vendor-card-mapping
Hrishabh17 Jun 4, 2024
d23f0cd
modify post-release script
Hrishabh17 Jun 4, 2024
7dd6145
Merge branch 'vendor-card-mapping' into disable-sage-fields
Hrishabh17 Jun 4, 2024
f1a31f1
bump accounting-mapping version
Hrishabh17 Jun 4, 2024
505fabc
modify the variable_name, add conditional update
Hrishabh17 Jun 5, 2024
e85f6b2
Add example objects
Hrishabh17 Jun 5, 2024
b1f39ca
Added loggers
Hrishabh17 Jun 5, 2024
555a72c
Added test cases
Hrishabh17 Jun 5, 2024
675c37a
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 Jun 5, 2024
84ba2e9
Dependent Field optimizations
Hrishabh17 Jun 6, 2024
6dd28a9
fix failing test
Hrishabh17 Jun 6, 2024
aa96f62
Added Import Log for Deps
Hrishabh17 Jun 6, 2024
51e76e5
Fix post-release script
Hrishabh17 Jun 6, 2024
6cbcb7d
Merge branch 'dep-field-optimization' into dep-import-log
Hrishabh17 Jun 6, 2024
6c54fdc
fix failing test cases
Hrishabh17 Jun 6, 2024
1da8a7d
Set cost category import to fail on cost code fail
Hrishabh17 Jun 7, 2024
9aefa9f
Modify handle_import_exception for both class and func
Hrishabh17 Jun 7, 2024
173d31a
Modify test cases
Hrishabh17 Jun 7, 2024
5dac65e
fix ordering of saving import_log
Hrishabh17 Jun 7, 2024
ef99211
Move the import_log creation to ImportLog method
Hrishabh17 Jun 7, 2024
771c939
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 Jun 10, 2024
0d688ac
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 Jun 11, 2024
53d90b1
Merge branch 'master' of github.com:fylein/fyle-sage-desktop-api
Hrishabh17 Jun 11, 2024
766beb3
Merged master and bumped accounting-mappings version
Hrishabh17 Jun 11, 2024
8393600
Add logger in import
Hrishabh17 Jun 12, 2024
7a6d4fa
Merge branch 'master' into dep-import-log
Hrishabh17 Jun 12, 2024
30b7c50
Merge branch 'master' into dep-import-log
Hrishabh17 Jun 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/fyle/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from apps.workspaces.models import Workspace, FyleCredential
from apps.fyle.models import ExpenseFilter, DependentFieldSetting
from apps.fyle.helpers import get_expense_fields

from apps.mappings.imports.queues import chain_import_fields_to_fyle

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand All @@ -39,6 +39,7 @@ def create(self, validated_data):
platform = PlatformConnector(fyle_credentials)

if refresh:
chain_import_fields_to_fyle(workspace_id=workspace_id)
platform.import_fyle_dimensions()
workspace.source_synced_at = datetime.now()
workspace.save(update_fields=['source_synced_at'])
Expand Down
10 changes: 7 additions & 3 deletions apps/mappings/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@


def handle_import_exceptions(func):
def new_fn(expense_attribute_instance, *args):
import_log: ImportLog = args[0]
def new_fn(expense_attribute_instance, *args, **kwargs):
import_log = None
if isinstance(expense_attribute_instance, ImportLog):
import_log: ImportLog = expense_attribute_instance
else:
import_log: ImportLog = args[0]
workspace_id = import_log.workspace_id
attribute_type = import_log.attribute_type
error = {
Expand All @@ -28,7 +32,7 @@ def new_fn(expense_attribute_instance, *args):
'response': None
}
try:
return func(expense_attribute_instance, *args)
return func(expense_attribute_instance, *args, **kwargs)
except WrongParamsError as exception:
error['message'] = exception.message
error['response'] = exception.response
Expand Down
Empty file added apps/mappings/helpers.py
Empty file.
7 changes: 7 additions & 0 deletions apps/mappings/imports/modules/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import logging
from typing import List
from datetime import (
datetime,
Expand All @@ -20,6 +21,10 @@
from apps.accounting_exports.models import Error


logger = logging.getLogger(__name__)
logger.level = logging.INFO


class Base:
"""
The Base class for all the modules
Expand Down Expand Up @@ -299,6 +304,8 @@ def post_to_fyle_and_sync(self, fyle_payload: List[object], resource_class, is_l
:param is_last_batch: bool
:param import_log: ImportLog object
"""
logger.info("| Importing {} to Fyle | Content: {{WORKSPACE_ID: {} Fyle Payload count: {} is_last_batch: {}}}".format(self.destination_field, self.workspace_id, len(fyle_payload), is_last_batch))

if fyle_payload and self.platform_class_name in ['expense_custom_fields', 'merchants']:
resource_class.post(fyle_payload)
elif fyle_payload:
Expand Down
7 changes: 5 additions & 2 deletions apps/mappings/imports/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ def auto_import_and_map_fyle_fields(workspace_id):

chain = Chain()

cost_code_import_log = ImportLog.create_import_log('COST_CODE', workspace_id)
cost_category_import_log = ImportLog.create_import_log('COST_CATEGORY', workspace_id)

chain.append('apps.mappings.tasks.sync_sage300_attributes', 'JOB', workspace_id)
chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CODE', workspace_id)
chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CATEGORY', workspace_id)
chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CODE', workspace_id, cost_code_import_log)
chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CATEGORY', workspace_id, cost_category_import_log)
chain.append('apps.sage300.dependent_fields.import_dependent_fields_to_fyle', workspace_id)

if import_log and import_log.status != 'COMPLETE':
Expand Down
14 changes: 14 additions & 0 deletions apps/mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ class Meta:
db_table = 'import_logs'
unique_together = ('workspace', 'attribute_type')

@classmethod
def create_import_log(self, attribute_type, workspace_id):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create()

"""
Create import logs set to IN_PROGRESS
"""
import_log, _ = self.objects.update_or_create(
workspace_id=workspace_id,
attribute_type=attribute_type,
defaults={
'status': 'IN_PROGRESS'
}
)
return import_log


class Version(BaseModel):
"""
Expand Down
7 changes: 4 additions & 3 deletions apps/mappings/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from apps.workspaces.models import Sage300Credential
from apps.sage300.utils import SageDesktopConnector
from apps.mappings.models import ImportLog


def sync_sage300_attributes(sage300_attribute_type: str, workspace_id: int):
def sync_sage300_attributes(sage300_attribute_type: str, workspace_id: int, import_log: ImportLog = None):
sage300_credentials: Sage300Credential = Sage300Credential.objects.get(workspace_id=workspace_id)

sage300_connection = SageDesktopConnector(
Expand All @@ -12,8 +13,8 @@ def sync_sage300_attributes(sage300_attribute_type: str, workspace_id: int):

sync_functions = {
'JOB': sage300_connection.sync_jobs,
'COST_CODE': sage300_connection.sync_cost_codes,
'COST_CATEGORY': sage300_connection.sync_cost_categories,
'COST_CODE': lambda:sage300_connection.sync_cost_codes(import_log),
'COST_CATEGORY': lambda:sage300_connection.sync_cost_categories(import_log),
'ACCOUNT': sage300_connection.sync_accounts,
'VENDOR': sage300_connection.sync_vendors,
'COMMITMENT': sage300_connection.sync_commitments,
Expand Down
29 changes: 25 additions & 4 deletions apps/sage300/dependent_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from apps.fyle.models import DependentFieldSetting
from apps.sage300.models import CostCategory
from apps.fyle.helpers import connect_to_platform
from apps.mappings.models import ImportLog
from apps.mappings.exceptions import handle_import_exceptions

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand Down Expand Up @@ -68,7 +70,8 @@ def create_dependent_custom_field_in_fyle(workspace_id: int, fyle_attribute_type
return platform.expense_custom_fields.post(expense_custom_field_payload)


def post_dependent_cost_code(dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict, is_enabled: bool = True) -> List[str]:
@handle_import_exceptions
def post_dependent_cost_code(import_log: ImportLog, dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict, is_enabled: bool = True) -> List[str]:
projects = CostCategory.objects.filter(**filters).values('job_name').annotate(cost_codes=ArrayAgg('cost_code_name', distinct=True))
projects_from_categories = [project['job_name'] for project in projects]
posted_cost_codes = []
Expand Down Expand Up @@ -103,10 +106,14 @@ def post_dependent_cost_code(dependent_field_setting: DependentFieldSetting, pla
logger.error(f'Exception while posting dependent cost code | Error: {exception} | Payload: {payload}')
raise

import_log.status = 'COMPLETE'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also store batches count while posting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store batch count in import log?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

import_log.error_log = []
import_log.save()
return posted_cost_codes


def post_dependent_cost_type(dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict):
@handle_import_exceptions
def post_dependent_cost_type(import_log: ImportLog, dependent_field_setting: DependentFieldSetting, platform: PlatformConnector, filters: Dict):
cost_categories = CostCategory.objects.filter(is_imported=False, **filters).values('cost_code_name').annotate(cost_categories=ArrayAgg('name', distinct=True))

for category in cost_categories:
Expand All @@ -129,6 +136,10 @@ def post_dependent_cost_type(dependent_field_setting: DependentFieldSetting, pla
logger.error(f'Exception while posting dependent cost type | Error: {exception} | Payload: {payload}')
raise

import_log.status = 'COMPLETE'
import_log.error_log = []
import_log.save()


def post_dependent_expense_field_values(workspace_id: int, dependent_field_setting: DependentFieldSetting, platform: PlatformConnector = None):
if not platform:
Expand All @@ -141,10 +152,20 @@ def post_dependent_expense_field_values(workspace_id: int, dependent_field_setti
if dependent_field_setting.last_successful_import_at:
filters['updated_at__gte'] = dependent_field_setting.last_successful_import_at

posted_cost_types = post_dependent_cost_code(dependent_field_setting, platform, filters)
cost_code_import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='COST_CODE').first()
cost_category_import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='COST_CATEGORY').first()

posted_cost_types = post_dependent_cost_code(cost_code_import_log, dependent_field_setting, platform, filters)
if posted_cost_types:
filters['cost_code_name__in'] = posted_cost_types
post_dependent_cost_type(dependent_field_setting, platform, filters)

if cost_code_import_log.status in ['FAILED', 'FATAL']:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't there be exception incase of errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both post_cost_code and post_cost_type method are decoarated with handle_import_exception

  1. If post of cost code fails, we handle in exception handler decorator and set the import log status to Failed.
  2. When the call returns we check the status, if its in Complete, we call the post of cost type (here if error occurs the status will be Failed/Fatal). Otherwise we mark complete.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also fixed the call ordering, it was wrong earlier. can check code again for this.

cost_category_import_log.status = 'FAILED'
cost_category_import_log.error_log = {'message': 'Importing COST_CODE failed'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're storing all details in the exception, why are we overriding?

Screenshot 2024-06-13 at 10 59 17 AM

cost_category_import_log.save()
return
else:
post_dependent_cost_type(cost_category_import_log, dependent_field_setting, platform, filters)

DependentFieldSetting.objects.filter(workspace_id=workspace_id).update(last_successful_import_at=datetime.now())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should bring this inside else block, else we might mark timestamp even for failed runs


Expand Down
4 changes: 3 additions & 1 deletion apps/sage300/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from apps.sage300.models import CostCategory
from apps.fyle.models import DependentFieldSetting
from apps.sage300.dependent_fields import post_dependent_cost_code
from apps.mappings.models import ImportLog

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand Down Expand Up @@ -134,9 +135,10 @@ def update_and_disable_cost_code(workspace_id: int, cost_codes_to_disable: Dict,
'job_id__in':list(cost_codes_to_disable.keys()),
'workspace_id': workspace_id
}
cost_code_import_log = ImportLog.create_import_log('COST_CODE', workspace_id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The create_import_log method in apps/mappings/models.py does not contain any try-except blocks to handle exceptions. This could lead to unhandled exceptions during import operations.

  • Location: apps/mappings/models.py, create_import_log method
Analysis chain

Ensure that the create_import_log method correctly handles all possible exceptions and edge cases since it's critical for tracking import operations.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify exception handling in `create_import_log` method.

# Test: Search for exception handling in `create_import_log` method. Expect: Proper exception handling.
rg --type python $'def create_import_log($$$) { try { $$$ } catch ($$$) { $$$ } }' apps/mappings/models.py

Length of output: 281



Script:

#!/bin/bash
# Description: Verify exception handling in `create_import_log` method.

# Find the definition of the `create_import_log` method in the codebase.
ast-grep --lang python --pattern 'def create_import_log($_) { $$$ }' 

# Check for try-except blocks within the `create_import_log` method.
ast-grep --lang python --pattern 'def create_import_log($_) { try: $$$ except: $$$ }'

Length of output: 959



Script:

#!/bin/bash
# Description: Verify exception handling in `create_import_log` method.

# Find the definition of the `create_import_log` method in the codebase.
rg --type python --pcre2 'def create_import_log\('

# Check for try-except blocks within the `create_import_log` method.
rg --type python --pcre2 'def create_import_log\([^\)]*\):(\n|.)*?try:(\n|.)*?except:'

Length of output: 203



Script:

#!/bin/bash
# Description: Verify exception handling in `create_import_log` method.

# Find the definition of the `create_import_log` method in the codebase.
rg --type py --pcre2 'def create_import_log\('

# Check for try-except blocks within the `create_import_log` method.
rg --type py --pcre2 'def create_import_log\([^\)]*\):(\n|.)*?try:(\n|.)*?except:'

Length of output: 220


# This call will disable the cost codes in Fyle that has old project name
posted_cost_codes = post_dependent_cost_code(dependent_field_setting, platform, filters, is_enabled=False)
posted_cost_codes = post_dependent_cost_code(cost_code_import_log, dependent_field_setting, platform, filters, is_enabled=False)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The post_dependent_cost_code function lacks any associated documentation comments or docstrings to describe its parameters and functionality. This needs to be addressed to ensure the changes are well-documented.

  • Location: apps/sage300/dependent_fields.py, around the post_dependent_cost_code function definition.

Please add appropriate docstrings or comments to document the new import_log parameter and the overall functionality of the function.

Analysis chain

Correct usage of the post_dependent_cost_code function with the new import_log parameter. Ensure that the function's modifications are well-documented to reflect these changes.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.

# Test: Search for updated documentation for `post_dependent_cost_code`. Expect: Updated documentation reflecting new parameters.
rg --type python $'def post_dependent_cost_code($$$) { $$$ }' apps/sage300/dependent_fields.py

Length of output: 255



Script:

#!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.

# Test: Search for the function definition and its associated documentation.
rg --type python 'def post_dependent_cost_code' -A 10 apps/sage300/dependent_fields.py

Length of output: 119



Script:

#!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.

# Test: Search for the function definition and its associated documentation.
rg --type py 'def post_dependent_cost_code' -A 10 apps/sage300/dependent_fields.py

Length of output: 748



Script:

#!/bin/bash
# Description: Verify documentation for `post_dependent_cost_code`.

# Test: Search for documentation comments or docstrings around the function definition.
rg --type py 'def post_dependent_cost_code' -A 20 -B 5 apps/sage300/dependent_fields.py

Length of output: 1329


logger.info(f"Disabled Cost Codes in Fyle | WORKSPACE_ID: {workspace_id} | COUNT: {len(posted_cost_codes)}")

Expand Down
7 changes: 5 additions & 2 deletions apps/sage300/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sage_desktop_sdk.sage_desktop_sdk import SageDesktopSDK
from apps.sage300.models import CostCategory
from apps.mappings.models import Version
from apps.mappings.exceptions import handle_import_exceptions

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand Down Expand Up @@ -262,7 +263,8 @@ def sync_commitment_items(self):
]
self._sync_data(commitment_items, 'COMMITMENT_ITEM', 'commitment_item', self.workspace_id, field_names, False)

def sync_cost_codes(self):
@handle_import_exceptions
def sync_cost_codes(self, _import_log = None):
"""
Synchronize cost codes from Sage Desktop SDK to your application
"""
Expand All @@ -272,7 +274,8 @@ def sync_cost_codes(self):
self._sync_data(cost_codes, 'COST_CODE', 'cost_code', self.workspace_id, field_names)
return []

def sync_cost_categories(self):
@handle_import_exceptions
def sync_cost_categories(self, import_log = None):
"""
Synchronize categories from Sage Desktop SDK to your application
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fyle==0.36.1

# Reusable Fyle Packages
fyle-rest-auth==1.7.2
fyle-accounting-mappings==1.33.0
fyle-accounting-mappings==1.33.1
fyle-integrations-platform-connector==1.36.3


Expand Down
1 change: 1 addition & 0 deletions tests/test_fyle/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

def test_import_fyle_attributes(mocker, api_client, test_connection, create_temp_workspace, add_fyle_credentials):
mocker.patch('fyle_integrations_platform_connector.fyle_integrations_platform_connector.PlatformConnector.import_fyle_dimensions', return_value=[])
mocker.patch('apps.fyle.serializers.chain_import_fields_to_fyle', return_value=None)

url = reverse('import-fyle-attributes', kwargs={'workspace_id': 1})

Expand Down
28 changes: 28 additions & 0 deletions tests/test_sage300/test_dependent_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import_dependent_fields_to_fyle
)
from apps.fyle.models import DependentFieldSetting
from apps.mappings.models import ImportLog


def test_construct_custom_field_placeholder():
Expand Down Expand Up @@ -64,14 +65,24 @@ def test_post_dependent_cost_code(
}

dependent_field_settings = DependentFieldSetting.objects.get(workspace_id=workspace_id)
cost_code_import_log = ImportLog.create_import_log('COST_CODE', workspace_id)

result = post_dependent_cost_code(
cost_code_import_log,
dependent_field_setting=dependent_field_settings,
platform=platform.return_value,
filters=filters
)

assert result == ['Direct Mail Campaign', 'Platform APIs']
assert cost_code_import_log.status == 'COMPLETE'

post_dependent_cost_code(
cost_code_import_log,
dependent_field_setting=dependent_field_settings,
platform=platform.return_value
)
assert cost_code_import_log.status == 'FATAL'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how it this going fatal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed a less param (removed filters). To explicitly raise an error.



def test_post_dependent_cost_type(
Expand All @@ -96,13 +107,24 @@ def test_post_dependent_cost_type(

dependent_field_settings = DependentFieldSetting.objects.get(workspace_id=workspace_id)

cost_category_import_log = ImportLog.create_import_log('COST_CATEGORY', workspace_id)

post_dependent_cost_type(
cost_category_import_log,
dependent_field_setting=dependent_field_settings,
platform=platform.return_value,
filters=filters
)

assert platform.return_value.dependent_fields.bulk_post_dependent_expense_field_values.call_count == 2
assert cost_category_import_log.status == 'COMPLETE'

post_dependent_cost_type(
cost_category_import_log,
dependent_field_setting=dependent_field_settings,
platform=platform.return_value
)
assert cost_category_import_log.status == 'FATAL'


def test_post_dependent_expense_field_values(
Expand All @@ -124,6 +146,9 @@ def test_post_dependent_expense_field_values(

dependent_field_settings = DependentFieldSetting.objects.get(workspace_id=workspace_id)

ImportLog.create_import_log('COST_CODE', workspace_id)
ImportLog.create_import_log('COST_CATEGORY', workspace_id)

post_dependent_expense_field_values(
workspace_id=workspace_id,
dependent_field_setting=dependent_field_settings
Expand All @@ -150,6 +175,9 @@ def test_import_dependent_fields_to_fyle(
'dependent_fields.bulk_post_dependent_expense_field_values'
)

ImportLog.create_import_log('COST_CODE', workspace_id)
ImportLog.create_import_log('COST_CATEGORY', workspace_id)

import_dependent_fields_to_fyle(workspace_id)

assert platform.return_value.dependent_fields.bulk_post_dependent_expense_field_values.call_count == 4
Expand Down
11 changes: 9 additions & 2 deletions tests/test_sage300/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fyle_accounting_mappings.models import DestinationAttribute
from apps.mappings.models import Version
from apps.workspaces.models import Workspace
from apps.mappings.models import ImportLog


def test_sage_desktop_connector(
Expand Down Expand Up @@ -453,6 +454,8 @@ def test_sync_cost_categories(
workspace_id=workspace_id
)

cost_category_import_log = ImportLog.create_import_log('COST_CATEGORY', workspace_id)

mock_category = [{
"Id": 1,
"JobId": "10064",
Expand Down Expand Up @@ -480,7 +483,9 @@ def test_sync_cost_categories(

sage_connector.connection.categories.get_all_categories.return_value = categories_generator

sage_connector.sync_cost_categories()
sage_connector.sync_cost_categories(cost_category_import_log)

assert Version.objects.get(workspace_id=workspace_id).cost_category == 2

assert Version.objects.get(workspace_id=workspace_id).cost_category == 2

Expand All @@ -501,6 +506,8 @@ def test_sync_cost_codes(
workspace_id=workspace_id
)

cost_code_import_log = ImportLog.create_import_log('COST_CODE', workspace_id)

Version.objects.update_or_create(
workspace_id=workspace_id,
cost_code=1,
Expand All @@ -520,5 +527,5 @@ def test_sync_cost_codes(

sage_connector.connection.cost_codes.get_all_costcodes.return_value = [[[mock_data]]]

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