From 696ea05ec906c90ad42ab6d8ba900c35c9902988 Mon Sep 17 00:00:00 2001 From: harshithaputtaswamy <44926484+harshithaputtaswamy@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:20:29 +0530 Subject: [PATCH] Added tests for previous bug reports (#352) * modified mappings and workspace tests * modified tests for fyle, mappings, workspace modules, 96.5% coverage * modified quickbooks module tests (#350) * modified quickbooks module tests * fixed comments * Add tests for prev bug reports * fixed comments --- .github/workflows/pytest.yml | 2 +- tests/test_fyle/fixtures.py | 22 +++++++++- tests/test_fyle/test_models.py | 13 +++--- tests/test_users/test_views.py | 16 ++++--- .../test_export_settings/test_views.py | 18 +++++++- .../test_import_settings/fixtures.py | 43 ++++++++++++++++--- .../test_import_settings/test_views.py | 21 ++++++++- 7 files changed, 112 insertions(+), 23 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 74de3cea..a20b12ce 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -16,7 +16,7 @@ jobs: run: | docker-compose -f docker-compose-pipeline.yml build docker-compose -f docker-compose-pipeline.yml up -d - docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov=. --junit-xml=test-reports/report.xml --cov-report=term-missing --cov-fail-under=90 | tee pytest-coverage.txt + docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov=. --junit-xml=test-reports/report.xml --cov-report=term-missing --cov-fail-under=97 | tee pytest-coverage.txt echo "STATUS=$(cat pytest-coverage.txt | grep 'Required test' | awk '{ print $1 }')" >> $GITHUB_ENV echo "FAILED=$(cat test-reports/report.xml | awk -F'=' '{print $5}' | awk -F' ' '{gsub(/"/, "", $1); print $1}')" >> $GITHUB_ENV env: diff --git a/tests/test_fyle/fixtures.py b/tests/test_fyle/fixtures.py index 17697f55..3450dac6 100644 --- a/tests/test_fyle/fixtures.py +++ b/tests/test_fyle/fixtures.py @@ -270,6 +270,24 @@ }, "user_id":"usqywo0f3nBY" } - } - + }, + 'get_all_orgs': [ + { + 'id': 'orHVw3ikkCxJ', + 'created_at': '2018-12-18T10:50:36.506Z', + 'updated_at': '2018-12-18T10:51:53.352Z', + 'name': 'Anagha Org', + 'domain': 'afyle.in', + 'currency': 'EUR', + 'branch_ifsc': None, + 'branch_account': None, + 'tally_bank_ledger': None, + 'tally_default_category': None, + 'tally_default_user': None, + 'corporate_credit_card_details': {'bank_name': None, + 'number_of_cards': None}, + 'verified': True, + 'lite': False, + 'dwolla_customers_metadata_id': None, + },], } diff --git a/tests/test_fyle/test_models.py b/tests/test_fyle/test_models.py index 6516e908..e9ec932e 100644 --- a/tests/test_fyle/test_models.py +++ b/tests/test_fyle/test_models.py @@ -1,9 +1,6 @@ -import random -import pytest from apps.fyle.models import _format_date, _group_expenses from apps.fyle.models import * from .fixtures import data -import datetime def test_default_fields(): expense_group_field = get_default_expense_group_fields() @@ -64,12 +61,14 @@ def test_create_expense_groups_by_report_id_fund_source(db): expense_group_settings.ccc_export_date_type = 'last_spent_at' expense_group_settings.save() - field = ExpenseAttribute.objects.filter(workspace_id=workspace_id).first() - field.attribute_type = 'COOL' + field = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type='PROJECT').last() + field.attribute_type = 'KILLUA' field.save() - expense_groups = _group_expenses([], ['claim_number', 'fund_source', 'projects', 'employee_email', 'report_id', 'cool'], 4) - assert expense_groups == [] + expenses = Expense.objects.filter(id=33).all() + + expense_groups = _group_expenses(expenses, ['claim_number', 'fund_source', 'project', 'employee_email', 'report_id', 'Killua'], 4) + assert expense_groups == [{'claim_number': 'C/2022/05/R/6', 'fund_source': 'PERSONAL', 'project': 'Bebe Rexha', 'employee_email': 'sravan.kumar@fyle.in', 'report_id': 'rpawE81idoYo', 'killua': '', 'total': 1, 'expense_ids': [33]}] expense_groups = ExpenseGroup.create_expense_groups_by_report_id_fund_source([expense_objects], workspace_id) assert len(expense_groups) == 1 diff --git a/tests/test_users/test_views.py b/tests/test_users/test_views.py index 09ea4c3b..c9d92ba5 100644 --- a/tests/test_users/test_views.py +++ b/tests/test_users/test_views.py @@ -1,23 +1,27 @@ from django.urls import reverse +import pytest + +from tests.test_fyle.fixtures import data as fyle_data # Will use paramaterize decorator of python later +@pytest.mark.django_db(databases=['default']) def test_get_profile_view(api_client, test_connection): - access_token = test_connection.access_token url = reverse('profile') - api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token)) + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token)) response = api_client.get(url) assert response.status_code == 200 -def test_get_fyle_orgs_view(mocker, api_client, test_connection): + +@pytest.mark.django_db(databases=['default']) +def test_get_fyle_orgs_view(api_client, test_connection, mocker): mocker.patch( 'apps.users.views.get_fyle_orgs', - return_value=['fyleforgotham.in'] + return_value=fyle_data['get_all_orgs'] ) - access_token = test_connection.access_token url = reverse('orgs') - api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token)) + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token)) response = api_client.get(url) assert response.status_code == 200 diff --git a/tests/test_workspaces/test_apis/test_export_settings/test_views.py b/tests/test_workspaces/test_apis/test_export_settings/test_views.py index 34a824b2..6f7c6d1a 100644 --- a/tests/test_workspaces/test_apis/test_export_settings/test_views.py +++ b/tests/test_workspaces/test_apis/test_export_settings/test_views.py @@ -31,9 +31,25 @@ def test_export_settings(api_client, test_connection): response = json.loads(response.content) assert dict_compare_keys(response, data['response']) == [], 'workspaces api returns a diff in the keys' + invalid_workspace_general_settings = data['export_settings'] + invalid_workspace_general_settings['workspace_general_settings'] = {} response = api_client.put( url, - data=data['export_settings_missing_values'], + data=invalid_workspace_general_settings, + format='json' + ) + + assert response.status_code == 400 + + invalid_expense_group_settings = data['export_settings'] + invalid_expense_group_settings['expense_group_settings'] = {} + invalid_expense_group_settings['workspace_general_settings'] = {'reimbursable_expenses_object': 'EXPENSE', + 'corporate_credit_card_expenses_object': 'BILL' + } + + response = api_client.put( + url, + data=invalid_expense_group_settings, format='json' ) diff --git a/tests/test_workspaces/test_apis/test_import_settings/fixtures.py b/tests/test_workspaces/test_apis/test_import_settings/fixtures.py index 53c28cee..de7390cc 100644 --- a/tests/test_workspaces/test_apis/test_import_settings/fixtures.py +++ b/tests/test_workspaces/test_apis/test_import_settings/fixtures.py @@ -66,10 +66,43 @@ 'response': {'workspace_general_settings':{'import_categories':True,'charts_of_accounts':['Expense'],'import_tax_codes':True,'import_vendors_as_merchants':True},'general_mappings':{'default_tax_code':{'name':'12.5% TR @12.5%','id':'22'}},'mapping_settings':[{'source_field':'COST_CENTER','destination_field':'CLASS','import_to_fyle':True,'is_custom':False,'source_placeholder':''},{'source_field':'PROJECT','destination_field':'DEPARTMENT','import_to_fyle':True,'is_custom':False,'source_placeholder':''},{'source_field':'CLASS','destination_field':'CUSTOMER','import_to_fyle':True,'is_custom':True,'source_placeholder':''}], 'workspace_id':9 }, - 'import_settings_missing_values': { - 'mapping_settings': None, - 'workspace_general_settings': {}, - 'general_mappings': {} + 'invalid_general_mappings': { + 'workspace_general_settings': { + 'import_categories': True, + 'charts_of_accounts': [ + 'Expense' + ], + 'import_tax_codes': True, + 'import_vendors_as_merchants': True + }, + 'general_mappings': { + }, + 'mapping_settings': [ + { + 'source_field': 'COST_CENTER', + 'destination_field': 'DEPARTMENT', + 'import_to_fyle': True, + 'is_custom': False, + 'source_placeholder': 'cost center' + } + ] + }, + 'invalid_mapping_settings': { + 'workspace_general_settings': { + 'import_categories': True, + 'charts_of_accounts': [ + 'Expense' + ], + 'import_tax_codes': True, + 'import_vendors_as_merchants': True + }, + 'general_mappings': { + 'default_tax_code': { + 'name': '12.5% TR @12.5%', + 'id': '22' + } + }, + 'mapping_settings': None }, -} \ No newline at end of file +} diff --git a/tests/test_workspaces/test_apis/test_import_settings/test_views.py b/tests/test_workspaces/test_apis/test_import_settings/test_views.py index 2d0177aa..14b05cb8 100644 --- a/tests/test_workspaces/test_apis/test_import_settings/test_views.py +++ b/tests/test_workspaces/test_apis/test_import_settings/test_views.py @@ -60,9 +60,28 @@ def test_import_settings(mocker, api_client, test_connection): ) assert response.status_code == 200 + invalid_workspace_general_settings = data['import_settings'] + invalid_workspace_general_settings['workspace_general_settings'] = {} response = api_client.put( url, - data=data['import_settings_missing_values'], + data=invalid_workspace_general_settings, + format='json' + ) + assert response.status_code == 400 + + response = json.loads(response.content) + assert response['non_field_errors'] == ['Workspace general settings are required'] + + response = api_client.put( + url, + data=data['invalid_general_mappings'], + format='json' + ) + assert response.status_code == 400 + + response = api_client.put( + url, + data=data['invalid_mapping_settings'], format='json' ) assert response.status_code == 400