task_config_schema_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package app
  2. import (
  3. "testing"
  4. )
  5. // Test structs that mirror the actual configuration structure
  6. type TestBaseConfigForTemplate struct {
  7. Enabled bool `json:"enabled"`
  8. ScanIntervalSeconds int `json:"scan_interval_seconds"`
  9. MaxConcurrent int `json:"max_concurrent"`
  10. }
  11. type TestTaskConfigForTemplate struct {
  12. TestBaseConfigForTemplate
  13. TaskSpecificField float64 `json:"task_specific_field"`
  14. AnotherSpecificField string `json:"another_specific_field"`
  15. }
  16. func TestGetTaskFieldValue_EmbeddedStructFields(t *testing.T) {
  17. config := &TestTaskConfigForTemplate{
  18. TestBaseConfigForTemplate: TestBaseConfigForTemplate{
  19. Enabled: true,
  20. ScanIntervalSeconds: 2400,
  21. MaxConcurrent: 5,
  22. },
  23. TaskSpecificField: 0.18,
  24. AnotherSpecificField: "test_value",
  25. }
  26. // Test embedded struct fields
  27. tests := []struct {
  28. fieldName string
  29. expectedValue interface{}
  30. description string
  31. }{
  32. {"enabled", true, "BaseConfig boolean field"},
  33. {"scan_interval_seconds", 2400, "BaseConfig integer field"},
  34. {"max_concurrent", 5, "BaseConfig integer field"},
  35. {"task_specific_field", 0.18, "Task-specific float field"},
  36. {"another_specific_field", "test_value", "Task-specific string field"},
  37. }
  38. for _, test := range tests {
  39. t.Run(test.description, func(t *testing.T) {
  40. result := getTaskFieldValue(config, test.fieldName)
  41. if result != test.expectedValue {
  42. t.Errorf("Field %s: expected %v (%T), got %v (%T)",
  43. test.fieldName, test.expectedValue, test.expectedValue, result, result)
  44. }
  45. })
  46. }
  47. }
  48. func TestGetTaskFieldValue_NonExistentField(t *testing.T) {
  49. config := &TestTaskConfigForTemplate{
  50. TestBaseConfigForTemplate: TestBaseConfigForTemplate{
  51. Enabled: true,
  52. ScanIntervalSeconds: 1800,
  53. MaxConcurrent: 3,
  54. },
  55. }
  56. result := getTaskFieldValue(config, "non_existent_field")
  57. if result != nil {
  58. t.Errorf("Expected nil for non-existent field, got %v", result)
  59. }
  60. }
  61. func TestGetTaskFieldValue_NilConfig(t *testing.T) {
  62. var config *TestTaskConfigForTemplate = nil
  63. result := getTaskFieldValue(config, "enabled")
  64. if result != nil {
  65. t.Errorf("Expected nil for nil config, got %v", result)
  66. }
  67. }
  68. func TestGetTaskFieldValue_EmptyStruct(t *testing.T) {
  69. config := &TestTaskConfigForTemplate{}
  70. // Test that we can extract zero values
  71. tests := []struct {
  72. fieldName string
  73. expectedValue interface{}
  74. description string
  75. }{
  76. {"enabled", false, "Zero value boolean"},
  77. {"scan_interval_seconds", 0, "Zero value integer"},
  78. {"max_concurrent", 0, "Zero value integer"},
  79. {"task_specific_field", 0.0, "Zero value float"},
  80. {"another_specific_field", "", "Zero value string"},
  81. }
  82. for _, test := range tests {
  83. t.Run(test.description, func(t *testing.T) {
  84. result := getTaskFieldValue(config, test.fieldName)
  85. if result != test.expectedValue {
  86. t.Errorf("Field %s: expected %v (%T), got %v (%T)",
  87. test.fieldName, test.expectedValue, test.expectedValue, result, result)
  88. }
  89. })
  90. }
  91. }
  92. func TestGetTaskFieldValue_NonStructConfig(t *testing.T) {
  93. var config interface{} = "not a struct"
  94. result := getTaskFieldValue(config, "enabled")
  95. if result != nil {
  96. t.Errorf("Expected nil for non-struct config, got %v", result)
  97. }
  98. }
  99. func TestGetTaskFieldValue_PointerToStruct(t *testing.T) {
  100. config := &TestTaskConfigForTemplate{
  101. TestBaseConfigForTemplate: TestBaseConfigForTemplate{
  102. Enabled: false,
  103. ScanIntervalSeconds: 900,
  104. MaxConcurrent: 2,
  105. },
  106. TaskSpecificField: 0.35,
  107. }
  108. // Test that pointers are handled correctly
  109. enabledResult := getTaskFieldValue(config, "enabled")
  110. if enabledResult != false {
  111. t.Errorf("Expected false for enabled field, got %v", enabledResult)
  112. }
  113. intervalResult := getTaskFieldValue(config, "scan_interval_seconds")
  114. if intervalResult != 900 {
  115. t.Errorf("Expected 900 for scan_interval_seconds field, got %v", intervalResult)
  116. }
  117. }
  118. func TestGetTaskFieldValue_FieldsWithJSONOmitempty(t *testing.T) {
  119. // Test struct with omitempty tags
  120. type TestConfigWithOmitempty struct {
  121. TestBaseConfigForTemplate
  122. OptionalField string `json:"optional_field,omitempty"`
  123. }
  124. config := &TestConfigWithOmitempty{
  125. TestBaseConfigForTemplate: TestBaseConfigForTemplate{
  126. Enabled: true,
  127. ScanIntervalSeconds: 1200,
  128. MaxConcurrent: 4,
  129. },
  130. OptionalField: "optional_value",
  131. }
  132. // Test that fields with omitempty are still found
  133. result := getTaskFieldValue(config, "optional_field")
  134. if result != "optional_value" {
  135. t.Errorf("Expected 'optional_value' for optional_field, got %v", result)
  136. }
  137. // Test embedded fields still work
  138. enabledResult := getTaskFieldValue(config, "enabled")
  139. if enabledResult != true {
  140. t.Errorf("Expected true for enabled field, got %v", enabledResult)
  141. }
  142. }
  143. func TestGetTaskFieldValue_DeepEmbedding(t *testing.T) {
  144. // Test with multiple levels of embedding
  145. type DeepBaseConfig struct {
  146. DeepField string `json:"deep_field"`
  147. }
  148. type MiddleConfig struct {
  149. DeepBaseConfig
  150. MiddleField int `json:"middle_field"`
  151. }
  152. type TopConfig struct {
  153. MiddleConfig
  154. TopField bool `json:"top_field"`
  155. }
  156. config := &TopConfig{
  157. MiddleConfig: MiddleConfig{
  158. DeepBaseConfig: DeepBaseConfig{
  159. DeepField: "deep_value",
  160. },
  161. MiddleField: 123,
  162. },
  163. TopField: true,
  164. }
  165. // Test that deeply embedded fields are found
  166. deepResult := getTaskFieldValue(config, "deep_field")
  167. if deepResult != "deep_value" {
  168. t.Errorf("Expected 'deep_value' for deep_field, got %v", deepResult)
  169. }
  170. middleResult := getTaskFieldValue(config, "middle_field")
  171. if middleResult != 123 {
  172. t.Errorf("Expected 123 for middle_field, got %v", middleResult)
  173. }
  174. topResult := getTaskFieldValue(config, "top_field")
  175. if topResult != true {
  176. t.Errorf("Expected true for top_field, got %v", topResult)
  177. }
  178. }
  179. // Benchmark to ensure performance is reasonable
  180. func BenchmarkGetTaskFieldValue(b *testing.B) {
  181. config := &TestTaskConfigForTemplate{
  182. TestBaseConfigForTemplate: TestBaseConfigForTemplate{
  183. Enabled: true,
  184. ScanIntervalSeconds: 1800,
  185. MaxConcurrent: 3,
  186. },
  187. TaskSpecificField: 0.25,
  188. AnotherSpecificField: "benchmark_test",
  189. }
  190. b.ResetTimer()
  191. for i := 0; i < b.N; i++ {
  192. // Test both embedded and regular fields
  193. _ = getTaskFieldValue(config, "enabled")
  194. _ = getTaskFieldValue(config, "task_specific_field")
  195. }
  196. }