partition_path_fix_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package engine
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. // TestPartitionPathHandling tests that partition paths are handled correctly
  8. // whether discoverTopicPartitions returns relative or absolute paths
  9. func TestPartitionPathHandling(t *testing.T) {
  10. engine := NewMockSQLEngine()
  11. t.Run("Mock discoverTopicPartitions returns correct paths", func(t *testing.T) {
  12. // Test that our mock engine handles absolute paths correctly
  13. engine.mockPartitions["test.user_events"] = []string{
  14. "/topics/test/user_events/v2025-09-03-15-36-29/0000-2520",
  15. "/topics/test/user_events/v2025-09-03-15-36-29/2521-5040",
  16. }
  17. partitions, err := engine.discoverTopicPartitions("test", "user_events")
  18. assert.NoError(t, err, "Should discover partitions without error")
  19. assert.Equal(t, 2, len(partitions), "Should return 2 partitions")
  20. assert.Contains(t, partitions[0], "/topics/test/user_events/", "Should contain absolute path")
  21. })
  22. t.Run("Mock discoverTopicPartitions handles relative paths", func(t *testing.T) {
  23. // Test relative paths scenario
  24. engine.mockPartitions["test.user_events"] = []string{
  25. "v2025-09-03-15-36-29/0000-2520",
  26. "v2025-09-03-15-36-29/2521-5040",
  27. }
  28. partitions, err := engine.discoverTopicPartitions("test", "user_events")
  29. assert.NoError(t, err, "Should discover partitions without error")
  30. assert.Equal(t, 2, len(partitions), "Should return 2 partitions")
  31. assert.True(t, !strings.HasPrefix(partitions[0], "/topics/"), "Should be relative path")
  32. })
  33. t.Run("Partition path building logic works correctly", func(t *testing.T) {
  34. topicBasePath := "/topics/test/user_events"
  35. testCases := []struct {
  36. name string
  37. relativePartition string
  38. expectedPath string
  39. }{
  40. {
  41. name: "Absolute path - use as-is",
  42. relativePartition: "/topics/test/user_events/v2025-09-03-15-36-29/0000-2520",
  43. expectedPath: "/topics/test/user_events/v2025-09-03-15-36-29/0000-2520",
  44. },
  45. {
  46. name: "Relative path - build full path",
  47. relativePartition: "v2025-09-03-15-36-29/0000-2520",
  48. expectedPath: "/topics/test/user_events/v2025-09-03-15-36-29/0000-2520",
  49. },
  50. }
  51. for _, tc := range testCases {
  52. t.Run(tc.name, func(t *testing.T) {
  53. var partitionPath string
  54. // This is the same logic from our fixed code
  55. if strings.HasPrefix(tc.relativePartition, "/topics/") {
  56. // Already a full path - use as-is
  57. partitionPath = tc.relativePartition
  58. } else {
  59. // Relative path - build full path
  60. partitionPath = topicBasePath + "/" + tc.relativePartition
  61. }
  62. assert.Equal(t, tc.expectedPath, partitionPath,
  63. "Partition path should be built correctly")
  64. // Ensure no double slashes
  65. assert.NotContains(t, partitionPath, "//",
  66. "Partition path should not contain double slashes")
  67. })
  68. }
  69. })
  70. }
  71. // TestPartitionPathLogic tests the core logic for handling partition paths
  72. func TestPartitionPathLogic(t *testing.T) {
  73. t.Run("Building partition paths from discovered partitions", func(t *testing.T) {
  74. // Test the specific partition path building that was causing issues
  75. topicBasePath := "/topics/ecommerce/user_events"
  76. // This simulates the discoverTopicPartitions returning absolute paths (realistic scenario)
  77. relativePartitions := []string{
  78. "/topics/ecommerce/user_events/v2025-09-03-15-36-29/0000-2520",
  79. }
  80. // This is the code from our fix - test it directly
  81. partitions := make([]string, len(relativePartitions))
  82. for i, relPartition := range relativePartitions {
  83. // Handle both relative and absolute partition paths from discoverTopicPartitions
  84. if strings.HasPrefix(relPartition, "/topics/") {
  85. // Already a full path - use as-is
  86. partitions[i] = relPartition
  87. } else {
  88. // Relative path - build full path
  89. partitions[i] = topicBasePath + "/" + relPartition
  90. }
  91. }
  92. // Verify the path was handled correctly
  93. expectedPath := "/topics/ecommerce/user_events/v2025-09-03-15-36-29/0000-2520"
  94. assert.Equal(t, expectedPath, partitions[0], "Absolute path should be used as-is")
  95. // Ensure no double slashes (this was the original bug)
  96. assert.NotContains(t, partitions[0], "//", "Path should not contain double slashes")
  97. })
  98. }