2023-03-05 23:35:41 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 18:52:37 +07:00
|
|
|
"git.nochill.in/nochill/naice_pos/util"
|
2023-03-22 12:06:09 +07:00
|
|
|
"github.com/google/uuid"
|
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)
|
2023-03-22 12:06:09 +07:00
|
|
|
user := createRandomUser(t) // TODO: Change to use UserMerchantTx
|
2023-03-05 23:35:41 +07:00
|
|
|
supplier, _ := createRandomSupplier(t)
|
|
|
|
product1, _ := createRandomProduct(t)
|
|
|
|
product2, _ := createRandomProduct(t)
|
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
results := make(chan PurchaseOrderTxResult)
|
|
|
|
|
2023-03-12 11:01:43 +07:00
|
|
|
purchaseProducts1Quantity := util.RandomFloat(1, 99)
|
|
|
|
purchaseProducts1Price := util.RandomFloat(999, 9999)
|
2023-03-06 15:15:11 +07:00
|
|
|
purchaseProducts1SubTotal := purchaseProducts1Price * purchaseProducts1Quantity
|
|
|
|
|
2023-03-12 11:01:43 +07:00
|
|
|
purchaseProducts2Quantity := util.RandomFloat(1, 99)
|
|
|
|
purchaseProducts2Price := util.RandomFloat(999, 9999)
|
2023-03-06 15:15:11 +07:00
|
|
|
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)
|
|
|
|
|
2023-03-13 09:24:59 +07:00
|
|
|
tries := 3
|
|
|
|
|
|
|
|
for i := 0; i < tries; i++ {
|
2023-03-06 15:15:11 +07:00
|
|
|
go func() {
|
|
|
|
result, err := store.PurchaseOrderTx(context.Background(), PurchasoOrderTxParams{
|
2023-03-22 12:06:09 +07:00
|
|
|
MerchantID: supplier.MerchantID, // GET FROM UserMerchantTx
|
|
|
|
CreatedBy: user.ID, // TODO: GET FROM UserMerchantTx
|
2023-03-06 15:15:11 +07:00
|
|
|
SupplierID: supplier.ID,
|
2023-03-19 18:17:17 +07:00
|
|
|
Code: sql.NullString{Valid: true, String: util.RandomTransactionCode("P", util.RandomInt(1, 10))},
|
2023-03-06 15:15:11 +07:00
|
|
|
IsPaid: true,
|
|
|
|
Total: product1.PurchasePrice + product2.PurchasePrice,
|
|
|
|
PaidNominal: product1.PurchasePrice + product2.PurchasePrice,
|
|
|
|
Note: sql.NullString{Valid: true, String: ""},
|
|
|
|
Products: products,
|
|
|
|
})
|
|
|
|
errs <- err
|
|
|
|
results <- result
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-03-13 09:24:59 +07:00
|
|
|
for i := 0; i < tries; i++ {
|
2023-03-06 15:15:11 +07:00
|
|
|
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
|
|
|
}
|
2023-03-13 09:24:59 +07:00
|
|
|
|
2023-03-22 12:06:09 +07:00
|
|
|
func TestCreateSaleOrder(t *testing.T) {
|
|
|
|
var products []SaleOrderProduct
|
|
|
|
|
|
|
|
store := NewStore(testDB)
|
|
|
|
user := createRandomUser(t)
|
|
|
|
customer, _ := createRandomCustomer(t)
|
|
|
|
product1, _ := createRandomProduct(t)
|
|
|
|
product2, _ := createRandomProduct(t)
|
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
results := make(chan SaleOrderTxResult)
|
|
|
|
|
|
|
|
saleProducts1Quantity := util.RandomFloat(1, 99)
|
|
|
|
saleProducts1Price := util.RandomFloat(999, 9999)
|
|
|
|
saleProducts1SubTotal := saleProducts1Price * saleProducts1Quantity
|
|
|
|
saleProducts1Profit := saleProducts1SubTotal - saleProducts1Quantity*product1.SellingPrice
|
|
|
|
|
|
|
|
saleProducts2Quantity := util.RandomFloat(1, 99)
|
|
|
|
saleProducts2Price := util.RandomFloat(999, 9999)
|
|
|
|
saleProducts2SubTotal := saleProducts2Price * saleProducts2Quantity
|
|
|
|
saleProducts2Profit := saleProducts2SubTotal - saleProducts2Quantity*product2.SellingPrice
|
|
|
|
|
|
|
|
saleProducts_1 := SaleOrderProduct{
|
|
|
|
ProductID: product1.ID,
|
|
|
|
Quantity: saleProducts1Quantity,
|
|
|
|
Sub_total: saleProducts1SubTotal,
|
|
|
|
Price: saleProducts1Price,
|
|
|
|
ProductName: product1.Name,
|
|
|
|
Profit: saleProducts1Profit,
|
|
|
|
}
|
|
|
|
|
|
|
|
saleProducts_2 := SaleOrderProduct{
|
|
|
|
ProductID: product2.ID,
|
|
|
|
Quantity: saleProducts2Quantity,
|
|
|
|
Sub_total: saleProducts2SubTotal,
|
|
|
|
Price: saleProducts2Price,
|
|
|
|
ProductName: product2.Name,
|
|
|
|
Profit: saleProducts2Profit,
|
|
|
|
}
|
|
|
|
|
|
|
|
products = append(products, saleProducts_1, saleProducts_2)
|
|
|
|
|
|
|
|
tries := 3
|
|
|
|
|
|
|
|
for i := 0; i < tries; i++ {
|
|
|
|
go func() {
|
|
|
|
result, err := store.SaleOrderTx(context.Background(), SaleOrderTxParams{
|
|
|
|
MerchantID: customer.MerchantID,
|
|
|
|
CreatedBy: user.ID,
|
|
|
|
CustomerID: uuid.NullUUID{Valid: true, UUID: customer.ID},
|
|
|
|
Code: util.RandomTransactionCode("S", util.RandomInt(1, 10)),
|
|
|
|
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 < tries; i++ {
|
|
|
|
err := <-errs
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := <-results
|
|
|
|
require.NotEmpty(t, result)
|
|
|
|
|
|
|
|
saleOrder := result.SaleOrder
|
|
|
|
require.NotEmpty(t, saleOrder)
|
|
|
|
require.Equal(t, saleOrder.MerchantID, customer.MerchantID) // TODO: Change to use UserMerchantTx result
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-13 09:24:59 +07:00
|
|
|
func TestCreateUserMerchant(t *testing.T) {
|
|
|
|
store := NewStore(testDB)
|
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
results := make(chan UserMerchantTxResult)
|
|
|
|
|
|
|
|
tries := 3
|
|
|
|
|
|
|
|
for i := 0; i < tries; i++ {
|
|
|
|
randString := util.RandomString(10)
|
|
|
|
password, err := util.HashPassword(randString)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
result, err := store.CreateUserMerchantTx(context.Background(), UserMerchantTxParams{
|
|
|
|
Email: util.RandomEmail(),
|
|
|
|
Fullname: util.RandomString(6),
|
|
|
|
Password: password,
|
|
|
|
OutletName: util.RandomString(6),
|
|
|
|
})
|
|
|
|
errs <- err
|
|
|
|
results <- result
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < tries; i++ {
|
|
|
|
err := <-errs
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := <-results
|
|
|
|
require.NotEmpty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|