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
9a9343b5
Commit
9a9343b5
authored
1 month ago
by
Neda Moeini
Browse files
Options
Downloads
Patches
Plain Diff
Add unittests for form validator
parent
dbdbfd1f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
test/conftest.py
+1
-1
1 addition, 1 deletion
test/conftest.py
test/test_file_validator/__init__.py
+0
-0
0 additions, 0 deletions
test/test_file_validator/__init__.py
test/test_file_validator/test_forms.py
+52
-0
52 additions, 0 deletions
test/test_file_validator/test_forms.py
with
53 additions
and
1 deletion
test/conftest.py
+
1
−
1
View file @
9a9343b5
...
...
@@ -99,7 +99,7 @@ def mock_meo_database(mocker):
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
=
"
12345
"
,
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
)
...
...
This diff is collapsed.
Click to expand it.
test/test_file_validator/__init__.py
0 → 100644
+
0
−
0
View file @
9a9343b5
This diff is collapsed.
Click to expand it.
test/test_file_validator/test_forms.py
0 → 100644
+
52
−
0
View file @
9a9343b5
import
csv
import
io
from
django.core.files.uploadedfile
import
SimpleUploadedFile
from
sage_validation.file_validator.forms
import
CSVUploadForm
def
test_valid_csv_upload
(
sample_input_file
,
mock_meo_database
):
"""
Test CSV upload with valid data.
"""
form
=
CSVUploadForm
(
files
=
{
"
file
"
:
sample_input_file
})
assert
form
.
is_valid
(),
f
"
Form errors:
{
form
.
errors
}
"
def
test_invalid_file_extension
():
"""
Test form rejects non-CSV files.
"""
bad_file
=
SimpleUploadedFile
(
"
test.txt
"
,
b
"
Some text content
"
,
content_type
=
"
text/plain
"
)
form
=
CSVUploadForm
(
files
=
{
"
file
"
:
bad_file
})
assert
not
form
.
is_valid
()
assert
"
File must be in CSV format.
"
in
form
.
errors
[
"
file
"
]
def
test_missing_required_columns
():
"""
Test form rejects CSV missing required headers.
"""
invalid_csv
=
SimpleUploadedFile
(
"
test.csv
"
,
b
"
AccountNumber,CBAccountNumber
\n
12345,54321
"
,
content_type
=
"
text/csv
"
)
form
=
CSVUploadForm
(
files
=
{
"
file
"
:
invalid_csv
})
assert
not
form
.
is_valid
()
assert
"
Missing required columns
"
in
str
(
form
.
errors
)
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
)
# Modify the first row's "Source" and "SYSTraderTranType" values
rows
[
0
][
"
Source
"
]
=
"
90
"
# Change Source from 80 to 90
rows
[
0
][
"
SYSTraderTranType
"
]
=
"
5
"
# Change SYSTraderTranType from 4 to 5
# Rebuild the CSV with modified values
output
=
io
.
StringIO
()
writer
=
csv
.
DictWriter
(
output
,
fieldnames
=
reader
.
fieldnames
)
writer
.
writeheader
()
writer
.
writerows
(
rows
)
modified_file
=
SimpleUploadedFile
(
"
test_modified.csv
"
,
output
.
getvalue
().
encode
(
"
utf-8
"
),
content_type
=
"
text/csv
"
)
form
=
CSVUploadForm
(
files
=
{
"
file
"
:
modified_file
})
assert
not
form
.
is_valid
(),
"
Form should be invalid due to incorrect Source and SYSTraderTranType values
"
assert
"
Row 1:
'
Source
'
must be 80
"
in
form
.
errors
[
"
file
"
][
0
]
assert
"
Row 1:
'
SYSTraderTranType
'
must be 4
"
in
form
.
errors
[
"
file
"
][
1
]
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