util_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package common
  2. import (
  3. "os"
  4. "path/filepath"
  5. "reflect"
  6. "strings"
  7. "testing"
  8. )
  9. func TestSimpleKVStore_Funcs(t *testing.T) {
  10. abs, _ := filepath.Abs("test.data")
  11. if f, _ := os.Stat(abs); f != nil {
  12. _ = os.Remove(abs)
  13. }
  14. ks := GetSimpleKVStore(abs)
  15. if e := ks.Open(); e != nil {
  16. t.Errorf("Failed to open data %s.", e)
  17. }
  18. _ = ks.Set("foo", "bar")
  19. v, _ := ks.Get("foo")
  20. reflect.DeepEqual("bar", v)
  21. _ = ks.Set("foo1", "bar1")
  22. v1, _ := ks.Get("foo1")
  23. reflect.DeepEqual("bar1", v1)
  24. if keys, e1 := ks.Keys(); e1 != nil {
  25. t.Errorf("Failed to get value: %s.", e1)
  26. } else {
  27. reflect.DeepEqual(2, len(keys))
  28. }
  29. if e2 := ks.Close(); e2 != nil {
  30. t.Errorf("Failed to close data: %s.", e2)
  31. }
  32. //if _, f := ks.Get("foo"); f {
  33. // t.Errorf("Should not find the foo key.")
  34. //}
  35. _ = ks.Open()
  36. if v, ok := ks.Get("foo"); ok {
  37. reflect.DeepEqual("bar", v)
  38. } else {
  39. t.Errorf("Should not find the foo key.")
  40. }
  41. ks.Delete("foo1")
  42. if keys, e1 := ks.Keys(); e1 != nil {
  43. t.Errorf("Failed to get value: %s.", e1)
  44. } else {
  45. reflect.DeepEqual(1, len(keys))
  46. }
  47. _ = os.Remove(abs)
  48. }
  49. func TestMapConvert_Funcs(t *testing.T) {
  50. source := map[interface{}]interface{}{
  51. "QUERY_TABLE": "VBAP",
  52. "ROWCOUNT": 10,
  53. "FIELDS": []interface{}{
  54. map[interface{}]interface{}{"FIELDNAME": "MANDT"},
  55. map[interface{}]interface{}{"FIELDNAME": "VBELN"},
  56. map[interface{}]interface{}{"FIELDNAME": "POSNR"},
  57. },
  58. }
  59. exp := map[string]interface{}{
  60. "QUERY_TABLE": "VBAP",
  61. "ROWCOUNT": 10,
  62. "FIELDS": []interface{}{
  63. map[string]interface{}{"FIELDNAME": "MANDT"},
  64. map[string]interface{}{"FIELDNAME": "VBELN"},
  65. map[string]interface{}{"FIELDNAME": "POSNR"},
  66. },
  67. }
  68. got := ConvertMap(source)
  69. if !reflect.DeepEqual(exp, got) {
  70. t.Errorf("result mismatch:\n\nexp=%s\n\ngot=%s\n\n", exp, got)
  71. }
  72. }
  73. func TestGetDataLoc_Funcs(t *testing.T) {
  74. d, err := GetDataLoc()
  75. if err != nil {
  76. t.Errorf("Errors when getting data loc: %s.", err)
  77. } else if !strings.HasSuffix(d, "kuiper/data/test") {
  78. t.Errorf("Unexpected data location %s", d)
  79. }
  80. }