// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package query

import (
	"context"
	"fmt"
	"testing"

	"web3/app/job/analysis/storage/mysql/model"

	"gorm.io/gen"
	"gorm.io/gen/field"
	"gorm.io/gorm/clause"
)

func init() {
	InitializeDB()
	err := db.AutoMigrate(&model.ChainInfo{})
	if err != nil {
		fmt.Printf("Error: AutoMigrate(&model.ChainInfo{}) fail: %s", err)
	}
}

func Test_chainInfoQuery(t *testing.T) {
	chainInfo := newChainInfo(db)
	chainInfo = *chainInfo.As(chainInfo.TableName())
	_do := chainInfo.WithContext(context.Background()).Debug()

	primaryKey := field.NewString(chainInfo.TableName(), clause.PrimaryKey)
	_, err := _do.Unscoped().Where(primaryKey.IsNotNull()).Delete()
	if err != nil {
		t.Error("clean table <chain_info> fail:", err)
		return
	}

	_, ok := chainInfo.GetFieldByName("")
	if ok {
		t.Error("GetFieldByName(\"\") from chainInfo success")
	}

	err = _do.Create(&model.ChainInfo{})
	if err != nil {
		t.Error("create item in table <chain_info> fail:", err)
	}

	err = _do.Save(&model.ChainInfo{})
	if err != nil {
		t.Error("create item in table <chain_info> fail:", err)
	}

	err = _do.CreateInBatches([]*model.ChainInfo{{}, {}}, 10)
	if err != nil {
		t.Error("create item in table <chain_info> fail:", err)
	}

	_, err = _do.Select(chainInfo.ALL).Take()
	if err != nil {
		t.Error("Take() on table <chain_info> fail:", err)
	}

	_, err = _do.First()
	if err != nil {
		t.Error("First() on table <chain_info> fail:", err)
	}

	_, err = _do.Last()
	if err != nil {
		t.Error("First() on table <chain_info> fail:", err)
	}

	_, err = _do.Where(primaryKey.IsNotNull()).FindInBatch(10, func(tx gen.Dao, batch int) error { return nil })
	if err != nil {
		t.Error("FindInBatch() on table <chain_info> fail:", err)
	}

	err = _do.Where(primaryKey.IsNotNull()).FindInBatches(&[]*model.ChainInfo{}, 10, func(tx gen.Dao, batch int) error { return nil })
	if err != nil {
		t.Error("FindInBatches() on table <chain_info> fail:", err)
	}

	_, err = _do.Select(chainInfo.ALL).Where(primaryKey.IsNotNull()).Order(primaryKey.Desc()).Find()
	if err != nil {
		t.Error("Find() on table <chain_info> fail:", err)
	}

	_, err = _do.Distinct(primaryKey).Take()
	if err != nil {
		t.Error("select Distinct() on table <chain_info> fail:", err)
	}

	_, err = _do.Select(chainInfo.ALL).Omit(primaryKey).Take()
	if err != nil {
		t.Error("Omit() on table <chain_info> fail:", err)
	}

	_, err = _do.Group(primaryKey).Find()
	if err != nil {
		t.Error("Group() on table <chain_info> fail:", err)
	}

	_, err = _do.Scopes(func(dao gen.Dao) gen.Dao { return dao.Where(primaryKey.IsNotNull()) }).Find()
	if err != nil {
		t.Error("Scopes() on table <chain_info> fail:", err)
	}

	_, _, err = _do.FindByPage(0, 1)
	if err != nil {
		t.Error("FindByPage() on table <chain_info> fail:", err)
	}

	_, err = _do.ScanByPage(&model.ChainInfo{}, 0, 1)
	if err != nil {
		t.Error("ScanByPage() on table <chain_info> fail:", err)
	}

	_, err = _do.Attrs(primaryKey).Assign(primaryKey).FirstOrInit()
	if err != nil {
		t.Error("FirstOrInit() on table <chain_info> fail:", err)
	}

	_, err = _do.Attrs(primaryKey).Assign(primaryKey).FirstOrCreate()
	if err != nil {
		t.Error("FirstOrCreate() on table <chain_info> fail:", err)
	}

	var _a _another
	var _aPK = field.NewString(_a.TableName(), clause.PrimaryKey)

	err = _do.Join(&_a, primaryKey.EqCol(_aPK)).Scan(map[string]interface{}{})
	if err != nil {
		t.Error("Join() on table <chain_info> fail:", err)
	}

	err = _do.LeftJoin(&_a, primaryKey.EqCol(_aPK)).Scan(map[string]interface{}{})
	if err != nil {
		t.Error("LeftJoin() on table <chain_info> fail:", err)
	}

	_, err = _do.Not().Or().Clauses().Take()
	if err != nil {
		t.Error("Not/Or/Clauses on table <chain_info> fail:", err)
	}
}
