Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sage Validation
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
Sage Validation
Commits
cdbcc75a
Commit
cdbcc75a
authored
4 weeks ago
by
Neda Moeini
Browse files
Options
Downloads
Patches
Plain Diff
Add more tests for endpoints
parent
917f8856
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!2
Feature/unit test for validations
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
sage_validation/settings.py
+1
-1
1 addition, 1 deletion
sage_validation/settings.py
test/conftest.py
+6
-0
6 additions, 0 deletions
test/conftest.py
test/test_file_validator/test_file_validator_endpoints.py
+52
-0
52 additions, 0 deletions
test/test_file_validator/test_file_validator_endpoints.py
with
59 additions
and
1 deletion
sage_validation/settings.py
+
1
−
1
View file @
cdbcc75a
...
...
@@ -18,7 +18,7 @@ BASE_DIR = Path(__file__).resolve().parent
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY
=
os
.
getenv
(
"
SECRET_KEY
"
)
SECRET_KEY
=
os
.
getenv
(
"
SECRET_KEY
"
,
"
test-secret-key
"
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG
=
True
...
...
This diff is collapsed.
Click to expand it.
test/conftest.py
+
6
−
0
View file @
cdbcc75a
...
...
@@ -4,6 +4,7 @@ from unittest.mock import MagicMock
import
pytest
from
django.core.files.uploadedfile
import
SimpleUploadedFile
from
faker
import
Faker
from
rest_framework.test
import
APIClient
from
sage_validation.file_validator.models
import
MeoCostCentres
,
MeoValidSuppliers
,
XxData
...
...
@@ -139,3 +140,8 @@ def mock_meo_database(mocker: MagicMock)-> None:
nominal_mock
.
filter
.
return_value
.
exists
.
return_value
=
True
mocker
.
patch
(
"
sage_validation.file_validator.models.MeoNominal.objects.using
"
,
return_value
=
nominal_mock
)
@pytest.fixture
def
api_client
()
->
APIClient
:
"""
Fixture to return Django API test client.
"""
return
APIClient
()
This diff is collapsed.
Click to expand it.
test/test_file_validator/test_file_validator_endpoints.py
0 → 100644
+
52
−
0
View file @
cdbcc75a
from
unittest.mock
import
MagicMock
import
pytest
from
django.core.files.uploadedfile
import
SimpleUploadedFile
from
django.urls.base
import
reverse
from
rest_framework.test
import
APIClient
UPLOAD_FILE_URL
=
reverse
(
"
upload-file
"
)
@pytest.mark.django_db
def
test_csv_upload_valid
(
api_client
:
APIClient
,
sample_input_file
:
SimpleUploadedFile
,
mock_meo_database
:
MagicMock
)
->
None
:
"""
Test that a valid CSV upload succeeds.
"""
response
=
api_client
.
post
(
UPLOAD_FILE_URL
,
{
"
file
"
:
sample_input_file
},
format
=
"
multipart
"
)
assert
response
.
status_code
==
200
assert
response
.
json
()[
"
status
"
]
==
"
success
"
assert
"
download_url
"
in
response
.
json
()
@pytest.mark.django_db
def
test_csv_upload_invalid_extension
(
api_client
:
APIClient
)
->
None
:
"""
Test that uploading a non-CSV file fails.
"""
bad_file
=
SimpleUploadedFile
(
"
test.txt
"
,
b
"
Invalid content
"
,
content_type
=
"
text/plain
"
)
response
=
api_client
.
post
(
UPLOAD_FILE_URL
,
{
"
file
"
:
bad_file
},
format
=
"
multipart
"
)
assert
response
.
status_code
==
400
assert
response
.
json
()[
"
status
"
]
==
"
error
"
assert
"
errors
"
in
response
.
json
()
@pytest.mark.django_db
def
test_csv_export_with_data
(
api_client
:
APIClient
)
->
None
:
"""
Test exporting a processed CSV file.
"""
url
=
reverse
(
"
export-file
"
)
# Simulate session data
session
=
api_client
.
session
session
[
"
validated_csv
"
]
=
[
{
"
AccountNumber
"
:
"
12345
"
,
"
TransactionDate
"
:
"
01/03/2024
"
,
"
NominalAnalysisNominalAccountNumber/1
"
:
"
N100
"
}
]
session
.
save
()
response
=
api_client
.
get
(
url
)
assert
response
.
status_code
==
200
assert
response
[
"
Content-Disposition
"
]
==
'
attachment; filename=
"
updated_file.csv
"'
assert
b
"
AccountNumber,TransactionDate,NominalAnalysisNominalAccountNumber/1
"
in
response
.
content
assert
b
"
12345,01/03/2024,N100
"
in
response
.
content
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment