Skip to content
Snippets Groups Projects
Commit adb279d1 authored by Neda Moeini's avatar Neda Moeini
Browse files

Make ruff happy

parent 36a671c6
No related branches found
No related tags found
1 merge request!2Feature/unit test for validations
import pytest
"""Fixtures for the sage_validation tests."""
from unittest.mock import MagicMock
import pytest
from django.core.files.uploadedfile import SimpleUploadedFile
from sage_validation.file_validator.models import MeoValidSuppliers, MeoCostCentres, XxData
from faker import Faker
from sage_validation.file_validator.models import MeoCostCentres, MeoValidSuppliers, XxData
@pytest.fixture
def sample_input_file():
"""Creates a sample valid CSV file for testing."""
csv_headers = ",".join([
"""Create a sample valid CSV file for testing."""
csv_headers_list = [
"AccountNumber",
"CBAccountNumber",
"DaysDiscountValid",
......@@ -46,9 +49,10 @@ def sample_input_file():
"TaxAnalysisDiscountValue/1",
"TaxAnalysisDiscountPercentage/1",
"TaxAnalysisTaxOnGoodsValue/1",
])
]
csv_headers = ",".join(csv_headers_list)
csv_content = ",".join([
csv_content_list = [
"12345", # AccountNumber
"54321", # CBAccountNumber
"30", # DaysDiscountValid
......@@ -86,9 +90,10 @@ def sample_input_file():
"30", # TaxAnalysisDiscountValue/1
"3.5", # TaxAnalysisDiscountPercentage/1
"180", # TaxAnalysisTaxOnGoodsValue/1
])
]
csv_content = ",".join(csv_content_list)
return SimpleUploadedFile("test.csv", f"{csv_headers}\n{csv_content}".encode("utf-8"), content_type="text/csv")
return SimpleUploadedFile("test.csv", f"{csv_headers}\n{csv_content}".encode(), content_type="text/csv")
@pytest.fixture
def mock_meo_database(mocker):
......@@ -98,7 +103,9 @@ def mock_meo_database(mocker):
# Mock MeoValidSuppliers
supplier_mock = MagicMock()
supplier_mock.all.return_value = [
MeoValidSuppliers(supplier_account_number=str(fake.random_int(min=10000, max=99999)), supplier_account_name=fake.company()),
MeoValidSuppliers(
supplier_account_number=str(fake.random_int(min=10000, max=99999)), supplier_account_name=fake.company()
),
MeoValidSuppliers(supplier_account_number="12345", supplier_account_name="Sample Narrative")
]
mocker.patch("sage_validation.file_validator.models.MeoValidSuppliers.objects.using", return_value=supplier_mock)
......@@ -123,7 +130,9 @@ def mock_meo_database(mocker):
# Mock MeoValidSageAccounts
sage_account_mock = MagicMock()
sage_account_mock.filter.return_value.exists.return_value = True
mocker.patch("sage_validation.file_validator.models.MeoValidSageAccounts.objects.using", return_value=sage_account_mock)
mocker.patch(
"sage_validation.file_validator.models.MeoValidSageAccounts.objects.using", return_value=sage_account_mock
)
# Mock MeoNominal
nominal_mock = MagicMock()
......
from sage_validation.settings import * # noqa: F403, F401
"""Settings for running tests."""
from sage_validation.settings import * # noqa: F403
DATABASES = {
"default": DATABASES["default"],
"default": DATABASES["default"], # noqa: F405
}
"""Tests for the file_validator forms."""
import csv
import io
......@@ -30,7 +31,6 @@ def test_missing_required_columns():
def test_source_and_trader_type_validation(sample_input_file, mock_meo_database):
"""Test validation for Source and SYSTraderTranType columns."""
csv_content = sample_input_file.read().decode("utf-8").splitlines()
reader = csv.DictReader(csv_content)
rows = list(reader)
......@@ -54,7 +54,6 @@ def test_source_and_trader_type_validation(sample_input_file, mock_meo_database)
def test_validate_nominal_analysis_account(sample_input_file, mock_meo_database):
"""Test validation for nominal analysis account."""
csv_content = sample_input_file.read().decode("utf-8").splitlines()
reader = csv.DictReader(csv_content)
rows = list(reader)
......@@ -69,8 +68,9 @@ def test_validate_nominal_analysis_account(sample_input_file, mock_meo_database)
form = CSVUploadForm(files={"file": modified_file})
assert not form.is_valid()
assert (f"Row 1: 'AccountNumber' must match 'Sample Narrative' in 'NominalAnalysisNominalAnalysisNarrative/1',"
f" but found 'Invalid Name'.") == form.errors["file"][0]
assert form.errors["file"][0] == (
"Row 1: 'AccountNumber' must match 'Sample Narrative' in 'NominalAnalysisNominalAnalysisNarrative/1'"
", but found 'Invalid Name'.")
def test_validate_nc_cc_dep_combination_against_meo_sage_account(sample_input_file, mock_meo_database):
......@@ -90,5 +90,6 @@ def test_validate_nc_cc_dep_combination_against_meo_sage_account(sample_input_fi
form = CSVUploadForm(files={"file": modified_file})
assert not form.is_valid()
assert "Row 1: 'NominalAnalysisNominalCostCentre/1' (Invalid_CC) is not a valid cost centre." in str(form.errors["file"][0])
assert ("Row 1: 'NominalAnalysisNominalCostCentre/1' (Invalid_CC) is not a valid cost centre."
in str(form.errors["file"][0]))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment