naise_pos/db/sqlc/product_test.go
2023-04-05 14:04:49 +07:00

134 lines
4.0 KiB
Go

package db
import (
"context"
"database/sql"
"testing"
"time"
"git.nochill.in/nochill/naice_pos/util"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func createRandomProduct(t *testing.T) (Product, CreateProductParams) {
sellingPrice := util.RandomFloat(999, 99999)
purchasePrice := util.RandomFloat(999, 9999)
stock := util.RandomFloat(10, 10000)
productCategory := createRandomProductCategory(t)
arg := CreateProductParams{
MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"),
ProductTypeID: 1,
Name: util.RandomString(10),
SellingPrice: sellingPrice,
PurchasePrice: purchasePrice,
ProductCategoryID: productCategory.ID,
Stock: stock,
Image: sql.NullString{Valid: false},
}
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) {
var lastProduct Product
for i := 0; i < 6; i++ {
lastProduct, _ = createRandomProduct(t)
}
arg := ListProductsParams{
MerchantID: lastProduct.MerchantID,
Limit: 5,
Offset: 0,
}
products, err := testQueries.ListProducts(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, products)
for _, product := range products {
require.NotEmpty(t, product)
require.Equal(t, lastProduct.MerchantID, product.MerchantID)
}
}