12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package exchange_test
- import (
- "fmt"
- "os"
- "testing"
- "time"
- "gorm.io/driver/sqlite"
- "gorm.io/gorm"
- "gorm.io/gorm/logger"
- )
- var (
- db *gorm.DB
- )
- var tables = []interface{}{
- &TestExchangeModel{},
- &TestExchangeCompositePrimaryKeyModel{},
- &Phone{},
- &Intro{},
- &ExtraIntro{},
- &Camera{},
- &ShoppingSite{},
- }
- type TestExchangeModel struct {
- ID uint `gorm:"primarykey"`
- Name string
- Age *int
- Birth *time.Time
- Appender string
- }
- type TestExchangeCompositePrimaryKeyModel struct {
- ID uint `gorm:"primarykey"`
- Name string `gorm:"primarykey"`
- Age *int
- Appender string
- }
- func TestMain(m *testing.M) {
- var err error
- db, err = gorm.Open(sqlite.Open("/tmp/test_exchange.db"), &gorm.Config{
- DisableForeignKeyConstraintWhenMigrating: true,
- })
- if err != nil {
- panic(err)
- }
- db.Logger = db.Logger.LogMode(logger.Info)
- migrateTables()
- s := m.Run()
- // dropTables()
- os.Exit(s)
- }
- func migrateTables() {
- if err := db.AutoMigrate(tables...); err != nil {
- panic(err)
- }
- }
- func dropTables() {
- var err error
- for _, m := range tables {
- stmt := &gorm.Statement{DB: db}
- stmt.Parse(m)
- err = db.Exec(fmt.Sprintf("drop table %s", stmt.Schema.Table)).Error
- if err != nil {
- panic(err)
- }
- }
- err = db.Exec(fmt.Sprintf("drop table phone_selling_shopping_site")).Error
- if err != nil {
- panic(err)
- }
- }
- func initTables() {
- dropTables()
- migrateTables()
- }
- func ptrInt(v int) *int {
- return &v
- }
- func ptrTime(v time.Time) *time.Time {
- return &v
- }
|