file_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package paladin_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "go-common/library/conf/paladin"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestNewFile(t *testing.T) {
  10. // test data
  11. path := "/tmp/test_conf/"
  12. assert.Nil(t, os.MkdirAll(path, 0700))
  13. assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`
  14. text = "hello"
  15. number = 100
  16. slice = [1, 2, 3]
  17. sliceStr = ["1", "2", "3"]
  18. `), 0644))
  19. // test client
  20. cli, err := paladin.NewFile(path + "test.toml")
  21. assert.Nil(t, err)
  22. assert.NotNil(t, cli)
  23. // test map
  24. m := paladin.Map{}
  25. text, err := cli.Get("test.toml").String()
  26. assert.Nil(t, err)
  27. assert.Nil(t, m.Set(text), "text")
  28. s, err := m.Get("text").String()
  29. assert.Nil(t, err)
  30. assert.Equal(t, s, "hello", "text")
  31. n, err := m.Get("number").Int64()
  32. assert.Nil(t, err)
  33. assert.Equal(t, n, int64(100), "number")
  34. }
  35. func TestNewFilePath(t *testing.T) {
  36. // test data
  37. path := "/tmp/test_conf/"
  38. assert.Nil(t, os.MkdirAll(path, 0700))
  39. assert.Nil(t, ioutil.WriteFile(path+"test.toml", []byte(`
  40. text = "hello"
  41. number = 100
  42. `), 0644))
  43. assert.Nil(t, ioutil.WriteFile(path+"abc.toml", []byte(`
  44. text = "hello"
  45. number = 100
  46. `), 0644))
  47. // test client
  48. cli, err := paladin.NewFile(path)
  49. assert.Nil(t, err)
  50. assert.NotNil(t, cli)
  51. // test map
  52. m := paladin.Map{}
  53. text, err := cli.Get("test.toml").String()
  54. assert.Nil(t, err)
  55. assert.Nil(t, m.Set(text), "text")
  56. s, err := m.Get("text").String()
  57. assert.Nil(t, err, s)
  58. assert.Equal(t, s, "hello", "text")
  59. n, err := m.Get("number").Int64()
  60. assert.Nil(t, err, s)
  61. assert.Equal(t, n, int64(100), "number")
  62. }