github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/bean/test/bean_test.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/isyscore/isc-gobase/bean"
     5  	"github.com/isyscore/isc-gobase/isc"
     6  	"github.com/magiconair/properties/assert"
     7  	"testing"
     8  )
     9  
    10  func TestAddBean(t *testing.T) {
    11  	tt := TestEntity{Name: "hello", Age: 12}
    12  	bean.AddBean("test", &tt)
    13  
    14  	t1 := bean.GetBean("test")
    15  	t2 := t1.(*TestEntity)
    16  	assert.Equal(t, t2.Name, tt.Name)
    17  
    18  	bean.Clean()
    19  }
    20  
    21  func TestGetFieldShow(t *testing.T) {
    22  	tt := TestEntity{Name: "value"}
    23  	// 添加注册
    24  	bean.AddBean("test", &tt)
    25  
    26  	// 获取值
    27  	actValue := bean.GetField("test", "Name")
    28  	assert.Equal(t, actValue, "value")
    29  
    30  	// 修改值
    31  	bean.SetField("test", "Name", "value-change")
    32  
    33  	// 查看
    34  	actValue = bean.GetField("test", "Name")
    35  	assert.Equal(t, actValue, "value-change")
    36  
    37  	bean.Clean()
    38  }
    39  
    40  func TestGetField(t *testing.T) {
    41  	tt := TestEntity{Name: "hello", Age: 12}
    42  	bean.AddBean("test", &tt)
    43  
    44  	fv := bean.GetField("test", "Name")
    45  	assert.Equal(t, fv, "hello")
    46  
    47  	bean.Clean()
    48  }
    49  
    50  func TestExist(t *testing.T) {
    51  	tt := TestEntity{}
    52  
    53  	bean.AddBean("test", &tt)
    54  	assert.Equal(t, bean.ExistBean("test"), true)
    55  
    56  	bean.Clean()
    57  }
    58  
    59  func TestCallFun(t *testing.T) {
    60  	tt := TestEntity{}
    61  
    62  	bean.AddBean("test", &tt)
    63  	fv := bean.CallFun("test", "Fun", map[string]any{})
    64  	assert.Equal(t, isc.ToString(fv[0]), "ok")
    65  
    66  	bean.Clean()
    67  }
    68  
    69  func TestCallFunPtr(t *testing.T) {
    70  	tt := TestEntity{}
    71  
    72  	bean.AddBean("test", &tt)
    73  	fv := bean.CallFun("test", "Fun", map[string]any{})
    74  	assert.Equal(t, isc.ToString(fv[0]), "ok")
    75  
    76  	bean.Clean()
    77  }
    78  
    79  func TestCallFunUpper(t *testing.T) {
    80  	tt := TestEntity{}
    81  
    82  	bean.AddBean("test", &tt)
    83  	fv := bean.CallFun("test", "Fun", map[string]any{})
    84  	assert.Equal(t, isc.ToString(fv[0]), "ok")
    85  
    86  	bean.Clean()
    87  }
    88  
    89  func TestCallFun1(t *testing.T) {
    90  	tt := TestEntity{}
    91  
    92  	bean.AddBean("test", &tt)
    93  
    94  	parameterMap := map[string]any{}
    95  	parameterMap["p1"] = "name"
    96  
    97  	fv := bean.CallFun("test", "Fun1", parameterMap)
    98  	assert.Equal(t, isc.ToString(fv[0]), "name")
    99  
   100  	bean.Clean()
   101  }
   102  
   103  func TestCallFun2(t *testing.T) {
   104  	tt := TestEntity{}
   105  
   106  	bean.AddBean("test", &tt)
   107  
   108  	parameterMap := map[string]any{}
   109  	parameterMap["p1"] = 12
   110  
   111  	fv := bean.CallFun("test", "Fun2", parameterMap)
   112  	assert.Equal(t, isc.ToInt(fv[0]), 12)
   113  
   114  	bean.Clean()
   115  }
   116  
   117  func TestCallFun3(t *testing.T) {
   118  	tt := TestEntity{}
   119  
   120  	bean.AddBean("test", &tt)
   121  
   122  	parameterMap := map[string]any{}
   123  	parameterMap["p1"] = "name"
   124  	parameterMap["p2"] = 12
   125  
   126  	fv := bean.CallFun("test", "Fun3", parameterMap)
   127  	assert.Equal(t, isc.ToInt(fv[0]), 12)
   128  
   129  	bean.Clean()
   130  }
   131  
   132  //// error: 暂时参数中的json到结构体的转换
   133  //func TestCallFun4(t *testing.T) {
   134  //	tt := TestEntity{}
   135  //	inner := TestInnerEntity{Address: "杭州"}
   136  //
   137  //	bean.AddBean("test", &tt)
   138  //
   139  //	parameterMap := map[string]any{}
   140  //	parameterMap["p1"] = "name"
   141  //	parameterMap["p2"] = 12
   142  //	parameterMap["p3"] = inner
   143  //
   144  //	fv := bean.CallFun("test", "Fun4", parameterMap)
   145  //	assert.Equal(t, isc.ToString(fv[0]), "杭州")
   146  //
   147  //	bean.Clean()
   148  //}
   149  
   150  //// error: 暂时参数中的json到结构体的转换
   151  //func TestCallFun4_1(t *testing.T) {
   152  //	tt := TestEntity{}
   153  //
   154  //	bean.AddBean("test", &tt)
   155  //
   156  //	parameterMap := map[string]any{}
   157  //	parameterMap["p1"] = "name"
   158  //	parameterMap["p2"] = 12
   159  //	parameterMap["p3"] = "{\"Address\": \"hangzhou\"}"
   160  //
   161  //	fv := bean.CallFun("test", "Fun4", parameterMap)
   162  //	assert.Equal(t, isc.ToString(fv[0]), "杭州")
   163  //
   164  //	bean.Clean()
   165  //}
   166  //
   167  //func TestCallFun4Ptr(t *testing.T) {
   168  //	tt := TestEntity{}
   169  //	inner := TestInnerEntity{Address: "杭州"}
   170  //
   171  //	bean.AddBean("test", &tt)
   172  //
   173  //	parameterMap := map[string]any{}
   174  //	parameterMap["p1"] = "name"
   175  //	parameterMap["p2"] = 12
   176  //	parameterMap["p3"] = &inner
   177  //
   178  //	fv := bean.CallFun("test", "Fun4Ptr", parameterMap)
   179  //	assert.Equal(t, isc.ToString(fv[0]), "杭州")
   180  //
   181  //	bean.Clean()
   182  //}
   183  
   184  func TestCallFun5(t *testing.T) {
   185  	tt := TestEntity{Age: 12}
   186  
   187  	bean.AddBean("test", &tt)
   188  
   189  	fv := bean.CallFun("test", "Fun5", map[string]any{})
   190  	assert.Equal(t, isc.ToInt(fv[0]), 12)
   191  
   192  	bean.Clean()
   193  }
   194  
   195  func TestCallFun6(t *testing.T) {
   196  	tt := TestEntity{Age: 12}
   197  
   198  	bean.AddBean("test", tt)
   199  
   200  	fv := bean.CallFun("test", "Fun5", map[string]any{})
   201  	assert.Equal(t, isc.ToInt(fv[0]), 12)
   202  
   203  	bean.Clean()
   204  }
   205  
   206  func TestCallFun7(t *testing.T) {
   207  	tt := TestEntity{Age: 12}
   208  
   209  	bean.AddBean("test", tt)
   210  	tt.Age = 13
   211  
   212  	fv := bean.CallFun("test", "Fun5", map[string]any{})
   213  	assert.Equal(t, isc.ToInt(fv[0]), 12)
   214  
   215  	bean.Clean()
   216  }
   217  
   218  func TestCallPtrFun1(t *testing.T) {
   219  	tt := TestEntity{Age: 12}
   220  
   221  	bean.AddBean("test", &tt)
   222  
   223  	fv := bean.CallFun("test", "PtrFun", map[string]any{})
   224  	assert.Equal(t, isc.ToString(fv[0]), "ok")
   225  
   226  	bean.Clean()
   227  }
   228  
   229  func TestCallPtrFun2(t *testing.T) {
   230  	tt := TestEntity{}
   231  
   232  	bean.AddBean("test", &tt)
   233  
   234  	parameterMap := map[string]any{}
   235  	parameterMap["p1"] = "name"
   236  
   237  	fv := bean.CallFun("test", "PtrFun1", parameterMap)
   238  	assert.Equal(t, isc.ToString(fv[0]), "name")
   239  
   240  	bean.Clean()
   241  }
   242  
   243  func TestCallPtrFun3(t *testing.T) {
   244  	tt := TestEntity{}
   245  
   246  	bean.AddBean("test", &tt)
   247  
   248  	parameterMap := map[string]any{}
   249  	parameterMap["p1"] = "name"
   250  
   251  	fv := bean.CallFun("test", "Fun1", parameterMap)
   252  	assert.Equal(t, isc.ToString(fv[0]), "name")
   253  
   254  	bean.Clean()
   255  }
   256  
   257  
   258  func TestSetField(t *testing.T) {
   259  	tt := TestEntity{}
   260  
   261  	bean.AddBean("test", &tt)
   262  	bean.SetField("test", "Name", "hello")
   263  
   264  	assert.Equal(t, tt.Name, "hello")
   265  
   266  	bean.Clean()
   267  }
   268  
   269  func TestSetField1(t *testing.T) {
   270  	tt := TestEntity{Age: 12}
   271  
   272  	bean.AddBean("test", &tt)
   273  	parameterMap := map[string]any{}
   274  	parameterMap["p1"] = 32
   275  
   276  	assert.Equal(t, tt.Age, 12)
   277  	bean.CallFun("test", "ChangeField", parameterMap)
   278  	assert.Equal(t, tt.Age, 32)
   279  
   280  	bean.Clean()
   281  }
   282  
   283  func TestSetFieldPtr(t *testing.T) {
   284  	tt := TestEntity{}
   285  
   286  	bean.AddBean("test", &tt)
   287  	bean.SetField("test", "Name", "hello")
   288  
   289  	assert.Equal(t, tt.Name, "hello")
   290  
   291  	bean.Clean()
   292  }
   293  
   294  func TestGetBeans(t *testing.T) {
   295  	tt1 := TestEntity{}
   296  	tt2 := TestEntity{}
   297  	tt3 := TestEntity{}
   298  
   299  	bean.AddBean("t1-name", &tt1)
   300  	bean.AddBean("t2-name2", &tt2)
   301  	bean.AddBean("t3-change", &tt3)
   302  
   303  	datas := bean.GetBeanNames("name")
   304  	assert.Equal(t, len(datas), 2)
   305  
   306  	datas = bean.GetBeanNames("")
   307  	assert.Equal(t, len(datas), 3)
   308  
   309  	bean.Clean()
   310  }
   311  
   312  //func TestServerBean(t *testing.T) {
   313  //	tt1 := TestEntity{Name: "t1", Age: 12}
   314  //	tt2 := TestEntity{Name: "t2", Age: 13}
   315  //	tt3 := TestEntity{Name: "t2", Age: 13}
   316  //
   317  //	bean.AddBean("t1-name", &tt1)
   318  //	bean.AddBean("t2-name2", &tt2)
   319  //	bean.AddBean("t3-change", &tt3)
   320  //
   321  //	server.Run()
   322  //}
   323  
   324  type ValueInnerEntity struct {
   325  	Name string
   326  	Age  int
   327  }
   328  
   329  type TestEntity struct {
   330  	Name string
   331  	Age  int
   332  }
   333  
   334  type TestInnerEntity struct {
   335  	Name    string
   336  	Address string
   337  }
   338  
   339  func (tt TestEntity) Fun() string {
   340  	return "ok"
   341  }
   342  
   343  func (tt TestEntity) Fun1(name string) string {
   344  	return name
   345  }
   346  
   347  func (tt TestEntity) Fun2(age int) int {
   348  	return age
   349  }
   350  
   351  func (tt TestEntity) Fun3(name string, age int) int {
   352  	return age
   353  }
   354  
   355  func (tt TestEntity) Fun4(name string, age int, inner TestInnerEntity) string {
   356  	return inner.Address
   357  }
   358  
   359  func (tt TestEntity) Fun4Ptr(name string, age int, inner *TestInnerEntity) string {
   360  	return inner.Address
   361  }
   362  
   363  func (tt TestEntity) Fun5() int {
   364  	return tt.Age
   365  }
   366  
   367  func (tt TestEntity) Fun6(age int) {
   368  	tt.Age = age
   369  }
   370  
   371  func (tt *TestEntity) PtrFun() string {
   372  	return "ok"
   373  }
   374  
   375  func (tt *TestEntity) PtrFun1(name string) string {
   376  	return name
   377  }
   378  
   379  func (tt *TestEntity) PtrFun2(age int) int {
   380  	return age
   381  }
   382  
   383  func (tt *TestEntity) ChangeField(age int) {
   384  	tt.Age = age
   385  }