edgex_source_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // +build edgex
  2. package extensions
  3. import (
  4. "fmt"
  5. "github.com/edgexfoundry/go-mod-core-contracts/models"
  6. "github.com/emqx/kuiper/common"
  7. "testing"
  8. )
  9. var es = EdgexSource{valueDescs: map[string]string{
  10. "b1" : "bool",
  11. "i1" : "int8",
  12. "i2" : "INT16",
  13. "i3" : "INT32",
  14. "i4" : "INT64",
  15. "i5" : "UINT8",
  16. "i6" : "UINT16",
  17. "i7" : "UINT32",
  18. "s1" : "String",
  19. "f1" : "Float32", //FLOAT32 will be handled by special case
  20. "f2" : "Float64", //FLOAT64 will be handled by special case
  21. "i8" : "UINT64", //UINT64 will be handled by special case
  22. },
  23. }
  24. func TestGetValue_Int(t *testing.T) {
  25. var testEvent = models.Event{Device: "test"}
  26. for i := 1; i < 8; i++{
  27. r1 := models.Reading{Name: fmt.Sprintf("i%d", i), Value: "1"}
  28. testEvent.Readings = append(testEvent.Readings, r1)
  29. }
  30. for _, r := range testEvent.Readings {
  31. if v, e := es.getValue(r, common.Log); e != nil {
  32. t.Errorf("%s", e)
  33. } else {
  34. expectOne(t, v)
  35. }
  36. }
  37. rf_01 := models.Reading{Name:"f1", Value:"fwtOaw=="}
  38. if v, e := es.getValue(rf_01, common.Log); e != nil {
  39. t.Errorf("%s", e)
  40. } else {
  41. if v1, ok := v.(float32); ok {
  42. if v1 != 1.8516986e+38 {
  43. t.Errorf("expected 1.8516986e+38, but it's %f.", v1)
  44. }
  45. }
  46. }
  47. r1 := models.Reading{Name: "i8", Value: "10796529505058023104"}
  48. if v, e := es.getValue(r1, common.Log); e != nil {
  49. t.Errorf("%s", e)
  50. } else {
  51. if v1, ok := v.(uint64); ok {
  52. if v1 != 10796529505058023104 {
  53. t.Errorf("expected 10796529505058023104, but it's %d.", v1)
  54. }
  55. }
  56. }
  57. }
  58. func expectOne(t *testing.T, expected interface{}) {
  59. if v1, ok := expected.(int); ok {
  60. if v1 != 1 {
  61. t.Errorf("expected 1, but it's %d.", v1)
  62. }
  63. } else {
  64. t.Errorf("expected int type, but it's %T.", expected)
  65. }
  66. }
  67. func TestGetValue_Float(t *testing.T) {
  68. var testEvent = models.Event{Device: "test"}
  69. for i := 1; i < 3; i++{
  70. r1 := models.Reading{Name: fmt.Sprintf("f%d", i), Value: "3.14"}
  71. testEvent.Readings = append(testEvent.Readings, r1)
  72. }
  73. for _, r := range testEvent.Readings {
  74. if v, e := es.getValue(r, common.Log); e != nil {
  75. t.Errorf("%s", e)
  76. } else {
  77. expectPi(t, v)
  78. }
  79. }
  80. }
  81. func expectPi(t *testing.T, expected interface{}) {
  82. if v1, ok := expected.(float64); ok {
  83. if v1 != 3.14 {
  84. t.Errorf("expected 3.14, but it's %f.", v1)
  85. }
  86. } else {
  87. t.Errorf("expected float type, but it's %T.", expected)
  88. }
  89. }
  90. func TestGetValue_Bool(t *testing.T) {
  91. ///////////True
  92. trues := []string{"1", "t", "T", "true", "TRUE", "True"}
  93. for _, v := range trues {
  94. r1 := models.Reading{Name: "b1", Value: v}
  95. if v, e := es.getValue(r1, common.Log); e != nil {
  96. t.Errorf("%s", e)
  97. } else {
  98. expectTrue(t, v)
  99. }
  100. }
  101. r1 := models.Reading{Name: "b1", Value: "TRue"}
  102. if _, e := es.getValue(r1, common.Log); e == nil {
  103. t.Errorf("%s", e)
  104. }
  105. ///////////False
  106. falses := []string{"0", "f", "F", "false", "FALSE", "False"}
  107. for _, v := range falses {
  108. r1 := models.Reading{Name: "b1", Value: v}
  109. if v, e := es.getValue(r1, common.Log); e != nil {
  110. t.Errorf("%s", e)
  111. } else {
  112. expectFalse(t, v)
  113. }
  114. }
  115. r1 = models.Reading{Name: "b1", Value: "FAlse"}
  116. if _, e := es.getValue(r1, common.Log); e == nil {
  117. t.Errorf("%s", e)
  118. }
  119. }
  120. func expectTrue(t *testing.T, expected interface{}) {
  121. if v1, ok := expected.(bool); ok {
  122. if !v1 {
  123. t.Errorf("expected true, but it's false.")
  124. }
  125. } else {
  126. t.Errorf("expected boolean type, but it's %t.", expected)
  127. }
  128. }
  129. func expectFalse(t *testing.T, expected interface{}) {
  130. if v1, ok := expected.(bool); ok {
  131. if v1 {
  132. t.Errorf("expected false, but it's true.")
  133. }
  134. } else {
  135. t.Errorf("expected boolean type, but it's %t.", expected)
  136. }
  137. }
  138. func TestWrongType(t *testing.T) {
  139. es1 := EdgexSource{valueDescs: map[string]string{
  140. "f": "FLOAT", //A not exsited type
  141. },
  142. }
  143. r1 := models.Reading{Name: "f", Value: "100"}
  144. if v, _ := es1.getValue(r1, common.Log); v != "100" {
  145. t.Errorf("Expected 100, but it's %s!", v)
  146. }
  147. }
  148. func TestWrongValue(t *testing.T) {
  149. var testEvent = models.Event{Device: "test"}
  150. r1 := models.Reading{Name: "b1", Value: "100"} //100 cannot be converted to a boolean value
  151. r2 := models.Reading{Name: "i1", Value: "int"} //'int' string cannot be converted to int value
  152. r3 := models.Reading{Name: "f1", Value: "float"} //'float' string cannot be converted to int value
  153. testEvent.Readings = append(testEvent.Readings, r1, r2, r3)
  154. for _, v := range testEvent.Readings {
  155. if _, e := es.getValue(v, common.Log); e == nil {
  156. t.Errorf("Expected an error!")
  157. }
  158. }
  159. }
  160. func TestCastToString(t *testing.T) {
  161. if v, ok := CastToString(12); v != "12" || !ok {
  162. t.Errorf("Failed to cast int.")
  163. }
  164. if v, ok := CastToString(true); v != "true" || !ok {
  165. t.Errorf("Failed to cast bool.")
  166. }
  167. if v, ok := CastToString("hello"); v != "hello" || !ok {
  168. t.Errorf("Failed to cast string.")
  169. }
  170. if v, ok := CastToString(12.3); v != "12.30" || !ok {
  171. t.Errorf("Failed to cast float.")
  172. }
  173. }