Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cicer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor 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
meskio
cicer
Commits
7be8bf53
Commit
7be8bf53
authored
4 years ago
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Add update del/add order product test
parent
d4780636
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/db/order.go
+1
-2
1 addition, 2 deletions
api/db/order.go
api/order_test.go
+143
-0
143 additions, 0 deletions
api/order_test.go
with
144 additions
and
2 deletions
api/db/order.go
+
1
−
2
View file @
7be8bf53
...
@@ -150,7 +150,7 @@ func updateOrderProducts(tx *gorm.DB, order Order, dbOrder Order) (products []Or
...
@@ -150,7 +150,7 @@ func updateOrderProducts(tx *gorm.DB, order Order, dbOrder Order) (products []Or
if
dbProduct
!=
nil
{
if
dbProduct
!=
nil
{
dbProduct
.
Price
=
product
.
Price
dbProduct
.
Price
=
product
.
Price
products
=
append
(
products
,
*
dbProduct
)
products
=
append
(
products
,
*
dbProduct
)
err
=
tx
.
Save
(
&
dbProduct
)
.
Error
err
=
tx
.
Save
(
dbProduct
)
.
Error
}
else
{
}
else
{
product
.
OrderID
=
uint
(
dbOrder
.
ID
)
product
.
OrderID
=
uint
(
dbOrder
.
ID
)
err
=
tx
.
Create
(
&
product
)
.
Error
err
=
tx
.
Create
(
&
product
)
.
Error
...
@@ -300,7 +300,6 @@ func calculateOrderPurchaseTotal(purchase []OrderPurchase, products []OrderProdu
...
@@ -300,7 +300,6 @@ func calculateOrderPurchaseTotal(purchase []OrderPurchase, products []OrderProdu
for
_
,
p
:=
range
purchase
{
for
_
,
p
:=
range
purchase
{
found
:=
false
found
:=
false
for
_
,
product
:=
range
products
{
for
_
,
product
:=
range
products
{
log
.
Printf
(
"%v"
,
p
.
OrderProduct
)
if
(
p
.
OrderProduct
!=
nil
&&
product
.
ProductCode
==
p
.
OrderProduct
.
ProductCode
)
||
if
(
p
.
OrderProduct
!=
nil
&&
product
.
ProductCode
==
p
.
OrderProduct
.
ProductCode
)
||
product
.
ID
==
p
.
OrderProductID
{
product
.
ID
==
p
.
OrderProductID
{
total
+=
product
.
Price
*
p
.
Amount
total
+=
product
.
Price
*
p
.
Amount
...
...
This diff is collapsed.
Click to expand it.
api/order_test.go
+
143
−
0
View file @
7be8bf53
...
@@ -447,6 +447,8 @@ func TestOrderUpdate(t *testing.T) {
...
@@ -447,6 +447,8 @@ func TestOrderUpdate(t *testing.T) {
}
}
order
:=
testOrder
order
:=
testOrder
order
.
Products
=
make
([]
db
.
OrderProduct
,
len
(
testOrder
.
Products
))
copy
(
order
.
Products
,
testOrder
.
Products
)
order
.
Products
[
0
]
.
Price
=
1000
order
.
Products
[
0
]
.
Price
=
1000
resp
=
tapi
.
doOrder
(
"PUT"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
order
,
nil
)
resp
=
tapi
.
doOrder
(
"PUT"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
order
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusAccepted
{
if
resp
.
StatusCode
!=
http
.
StatusAccepted
{
...
@@ -500,6 +502,8 @@ func TestOrderUpdateProduct(t *testing.T) {
...
@@ -500,6 +502,8 @@ func TestOrderUpdateProduct(t *testing.T) {
}
}
order
:=
testOrder
order
:=
testOrder
order
.
Products
=
make
([]
db
.
OrderProduct
,
len
(
testOrder
.
Products
))
copy
(
order
.
Products
,
testOrder
.
Products
)
order
.
Products
[
0
]
.
Price
=
testProduct2
.
Price
order
.
Products
[
0
]
.
Price
=
testProduct2
.
Price
order
.
Products
[
0
]
.
ProductCode
=
testProduct2
.
Code
order
.
Products
[
0
]
.
ProductCode
=
testProduct2
.
Code
resp
=
tapi
.
doOrder
(
"PUT"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
order
,
nil
)
resp
=
tapi
.
doOrder
(
"PUT"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
order
,
nil
)
...
@@ -538,6 +542,145 @@ func TestOrderUpdateProduct(t *testing.T) {
...
@@ -538,6 +542,145 @@ func TestOrderUpdateProduct(t *testing.T) {
}
}
}
}
func
TestOrderUpdateAddProduct
(
t
*
testing
.
T
)
{
tapi
:=
newTestAPI
(
t
)
defer
tapi
.
close
()
tapi
.
addTestMember
()
tapi
.
addTestOrder
()
var
orders
[]
db
.
Order
resp
:=
tapi
.
do
(
"GET"
,
"/order/active"
,
nil
,
&
orders
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get orders:"
,
resp
.
Status
)
}
purchase
:=
[]
db
.
OrderPurchase
{
{
OrderProductID
:
orders
[
0
]
.
Products
[
0
]
.
ID
,
Amount
:
3
,
},
}
resp
=
tapi
.
do
(
"POST"
,
fmt
.
Sprintf
(
"/order/%d/purchase"
,
orders
[
0
]
.
ID
),
purchase
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusCreated
{
t
.
Fatal
(
"Can't create order:"
,
resp
.
Status
)
}
order
:=
testOrder
order
.
Products
=
make
([]
db
.
OrderProduct
,
len
(
testOrder
.
Products
))
copy
(
order
.
Products
,
testOrder
.
Products
)
order
.
Products
=
append
(
order
.
Products
,
db
.
OrderProduct
{
ProductCode
:
testProduct2
.
Code
,
Price
:
testProduct2
.
Price
,
})
resp
=
tapi
.
doOrder
(
"PUT"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
order
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusAccepted
{
tapi
.
t
.
Fatal
(
"Can't update order:"
,
resp
.
Status
)
}
var
transactions
[]
db
.
Transaction
resp
=
tapi
.
do
(
"GET"
,
"/transaction/mine"
,
nil
,
&
transactions
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get transactions:"
,
resp
.
Status
)
}
if
len
(
transactions
)
!=
1
{
t
.
Fatal
(
"Wrong number of transactions"
,
len
(
orders
),
orders
)
}
expectedTotal
:=
-
(
order
.
Products
[
0
]
.
Price
*
3
)
if
transactions
[
0
]
.
Total
!=
expectedTotal
{
t
.
Error
(
"Wrong total"
,
transactions
[
0
]
.
Total
,
expectedTotal
)
}
}
func
TestOrderUpdateDelProduct
(
t
*
testing
.
T
)
{
tapi
:=
newTestAPI
(
t
)
defer
tapi
.
close
()
tapi
.
addTestMember
()
order
:=
testOrder
order
.
Products
=
make
([]
db
.
OrderProduct
,
len
(
testOrder
.
Products
))
copy
(
order
.
Products
,
testOrder
.
Products
)
order
.
Products
=
append
(
order
.
Products
,
db
.
OrderProduct
{
ProductCode
:
testProduct2
.
Code
,
Price
:
testProduct2
.
Price
,
})
resp
:=
tapi
.
doOrder
(
"POST"
,
"/order"
,
order
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusCreated
{
tapi
.
t
.
Fatal
(
"Can't create order:"
,
resp
.
Status
)
}
var
orders
[]
db
.
Order
resp
=
tapi
.
do
(
"GET"
,
"/order/active"
,
nil
,
&
orders
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get orders:"
,
resp
.
Status
)
}
purchase
:=
[]
db
.
OrderPurchase
{
{
OrderProductID
:
orders
[
0
]
.
Products
[
0
]
.
ID
,
Amount
:
3
,
},
{
OrderProductID
:
orders
[
0
]
.
Products
[
1
]
.
ID
,
Amount
:
2
,
},
}
resp
=
tapi
.
do
(
"POST"
,
fmt
.
Sprintf
(
"/order/%d/purchase"
,
orders
[
0
]
.
ID
),
purchase
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusCreated
{
t
.
Fatal
(
"Can't create order:"
,
resp
.
Status
)
}
var
transactions
[]
db
.
Transaction
resp
=
tapi
.
do
(
"GET"
,
"/transaction/mine"
,
nil
,
&
transactions
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get transactions:"
,
resp
.
Status
)
}
if
len
(
transactions
)
!=
1
{
t
.
Fatal
(
"Wrong number of transactions"
,
len
(
orders
),
orders
)
}
expectedTotal
:=
-
(
order
.
Products
[
0
]
.
Price
*
3
+
order
.
Products
[
1
]
.
Price
*
2
)
if
transactions
[
0
]
.
Total
!=
expectedTotal
{
t
.
Error
(
"Wrong total"
,
transactions
[
0
]
.
Total
,
expectedTotal
)
}
if
len
(
transactions
[
0
]
.
OrderPurchase
)
!=
2
{
t
.
Error
(
"Wrong purchases"
,
transactions
[
0
]
.
OrderPurchase
)
}
order
.
Products
=
[]
db
.
OrderProduct
{
order
.
Products
[
0
]}
resp
=
tapi
.
doOrder
(
"PUT"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
order
,
nil
)
if
resp
.
StatusCode
!=
http
.
StatusAccepted
{
tapi
.
t
.
Fatal
(
"Can't update order:"
,
resp
.
Status
)
}
var
orderResponse
OrderGetResponse
resp
=
tapi
.
do
(
"GET"
,
fmt
.
Sprintf
(
"/order/%d"
,
orders
[
0
]
.
ID
),
nil
,
&
orderResponse
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get order:"
,
resp
.
Status
)
}
if
len
(
orderResponse
.
Order
.
Products
)
!=
1
{
t
.
Fatal
(
"Wrong len of products:"
,
len
(
orderResponse
.
Order
.
Products
),
orderResponse
.
Order
.
Products
)
}
if
orderResponse
.
Order
.
Products
[
0
]
.
ProductCode
!=
testProduct
.
Code
{
t
.
Error
(
"Wrong product code:"
,
orderResponse
.
Order
.
Products
)
}
if
orderResponse
.
Order
.
Products
[
0
]
.
Price
!=
testProduct
.
Price
{
t
.
Error
(
"Wrong product price:"
,
orderResponse
.
Order
.
Products
)
}
resp
=
tapi
.
do
(
"GET"
,
"/transaction/mine"
,
nil
,
&
transactions
)
if
resp
.
StatusCode
!=
http
.
StatusOK
{
t
.
Fatal
(
"Can't get transactions:"
,
resp
.
Status
)
}
if
len
(
transactions
)
!=
1
{
t
.
Fatal
(
"Wrong number of transactions"
,
len
(
orders
),
orders
)
}
if
transactions
[
0
]
.
Total
!=
-
testProduct
.
Price
*
3
{
t
.
Error
(
"Wrong total"
,
transactions
[
0
]
.
Total
)
}
if
len
(
transactions
[
0
]
.
OrderPurchase
)
!=
1
{
t
.
Error
(
"Wrong purchases"
,
transactions
[
0
]
.
OrderPurchase
)
}
}
func
TestOrderUpdateReactivate
(
t
*
testing
.
T
)
{
func
TestOrderUpdateReactivate
(
t
*
testing
.
T
)
{
tapi
:=
newTestAPI
(
t
)
tapi
:=
newTestAPI
(
t
)
defer
tapi
.
close
()
defer
tapi
.
close
()
...
...
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