github.com/zhongdalu/gf@v1.0.0/g/database/gdb/gdb_unit_z_struct_test.go (about)

     1  // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package gdb_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/zhongdalu/gf/g"
    13  	"github.com/zhongdalu/gf/g/os/gtime"
    14  	"github.com/zhongdalu/gf/g/test/gtest"
    15  )
    16  
    17  func Test_Model_Inherit_Insert(t *testing.T) {
    18  	table := createTable()
    19  	defer dropTable(table)
    20  
    21  	gtest.Case(t, func() {
    22  		type Base struct {
    23  			Id         int    `json:"id"`
    24  			Uid        int    `json:"uid"`
    25  			CreateTime string `json:"create_time"`
    26  		}
    27  		type User struct {
    28  			Base
    29  			Passport string `json:"passport"`
    30  			Password string `json:"password"`
    31  			Nickname string `json:"nickname"`
    32  		}
    33  		result, err := db.Table(table).Filter().Data(User{
    34  			Passport: "john-test",
    35  			Password: "123456",
    36  			Nickname: "John",
    37  			Base: Base{
    38  				Id:         100,
    39  				Uid:        100,
    40  				CreateTime: gtime.Now().String(),
    41  			},
    42  		}).Insert()
    43  		gtest.Assert(err, nil)
    44  		n, _ := result.RowsAffected()
    45  		gtest.Assert(n, 1)
    46  		value, err := db.Table(table).Fields("passport").Where("id=100").Value()
    47  		gtest.Assert(err, nil)
    48  		gtest.Assert(value.String(), "john-test")
    49  	})
    50  }
    51  
    52  func Test_Model_Inherit_MapToStruct(t *testing.T) {
    53  	table := createTable()
    54  	defer dropTable(table)
    55  
    56  	gtest.Case(t, func() {
    57  		type Ids struct {
    58  			Id  int `json:"id"`
    59  			Uid int `json:"uid"`
    60  		}
    61  		type Base struct {
    62  			Ids
    63  			CreateTime string `json:"create_time"`
    64  		}
    65  		type User struct {
    66  			Base
    67  			Passport string `json:"passport"`
    68  			Password string `json:"password"`
    69  			Nickname string `json:"nickname"`
    70  		}
    71  		data := g.Map{
    72  			"id":          100,
    73  			"uid":         101,
    74  			"passport":    "t1",
    75  			"password":    "123456",
    76  			"nickname":    "T1",
    77  			"create_time": gtime.Now().String(),
    78  		}
    79  		result, err := db.Table(table).Filter().Data(data).Insert()
    80  		gtest.Assert(err, nil)
    81  		n, _ := result.RowsAffected()
    82  		gtest.Assert(n, 1)
    83  
    84  		one, err := db.Table(table).Where("id=100").One()
    85  		gtest.Assert(err, nil)
    86  
    87  		user := new(User)
    88  
    89  		gtest.Assert(one.ToStruct(user), nil)
    90  		gtest.Assert(user.Id, data["id"])
    91  		gtest.Assert(user.Passport, data["passport"])
    92  		gtest.Assert(user.Password, data["password"])
    93  		gtest.Assert(user.Nickname, data["nickname"])
    94  		gtest.Assert(user.CreateTime, data["create_time"])
    95  
    96  	})
    97  
    98  }