github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/config/test/config_test.go (about) 1 package test 2 3 import ( 4 "github.com/isyscore/isc-gobase/config" 5 "github.com/isyscore/isc-gobase/isc" 6 "github.com/isyscore/isc-gobase/logger" 7 "github.com/magiconair/properties/assert" 8 "os" 9 "testing" 10 ) 11 12 //key1: 13 // key2: 14 // intdata: 12 15 // strdata: "data" 16 // booldata: true 17 // int64data: 12 18 // floatdata: 12.3 19 // objdata: 20 // field1: 1 21 // field2: "value2" 22 // arraydata: 23 // - 1 24 // - 2 25 // - 3 26 // arrayobjdata: 27 // - field1: 1 28 // field2: "name1" 29 // - field1: 2 30 // field2: "name2" 31 func TestLoadConfig(t *testing.T) { 32 config.LoadConfig() 33 34 assert.Equal(t, config.GetValueInt("key1.key2.intdata"), 12) 35 assert.Equal(t, config.GetValueString("key1.key2.strdata"), "data") 36 assert.Equal(t, config.GetValueBool("key1.key2.booldata"), true) 37 assert.Equal(t, config.GetValueInt64("key1.key2.int64data"), isc.ToInt64(12)) 38 assert.Equal(t, config.GetValueFloat32("key1.key2.floatdata"), isc.ToFloat32(12.3)) 39 40 expectData := map[string]any{ 41 "field1": 1, 42 "field2": "value2", 43 } 44 actData := map[string]any{} 45 err := config.GetValueObject("key1.key2.objdata", &actData) 46 if err != nil { 47 return 48 } 49 assert.Equal(t, actData, expectData) 50 assert.Equal(t, config.GetValueArrayInt("key1.key2.arraydata"), []int{1, 2, 3}) 51 } 52 53 // 测试小驼峰 54 func TestSmall(t *testing.T) { 55 config.LoadConfig() 56 57 entity1 := SmallEntity{} 58 _ = config.GetValueObject("key1.ok1", &entity1) 59 assert.Equal(t, entity1.NameAge, 32) 60 assert.Equal(t, entity1.HaoDeOk, 12) 61 62 entity2 := SmallEntity{} 63 _ = config.GetValueObject("key1.ok2", &entity2) 64 assert.Equal(t, entity2.NameAge, 32) 65 assert.Equal(t, entity2.HaoDeOk, 12) 66 67 entity3 := SmallEntity{} 68 _ = config.GetValueObject("key1.ok3", &entity3) 69 assert.Equal(t, entity3.NameAge, 32) 70 assert.Equal(t, entity3.HaoDeOk, 12) 71 72 entity4 := SmallEntity{} 73 _ = config.GetValueObject("key1.ok4", &entity4) 74 assert.Equal(t, entity4.NameAge, 32) 75 assert.Equal(t, entity4.HaoDeOk, 12) 76 } 77 78 type SmallEntity struct { 79 HaoDeOk int 80 NameAge int 81 } 82 83 // 测试兼容标签:json、yaml 84 func TestJsonOrYamlTag(t *testing.T) { 85 config.LoadConfig() 86 87 entity1 := SmallEntityJsonTag{} 88 _ = config.GetValueObject("key1.json", &entity1) 89 assert.Equal(t, entity1.NameAge, 32) 90 assert.Equal(t, entity1.HaoDeOk, 12) 91 92 entity2 := SmallEntityYamlTag{} 93 _ = config.GetValueObject("key1.yaml", &entity2) 94 assert.Equal(t, entity2.NameAge, 32) 95 assert.Equal(t, entity2.HaoDeOk, 12) 96 } 97 98 type SmallEntityJsonTag struct { 99 HaoDeOk int `json:"test_haode"` 100 NameAge int `json:"test_namehaha"` 101 } 102 103 type SmallEntityYamlTag struct { 104 HaoDeOk int `yaml:"test_haode"` 105 NameAge int `yaml:"test_namehaha"` 106 } 107 108 // 测试读取某个文件 109 func TestRead(t *testing.T) { 110 config.LoadFile("./application-local.yaml") 111 112 en := EntityTest{} 113 err := config.GetValueObject("entity", &en) 114 if err != nil { 115 logger.Warn("转换告警") 116 return 117 } 118 119 assert.Equal(t, en.Name, "name-change") 120 } 121 122 type EntityTest struct { 123 Name string 124 } 125 126 // 测试append 127 func TestAppend(t *testing.T) { 128 config.LoadFile("./application-append-original.yaml") 129 config.AppendFile("./application-append.yaml") 130 131 assert.Equal(t, config.GetValueString("a.b.c"), "c-value-change") 132 assert.Equal(t, config.GetValueString("a.b.d"), "d-value") 133 assert.Equal(t, config.GetValueString("a.b.e.f"), "f-value") 134 } 135 136 // 测试cm文件位置定制化 137 func TestConfigInit(t *testing.T) { 138 _ = os.Setenv("base.config.additional-location", "./application-append.yaml") 139 config.LoadConfigFromRelativePath("./application-append-original.yaml") 140 141 assert.Equal(t, config.GetValueString("a.b.c"), "c-value-change") 142 assert.Equal(t, config.GetValueString("a.b.d"), "d-value") 143 assert.Equal(t, config.GetValueString("a.b.e.f"), "f-value") 144 } 145 146 // 测试:yaml占位符的功能 147 func TestPlaceHolder1(t *testing.T) { 148 config.LoadFile("./application-place1.yml") 149 150 assert.Equal(t, config.GetValueString("place.name"), "test") 151 assert.Equal(t, config.GetValueString("test.name"), "test") 152 assert.Equal(t, config.GetValueString("test.name2"), "test2") 153 } 154 155 // 测试:yaml占位符的功能 156 func TestPlaceHolder2(t *testing.T) { 157 config.LoadFile("./application-place1.yaml") 158 159 assert.Equal(t, config.GetValueString("place.name"), "test") 160 assert.Equal(t, config.GetValueString("test.name"), "test") 161 assert.Equal(t, config.GetValueString("test.name2"), "test2") 162 } 163 164 // 测试:yaml占位符的功能 165 func TestPlaceHolder3(t *testing.T) { 166 config.LoadFile("./application-place1.json") 167 168 assert.Equal(t, config.GetValueString("place.name"), "test") 169 assert.Equal(t, config.GetValueString("test.name"), "test") 170 assert.Equal(t, config.GetValueString("test.name2"), "test2") 171 } 172 173 // 测试:yaml占位符的功能 174 func TestPlaceHolder4(t *testing.T) { 175 config.LoadFile("./application-place1.properties") 176 177 assert.Equal(t, config.GetValueString("place.name"), "test") 178 assert.Equal(t, config.GetValueString("test.name"), "test") 179 assert.Equal(t, config.GetValueString("test.name2"), "test2") 180 }