2023-03-05 23:35:41 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
"git.nochill.in/nochill/nice_pos/util"
|
2023-03-05 23:35:41 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPurchaseOrder(t *testing.T) {
|
2023-03-06 15:15:11 +07:00
|
|
|
var products []PurchaseOrderProduct
|
|
|
|
|
2023-03-05 23:35:41 +07:00
|
|
|
store := NewStore(testDB)
|
|
|
|
supplier, _ := createRandomSupplier(t)
|
|
|
|
product1, _ := createRandomProduct(t)
|
|
|
|
product2, _ := createRandomProduct(t)
|
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
results := make(chan PurchaseOrderTxResult)
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
purchaseProducts1Quantity, _ := util.RandomFloat(1, 99)
|
|
|
|
purchaseProducts1Price, _ := util.RandomFloat(999, 9999)
|
|
|
|
purchaseProducts1SubTotal := purchaseProducts1Price * purchaseProducts1Quantity
|
|
|
|
|
|
|
|
purchaseProducts2Quantity, _ := util.RandomFloat(1, 99)
|
|
|
|
purchaseProducts2Price, _ := util.RandomFloat(999, 9999)
|
|
|
|
purchaseProducts2SubTotal := purchaseProducts2Price * purchaseProducts2Quantity
|
|
|
|
|
|
|
|
purchaseProducts_1 := PurchaseOrderProduct{
|
|
|
|
ProductID: product1.ID,
|
|
|
|
Quantity: purchaseProducts1Quantity,
|
|
|
|
Sub_total: purchaseProducts1SubTotal,
|
|
|
|
Price: purchaseProducts1Price,
|
|
|
|
}
|
|
|
|
|
|
|
|
purchaseProducts_2 := PurchaseOrderProduct{
|
|
|
|
ProductID: product2.ID,
|
|
|
|
Quantity: purchaseProducts2Quantity,
|
|
|
|
Sub_total: purchaseProducts2SubTotal,
|
|
|
|
Price: purchaseProducts2Price,
|
|
|
|
}
|
|
|
|
|
|
|
|
products = append(products, purchaseProducts_1, purchaseProducts_2)
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
go func() {
|
|
|
|
result, err := store.PurchaseOrderTx(context.Background(), PurchasoOrderTxParams{
|
|
|
|
MerchantID: supplier.MerchantID,
|
|
|
|
SupplierID: supplier.ID,
|
|
|
|
Code: sql.NullString{Valid: true, String: ""},
|
|
|
|
IsPaid: true,
|
|
|
|
Total: product1.PurchasePrice + product2.PurchasePrice,
|
|
|
|
PaidNominal: product1.PurchasePrice + product2.PurchasePrice,
|
|
|
|
Note: sql.NullString{Valid: true, String: ""},
|
|
|
|
Products: products,
|
|
|
|
})
|
|
|
|
errs <- err
|
|
|
|
results <- result
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
err := <-errs
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := <-results
|
|
|
|
require.NotEmpty(t, result)
|
2023-03-05 23:35:41 +07:00
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
purchaseOrder := result.PurchaseOrder
|
|
|
|
require.NotEmpty(t, purchaseOrder)
|
|
|
|
require.Equal(t, purchaseOrder.MerchantID, supplier.MerchantID)
|
|
|
|
require.Equal(t, purchaseOrder.SupplierID, supplier.ID)
|
|
|
|
require.NotZero(t, purchaseOrder.PaidNominal, product1.PurchasePrice+product2.PurchasePrice)
|
|
|
|
}
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|