135 lines
3.8 KiB
Go
135 lines
3.8 KiB
Go
package pricelist
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.mchus.pro/mchus/priceforge/internal/models"
|
|
"git.mchus.pro/mchus/priceforge/internal/repository"
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestCreateEstimatePricelist_ExcludesHiddenLots(t *testing.T) {
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
|
|
if err := db.AutoMigrate(
|
|
&models.Pricelist{},
|
|
&models.PricelistItem{},
|
|
&models.Lot{},
|
|
); err != nil {
|
|
t.Fatalf("automigrate: %v", err)
|
|
}
|
|
|
|
if err := db.Exec(`
|
|
CREATE TABLE qt_lot_metadata (
|
|
lot_name TEXT PRIMARY KEY,
|
|
current_price REAL,
|
|
price_method TEXT,
|
|
price_period_days INTEGER,
|
|
price_coefficient REAL,
|
|
manual_price REAL,
|
|
meta_prices TEXT,
|
|
is_hidden INTEGER
|
|
)
|
|
`).Error; err != nil {
|
|
t.Fatalf("create qt_lot_metadata: %v", err)
|
|
}
|
|
|
|
catStorage := "STORAGE"
|
|
if err := db.Create(&models.Lot{LotName: "SSD_VISIBLE", LotDescription: "Visible", LotCategory: &catStorage}).Error; err != nil {
|
|
t.Fatalf("seed visible lot: %v", err)
|
|
}
|
|
if err := db.Create(&models.Lot{LotName: "SSD_HIDDEN", LotDescription: "Hidden", LotCategory: &catStorage}).Error; err != nil {
|
|
t.Fatalf("seed hidden lot: %v", err)
|
|
}
|
|
|
|
if err := db.Exec(`
|
|
INSERT INTO qt_lot_metadata (lot_name, current_price, price_method, price_period_days, price_coefficient, manual_price, meta_prices, is_hidden)
|
|
VALUES
|
|
('SSD_VISIBLE', 100.0, 'median', 90, 0, NULL, '', 0),
|
|
('SSD_HIDDEN', 200.0, 'median', 90, 0, NULL, '', 1)
|
|
`).Error; err != nil {
|
|
t.Fatalf("seed metadata: %v", err)
|
|
}
|
|
|
|
repo := repository.NewPricelistRepository(db)
|
|
svc := NewService(db, repo, nil, nil)
|
|
|
|
pl, err := svc.CreateForSourceWithProgress("tester", string(models.PricelistSourceEstimate), nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("create estimate pricelist: %v", err)
|
|
}
|
|
|
|
var items []models.PricelistItem
|
|
if err := db.Where("pricelist_id = ?", pl.ID).Order("lot_name").Find(&items).Error; err != nil {
|
|
t.Fatalf("load pricelist items: %v", err)
|
|
}
|
|
if len(items) != 1 {
|
|
t.Fatalf("expected 1 item (visible only), got %d", len(items))
|
|
}
|
|
if items[0].LotName != "SSD_VISIBLE" {
|
|
t.Fatalf("unexpected lot in estimate pricelist: %s", items[0].LotName)
|
|
}
|
|
}
|
|
|
|
func TestCreateEstimatePricelist_DefaultCategoryWhenLotMissing(t *testing.T) {
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
|
|
if err := db.AutoMigrate(
|
|
&models.Pricelist{},
|
|
&models.PricelistItem{},
|
|
&models.Lot{},
|
|
&models.Category{},
|
|
); err != nil {
|
|
t.Fatalf("automigrate: %v", err)
|
|
}
|
|
|
|
if err := db.Exec(`
|
|
CREATE TABLE qt_lot_metadata (
|
|
lot_name TEXT PRIMARY KEY,
|
|
current_price REAL,
|
|
price_method TEXT,
|
|
price_period_days INTEGER,
|
|
price_coefficient REAL,
|
|
manual_price REAL,
|
|
meta_prices TEXT,
|
|
is_hidden INTEGER
|
|
)
|
|
`).Error; err != nil {
|
|
t.Fatalf("create qt_lot_metadata: %v", err)
|
|
}
|
|
|
|
if err := db.Exec(`
|
|
INSERT INTO qt_lot_metadata (lot_name, current_price, price_method, price_period_days, price_coefficient, manual_price, meta_prices, is_hidden)
|
|
VALUES ('LOT_WITHOUT_ROW', 150.0, 'median', 90, 0, NULL, '', 0)
|
|
`).Error; err != nil {
|
|
t.Fatalf("seed metadata: %v", err)
|
|
}
|
|
|
|
repo := repository.NewPricelistRepository(db)
|
|
svc := NewService(db, repo, nil, nil)
|
|
|
|
pl, err := svc.CreateForSourceWithProgress("tester", string(models.PricelistSourceEstimate), nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("create estimate pricelist: %v", err)
|
|
}
|
|
|
|
var item models.PricelistItem
|
|
if err := db.Where("pricelist_id = ? AND lot_name = ?", pl.ID, "LOT_WITHOUT_ROW").First(&item).Error; err != nil {
|
|
t.Fatalf("load pricelist item: %v", err)
|
|
}
|
|
if item.LotCategory == nil || *item.LotCategory != models.DefaultLotCategoryCode {
|
|
got := "<nil>"
|
|
if item.LotCategory != nil {
|
|
got = *item.LotCategory
|
|
}
|
|
t.Fatalf("expected default category %q, got %q", models.DefaultLotCategoryCode, got)
|
|
}
|
|
}
|