GoAdminGroup / go-admin

A golang framework helps gopher to build a data visualization and admin panel in ten minutes
https://www.go-admin.com
Apache License 2.0
8.21k stars 1.34k forks source link

[BUG] Inability to test reliably... #610

Open sean- opened 7 months ago

sean- commented 7 months ago

Bug Description

There are a number of global variables used throughout the code base. When trying to use normal gin testing practices where a routing engine is embedded in a Server struct, it is impossible to reconfigure the database or services without encountering panic()'s. For instance:

How to reproduce [describe the steps how to reproduce the bug]

Call engine.AddConfig() twice in the same running process:

https://github.com/GoAdminGroup/example/blob/ffac70b8ea3a48080022f0057a128d62a733ebaf/main.go#L58-L63

Run the above, or whatever, in the equivalent of:

import (
  gacfg "github.com/GoAdminGroup/go-admin/modules/config"
  "github.com/GoAdminGroup/go-admin/engine"
)

func Test_MyTest(t *testing.T) {
  cfg1 := &gacfg.Config{}
  cfg2 := &gacfg.Config{}
  eng1 := engine.Default()
  eng2 := engine.Default()
  eng1.AddConfig(cfg1)
  eng2.AddConfig(cfg2)
}

Expect [describe your expect result]

I expect this to work w/o any panics. I'd like to figure out how to use go-admin without sacrificing standard testing practices.

Versions