naise_pos/db/sqlc/product_test.go

132 lines
3.9 KiB
Go
Raw Normal View History

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-16 20:37:20 +07:00
productCategory := createRandomProductCategory(t)
2023-03-06 15:15:11 +07:00
2023-03-05 23:35:41 +07:00
arg := CreateProductParams{
2023-03-22 12:06:09 +07:00
MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"),
2023-03-16 20:37:20 +07:00
Name: util.RandomString(10),
SellingPrice: sellingPrice,
PurchasePrice: purchasePrice,
ProductCategoryID: productCategory.ID,
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) {
2023-03-15 15:00:36 +07:00
var lastProduct Product
2023-03-05 23:35:41 +07:00
for i := 0; i < 6; i++ {
2023-03-15 15:00:36 +07:00
lastProduct, _ = createRandomProduct(t)
2023-03-05 23:35:41 +07:00
}
arg := ListProductsParams{
2023-03-15 15:00:36 +07:00
MerchantID: lastProduct.MerchantID,
Limit: 5,
Offset: 0,
2023-03-05 23:35:41 +07:00
}
products, err := testQueries.ListProducts(context.Background(), arg)
require.NoError(t, err)
2023-03-15 15:00:36 +07:00
require.NotEmpty(t, products)
2023-03-05 23:35:41 +07:00
for _, product := range products {
require.NotEmpty(t, product)
2023-03-15 15:00:36 +07:00
require.Equal(t, lastProduct.MerchantID, product.MerchantID)
2023-03-05 23:35:41 +07:00
}
}