config_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package database
  2. import (
  3. "gorm.io/driver/mysql"
  4. "gorm.io/gorm"
  5. "testing"
  6. )
  7. var dsn0 = "dsn0"
  8. var dsn1 = "dsn1"
  9. var tables = []interface{}{"sys_user", "sys_role"}
  10. func TestDBConfig_Init(t *testing.T) {
  11. type fields struct {
  12. dsn string
  13. connMaxIdleTime int
  14. connMaxLifetime int
  15. maxIdleConns int
  16. maxOpenConns int
  17. registers []ResolverConfigure
  18. }
  19. type args struct {
  20. config *gorm.Config
  21. open func(string) gorm.Dialector
  22. }
  23. registers := make([]ResolverConfigure, 0)
  24. registers = append(registers, &DBResolverConfig{
  25. sources: []string{dsn0},
  26. replicas: []string{dsn1},
  27. policy: "random",
  28. tables: tables,
  29. })
  30. registers = append(registers, &DBResolverConfig{
  31. sources: []string{dsn0},
  32. replicas: []string{dsn1},
  33. policy: "random",
  34. tables: tables,
  35. })
  36. registers = append(registers, &DBResolverConfig{
  37. sources: []string{dsn0},
  38. replicas: []string{dsn1},
  39. policy: "random",
  40. //tables: tables,
  41. })
  42. tests := []struct {
  43. name string
  44. fields fields
  45. args args
  46. wantErr bool
  47. }{
  48. {
  49. "test0",
  50. fields{
  51. dsn: dsn0,
  52. },
  53. args{
  54. config: &gorm.Config{},
  55. open: mysql.Open,
  56. },
  57. false,
  58. },
  59. {
  60. "test1",
  61. fields{
  62. dsn: dsn0,
  63. connMaxIdleTime: 600,
  64. connMaxLifetime: 60,
  65. maxIdleConns: 200,
  66. maxOpenConns: 100,
  67. registers: registers,
  68. },
  69. args{
  70. config: &gorm.Config{},
  71. open: mysql.Open,
  72. },
  73. false,
  74. },
  75. }
  76. for _, tt := range tests {
  77. t.Run(tt.name, func(t *testing.T) {
  78. e := &DBConfig{
  79. dsn: tt.fields.dsn,
  80. connMaxIdleTime: tt.fields.connMaxIdleTime,
  81. connMaxLifetime: tt.fields.connMaxLifetime,
  82. maxIdleConns: tt.fields.maxIdleConns,
  83. maxOpenConns: tt.fields.maxOpenConns,
  84. registers: tt.fields.registers,
  85. }
  86. _, err := e.Init(tt.args.config, tt.args.open)
  87. if (err != nil) != tt.wantErr {
  88. t.Errorf("Init() error = %v, wantErr %v", err, tt.wantErr)
  89. return
  90. }
  91. })
  92. }
  93. }