feat 添加测试用例

This commit is contained in:
timerzz 2024-08-04 20:46:49 +08:00
parent 015781e484
commit 4bef487066
4 changed files with 82 additions and 65 deletions

View File

@ -6,10 +6,6 @@ import (
"gorm.io/gorm/clause"
)
type Storage struct {
ArticleApi
}
type ArticleApi interface {
Upsert(article v2.Article) error
Find(query FindArticleQuery) (articles []v2.Article, err error)

View File

@ -0,0 +1,63 @@
package storage
import (
"fmt"
"testing"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func GetDevDB() (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432")
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
}
var article = v2.Article{
Name: "test1",
EnglishName: "test1",
Pid: "111",
Brand: "col",
Desc: "desc",
Image: "http://123",
Available: false,
CostPrice: 123,
Providers: []v2.ProviderArticle{
{
Pid: "111",
Brand: "col1",
Link: "http://123",
ProviderId: "coach2",
SkuID: "sku1",
Cost: v2.ProviderPrice{
OriginalPrice: 111,
FinalPrice: 111,
CalMark: "uuuuu",
},
HistoryPrice: []v2.ProviderPrice{
{
OriginalPrice: 111,
FinalPrice: 111,
CalMark: "uuuuu",
},
},
},
},
SellPrice: 111,
Sellers: []v2.SellerArticle{},
Rate: 111,
Remark: "111",
}
func TestUpsetArticle(t *testing.T) {
db, _ := GetDevDB()
storage := NewStorage(db)
err := db.AutoMigrate(&v2.Article{}, &v2.ProviderArticle{}, &v2.ProviderPrice{}, &v2.SellerArticle{}, &v2.SellerPrice{})
if err != nil {
t.Fatal(err)
}
if err = storage.Article().Upsert(article); err != nil {
t.Fatal(err)
}
}

View File

@ -0,0 +1,19 @@
package storage
import "gorm.io/gorm"
type Storage struct {
articleApi ArticleApi
}
func NewStorage(db *gorm.DB) *Storage {
return &Storage{
articleApi: &articleApi{
db: db,
},
}
}
func (s *Storage) Article() ArticleApi {
return s.articleApi
}

View File

@ -1,61 +0,0 @@
package v2
import (
"fmt"
"testing"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func TestModel(t *testing.T) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432")
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
err = db.AutoMigrate(&Article{}, &ProviderArticle{}, &ProviderPrice{}, &SellerArticle{}, &SellerPrice{})
if err != nil {
t.Fatal(err)
}
var article = Article{
Name: "test",
EnglishName: "test",
Pid: "1299999922",
Brand: "col",
Desc: "desc",
Image: "http://123",
Available: false,
CostPrice: 123,
Providers: []ProviderArticle{
{
Pid: "999",
Brand: "col",
Link: "http://123",
ProviderId: "coach",
SkuID: "sku999999",
Cost: ProviderPrice{
OriginalPrice: 000,
FinalPrice: 0000,
CalMark: "uuuuu",
},
HistoryPrice: []ProviderPrice{
{
OriginalPrice: 0000,
FinalPrice: 000,
CalMark: "uuuuu",
},
},
},
},
SellPrice: 321,
Sellers: []SellerArticle{},
Rate: 123,
Remark: "jjjjj",
}
db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "pid"}, {Name: "brand"}},
DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at"}),
}).Create(&article)
}