mock_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package engine
  2. import (
  3. "context"
  4. "testing"
  5. )
  6. func TestMockBrokerClient_BasicFunctionality(t *testing.T) {
  7. mockBroker := NewMockBrokerClient()
  8. // Test ListNamespaces
  9. namespaces, err := mockBroker.ListNamespaces(context.Background())
  10. if err != nil {
  11. t.Fatalf("Expected no error, got %v", err)
  12. }
  13. if len(namespaces) != 2 {
  14. t.Errorf("Expected 2 namespaces, got %d", len(namespaces))
  15. }
  16. // Test ListTopics
  17. topics, err := mockBroker.ListTopics(context.Background(), "default")
  18. if err != nil {
  19. t.Fatalf("Expected no error, got %v", err)
  20. }
  21. if len(topics) != 2 {
  22. t.Errorf("Expected 2 topics in default namespace, got %d", len(topics))
  23. }
  24. // Test GetTopicSchema
  25. schema, err := mockBroker.GetTopicSchema(context.Background(), "default", "user_events")
  26. if err != nil {
  27. t.Fatalf("Expected no error, got %v", err)
  28. }
  29. if len(schema.Fields) != 3 {
  30. t.Errorf("Expected 3 fields in user_events schema, got %d", len(schema.Fields))
  31. }
  32. }
  33. func TestMockBrokerClient_FailureScenarios(t *testing.T) {
  34. mockBroker := NewMockBrokerClient()
  35. // Configure mock to fail
  36. mockBroker.SetFailure(true, "simulated broker failure")
  37. // Test that operations fail as expected
  38. _, err := mockBroker.ListNamespaces(context.Background())
  39. if err == nil {
  40. t.Error("Expected error when mock is configured to fail")
  41. }
  42. _, err = mockBroker.ListTopics(context.Background(), "default")
  43. if err == nil {
  44. t.Error("Expected error when mock is configured to fail")
  45. }
  46. _, err = mockBroker.GetTopicSchema(context.Background(), "default", "user_events")
  47. if err == nil {
  48. t.Error("Expected error when mock is configured to fail")
  49. }
  50. // Test that filer client also fails
  51. _, err = mockBroker.GetFilerClient()
  52. if err == nil {
  53. t.Error("Expected error when mock is configured to fail")
  54. }
  55. // Reset mock to working state
  56. mockBroker.SetFailure(false, "")
  57. // Test that operations work again
  58. namespaces, err := mockBroker.ListNamespaces(context.Background())
  59. if err != nil {
  60. t.Errorf("Expected no error after resetting mock, got %v", err)
  61. }
  62. if len(namespaces) == 0 {
  63. t.Error("Expected namespaces after resetting mock")
  64. }
  65. }
  66. func TestMockBrokerClient_TopicManagement(t *testing.T) {
  67. mockBroker := NewMockBrokerClient()
  68. // Test ConfigureTopic (add a new topic)
  69. err := mockBroker.ConfigureTopic(context.Background(), "test", "new-topic", 1, nil)
  70. if err != nil {
  71. t.Fatalf("Expected no error, got %v", err)
  72. }
  73. // Verify the topic was added
  74. topics, err := mockBroker.ListTopics(context.Background(), "test")
  75. if err != nil {
  76. t.Fatalf("Expected no error, got %v", err)
  77. }
  78. foundNewTopic := false
  79. for _, topic := range topics {
  80. if topic == "new-topic" {
  81. foundNewTopic = true
  82. break
  83. }
  84. }
  85. if !foundNewTopic {
  86. t.Error("Expected new-topic to be in the topics list")
  87. }
  88. // Test DeleteTopic
  89. err = mockBroker.DeleteTopic(context.Background(), "test", "new-topic")
  90. if err != nil {
  91. t.Fatalf("Expected no error, got %v", err)
  92. }
  93. // Verify the topic was removed
  94. topics, err = mockBroker.ListTopics(context.Background(), "test")
  95. if err != nil {
  96. t.Fatalf("Expected no error, got %v", err)
  97. }
  98. for _, topic := range topics {
  99. if topic == "new-topic" {
  100. t.Error("Expected new-topic to be removed from topics list")
  101. }
  102. }
  103. }
  104. func TestSQLEngineWithMockBrokerClient_ErrorHandling(t *testing.T) {
  105. // Create an engine with a failing mock broker
  106. mockBroker := NewMockBrokerClient()
  107. mockBroker.SetFailure(true, "mock broker unavailable")
  108. catalog := &SchemaCatalog{
  109. databases: make(map[string]*DatabaseInfo),
  110. currentDatabase: "default",
  111. brokerClient: mockBroker,
  112. }
  113. engine := &SQLEngine{catalog: catalog}
  114. // Test that queries fail gracefully with proper error messages
  115. result, err := engine.ExecuteSQL(context.Background(), "SELECT * FROM nonexistent_topic")
  116. // ExecuteSQL itself should not return an error, but the result should contain an error
  117. if err != nil {
  118. // If ExecuteSQL returns an error, that's also acceptable for this test
  119. t.Logf("ExecuteSQL returned error (acceptable): %v", err)
  120. return
  121. }
  122. // Should have an error in the result when broker is unavailable
  123. if result.Error == nil {
  124. t.Error("Expected error in query result when broker is unavailable")
  125. } else {
  126. t.Logf("Got expected error in result: %v", result.Error)
  127. }
  128. }