gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/modifyx2/modify_test.go (about) 1 package modifyx 2 3 import ( 4 "gitee.com/zhongguo168a/gocodes/datax" 5 "github.com/stretchr/testify/assert" 6 "testing" 7 "time" 8 ) 9 10 var ( 11 root *Root 12 testPool = NewPool() 13 ) 14 15 func initSample() { 16 root = NewRoot(NewSourceWith(map[string]interface{}{ 17 "Int": 1, 18 "Float": 1.0, 19 "Map": map[string]interface{}{ 20 "Int": 1, 21 "Float": 1.0, 22 }, 23 "MapStruct": map[string]interface{}{ 24 "0": map[string]interface{}{ 25 "Int": 1, 26 "Float": 1.0, 27 }, 28 }, 29 "ArrayInt": datax.A{1, 2, 3}, 30 })) 31 root.GetPools().SetHost(testPool) 32 } 33 34 func TestNewSourceVersion(t *testing.T) { 35 initSample() 36 37 assert.Equal(t, 1, root.RefInt("Int")) 38 39 testPool.AddBonus(NewBonusAll("/Int", 100, 0, 1.0), int(time.Now().Unix())) 40 assert.Equal(t, 101, root.RefInt("Int")) 41 42 testPool.AddBonus(NewBonusAll("/Int", 0, 1.0, 1.0), int(time.Now().Unix())) 43 assert.Equal(t, 102, root.RefInt("Int")) 44 45 testPool.AddBonus(NewBonusAll("/Int", 0, 0.0, 2.0), int(time.Now().Unix())) 46 assert.Equal(t, 104, root.RefInt("Int")) 47 48 testPool.AddBonus(NewBonusAll("/Int", -4, 0.0, 1.0), int(time.Now().Unix())) 49 assert.Equal(t, 100, root.RefInt("Int")) 50 51 } 52 func TestGetMap(t *testing.T) { 53 initSample() 54 55 getMap := root.RefMap("Map") 56 57 assert.Equal(t, false, getMap.RefIsNil()) 58 assert.Equal(t, 2, getMap.RefLength()) 59 assert.Equal(t, 1, getMap.RefInt("Int")) 60 assert.Equal(t, 1.0, getMap.RefFloat64("Float")) 61 62 testPool.AddBonus(NewBonusAll("/Map/Int", 100, 0, 1.0), int(time.Now().Unix())) 63 assert.Equal(t, 101, getMap.RefInt("Int")) 64 } 65 66 func TestGetMapStruct(t *testing.T) { 67 initSample() 68 69 getObj := root.RefMap("MapStruct") 70 assert.Equal(t, false, getObj.RefIsNil()) 71 72 getObj0 := getObj.RefMap("0") 73 getObj0.SetRuntimeInt("Int", 1) 74 getObj0.SetRuntimeFloat("Float", 1.0) 75 assert.Equal(t, 1, getObj0.RefInt("Int")) 76 assert.Equal(t, 1.0, getObj0.RefFloat64("Float")) 77 78 testPool.AddBonus(NewBonusAll("/MapStruct/0/Int", 100, 0, 1.0), int(time.Now().Unix())) 79 assert.Equal(t, 101, getObj0.RefInt("Int")) 80 } 81 82 func TestGetArrayInt(t *testing.T) { 83 initSample() 84 85 obj := root.RefArray("ArrayInt") 86 87 assert.Equal(t, 1, obj.RefInt(0)) 88 assert.Equal(t, 2, obj.RefInt(1)) 89 assert.Equal(t, 3, obj.RefInt(2)) 90 91 testPool.AddBonus(NewBonusAll("/ArrayInt/0", 100, 0, 1.0), int(time.Now().Unix())) 92 assert.Equal(t, 101, obj.RefInt(0)) 93 94 testPool.AddBonus(NewBonusAll("/ArrayInt/2", 200, 0, 1.0), int(time.Now().Unix())) 95 assert.Equal(t, 203, obj.RefInt(2)) 96 }