2023-03-05 23:35:41 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.nochill.in/nochill/nice_pos/util"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createRandomProduct(t *testing.T) (Product, CreateProductParams) {
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
sellingPrice, _ := util.RandomFloat(999, 99999)
|
|
|
|
purchasePrice, _ := util.RandomFloat(999, 9999)
|
|
|
|
stock, _ := util.RandomFloat(10, 10000)
|
|
|
|
|
2023-03-05 23:35:41 +07:00
|
|
|
arg := CreateProductParams{
|
|
|
|
MerchantID: uuid.MustParse("1f81d072-0c98-4e7e-9ceb-233d2eadb674"),
|
|
|
|
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),
|
|
|
|
Stock: 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.Equal(t, arg.Stock, updatedProduct.Stock)
|
|
|
|
|
|
|
|
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,
|
|
|
|
Offset: 5,
|
|
|
|
}
|
|
|
|
|
|
|
|
products, err := testQueries.ListProducts(context.Background(), arg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, products, 5)
|
|
|
|
|
|
|
|
for _, product := range products {
|
|
|
|
require.NotEmpty(t, product)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|