2023-03-05 23:35:41 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 18:52:37 +07:00
|
|
|
"git.nochill.in/nochill/naice_pos/util"
|
2023-03-05 23:35:41 +07:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createRandomProduct(t *testing.T) (Product, CreateProductParams) {
|
|
|
|
|
2023-03-12 11:01:43 +07:00
|
|
|
sellingPrice := util.RandomFloat(999, 99999)
|
|
|
|
purchasePrice := util.RandomFloat(999, 9999)
|
|
|
|
stock := util.RandomFloat(10, 10000)
|
2023-03-06 15:15:11 +07:00
|
|
|
|
2023-03-05 23:35:41 +07:00
|
|
|
arg := CreateProductParams{
|
2023-03-07 18:52:37 +07:00
|
|
|
MerchantID: uuid.MustParse("d9b0e126-991e-46ac-8c61-5efdd605f75d"),
|
2023-03-05 23:35:41 +07:00
|
|
|
Name: util.RandomString(10),
|
2023-03-06 15:15:11 +07:00
|
|
|
SellingPrice: sellingPrice,
|
|
|
|
PurchasePrice: purchasePrice,
|
|
|
|
Stock: stock,
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
product, err := testQueries.CreateProduct(context.Background(), arg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return product, arg
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateProduct(t *testing.T) {
|
|
|
|
product, arg := createRandomProduct(t)
|
|
|
|
require.NotEmpty(t, product)
|
|
|
|
|
|
|
|
require.Equal(t, arg.Name, product.Name)
|
|
|
|
require.Equal(t, arg.MerchantID, product.MerchantID)
|
|
|
|
require.Equal(t, arg.SellingPrice, product.SellingPrice)
|
|
|
|
require.Equal(t, arg.PurchasePrice, product.PurchasePrice)
|
|
|
|
require.Equal(t, arg.Stock, product.Stock)
|
|
|
|
|
|
|
|
require.NotZero(t, product.ID)
|
|
|
|
require.NotZero(t, product.CreatedAt)
|
|
|
|
require.NotZero(t, product.UpdatedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetProduct(t *testing.T) {
|
|
|
|
createProduct, _ := createRandomProduct(t)
|
|
|
|
getProduct, err := testQueries.GetProduct(context.Background(), createProduct.ID)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, getProduct)
|
|
|
|
|
|
|
|
require.Equal(t, createProduct.ID, getProduct.ID)
|
|
|
|
require.Equal(t, createProduct.MerchantID, getProduct.MerchantID)
|
|
|
|
require.Equal(t, createProduct.Name, getProduct.Name)
|
|
|
|
require.Equal(t, createProduct.PurchasePrice, getProduct.PurchasePrice)
|
|
|
|
require.Equal(t, createProduct.SellingPrice, getProduct.SellingPrice)
|
|
|
|
require.Equal(t, createProduct.Stock, getProduct.Stock)
|
|
|
|
|
|
|
|
require.WithinDuration(t, createProduct.CreatedAt.Time, getProduct.CreatedAt.Time, time.Second)
|
|
|
|
require.WithinDuration(t, createProduct.UpdatedAt.Time, getProduct.UpdatedAt.Time, time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateProduct(t *testing.T) {
|
|
|
|
createProduct, _ := createRandomProduct(t)
|
|
|
|
getProduct, err := testQueries.GetProduct(context.Background(), createProduct.ID)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
arg := UpdateProductParams{
|
|
|
|
ID: getProduct.ID,
|
|
|
|
Name: util.RandomString(6),
|
|
|
|
SellingPrice: float64(200),
|
|
|
|
PurchasePrice: float64(200),
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedProduct, err := testQueries.UpdateProduct(context.Background(), arg)
|
|
|
|
|
|
|
|
require.NotEmpty(t, updatedProduct)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, arg.ID, updatedProduct.ID)
|
|
|
|
require.Equal(t, arg.Name, updatedProduct.Name)
|
|
|
|
require.Equal(t, arg.PurchasePrice, updatedProduct.PurchasePrice)
|
|
|
|
require.Equal(t, arg.SellingPrice, updatedProduct.SellingPrice)
|
|
|
|
|
|
|
|
require.NotSame(t, createProduct.Name, updatedProduct.Name)
|
|
|
|
require.NotSame(t, createProduct.SellingPrice, updatedProduct.SellingPrice)
|
|
|
|
require.NotSame(t, createProduct.PurchasePrice, updatedProduct.PurchasePrice)
|
|
|
|
require.NotSame(t, createProduct.Stock, updatedProduct.Stock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteProduct(t *testing.T) {
|
|
|
|
product1, _ := createRandomProduct(t)
|
|
|
|
err := testQueries.DeleteProduct(context.Background(), product1.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
product2, err := testQueries.GetProduct(context.Background(), product1.ID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.EqualError(t, err, sql.ErrNoRows.Error())
|
|
|
|
require.Empty(t, product2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetProducts(t *testing.T) {
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
createRandomProduct(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := ListProductsParams{
|
|
|
|
Limit: 5,
|
2023-03-07 18:52:37 +07:00
|
|
|
Offset: 1,
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
products, err := testQueries.ListProducts(context.Background(), arg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, products, 5)
|
|
|
|
|
|
|
|
for _, product := range products {
|
|
|
|
require.NotEmpty(t, product)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|