|
| 1 | +package regresql |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/boringsql/queries" |
| 9 | +) |
| 10 | + |
| 11 | +func TestTestSummaryAddResultPending(t *testing.T) { |
| 12 | + summary := NewTestSummary() |
| 13 | + |
| 14 | + // Add a pending result |
| 15 | + summary.AddResult(TestResult{ |
| 16 | + Name: "test1", |
| 17 | + Status: "pending", |
| 18 | + }) |
| 19 | + |
| 20 | + if summary.Total != 1 { |
| 21 | + t.Errorf("Expected Total=1, got %d", summary.Total) |
| 22 | + } |
| 23 | + if summary.Pending != 1 { |
| 24 | + t.Errorf("Expected Pending=1, got %d", summary.Pending) |
| 25 | + } |
| 26 | + if summary.Passed != 0 { |
| 27 | + t.Errorf("Expected Passed=0, got %d", summary.Passed) |
| 28 | + } |
| 29 | + if summary.Failed != 0 { |
| 30 | + t.Errorf("Expected Failed=0, got %d", summary.Failed) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestTestSummaryMixedStatuses(t *testing.T) { |
| 35 | + summary := NewTestSummary() |
| 36 | + |
| 37 | + summary.AddResult(TestResult{Name: "passed1", Status: "passed"}) |
| 38 | + summary.AddResult(TestResult{Name: "pending1", Status: "pending"}) |
| 39 | + summary.AddResult(TestResult{Name: "failed1", Status: "failed"}) |
| 40 | + summary.AddResult(TestResult{Name: "pending2", Status: "pending"}) |
| 41 | + summary.AddResult(TestResult{Name: "skipped1", Status: "skipped"}) |
| 42 | + |
| 43 | + if summary.Total != 5 { |
| 44 | + t.Errorf("Expected Total=5, got %d", summary.Total) |
| 45 | + } |
| 46 | + if summary.Passed != 1 { |
| 47 | + t.Errorf("Expected Passed=1, got %d", summary.Passed) |
| 48 | + } |
| 49 | + if summary.Failed != 1 { |
| 50 | + t.Errorf("Expected Failed=1, got %d", summary.Failed) |
| 51 | + } |
| 52 | + if summary.Pending != 2 { |
| 53 | + t.Errorf("Expected Pending=2, got %d", summary.Pending) |
| 54 | + } |
| 55 | + if summary.Skipped != 1 { |
| 56 | + t.Errorf("Expected Skipped=1, got %d", summary.Skipped) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestCompareResultSetsToResultsMissingExpectedFile(t *testing.T) { |
| 61 | + // Create temp directories |
| 62 | + tmpDir, err := os.MkdirTemp("", "regresql-test") |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 65 | + } |
| 66 | + defer os.RemoveAll(tmpDir) |
| 67 | + |
| 68 | + regressDir := filepath.Join(tmpDir, "regress") |
| 69 | + outDir := filepath.Join(regressDir, "out") |
| 70 | + expectedDir := filepath.Join(regressDir, "expected") |
| 71 | + |
| 72 | + if err := os.MkdirAll(outDir, 0755); err != nil { |
| 73 | + t.Fatalf("Failed to create out dir: %v", err) |
| 74 | + } |
| 75 | + if err := os.MkdirAll(expectedDir, 0755); err != nil { |
| 76 | + t.Fatalf("Failed to create expected dir: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Create an actual result file (simulating query output) |
| 80 | + actualFile := filepath.Join(outDir, "test_query.json") |
| 81 | + actualContent := `{"columns":["id"],"rows":[[1]]}` |
| 82 | + if err := os.WriteFile(actualFile, []byte(actualContent), 0644); err != nil { |
| 83 | + t.Fatalf("Failed to write actual file: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Create a minimal query for the plan |
| 87 | + bqQuery, _ := queries.NewQuery("test", "test.sql", "SELECT 1", nil) |
| 88 | + query := &Query{Query: bqQuery} |
| 89 | + |
| 90 | + // Create a plan with the result set (no expected file exists) |
| 91 | + plan := &Plan{ |
| 92 | + Query: query, |
| 93 | + ResultSets: []ResultSet{ |
| 94 | + {Filename: actualFile}, |
| 95 | + }, |
| 96 | + Names: []string{"default"}, |
| 97 | + Bindings: []map[string]any{{}}, |
| 98 | + } |
| 99 | + |
| 100 | + // Run comparison - expected file doesn't exist |
| 101 | + results := plan.CompareResultSetsToResults(regressDir, expectedDir) |
| 102 | + |
| 103 | + if len(results) != 1 { |
| 104 | + t.Fatalf("Expected 1 result, got %d", len(results)) |
| 105 | + } |
| 106 | + |
| 107 | + result := results[0] |
| 108 | + if result.Status != "pending" { |
| 109 | + t.Errorf("Expected status='pending', got '%s'", result.Status) |
| 110 | + } |
| 111 | + if result.Error == "" { |
| 112 | + t.Error("Expected error message to be set for pending result") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestCompareResultSetsToResultsWithExpectedFile(t *testing.T) { |
| 117 | + // Create temp directories |
| 118 | + tmpDir, err := os.MkdirTemp("", "regresql-test") |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 121 | + } |
| 122 | + defer os.RemoveAll(tmpDir) |
| 123 | + |
| 124 | + regressDir := filepath.Join(tmpDir, "regress") |
| 125 | + outDir := filepath.Join(regressDir, "out") |
| 126 | + expectedDir := filepath.Join(regressDir, "expected") |
| 127 | + |
| 128 | + if err := os.MkdirAll(outDir, 0755); err != nil { |
| 129 | + t.Fatalf("Failed to create out dir: %v", err) |
| 130 | + } |
| 131 | + if err := os.MkdirAll(expectedDir, 0755); err != nil { |
| 132 | + t.Fatalf("Failed to create expected dir: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + // Create matching actual and expected files |
| 136 | + content := `{"columns":["id"],"rows":[[1]]}` |
| 137 | + actualFile := filepath.Join(outDir, "test_query.json") |
| 138 | + expectedFile := filepath.Join(expectedDir, "test_query.json") |
| 139 | + |
| 140 | + if err := os.WriteFile(actualFile, []byte(content), 0644); err != nil { |
| 141 | + t.Fatalf("Failed to write actual file: %v", err) |
| 142 | + } |
| 143 | + if err := os.WriteFile(expectedFile, []byte(content), 0644); err != nil { |
| 144 | + t.Fatalf("Failed to write expected file: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + bqQuery2, _ := queries.NewQuery("test", "test.sql", "SELECT 1", nil) |
| 148 | + query := &Query{Query: bqQuery2} |
| 149 | + |
| 150 | + plan := &Plan{ |
| 151 | + Query: query, |
| 152 | + ResultSets: []ResultSet{ |
| 153 | + { |
| 154 | + Filename: actualFile, |
| 155 | + Cols: []string{"id"}, |
| 156 | + Rows: [][]any{{float64(1)}}, // JSON unmarshals numbers as float64 |
| 157 | + }, |
| 158 | + }, |
| 159 | + Names: []string{"default"}, |
| 160 | + Bindings: []map[string]any{{}}, |
| 161 | + } |
| 162 | + |
| 163 | + results := plan.CompareResultSetsToResults(regressDir, expectedDir) |
| 164 | + |
| 165 | + if len(results) != 1 { |
| 166 | + t.Fatalf("Expected 1 result, got %d", len(results)) |
| 167 | + } |
| 168 | + |
| 169 | + result := results[0] |
| 170 | + if result.Status != "passed" { |
| 171 | + t.Errorf("Expected status='passed', got '%s' (error: %s)", result.Status, result.Error) |
| 172 | + } |
| 173 | +} |
0 commit comments