config_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package config
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/yuguorong/go/log"
  6. )
  7. func TestConfigSet(T *testing.T) {
  8. cfg := GetSysConfig()
  9. cfg.LoadConfig()
  10. vi := cfg.GetValue("test", 1)
  11. cfg.SetValue("test", 2)
  12. cfg.SetValue("test", 2)
  13. cfg.SetValue("test", "2")
  14. log.Info(vi)
  15. cfg.Save()
  16. }
  17. type CloudServerConf struct {
  18. ServerUrl string
  19. AppId string
  20. AppSecret string
  21. }
  22. func TestLoadConf(T *testing.T) {
  23. sys := GetSysConfig()
  24. sys.LoadConfig()
  25. conf := sys.GetValue("cloudserver/config", &CloudServerConf{})
  26. log.Info(conf)
  27. res := &CloudServerConf{
  28. AppId: "fvWmjGCU",
  29. AppSecret: "054e7df0881eff8328858092f9e8ac0b0f356676",
  30. ServerUrl: "https://test-admin.pacom.cn",
  31. }
  32. sys.SetValue("cloudserver/config", res)
  33. conf = sys.GetValue("cloudserver/config", &CloudServerConf{})
  34. log.Info(conf)
  35. sys.Save()
  36. }
  37. type AmmeterModel struct {
  38. Id string `json:"id" gorm:"-"`
  39. DevName string `json:"devName" gorm:"primarykey"`
  40. Code string `json:"code" `
  41. DutSn string `json:"dutSn" gorm:"-"`
  42. Address string `json:"address"`
  43. Protocol string `json:"protocol" gorm:"-"`
  44. GwDevId string `json:"gwDevId" gorm:"-"`
  45. TransRadio string `json:"transformerRatio" gorm:"-"`
  46. }
  47. type Ammeter struct {
  48. AmmeterModel
  49. timestamp int32
  50. TotalEnergy float64
  51. TransDeno float64 ` gorm:"-"`
  52. TransDiv float64 ` gorm:"-"`
  53. }
  54. func TestDBConf(T *testing.T) {
  55. GetDB().CreateTbl(&Ammeter{})
  56. v0 := &Ammeter{
  57. TransDeno: 3,
  58. TransDiv: 2,
  59. }
  60. GetDB().Find(v0)
  61. log.Info(v0)
  62. v0.DevName = ""
  63. GetDB().Find(v0, "dev_name='test2'")
  64. log.Info(v0)
  65. if v0.DevName != "" {
  66. v1 := &Ammeter{
  67. TotalEnergy: 1373.78,
  68. timestamp: int32(time.Now().Unix()),
  69. AmmeterModel: AmmeterModel{
  70. DevName: "test3",
  71. Code: "2108",
  72. Address: "123456",
  73. },
  74. }
  75. GetDB().Save(v1)
  76. } else {
  77. v0.TotalEnergy += 100
  78. GetDB().Save(v0)
  79. }
  80. }