github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/config/zz_config_test.go (about) 1 package config_test 2 3 import ( 4 "context" 5 "runtime" 6 "testing" 7 8 "github.com/agiledragon/gomonkey/v2" 9 "github.com/golang/mock/gomock" 10 . "github.com/onsi/gomega" 11 "github.com/pkg/errors" 12 13 confid "github.com/machinefi/w3bstream/pkg/depends/conf/id" 14 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 15 "github.com/machinefi/w3bstream/pkg/depends/x/contextx" 16 "github.com/machinefi/w3bstream/pkg/enums" 17 "github.com/machinefi/w3bstream/pkg/errors/status" 18 "github.com/machinefi/w3bstream/pkg/models" 19 "github.com/machinefi/w3bstream/pkg/modules/config" 20 mock_sqlx "github.com/machinefi/w3bstream/pkg/test/mock_depends_kit_sqlx" 21 "github.com/machinefi/w3bstream/pkg/test/patch_models" 22 "github.com/machinefi/w3bstream/pkg/test/patch_modules" 23 "github.com/machinefi/w3bstream/pkg/test/patch_std" 24 "github.com/machinefi/w3bstream/pkg/types" 25 "github.com/machinefi/w3bstream/pkg/types/wasm" 26 ) 27 28 func TestMarshal(t *testing.T) { 29 if runtime.GOOS == `darwin` { 30 return 31 } 32 33 patch := gomonkey.NewPatches() 34 defer patch.Reset() 35 36 patch = patch_std.JsonMarshal(patch, nil, errors.New("any")) 37 _, err := config.Marshal(&wasm.Cache{}) 38 mock_sqlx.ExpectError(t, err, status.ConfigParseFailed, "any") 39 40 patch = patch_std.JsonMarshal(patch, []byte("any"), nil) 41 _, err = config.Marshal(&wasm.Cache{}) 42 NewWithT(t).Expect(err).To(BeNil()) 43 } 44 45 func TestUnmarshal(t *testing.T) { 46 patch := gomonkey.NewPatches() 47 defer patch.Reset() 48 49 t.Run("#Failed", func(t *testing.T) { 50 t.Run("#InvalidConfigType", func(t *testing.T) { 51 _, err := config.Unmarshal([]byte("any"), enums.ConfigType(100)) 52 mock_sqlx.ExpectError(t, err, status.InvalidConfigType) 53 }) 54 t.Run("#ConfigParseFailed", func(t *testing.T) { 55 if runtime.GOOS == `darwin` { 56 return 57 } 58 59 patch = patch_std.JsonUnmarshal(patch, errors.New("any")) 60 _, err := config.Unmarshal([]byte("any"), enums.CONFIG_TYPE__INSTANCE_CACHE) 61 mock_sqlx.ExpectError(t, err, status.ConfigParseFailed, "any") 62 }) 63 }) 64 } 65 66 func TestConfig(t *testing.T) { 67 ctrl := gomock.NewController(t) 68 defer ctrl.Finish() 69 70 d := mock_sqlx.NewTx(ctrl) 71 ctx := contextx.WithContextCompose( 72 types.WithMgrDBExecutorContext(d), 73 confid.WithSFIDGeneratorContext(confid.MustNewSFIDGenerator()), 74 )(context.Background()) 75 76 d.MockDBExecutor.EXPECT().T(gomock.Any()).Return(&builder.Table{}).AnyTimes() 77 d.MockTxExecutor.EXPECT().IsTx().Return(true).AnyTimes() 78 d.MockDBExecutor.EXPECT().Context().Return(ctx).AnyTimes() 79 80 cause := func(msg string) error { return errors.New(msg) } 81 82 t.Run("#GetValueBySFID", func(t *testing.T) { 83 patch := gomonkey.NewPatches() 84 defer patch.Reset() 85 86 t.Run("#Failed", func(t *testing.T) { 87 t.Run("#NotFoundError", func(t *testing.T) { 88 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound).Times(1) 89 _, err := config.GetValueBySFID(ctx, 100) 90 mock_sqlx.ExpectError(t, err, status.ConfigNotFound) 91 }) 92 t.Run("#DatabaseError", func(t *testing.T) { 93 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrDatabase).Times(1) 94 _, err := config.GetValueBySFID(ctx, 100) 95 mock_sqlx.ExpectError(t, err, status.DatabaseError) 96 }) 97 }) 98 t.Run("#Success", func(t *testing.T) { 99 if runtime.GOOS == `darwin` { 100 return 101 } 102 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1) 103 104 patch = patch_std.JsonUnmarshal(patch, nil) 105 patch = patch_modules.TypesWasmNewConfigurationByType(patch, &wasm.Cache{}, nil) 106 defer patch.Reset() 107 108 _, err := config.GetValueBySFID(ctx, 100) 109 NewWithT(t).Expect(err).To(BeNil()) 110 }) 111 }) 112 113 t.Run("#GetValueByRelAndType", func(t *testing.T) { 114 patch := gomonkey.NewPatches() 115 defer patch.Reset() 116 117 t.Run("#Failed", func(t *testing.T) { 118 t.Run("#NotFoundError", func(t *testing.T) { 119 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound).Times(1) 120 _, err := config.GetValueByRelAndType(ctx, 100, enums.CONFIG_TYPE__INSTANCE_CACHE) 121 mock_sqlx.ExpectError(t, err, status.ConfigNotFound) 122 }) 123 t.Run("#DatabaseError", func(t *testing.T) { 124 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrDatabase).Times(1) 125 _, err := config.GetValueByRelAndType(ctx, 100, enums.CONFIG_TYPE__INSTANCE_CACHE) 126 mock_sqlx.ExpectError(t, err, status.DatabaseError) 127 }) 128 }) 129 t.Run("#Success", func(t *testing.T) { 130 if runtime.GOOS == `darwin` { 131 return 132 } 133 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1) 134 135 patch = patch_std.JsonUnmarshal(patch, nil) 136 patch = patch_modules.TypesWasmNewConfigurationByType(patch, &wasm.Cache{}, nil) 137 defer patch.Reset() 138 139 _, err := config.GetValueByRelAndType(ctx, 100, enums.CONFIG_TYPE__INSTANCE_CACHE) 140 NewWithT(t).Expect(err).To(BeNil()) 141 }) 142 }) 143 144 t.Run("#Upsert", func(t *testing.T) { 145 if runtime.GOOS == `darwin` { 146 return 147 } 148 patch := gomonkey.NewPatches() 149 defer patch.Reset() 150 151 t.Run("#Failed", func(t *testing.T) { 152 t.Run("#TxTryFetchAndUninitOldConfig", func(t *testing.T) { 153 t.Run("#FetchByRelIDAndTypeFailed", func(t *testing.T) { 154 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(cause(t.Name())).Times(1) 155 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 156 t.Log(err) 157 mock_sqlx.ExpectError(t, err, status.DatabaseError) 158 }) 159 t.Run("#UnmarshalConfigFailed", func(t *testing.T) { 160 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1) 161 patch = patch_modules.ConfigUnmarshal(patch, nil, cause(t.Name())) 162 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 163 NewWithT(t).Expect(err.Error()).To(Equal(t.Name())) 164 }) 165 166 patch = patch_modules.ConfigUnmarshal(patch, &wasm.Cache{}, nil) 167 168 t.Run("#UninitConfigFailed", func(t *testing.T) { 169 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1) 170 patch = patch_modules.TypesWasmUninitConfiguration(patch, cause(t.Name())) 171 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 172 t.Log(err) 173 mock_sqlx.ExpectError(t, err, status.ConfigUninitFailed) 174 }) 175 }) 176 177 patch = patch_modules.TypesWasmUninitConfiguration(patch, nil) 178 179 t.Run("#TxCreateOrUpdateConfig", func(t *testing.T) { 180 t.Run("#ConfigMarshalFailed", func(t *testing.T) { 181 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound).Times(1) 182 patch = patch_modules.ConfigMarshal(patch, nil, cause(t.Name())) 183 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 184 NewWithT(t).Expect(err.Error()).To(Equal(t.Name())) 185 }) 186 187 patch = patch_modules.ConfigMarshal(patch, []byte("any"), nil) 188 189 t.Run("#CreateConfigFailed", func(t *testing.T) { 190 t.Run("#ConflictError", func(t *testing.T) { 191 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound).Times(1) 192 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, mock_sqlx.ErrConflict).Times(1) 193 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 194 mock_sqlx.ExpectError(t, err, status.ConfigConflict) 195 }) 196 t.Run("#DatabaseError", func(t *testing.T) { 197 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound).Times(1) 198 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, mock_sqlx.ErrDatabase).Times(1) 199 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 200 mock_sqlx.ExpectError(t, err, status.DatabaseError) 201 }) 202 }) 203 204 t.Run("#UpdateConfigFailed", func(t *testing.T) { 205 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(nil).Times(1) 206 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, cause(t.Name())).Times(1) 207 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 208 mock_sqlx.ExpectError(t, err, status.DatabaseError, t.Name()) 209 }) 210 }) 211 212 t.Run("#TxInitConfigFailed", func(t *testing.T) { 213 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound).Times(1) 214 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(1) 215 216 patch = patch_modules.TypesWasmInitConfiguration(patch, cause(t.Name())) 217 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 218 t.Log(err) 219 mock_sqlx.ExpectError(t, err, status.ConfigInitFailed) 220 }) 221 }) 222 223 t.Run("#Success", func(t *testing.T) { 224 patch = patch_modules.TypesWasmInitConfiguration(patch, nil) 225 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(mock_sqlx.ErrNotFound).Times(1) 226 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(1) 227 228 _, err := config.Upsert(ctx, 100, &wasm.Cache{}) 229 NewWithT(t).Expect(err).To(BeNil()) 230 }) 231 }) 232 233 t.Run("#List", func(t *testing.T) { 234 patch := gomonkey.NewPatches() 235 defer patch.Reset() 236 237 t.Run("#Failed", func(t *testing.T) { 238 t.Run("#DatabaseListFailed", func(t *testing.T) { 239 d.MockDBExecutor.EXPECT().QueryAndScan(gomock.Any(), gomock.Any()).Return(cause(t.Name())).Times(1) 240 _, err := config.List(ctx, &config.CondArgs{}) 241 mock_sqlx.ExpectError(t, err, status.DatabaseError, t.Name()) 242 }) 243 244 t.Run("#ConfigUnmarshalFailed", func(t *testing.T) { 245 if runtime.GOOS == `darwin` { 246 return 247 } 248 249 patch = patch_models.ConfigList(patch, []models.Config{{}}, nil) 250 patch = patch_modules.ConfigUnmarshal(patch, nil, cause(t.Name())) 251 _, err := config.List(ctx, &config.CondArgs{}) 252 NewWithT(t).Expect(err.Error()).To(Equal(t.Name())) 253 }) 254 }) 255 256 t.Run("#Success", func(t *testing.T) { 257 if runtime.GOOS == `darwin` { 258 return 259 } 260 261 patch = patch_modules.ConfigUnmarshal(patch, &wasm.Cache{}, nil) 262 _, err := config.List(ctx, &config.CondArgs{}) 263 NewWithT(t).Expect(err).To(BeNil()) 264 }) 265 }) 266 267 t.Run("#Remove", func(t *testing.T) { 268 if runtime.GOOS == `darwin` { 269 return 270 } 271 272 patch := gomonkey.NewPatches() 273 defer patch.Reset() 274 275 t.Run("#Failed", func(t *testing.T) { 276 t.Run("#ConfigListFailed", func(t *testing.T) { 277 patch = patch_modules.ConfigList(patch, nil, cause(t.Name())) 278 279 err := config.Remove(ctx, &config.CondArgs{}) 280 NewWithT(t).Expect(err.Error()).To(Equal(t.Name())) 281 }) 282 t.Run("#RemoveConfigModelFailed", func(t *testing.T) { 283 patch = patch_modules.ConfigList(patch, []*config.Detail{{ 284 Configuration: &wasm.Cache{}, 285 }}, nil) 286 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, cause(t.Name())).Times(1) 287 288 err := config.Remove(ctx, &config.CondArgs{}) 289 mock_sqlx.ExpectError(t, err, status.DatabaseError, t.Name()) 290 }) 291 t.Run("#WasmUninitConfigurationFailed", func(t *testing.T) { 292 patch = patch_modules.TypesWasmUninitConfiguration(patch, cause(t.Name())) 293 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(1) 294 295 err := config.Remove(ctx, &config.CondArgs{}) 296 t.Log(err) 297 mock_sqlx.ExpectError(t, err, status.ConfigUninitFailed) 298 }) 299 }) 300 t.Run("#Success", func(t *testing.T) { 301 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(1) 302 patch = patch_modules.TypesWasmUninitConfiguration(patch, nil) 303 304 err := config.Remove(ctx, &config.CondArgs{}) 305 NewWithT(t).Expect(err).To(BeNil()) 306 }) 307 }) 308 309 t.Run("#Create", func(t *testing.T) { 310 if runtime.GOOS == `darwin` { 311 return 312 } 313 patch := gomonkey.NewPatches() 314 defer patch.Reset() 315 316 t.Run("#Failed", func(t *testing.T) { 317 t.Run("#ConfigMarshalFailed", func(t *testing.T) { 318 patch = patch_std.JsonMarshal(patch, nil, cause(t.Name())) 319 _, err := config.Create(ctx, 100, &wasm.Cache{}) 320 mock_sqlx.ExpectError(t, err, status.ConfigParseFailed, t.Name()) 321 }) 322 323 patch = patch_modules.ConfigMarshal(patch, []byte("any"), nil) 324 325 t.Run("#CreateConfigDatabaseFailed", func(t *testing.T) { 326 t.Run("#ConflictError", func(t *testing.T) { 327 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, mock_sqlx.ErrConflict).Times(1) 328 _, err := config.Create(ctx, 100, &wasm.Cache{}) 329 mock_sqlx.ExpectError(t, err, status.ConfigConflict) 330 }) 331 t.Run("#DatabaseError", func(t *testing.T) { 332 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, mock_sqlx.ErrDatabase).Times(1) 333 _, err := config.Create(ctx, 100, &wasm.Cache{}) 334 mock_sqlx.ExpectError(t, err, status.DatabaseError) 335 }) 336 }) 337 338 t.Run("#WasmInitConfigurationFailed", func(t *testing.T) { 339 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(1) 340 patch = patch_modules.TypesWasmInitConfiguration(patch, cause(t.Name())) 341 _, err := config.Create(ctx, 100, &wasm.Cache{}) 342 t.Log(err) 343 mock_sqlx.ExpectError(t, err, status.ConfigInitFailed) 344 }) 345 }) 346 t.Run("#Success", func(t *testing.T) { 347 d.MockDBExecutor.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(1) 348 patch = patch_modules.TypesWasmInitConfiguration(patch, nil) 349 _, err := config.Create(ctx, 100, &wasm.Cache{}) 350 NewWithT(t).Expect(err).To(BeNil()) 351 }) 352 }) 353 } 354 355 func TestCondArgs(t *testing.T) { 356 for _, c := range []*config.CondArgs{ 357 {}, 358 { 359 ConfigIDs: types.SFIDs{100, 101}, 360 RelIDs: types.SFIDs{100, 101}, 361 Types: []enums.ConfigType{ 362 enums.CONFIG_TYPE__PROJECT_DATABASE, 363 enums.CONFIG_TYPE__INSTANCE_CACHE, 364 }}, 365 } { 366 t.Log(builder.ResolveExpr(c.Condition()).Query()) 367 } 368 } 369 370 func TestDetail(t *testing.T) { 371 d := &config.Detail{RelID: 100, Configuration: &wasm.Cache{}} 372 t.Run("#String", func(t *testing.T) { 373 t.Log(d.String()) 374 }) 375 t.Run("#Log", func(t *testing.T) { 376 NewWithT(t).Expect(d.Log(nil)).To(Equal(d.String())) 377 t.Log(d.Log(errors.New("any"))) 378 }) 379 }