util_test.go 1.8 KB

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