github.heygears.com/openimsdk/tools@v0.0.49/utils/datautil/datautil_test.go (about)

     1  // Copyright © 2023 OpenIM. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package datautil
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"testing"
    21  )
    22  
    23  func TestSliceSubFunc(t *testing.T) {
    24  	type A struct {
    25  		ID  string
    26  		Num int
    27  	}
    28  	arr1 := []A{{ID: "1", Num: 1}, {ID: "2", Num: 2}, {ID: "3", Num: 3}, {ID: "4", Num: 4}, {ID: "5", Num: 5}}
    29  	arr2 := []A{{ID: "2", Num: 2}, {ID: "4", Num: 3}, {ID: "5", Num: 3}}
    30  	fmt.Println(SliceSubFunc(arr1, arr2, func(i A) string { return i.ID }))
    31  	// [{1 1} {3 3}]
    32  }
    33  
    34  func TestSliceSub(t *testing.T) {
    35  	type A struct {
    36  		ID  string
    37  		Num int
    38  	}
    39  	arr1 := []A{{ID: "1", Num: 1}, {ID: "2", Num: 2}, {ID: "3", Num: 3}, {ID: "4", Num: 4}, {ID: "5", Num: 5}}
    40  	arr2 := []A{{ID: "2", Num: 2}, {ID: "4", Num: 3}, {ID: "5", Num: 3}}
    41  	fmt.Println(SliceSub(arr1, arr2))
    42  	// [{1 1} {3 3} {4 4} {5 5}]
    43  }
    44  
    45  func TestDistinct(t *testing.T) {
    46  	arr := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
    47  	fmt.Println(Distinct(arr))
    48  }
    49  
    50  func TestDeleteAt(t *testing.T) {
    51  	arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    52  	fmt.Println(Delete(arr, 0, 1, -1, -2))
    53  	fmt.Println(Delete(arr))
    54  	fmt.Println(Delete(arr, 1))
    55  }
    56  
    57  func TestSliceToMap(t *testing.T) {
    58  	type Item struct {
    59  		ID   string
    60  		Name string
    61  	}
    62  	list := []Item{
    63  		{ID: "111", Name: "111"},
    64  		{ID: "222", Name: "222"},
    65  		{ID: "333", Name: "333"},
    66  	}
    67  
    68  	m := SliceToMap(list, func(t Item) string {
    69  		return t.ID
    70  	})
    71  
    72  	fmt.Printf("%+v\n", m)
    73  
    74  }
    75  
    76  func TestIndexOf(t *testing.T) {
    77  	arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    78  
    79  	fmt.Println(IndexOf(3, arr...))
    80  
    81  }
    82  
    83  func TestSort(t *testing.T) {
    84  	arr := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
    85  	fmt.Println(Sort(arr, false))
    86  }
    87  
    88  func TestBothExist(t *testing.T) {
    89  	arr1 := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
    90  	arr2 := []int{6, 1, 3}
    91  	arr3 := []int{5, 1, 3, 6}
    92  	fmt.Println(BothExist(arr1, arr2, arr3))
    93  }
    94  
    95  func TestCompleteAny(t *testing.T) {
    96  	type Item struct {
    97  		ID    int
    98  		Value string
    99  	}
   100  
   101  	ids := []int{1, 2, 3, 4, 5, 6, 7, 8}
   102  
   103  	var list []Item
   104  
   105  	for _, id := range ids {
   106  		list = append(list, Item{
   107  			ID:    id,
   108  			Value: fmt.Sprintf("%d", id*1000),
   109  		})
   110  	}
   111  
   112  	DeleteAt(&list, -1)
   113  	DeleteAt(&ids, -1)
   114  
   115  	ok := Complete(ids, Slice(list, func(t Item) int {
   116  		return t.ID
   117  	}))
   118  
   119  	fmt.Printf("%+v\n", ok)
   120  }
   121  
   122  func TestStructFieldNotNilReplace(t *testing.T) {
   123  	type Req struct {
   124  		GroupID      string `json:"groupID"`
   125  		GroupName    string `json:"groupName"`
   126  		Notification string `json:"notification"`
   127  		Introduction string `json:"introduction"`
   128  		Count        int64  `json:"faceURL"`
   129  		OwnerUserID  string `json:"ownerUserID"`
   130  	}
   131  
   132  	tests := []struct {
   133  		name string
   134  		req  Req
   135  		resp Req
   136  		want Req
   137  	}{
   138  		{
   139  			name: "One by one conversion",
   140  			req: Req{
   141  				GroupID:      "groupID",
   142  				GroupName:    "groupName",
   143  				Notification: "notification",
   144  				Introduction: "introduction",
   145  				Count:        123,
   146  				OwnerUserID:  "ownerUserID",
   147  			},
   148  			resp: Req{
   149  				GroupID:      "ID",
   150  				GroupName:    "Name",
   151  				Notification: "notification",
   152  				Introduction: "introduction",
   153  				Count:        456,
   154  				OwnerUserID:  "ownerUserID",
   155  			},
   156  			want: Req{
   157  				GroupID:      "groupID",
   158  				GroupName:    "groupName",
   159  				Notification: "notification",
   160  				Introduction: "introduction",
   161  				Count:        123,
   162  				OwnerUserID:  "ownerUserID",
   163  			},
   164  		},
   165  		{
   166  			name: "Changing the values of some fields",
   167  			req: Req{
   168  				GroupID:      "groupID",
   169  				GroupName:    "groupName",
   170  				Notification: "",
   171  				Introduction: "",
   172  				Count:        123,
   173  				OwnerUserID:  "ownerUserID",
   174  			},
   175  			resp: Req{
   176  				GroupID:      "ID",
   177  				GroupName:    "Name",
   178  				Notification: "notification",
   179  				Introduction: "introduction",
   180  				Count:        456,
   181  				OwnerUserID:  "ownerUserID",
   182  			},
   183  			want: Req{
   184  				GroupID:      "groupID",
   185  				GroupName:    "groupName",
   186  				Notification: "notification",
   187  				Introduction: "introduction",
   188  				Count:        123,
   189  				OwnerUserID:  "ownerUserID",
   190  			},
   191  		},
   192  	}
   193  
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			StructFieldNotNilReplace(&tt.resp, &tt.req)
   197  			fmt.Println(tt.resp)
   198  			if !reflect.DeepEqual(tt.want, tt.resp) {
   199  				t.Errorf("%v have a err,%v", tt.name, tt.want)
   200  			}
   201  		})
   202  	}
   203  
   204  	type Req11 struct {
   205  		GroupID      *string `json:"groupID"`
   206  		GroupName    *string `json:"groupName"`
   207  		Notification *string `json:"notification"`
   208  		Introduction *string `json:"introduction"`
   209  		Count        *int64  `json:"faceURL"`
   210  		OwnerUserID  *string `json:"ownerUserID"`
   211  	}
   212  
   213  	type Req1 struct {
   214  		Re  []*Req
   215  		Re1 *Req
   216  		Re2 Req11
   217  	}
   218  	r := Req{
   219  		GroupID:      "groupID1",
   220  		GroupName:    "groupName2",
   221  		Notification: "1",
   222  		Introduction: "1",
   223  		Count:        123,
   224  		OwnerUserID:  "ownerUserID1",
   225  	}
   226  	tests1 := []struct {
   227  		name string
   228  		req  Req1
   229  		resp Req1
   230  		want Req1
   231  	}{
   232  		{
   233  			name: "name",
   234  			req: Req1{
   235  				Re: []*Req{
   236  					{
   237  						GroupID:      "groupID1",
   238  						GroupName:    "groupName2",
   239  						Notification: "1",
   240  						Introduction: "1",
   241  						Count:        123,
   242  						OwnerUserID:  "ownerUserID1",
   243  					},
   244  					{
   245  						GroupID:      "groupID2",
   246  						GroupName:    "groupName2",
   247  						Notification: "2",
   248  						Introduction: "2",
   249  						Count:        456,
   250  						OwnerUserID:  "ownerUserID2",
   251  					},
   252  				},
   253  				Re1: &r,
   254  				Re2: Req11{
   255  					GroupID:      &r.GroupID,
   256  					GroupName:    &r.GroupName,
   257  					Notification: &r.Notification,
   258  					Introduction: &r.Introduction,
   259  					Count:        &r.Count,
   260  					OwnerUserID:  &r.OwnerUserID,
   261  				},
   262  			},
   263  			resp: Req1{},
   264  			want: Req1{
   265  				Re: []*Req{
   266  					{
   267  						GroupID:      "groupID1",
   268  						GroupName:    "groupName2",
   269  						Notification: "1",
   270  						Introduction: "1",
   271  						Count:        123,
   272  						OwnerUserID:  "ownerUserID1",
   273  					},
   274  					{
   275  						GroupID:      "groupID2",
   276  						GroupName:    "groupName2",
   277  						Notification: "2",
   278  						Introduction: "2",
   279  						Count:        456,
   280  						OwnerUserID:  "ownerUserID2",
   281  					},
   282  				},
   283  				Re1: &r,
   284  				Re2: Req11{
   285  					GroupID:      &r.GroupID,
   286  					GroupName:    &r.GroupName,
   287  					Notification: &r.Notification,
   288  					Introduction: &r.Introduction,
   289  					Count:        &r.Count,
   290  					OwnerUserID:  &r.OwnerUserID,
   291  				},
   292  			},
   293  		},
   294  	}
   295  	for _, tt := range tests1 {
   296  		t.Run(tt.name, func(t *testing.T) {
   297  			StructFieldNotNilReplace(&tt.resp, &tt.req)
   298  			fmt.Println(tt.resp)
   299  			if !reflect.DeepEqual(tt.want, tt.resp) {
   300  				t.Errorf("%v have a err,%v", tt.name, tt.want)
   301  			}
   302  		})
   303  	}
   304  
   305  }