Improving tests
This commit is contained in:
parent
911cdd3019
commit
fa12207e07
149
save_test.go
149
save_test.go
|
@ -10,8 +10,8 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"git.bit5.ru/backend/dbmeta"
|
||||
"git.bit5.ru/backend/db"
|
||||
"git.bit5.ru/backend/dbmeta"
|
||||
"git.bit5.ru/backend/meta"
|
||||
|
||||
"github.com/go-logr/stdr"
|
||||
|
@ -19,38 +19,40 @@ import (
|
|||
|
||||
var logger = stdr.New(log.New(os.Stdout, "", log.Lshortfile))
|
||||
|
||||
//TODO create the tables below before tests
|
||||
//CREATE TABLE `player` (
|
||||
// `id` int unsigned NOT NULL,
|
||||
// `client_version` char(16) NOT NULL DEFAULT '',
|
||||
// `reg_time` int unsigned NOT NULL DEFAULT '0',
|
||||
// `name` varchar(255) NOT NULL DEFAULT '',
|
||||
// `utc_delta` tinyint(1) NOT NULL DEFAULT '0',
|
||||
// PRIMARY KEY (`id`)
|
||||
//) ENGINE=InnoDB DEFAULT
|
||||
//
|
||||
// CREATE TABLE `item` (
|
||||
// `player_id` int unsigned NOT NULL DEFAULT '0',
|
||||
// `id` int unsigned NOT NULL DEFAULT '0',
|
||||
// `proto_id` int unsigned NOT NULL DEFAULT '0',
|
||||
// `amount` bigint NOT NULL DEFAULT '0',
|
||||
// PRIMARY KEY (`player_id`,`id`),
|
||||
// KEY `proto_id` (`proto_id`)
|
||||
//) ENGINE=InnoDB DEFAULT
|
||||
|
||||
func getDBC() *db.DBC {
|
||||
s := db.Settings{Host: "127.0.0.1", Port: "3306", User: "root", Pass: "test", Name: "tests_shard_1", Prefix: "tests"}
|
||||
s := db.Settings{Host: "127.0.0.1", Port: "3306", User: "root", Pass: "test", Name: "tests", Prefix: "tests"}
|
||||
dbc := db.GetDBC(db.OpenPool(s), logger)
|
||||
return dbc
|
||||
}
|
||||
|
||||
func cleanStorage(db *db.DBC) {
|
||||
var tables []string
|
||||
db.SelectBySQL("SHOW TABLES").LoadValues(&tables)
|
||||
for _, t := range tables {
|
||||
_, err := db.DeleteFrom(t).Exec()
|
||||
func setupStorage(db *db.DBC) {
|
||||
|
||||
var sqls []string
|
||||
sqls = append(sqls, "DROP TABLE IF EXISTS player")
|
||||
sqls = append(sqls, `CREATE TABLE player (
|
||||
id int unsigned NOT NULL,
|
||||
client_version char(16) NOT NULL DEFAULT '',
|
||||
reg_time int unsigned NOT NULL DEFAULT '0',
|
||||
name varchar(255) NOT NULL DEFAULT '',
|
||||
utc_delta tinyint(1) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB
|
||||
`)
|
||||
sqls = append(sqls, "DROP TABLE IF EXISTS item")
|
||||
sqls = append(sqls, `CREATE TABLE item (
|
||||
player_id int unsigned NOT NULL DEFAULT '0',
|
||||
id int unsigned NOT NULL DEFAULT '0',
|
||||
proto_id int unsigned NOT NULL DEFAULT '0',
|
||||
amount bigint NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (player_id,id),
|
||||
KEY proto_id (proto_id)
|
||||
) ENGINE=InnoDB
|
||||
`)
|
||||
|
||||
for _, sql := range sqls {
|
||||
_, err := db.DB().Exec(sql)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
panic(sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +61,7 @@ func TestSaveRow(t *testing.T) {
|
|||
ctx := context.TODO()
|
||||
|
||||
conn := getDBC()
|
||||
cleanStorage(conn)
|
||||
setupStorage(conn)
|
||||
|
||||
//DataPlayer index
|
||||
//id 0
|
||||
|
@ -84,7 +86,7 @@ func TestSaveItemCollectionWithMask(t *testing.T) {
|
|||
ctx := context.TODO()
|
||||
|
||||
conn := getDBC()
|
||||
cleanStorage(conn)
|
||||
setupStorage(conn)
|
||||
|
||||
ownerId := uint32(1)
|
||||
|
||||
|
@ -128,7 +130,7 @@ func TestSaveItemCollectionWithoutMask(t *testing.T) {
|
|||
ctx := context.TODO()
|
||||
|
||||
conn := getDBC()
|
||||
cleanStorage(conn)
|
||||
setupStorage(conn)
|
||||
|
||||
ownerId := uint32(1)
|
||||
|
||||
|
@ -177,19 +179,17 @@ func TestRemoveIds(t *testing.T) {
|
|||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
type DataPlayer struct {
|
||||
|
||||
Id uint32 `json:"id" db:"id"`
|
||||
Client_version string `json:"client_version" db:"client_version"`
|
||||
Reg_time uint32 `json:"reg_time" db:"reg_time"`
|
||||
Name string `json:"name" db:"name"`
|
||||
Utc_delta int32 `json:"utc_delta" db:"utc_delta"`
|
||||
fieldsMask meta.FieldsMask
|
||||
|
||||
}
|
||||
|
||||
var _DataPlayer_class_props map[string]string = map[string]string{"POD": "","bitfields": "","pkey": "id","table": "player","owner": "id","id": "id","cloneable": "",}
|
||||
var _DataPlayer_class_props map[string]string = map[string]string{"POD": "", "bitfields": "", "pkey": "id", "table": "player", "owner": "id", "id": "id", "cloneable": ""}
|
||||
var _DataPlayer_class_fields []string = []string{"id", "client_version", "reg_time", "name", "utc_delta"}
|
||||
var _DataPlayer_fields_props meta.ClassFieldsProps = map[string]map[string]string{"Id" : map[string]string{"optional": "1",},"Client_version" : map[string]string{"strmax": "16","optional": "1",},"Reg_time" : map[string]string{"default": "","optional": "1",},"Name" : map[string]string{"default": "\"\"","optional": "1",},"Utc_delta" : map[string]string{"default": "","optional": "1",}}
|
||||
var _DataPlayer_fields_props meta.ClassFieldsProps = map[string]map[string]string{"Id": map[string]string{"optional": "1"}, "Client_version": map[string]string{"strmax": "16", "optional": "1"}, "Reg_time": map[string]string{"default": "", "optional": "1"}, "Name": map[string]string{"default": "\"\"", "optional": "1"}, "Utc_delta": map[string]string{"default": "", "optional": "1"}}
|
||||
|
||||
func DataPlayer_CLASS_ID() uint32 {
|
||||
return 74407040
|
||||
|
@ -281,7 +281,9 @@ func (self *DataPlayer) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(0)) {
|
||||
if err := reader.ReadU32(&self.Id, "id"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadU32(&self.Id, "id"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
if _cont_size <= 0 {
|
||||
return nil
|
||||
|
@ -290,7 +292,9 @@ func (self *DataPlayer) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(1)) {
|
||||
if err := reader.ReadString(&self.Client_version, "client_version"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadString(&self.Client_version, "client_version"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
if _cont_size <= 0 {
|
||||
return nil
|
||||
|
@ -299,7 +303,9 @@ func (self *DataPlayer) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(2)) {
|
||||
if err := reader.ReadU32(&self.Reg_time, "reg_time"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadU32(&self.Reg_time, "reg_time"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
if _cont_size <= 0 {
|
||||
return nil
|
||||
|
@ -308,7 +314,9 @@ func (self *DataPlayer) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(3)) {
|
||||
if err := reader.ReadString(&self.Name, "name"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadString(&self.Name, "name"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
if _cont_size <= 0 {
|
||||
return nil
|
||||
|
@ -317,7 +325,9 @@ func (self *DataPlayer) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(4)) {
|
||||
if err := reader.ReadI32(&self.Utc_delta, "utc_delta"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadI32(&self.Utc_delta, "utc_delta"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -328,15 +338,25 @@ func (self *DataPlayer) Write(writer meta.Writer) error {
|
|||
|
||||
func (self *DataPlayer) WriteFields(writer meta.Writer) error {
|
||||
|
||||
if err := writer.WriteU32(self.Id, "id"); err != nil { return err }
|
||||
if err := writer.WriteU32(self.Id, "id"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.WriteString(self.Client_version, "client_version"); err != nil { return err }
|
||||
if err := writer.WriteString(self.Client_version, "client_version"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.WriteU32(self.Reg_time, "reg_time"); err != nil { return err }
|
||||
if err := writer.WriteU32(self.Reg_time, "reg_time"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.WriteString(self.Name, "name"); err != nil { return err }
|
||||
if err := writer.WriteString(self.Name, "name"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.WriteI32(self.Utc_delta, "utc_delta"); err != nil { return err }
|
||||
if err := writer.WriteI32(self.Utc_delta, "utc_delta"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -355,18 +375,16 @@ func(self *DataPlayer) GetMask() meta.FieldsMask {
|
|||
}
|
||||
|
||||
type DataItem struct {
|
||||
|
||||
Id uint32 `json:"id" db:"id"`
|
||||
Player_id uint32 `json:"player_id" db:"player_id"`
|
||||
Proto_id uint32 `json:"proto_id" db:"proto_id"`
|
||||
Amount int64 `json:"amount" db:"amount"`
|
||||
fieldsMask meta.FieldsMask
|
||||
|
||||
}
|
||||
|
||||
var _DataItem_class_props map[string]string = map[string]string{"POD": "","table": "item","id": "id","owner": "player_id","bitfields": "",}
|
||||
var _DataItem_class_fields []string = []string{"id","player_id","proto_id","amount",}
|
||||
var _DataItem_fields_props meta.ClassFieldsProps = map[string]map[string]string{"Id" : map[string]string{"optional": "1",},"Player_id" : map[string]string{"optional": "1",},"Proto_id" : map[string]string{"obscured": "","optional": "1",},"Amount" : map[string]string{"obscured": "","optional": "1",},}
|
||||
var _DataItem_class_props map[string]string = map[string]string{"POD": "", "table": "item", "id": "id", "owner": "player_id", "bitfields": ""}
|
||||
var _DataItem_class_fields []string = []string{"id", "player_id", "proto_id", "amount"}
|
||||
var _DataItem_fields_props meta.ClassFieldsProps = map[string]map[string]string{"Id": map[string]string{"optional": "1"}, "Player_id": map[string]string{"optional": "1"}, "Proto_id": map[string]string{"obscured": "", "optional": "1"}, "Amount": map[string]string{"obscured": "", "optional": "1"}}
|
||||
|
||||
func DataItem_CLASS_ID() uint32 {
|
||||
return 263721017
|
||||
|
@ -457,7 +475,9 @@ func (self *DataItem) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(0)) {
|
||||
if err := reader.ReadU32(&self.Id, "id"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadU32(&self.Id, "id"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
if _cont_size <= 0 {
|
||||
return nil
|
||||
|
@ -466,7 +486,9 @@ func (self *DataItem) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(1)) {
|
||||
if err := reader.ReadU32(&self.Player_id, "player_id"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadU32(&self.Player_id, "player_id"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
if _cont_size <= 0 {
|
||||
return nil
|
||||
|
@ -475,7 +497,9 @@ func (self *DataItem) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(2)) {
|
||||
if err := reader.ReadU32(&self.Proto_id, "proto_id"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadU32(&self.Proto_id, "proto_id"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
if _cont_size <= 0 {
|
||||
return nil
|
||||
|
@ -484,7 +508,9 @@ func (self *DataItem) ReadFields(reader meta.Reader) error {
|
|||
_cont_size--
|
||||
}
|
||||
if !use_mask || (use_mask && self.HasValue(3)) {
|
||||
if err := reader.ReadI64(&self.Amount, "amount"); err != nil { return /*optional*/nil }
|
||||
if err := reader.ReadI64(&self.Amount, "amount"); err != nil {
|
||||
return /*optional*/ nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -495,13 +521,21 @@ func (self *DataItem) Write(writer meta.Writer) error {
|
|||
|
||||
func (self *DataItem) WriteFields(writer meta.Writer) error {
|
||||
|
||||
if err := writer.WriteU32(self.Id, "id"); err != nil { return err }
|
||||
if err := writer.WriteU32(self.Id, "id"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.WriteU32(self.Player_id, "player_id"); err != nil { return err }
|
||||
if err := writer.WriteU32(self.Player_id, "player_id"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.WriteU32(self.Proto_id, "proto_id"); err != nil { return err }
|
||||
if err := writer.WriteU32(self.Proto_id, "proto_id"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := writer.WriteI64(self.Amount, "amount"); err != nil { return err }
|
||||
if err := writer.WriteI64(self.Amount, "amount"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -567,4 +601,3 @@ func (self *DataItem) Export(data []interface{}) {
|
|||
data[2] = self.Proto_id
|
||||
data[3] = self.Amount
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue