registry_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package workflow
  2. import (
  3. "testing"
  4. )
  5. func TestParseParamDeclaration(t *testing.T) {
  6. tests := []struct {
  7. name string
  8. declaration string
  9. expected *ParamDeclaration
  10. shouldError bool
  11. }{
  12. {
  13. name: "simple string param",
  14. declaration: "userId(STRING)",
  15. expected: &ParamDeclaration{
  16. Name: "userId",
  17. Type: "STRING",
  18. },
  19. shouldError: false,
  20. },
  21. {
  22. name: "int param",
  23. declaration: "maxLimit(INT)",
  24. expected: &ParamDeclaration{
  25. Name: "maxLimit",
  26. Type: "INT",
  27. },
  28. shouldError: false,
  29. },
  30. {
  31. name: "object param",
  32. declaration: "config(OBJECT)",
  33. expected: &ParamDeclaration{
  34. Name: "config",
  35. Type: "OBJECT",
  36. },
  37. shouldError: false,
  38. },
  39. {
  40. name: "array param",
  41. declaration: "items([STRING])",
  42. expected: &ParamDeclaration{
  43. Name: "items",
  44. Type: "[STRING]",
  45. },
  46. shouldError: false,
  47. },
  48. {
  49. name: "invalid - starts with $",
  50. declaration: "$userId(STRING)",
  51. expected: nil,
  52. shouldError: true,
  53. },
  54. {
  55. name: "invalid - starts with underscore",
  56. declaration: "_local(STRING)",
  57. expected: nil,
  58. shouldError: true,
  59. },
  60. {
  61. name: "invalid - missing type",
  62. declaration: "userId",
  63. expected: nil,
  64. shouldError: true,
  65. },
  66. {
  67. name: "invalid - missing parentheses",
  68. declaration: "userIdSTRING",
  69. expected: nil,
  70. shouldError: true,
  71. },
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.name, func(t *testing.T) {
  75. result, err := ParseParamDeclaration(tt.declaration)
  76. if tt.shouldError {
  77. if err == nil {
  78. t.Fatal("expected error but got none")
  79. }
  80. return
  81. }
  82. if err != nil {
  83. t.Fatalf("unexpected error: %v", err)
  84. }
  85. if result.Name != tt.expected.Name {
  86. t.Errorf("expected Name=%s, got %s", tt.expected.Name, result.Name)
  87. }
  88. if result.Type != tt.expected.Type {
  89. t.Errorf("expected Type=%s, got %s", tt.expected.Type, result.Type)
  90. }
  91. })
  92. }
  93. }
  94. func TestGetParamDeclarations(t *testing.T) {
  95. registry := &Registry{
  96. Params: []string{
  97. "userId(STRING)",
  98. "maxLimit(INT)",
  99. "config(OBJECT)",
  100. },
  101. }
  102. params, err := registry.GetParamDeclarations()
  103. if err != nil {
  104. t.Fatalf("unexpected error: %v", err)
  105. }
  106. if len(params) != 3 {
  107. t.Fatalf("expected 3 parameters, got %d", len(params))
  108. }
  109. // Check userId
  110. if params["userId"] == nil {
  111. t.Error("userId parameter not found")
  112. } else {
  113. if params["userId"].Type != "STRING" {
  114. t.Errorf("expected userId type STRING, got %s", params["userId"].Type)
  115. }
  116. }
  117. // Check maxLimit
  118. if params["maxLimit"] == nil {
  119. t.Error("maxLimit parameter not found")
  120. } else {
  121. if params["maxLimit"].Type != "INT" {
  122. t.Errorf("expected maxLimit type INT, got %s", params["maxLimit"].Type)
  123. }
  124. }
  125. // Check config
  126. if params["config"] == nil {
  127. t.Error("config parameter not found")
  128. } else {
  129. if params["config"].Type != "OBJECT" {
  130. t.Errorf("expected config type OBJECT, got %s", params["config"].Type)
  131. }
  132. }
  133. }
  134. func TestValidateRegistryWithParams(t *testing.T) {
  135. t.Run("valid registry with params", func(t *testing.T) {
  136. registry := &Registry{
  137. Params: []string{
  138. "userId(STRING)",
  139. "maxLimit(INT)",
  140. },
  141. Vars: []string{
  142. "$result(STRING)",
  143. },
  144. }
  145. err := registry.ValidateRegistry()
  146. if err != nil {
  147. t.Errorf("unexpected error: %v", err)
  148. }
  149. })
  150. t.Run("duplicate parameter names", func(t *testing.T) {
  151. registry := &Registry{
  152. Params: []string{
  153. "userId(STRING)",
  154. "userId(INT)",
  155. },
  156. }
  157. err := registry.ValidateRegistry()
  158. if err == nil {
  159. t.Fatal("expected error for duplicate parameter names")
  160. }
  161. })
  162. t.Run("invalid parameter format", func(t *testing.T) {
  163. registry := &Registry{
  164. Params: []string{
  165. "invalidFormat",
  166. },
  167. }
  168. err := registry.ValidateRegistry()
  169. if err == nil {
  170. t.Fatal("expected error for invalid parameter format")
  171. }
  172. })
  173. }