github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_scan_list_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
     6  
     7  package gconv_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/gogf/gf/v2/test/gtest"
    13  	"github.com/gogf/gf/v2/util/gconv"
    14  )
    15  
    16  func TestScanList(t *testing.T) {
    17  	type EntityUser struct {
    18  		Uid  int
    19  		Name string
    20  	}
    21  
    22  	type EntityUserDetail struct {
    23  		Uid     int
    24  		Address string
    25  	}
    26  
    27  	type EntityUserScores struct {
    28  		Id    int
    29  		Uid   int
    30  		Score int
    31  	}
    32  
    33  	// Test for struct attribute.
    34  	gtest.C(t, func(t *gtest.T) {
    35  		type Entity struct {
    36  			User       EntityUser
    37  			UserDetail EntityUserDetail
    38  			UserScores []EntityUserScores
    39  		}
    40  
    41  		var (
    42  			err         error
    43  			entities    []Entity
    44  			entityUsers = []EntityUser{
    45  				{Uid: 1, Name: "name1"},
    46  				{Uid: 2, Name: "name2"},
    47  				{Uid: 3, Name: "name3"},
    48  			}
    49  			userDetails = []EntityUserDetail{
    50  				{Uid: 1, Address: "address1"},
    51  				{Uid: 2, Address: "address2"},
    52  			}
    53  			userScores = []EntityUserScores{
    54  				{Id: 10, Uid: 1, Score: 100},
    55  				{Id: 11, Uid: 1, Score: 60},
    56  				{Id: 20, Uid: 2, Score: 99},
    57  			}
    58  		)
    59  		err = gconv.ScanList(entityUsers, &entities, "User")
    60  		t.AssertNil(err)
    61  
    62  		err = gconv.ScanList(userDetails, &entities, "UserDetail", "User", "uid")
    63  		t.AssertNil(err)
    64  
    65  		err = gconv.ScanList(userScores, &entities, "UserScores", "User", "uid")
    66  		t.AssertNil(err)
    67  
    68  		t.Assert(len(entities), 3)
    69  		t.Assert(entities[0].User, entityUsers[0])
    70  		t.Assert(entities[1].User, entityUsers[1])
    71  		t.Assert(entities[2].User, entityUsers[2])
    72  
    73  		t.Assert(entities[0].UserDetail, userDetails[0])
    74  		t.Assert(entities[1].UserDetail, userDetails[1])
    75  		t.Assert(entities[2].UserDetail, EntityUserDetail{})
    76  
    77  		t.Assert(len(entities[0].UserScores), 2)
    78  		t.Assert(entities[0].UserScores[0], userScores[0])
    79  		t.Assert(entities[0].UserScores[1], userScores[1])
    80  
    81  		t.Assert(len(entities[1].UserScores), 1)
    82  		t.Assert(entities[1].UserScores[0], userScores[2])
    83  
    84  		t.Assert(len(entities[2].UserScores), 0)
    85  	})
    86  
    87  	// Test for pointer attribute.
    88  	gtest.C(t, func(t *gtest.T) {
    89  		type Entity struct {
    90  			User       *EntityUser
    91  			UserDetail *EntityUserDetail
    92  			UserScores []*EntityUserScores
    93  		}
    94  
    95  		var (
    96  			err         error
    97  			entities    []*Entity
    98  			entityUsers = []*EntityUser{
    99  				{Uid: 1, Name: "name1"},
   100  				{Uid: 2, Name: "name2"},
   101  				{Uid: 3, Name: "name3"},
   102  			}
   103  			userDetails = []*EntityUserDetail{
   104  				{Uid: 1, Address: "address1"},
   105  				{Uid: 2, Address: "address2"},
   106  			}
   107  			userScores = []*EntityUserScores{
   108  				{Id: 10, Uid: 1, Score: 100},
   109  				{Id: 11, Uid: 1, Score: 60},
   110  				{Id: 20, Uid: 2, Score: 99},
   111  			}
   112  		)
   113  		err = gconv.ScanList(entityUsers, &entities, "User")
   114  		t.AssertNil(err)
   115  
   116  		err = gconv.ScanList(userDetails, &entities, "UserDetail", "User", "uid")
   117  		t.AssertNil(err)
   118  
   119  		err = gconv.ScanList(userScores, &entities, "UserScores", "User", "uid")
   120  		t.AssertNil(err)
   121  
   122  		t.Assert(len(entities), 3)
   123  		t.Assert(entities[0].User, entityUsers[0])
   124  		t.Assert(entities[1].User, entityUsers[1])
   125  		t.Assert(entities[2].User, entityUsers[2])
   126  
   127  		t.Assert(entities[0].UserDetail, userDetails[0])
   128  		t.Assert(entities[1].UserDetail, userDetails[1])
   129  		t.Assert(entities[2].UserDetail, nil)
   130  
   131  		t.Assert(len(entities[0].UserScores), 2)
   132  		t.Assert(entities[0].UserScores[0], userScores[0])
   133  		t.Assert(entities[0].UserScores[1], userScores[1])
   134  
   135  		t.Assert(len(entities[1].UserScores), 1)
   136  		t.Assert(entities[1].UserScores[0], userScores[2])
   137  
   138  		t.Assert(len(entities[2].UserScores), 0)
   139  	})
   140  
   141  	// Test struct embedded attribute.
   142  	gtest.C(t, func(t *gtest.T) {
   143  		type Entity struct {
   144  			EntityUser
   145  			UserDetail EntityUserDetail
   146  			UserScores []EntityUserScores
   147  		}
   148  
   149  		var (
   150  			err         error
   151  			entities    []Entity
   152  			entityUsers = []EntityUser{
   153  				{Uid: 1, Name: "name1"},
   154  				{Uid: 2, Name: "name2"},
   155  				{Uid: 3, Name: "name3"},
   156  			}
   157  			userDetails = []EntityUserDetail{
   158  				{Uid: 1, Address: "address1"},
   159  				{Uid: 2, Address: "address2"},
   160  			}
   161  			userScores = []EntityUserScores{
   162  				{Id: 10, Uid: 1, Score: 100},
   163  				{Id: 11, Uid: 1, Score: 60},
   164  				{Id: 20, Uid: 2, Score: 99},
   165  			}
   166  		)
   167  		err = gconv.Scan(entityUsers, &entities)
   168  		t.AssertNil(err)
   169  
   170  		err = gconv.ScanList(userDetails, &entities, "UserDetail", "uid")
   171  		t.AssertNil(err)
   172  
   173  		err = gconv.ScanList(userScores, &entities, "UserScores", "uid")
   174  		t.AssertNil(err)
   175  
   176  		t.Assert(len(entities), 3)
   177  		t.Assert(entities[0].EntityUser, entityUsers[0])
   178  		t.Assert(entities[1].EntityUser, entityUsers[1])
   179  		t.Assert(entities[2].EntityUser, entityUsers[2])
   180  
   181  		t.Assert(entities[0].UserDetail, userDetails[0])
   182  		t.Assert(entities[1].UserDetail, userDetails[1])
   183  		t.Assert(entities[2].UserDetail, EntityUserDetail{})
   184  
   185  		t.Assert(len(entities[0].UserScores), 2)
   186  		t.Assert(entities[0].UserScores[0], userScores[0])
   187  		t.Assert(entities[0].UserScores[1], userScores[1])
   188  
   189  		t.Assert(len(entities[1].UserScores), 1)
   190  		t.Assert(entities[1].UserScores[0], userScores[2])
   191  
   192  		t.Assert(len(entities[2].UserScores), 0)
   193  	})
   194  
   195  	// Test struct embedded pointer attribute.
   196  	gtest.C(t, func(t *gtest.T) {
   197  		type Entity struct {
   198  			*EntityUser
   199  			UserDetail *EntityUserDetail
   200  			UserScores []*EntityUserScores
   201  		}
   202  
   203  		var (
   204  			err         error
   205  			entities    []Entity
   206  			entityUsers = []EntityUser{
   207  				{Uid: 1, Name: "name1"},
   208  				{Uid: 2, Name: "name2"},
   209  				{Uid: 3, Name: "name3"},
   210  			}
   211  			userDetails = []EntityUserDetail{
   212  				{Uid: 1, Address: "address1"},
   213  				{Uid: 2, Address: "address2"},
   214  			}
   215  			userScores = []EntityUserScores{
   216  				{Id: 10, Uid: 1, Score: 100},
   217  				{Id: 11, Uid: 1, Score: 60},
   218  				{Id: 20, Uid: 2, Score: 99},
   219  			}
   220  		)
   221  		err = gconv.Scan(entityUsers, &entities)
   222  		t.AssertNil(err)
   223  
   224  		err = gconv.ScanList(userDetails, &entities, "UserDetail", "uid")
   225  		t.AssertNil(err)
   226  
   227  		err = gconv.ScanList(userScores, &entities, "UserScores", "uid")
   228  		t.AssertNil(err)
   229  
   230  		t.Assert(len(entities), 3)
   231  		t.Assert(entities[0].EntityUser, entityUsers[0])
   232  		t.Assert(entities[1].EntityUser, entityUsers[1])
   233  		t.Assert(entities[2].EntityUser, entityUsers[2])
   234  
   235  		t.Assert(entities[0].UserDetail, userDetails[0])
   236  		t.Assert(entities[1].UserDetail, userDetails[1])
   237  		t.Assert(entities[2].UserDetail, nil)
   238  
   239  		t.Assert(len(entities[0].UserScores), 2)
   240  		t.Assert(entities[0].UserScores[0], userScores[0])
   241  		t.Assert(entities[0].UserScores[1], userScores[1])
   242  
   243  		t.Assert(len(entities[1].UserScores), 1)
   244  		t.Assert(entities[1].UserScores[0], userScores[2])
   245  
   246  		t.Assert(len(entities[2].UserScores), 0)
   247  	})
   248  
   249  	// Test for special types.
   250  	gtest.C(t, func(t *gtest.T) {
   251  		type Entity struct {
   252  			User       EntityUser
   253  			UserDetail EntityUserDetail
   254  			UserScores []EntityUserScores
   255  		}
   256  
   257  		var (
   258  			err         error
   259  			entities    []Entity
   260  			entityUsers = []EntityUser{
   261  				{Uid: 1, Name: "name1"},
   262  				{Uid: 2, Name: "name2"},
   263  				{Uid: 3, Name: "name3"},
   264  			}
   265  			userDetails = []EntityUserDetail{
   266  				{Uid: 1, Address: "address1"},
   267  				{Uid: 2, Address: "address2"},
   268  			}
   269  			//userScores = []EntityUserScores{
   270  			//	{Id: 10, Uid: 1, Score: 100},
   271  			//	{Id: 11, Uid: 1, Score: 60},
   272  			//	{Id: 20, Uid: 2, Score: 99},
   273  			//}
   274  		)
   275  
   276  		err = gconv.ScanList(nil, nil, "")
   277  		t.AssertNil(err)
   278  
   279  		err = gconv.ScanList(entityUsers, &entities, "")
   280  		t.AssertNE(err, nil)
   281  
   282  		err = gconv.ScanList(entityUsers, &entities, "User")
   283  		t.AssertNil(err)
   284  
   285  		err = gconv.ScanList(userDetails, entities, "User")
   286  		t.AssertNE(err, nil)
   287  
   288  		var a int = 1
   289  		err = gconv.ScanList(userDetails, &a, "User")
   290  		t.AssertNE(err, nil)
   291  	})
   292  }
   293  
   294  func TestScanListErr(t *testing.T) {
   295  	type EntityUser struct {
   296  		Uid  int
   297  		Name string
   298  	}
   299  
   300  	type EntityUserDetail struct {
   301  		Uid     int
   302  		Address string
   303  	}
   304  
   305  	type EntityUserScores struct {
   306  		Id    int
   307  		Uid   int
   308  		Score int
   309  	}
   310  
   311  	gtest.C(t, func(t *gtest.T) {
   312  		type Entity struct {
   313  			User       EntityUser
   314  			UserDetail EntityUserDetail
   315  			UserScores []EntityUserScores
   316  		}
   317  
   318  		var (
   319  			err         error
   320  			entities    []Entity
   321  			entityUsers = []EntityUser{
   322  				{Uid: 1, Name: "name1"},
   323  				{Uid: 2, Name: "name2"},
   324  				{Uid: 3, Name: "name3"},
   325  			}
   326  			userDetails = []EntityUserDetail{
   327  				{Uid: 1, Address: "address1"},
   328  				{Uid: 2, Address: "address2"},
   329  			}
   330  			userScores = []EntityUserScores{
   331  				{Id: 10, Uid: 1, Score: 100},
   332  				{Id: 11, Uid: 1, Score: 60},
   333  				{Id: 20, Uid: 2, Score: 99},
   334  			}
   335  		)
   336  		err = gconv.ScanList(entityUsers, &entities, "User")
   337  		t.AssertNil(err)
   338  
   339  		err = gconv.ScanList(userDetails, &entities, "UserDetail", "User", "uuid")
   340  		t.AssertNE(err, nil)
   341  
   342  		err = gconv.ScanList(userScores, &entities, "UserScores", "User", "uid")
   343  		t.AssertNil(err)
   344  	})
   345  }