MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestFormatter_FormatFile

Function TestFormatter_FormatFile

cmd/gosqlx/cmd/formatter_test.go:26–106  ·  view source on GitHub ↗

TestFormatter_FormatFile tests single file formatting

(t *testing.T)

Source from the content-addressed store, hash-verified

24
25// TestFormatter_FormatFile tests single file formatting
26func TestFormatter_FormatFile(t *testing.T) {
27 tmpDir := t.TempDir()
28
29 tests := []struct {
30 name string
31 filename string
32 content string
33 expectChanged bool
34 expectError bool
35 errorContains string
36 }{
37 {
38 name: "valid SQL file - basic SELECT",
39 filename: "query.sql",
40 content: "SELECT * FROM users WHERE active = true",
41 expectChanged: true, // Formatter adds indentation and newlines
42 expectError: false,
43 },
44 {
45 name: "valid empty file",
46 filename: "empty.sql",
47 content: "",
48 expectChanged: false,
49 expectError: false,
50 },
51 {
52 name: "SQL needing formatting",
53 filename: "unformatted.sql",
54 content: "select*from users",
55 expectChanged: true,
56 expectError: false,
57 },
58 {
59 name: "invalid SQL",
60 filename: "invalid.sql",
61 content: "SELECT * FROM",
62 expectChanged: false,
63 expectError: true,
64 errorContains: "parsing failed",
65 },
66 }
67
68 for _, tt := range tests {
69 t.Run(tt.name, func(t *testing.T) {
70 // Create test file
71 filepath := filepath.Join(tmpDir, tt.filename)
72 if err := os.WriteFile(filepath, []byte(tt.content), 0644); err != nil {
73 t.Fatalf("Failed to create test file: %v", err)
74 }
75
76 // Create formatter
77 var outBuf, errBuf bytes.Buffer
78 formatter := NewFormatter(&outBuf, &errBuf, CLIFormatterOptions{
79 IndentSize: 2,
80 Uppercase: true,
81 Compact: false,
82 })
83

Callers

nothing calls this directly

Calls 6

formatFileMethod · 0.95
RunMethod · 0.80
NewFormatterFunction · 0.70
FatalfMethod · 0.65
ErrorfMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected