Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
G1_ShoppingApp
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
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
G1_ShoppingApp
G1_ShoppingApp
Commits
2a7da387
Commit
2a7da387
authored
2 months ago
by
duyanhehe
Browse files
Options
Downloads
Patches
Plain Diff
add test product and payment
parent
922dcaff
No related branches found
No related tags found
No related merge requests found
Pipeline
#9127
failed
2 months ago
Stage: install
Stage: test
Stage: run
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/backend/schemas/payment.py
+10
-9
10 additions, 9 deletions
app/backend/schemas/payment.py
app/tests/test_payment.py
+103
-0
103 additions, 0 deletions
app/tests/test_payment.py
app/tests/test_product.py
+73
-0
73 additions, 0 deletions
app/tests/test_product.py
with
186 additions
and
9 deletions
app/backend/schemas/payment.py
+
10
−
9
View file @
2a7da387
...
@@ -30,6 +30,9 @@ class PaymentCreate(BaseModel):
...
@@ -30,6 +30,9 @@ class PaymentCreate(BaseModel):
"""
Validate expiry date format (MM/YY) and ensure it
'
s in the future.
"""
"""
Validate expiry date format (MM/YY) and ensure it
'
s in the future.
"""
try
:
try
:
expiry_obj
=
datetime
.
strptime
(
value
,
"
%m/%y
"
)
expiry_obj
=
datetime
.
strptime
(
value
,
"
%m/%y
"
)
except
ValueError
:
raise
ValueError
(
"
Invalid expiry date format. Use MM/YY
"
)
current_date
=
datetime
.
now
()
current_date
=
datetime
.
now
()
if
expiry_obj
.
year
<
current_date
.
year
or
(
if
expiry_obj
.
year
<
current_date
.
year
or
(
...
@@ -39,8 +42,6 @@ class PaymentCreate(BaseModel):
...
@@ -39,8 +42,6 @@ class PaymentCreate(BaseModel):
raise
ValueError
(
"
Card expiry date must be in the future
"
)
raise
ValueError
(
"
Card expiry date must be in the future
"
)
return
value
return
value
except
ValueError
:
raise
ValueError
(
"
Invalid expiry date format. Use MM/YY
"
)
class
PaymentRead
(
BaseModel
):
class
PaymentRead
(
BaseModel
):
...
...
This diff is collapsed.
Click to expand it.
app/tests/test_payment.py
0 → 100644
+
103
−
0
View file @
2a7da387
import
pytest
from
datetime
import
datetime
from
fastapi
import
HTTPException
from
app.backend.schemas.payment
import
PaymentCreate
,
PaymentRead
from
app.backend.models.models
import
Payment
from
app.backend.utils.security
import
encrypt_card_number
,
decrypt_card_number
def
test_payment_create_validation
():
# Valid payment data
valid_data
=
{
"
payment_method
"
:
"
Credit Card
"
,
"
card_number
"
:
"
4111111111111111
"
,
"
cvv
"
:
"
123
"
,
"
expiry_date
"
:
f
"
{
(
datetime
.
now
().
month
)
:
02
d
}
/
{
str
(
datetime
.
now
().
year
+
1
)[
2
:
]
}
"
,
}
payment
=
PaymentCreate
(
**
valid_data
)
assert
payment
.
payment_method
==
valid_data
[
"
payment_method
"
]
assert
decrypt_card_number
(
payment
.
card_number
)
==
valid_data
[
"
card_number
"
]
assert
payment
.
expiry_date
==
valid_data
[
"
expiry_date
"
]
# Invalid card number
with
pytest
.
raises
(
ValueError
):
PaymentCreate
(
payment_method
=
"
Credit Card
"
,
card_number
=
"
123
"
,
# Too short
cvv
=
"
123
"
,
expiry_date
=
"
12/25
"
,
)
# Invalid CVV
with
pytest
.
raises
(
ValueError
):
PaymentCreate
(
payment_method
=
"
Credit Card
"
,
card_number
=
"
4111111111111111
"
,
cvv
=
"
12
"
,
# Too short
expiry_date
=
"
12/25
"
,
)
# Invalid expiry date format
with
pytest
.
raises
(
ValueError
):
PaymentCreate
(
payment_method
=
"
Credit Card
"
,
card_number
=
"
4111111111111111
"
,
cvv
=
"
123
"
,
expiry_date
=
"
13/25
"
,
# Invalid month
)
def
test_payment_read
():
# Create mock payment object
payment_obj
=
Payment
(
id
=
1
,
user_id
=
1
,
payment_method
=
"
Credit Card
"
,
card_number
=
encrypt_card_number
(
"
4111111111111111
"
),
expiry_date
=
"
12/25
"
,
created_at
=
datetime
.
now
(),
)
# Test PaymentRead model
payment_read
=
PaymentRead
.
from_orm
(
payment_obj
)
assert
payment_read
.
id
==
1
assert
payment_read
.
user_id
==
1
assert
payment_read
.
payment_method
==
"
Credit Card
"
assert
payment_read
.
card_number
==
"
4111111111111111
"
assert
payment_read
.
expiry_date
==
"
12/25
"
def
test_expiry_date_validation
():
# Test past date
past_date
=
f
"
{
(
datetime
.
now
().
month
-
1
)
:
02
d
}
/
{
str
(
datetime
.
now
().
year
)[
2
:
]
}
"
with
pytest
.
raises
(
ValueError
,
match
=
"
Card expiry date must be in the future
"
):
PaymentCreate
(
payment_method
=
"
Credit Card
"
,
card_number
=
"
4111111111111111
"
,
cvv
=
"
123
"
,
expiry_date
=
past_date
,
)
# Test future date
future_date
=
f
"
{
datetime
.
now
().
month
:
02
d
}
/
{
str
(
datetime
.
now
().
year
+
1
)[
2
:
]
}
"
payment
=
PaymentCreate
(
payment_method
=
"
Credit Card
"
,
card_number
=
"
4111111111111111
"
,
cvv
=
"
123
"
,
expiry_date
=
future_date
,
)
assert
payment
.
expiry_date
==
future_date
def
test_card_encryption
():
card_number
=
"
4111111111111111
"
payment
=
PaymentCreate
(
payment_method
=
"
Credit Card
"
,
card_number
=
card_number
,
cvv
=
"
123
"
,
expiry_date
=
f
"
{
datetime
.
now
().
month
:
02
d
}
/
{
str
(
datetime
.
now
().
year
+
1
)[
2
:
]
}
"
,
)
# Verify encryption
assert
payment
.
card_number
!=
card_number
assert
decrypt_card_number
(
payment
.
card_number
)
==
card_number
This diff is collapsed.
Click to expand it.
app/tests/test_product.py
0 → 100644
+
73
−
0
View file @
2a7da387
import
pytest
from
datetime
import
datetime
from
app.backend.schemas.product
import
(
ProductCreate
,
ProductRead
,
ProductUpdate
,
ProductImageCreate
,
ProductImageRead
,
CategoryBase
,
)
def
test_product_create_schema
():
# Test valid product creation
product_data
=
{
"
shop_id
"
:
1
,
"
category_id
"
:
2
,
"
name
"
:
"
Test Product
"
,
"
description
"
:
"
Test Description
"
,
"
price
"
:
9.99
,
"
stock
"
:
10
,
}
product
=
ProductCreate
(
**
product_data
)
assert
product
.
shop_id
==
1
assert
product
.
name
==
"
Test Product
"
assert
product
.
price
==
9.99
# Test optional fields
product_minimal
=
{
"
shop_id
"
:
1
,
"
name
"
:
"
Test Product
"
,
"
price
"
:
9.99
,
"
stock
"
:
10
}
product
=
ProductCreate
(
**
product_minimal
)
assert
product
.
category_id
is
None
assert
product
.
description
is
None
def
test_product_read_schema
():
product_data
=
{
"
id
"
:
1
,
"
shop_id
"
:
1
,
"
category_id
"
:
2
,
"
name
"
:
"
Test Product
"
,
"
description
"
:
"
Test Description
"
,
"
price
"
:
9.99
,
"
stock
"
:
10
,
"
created_at
"
:
datetime
.
now
(),
"
images
"
:
[],
"
category
"
:
{
"
id
"
:
2
,
"
name
"
:
"
Test Category
"
},
}
product
=
ProductRead
(
**
product_data
)
assert
product
.
id
==
1
assert
isinstance
(
product
.
created_at
,
datetime
)
assert
isinstance
(
product
.
category
,
CategoryBase
)
def
test_product_update_schema
():
# Test partial updates
update_data
=
{
"
name
"
:
"
Updated Name
"
,
"
price
"
:
19.99
}
update
=
ProductUpdate
(
**
update_data
)
assert
update
.
name
==
"
Updated Name
"
assert
update
.
price
==
19.99
assert
update
.
description
is
None
def
test_product_image_schemas
():
# Test image create
image_data
=
{
"
product_id
"
:
1
,
"
image_url
"
:
"
http://example.com/image.jpg
"
}
image
=
ProductImageCreate
(
**
image_data
)
assert
image
.
product_id
==
1
# Test image read
read_data
=
{
"
id
"
:
1
,
"
product_id
"
:
1
,
"
image_url
"
:
"
http://example.com/image.jpg
"
}
image
=
ProductImageRead
(
**
read_data
)
assert
image
.
id
==
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