mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-06-12 17:39:42 +08:00
✨ feat(external-sql): 完善外部 SQL 目录文件管理
- 新增外部 SQL 文件的新建、重命名、删除和目录管理接口 - 后端限制 SQL 目录只加载 .sql 文件并补充目录操作测试 - 前端补齐 Wails 类型、浏览器 mock 和外部 SQL 树过滤逻辑 - 支持从外部 SQL 文件标签定位到侧栏目录节点
This commit is contained in:
@@ -84,6 +84,200 @@ type SQLDirectoryEntry struct {
|
||||
Children []SQLDirectoryEntry `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
func normalizeSQLFileName(rawName string) (string, error) {
|
||||
name := strings.TrimSpace(rawName)
|
||||
if name == "" {
|
||||
return "", fmt.Errorf("SQL 文件名不能为空")
|
||||
}
|
||||
if strings.ContainsAny(name, `/\`) || name == "." || name == ".." {
|
||||
return "", fmt.Errorf("SQL 文件名不能包含路径分隔符")
|
||||
}
|
||||
if !strings.EqualFold(filepath.Ext(name), ".sql") {
|
||||
name += ".sql"
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func normalizeSQLDirectoryName(rawName string) (string, error) {
|
||||
name := strings.TrimSpace(rawName)
|
||||
if name == "" {
|
||||
return "", fmt.Errorf("目录名不能为空")
|
||||
}
|
||||
if strings.ContainsAny(name, `/\`) || name == "." || name == ".." {
|
||||
return "", fmt.Errorf("目录名不能包含路径分隔符")
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func normalizeSQLDirectoryPath(directoryPath string) (string, error) {
|
||||
target := strings.TrimSpace(directoryPath)
|
||||
if target == "" {
|
||||
return "", fmt.Errorf("目录路径不能为空")
|
||||
}
|
||||
if abs, err := filepath.Abs(target); err == nil {
|
||||
target = abs
|
||||
}
|
||||
info, err := os.Stat(target)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("无法读取目录信息: %w", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return "", fmt.Errorf("所选路径不是目录")
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func normalizeExistingSQLDirectoryPath(directoryPath string) (string, os.FileInfo, error) {
|
||||
target := strings.TrimSpace(directoryPath)
|
||||
if target == "" {
|
||||
return "", nil, fmt.Errorf("目录路径不能为空")
|
||||
}
|
||||
if abs, err := filepath.Abs(target); err == nil {
|
||||
target = abs
|
||||
}
|
||||
info, err := os.Stat(target)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("无法读取目录信息: %w", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return "", nil, fmt.Errorf("所选路径不是目录")
|
||||
}
|
||||
return target, info, nil
|
||||
}
|
||||
|
||||
func normalizeExistingSQLFilePath(filePath string) (string, os.FileInfo, error) {
|
||||
target := strings.TrimSpace(filePath)
|
||||
if target == "" {
|
||||
return "", nil, fmt.Errorf("文件路径不能为空")
|
||||
}
|
||||
if abs, err := filepath.Abs(target); err == nil {
|
||||
target = abs
|
||||
}
|
||||
info, err := os.Stat(target)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("无法读取文件信息: %w", err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return "", nil, fmt.Errorf("所选路径不是 SQL 文件")
|
||||
}
|
||||
if !strings.EqualFold(filepath.Ext(target), ".sql") {
|
||||
return "", nil, fmt.Errorf("仅支持 SQL 文件")
|
||||
}
|
||||
return target, info, nil
|
||||
}
|
||||
|
||||
func createSQLFileInDirectory(directoryPath string, rawName string) connection.QueryResult {
|
||||
directory, err := normalizeSQLDirectoryPath(directoryPath)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
name, err := normalizeSQLFileName(rawName)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
target := filepath.Join(directory, name)
|
||||
if _, err := os.Stat(target); err == nil {
|
||||
return connection.QueryResult{Success: false, Message: "SQL 文件已存在"}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法读取文件信息: %v", err)}
|
||||
}
|
||||
if err := os.WriteFile(target, []byte(""), 0o644); err != nil {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法创建 SQL 文件: %v", err)}
|
||||
}
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"filePath": target, "name": filepath.Base(target)}}
|
||||
}
|
||||
|
||||
func createSQLDirectoryInDirectory(parentPath string, rawName string) connection.QueryResult {
|
||||
parent, err := normalizeSQLDirectoryPath(parentPath)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
name, err := normalizeSQLDirectoryName(rawName)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
target := filepath.Join(parent, name)
|
||||
if _, err := os.Stat(target); err == nil {
|
||||
return connection.QueryResult{Success: false, Message: "目录已存在"}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法读取目录信息: %v", err)}
|
||||
}
|
||||
if err := os.Mkdir(target, 0o755); err != nil {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法创建目录: %v", err)}
|
||||
}
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"directoryPath": target, "name": filepath.Base(target)}}
|
||||
}
|
||||
|
||||
func deleteSQLFileByPath(filePath string) connection.QueryResult {
|
||||
target, _, err := normalizeExistingSQLFilePath(filePath)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
if err := os.Remove(target); err != nil {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法删除 SQL 文件: %v", err)}
|
||||
}
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"filePath": target}}
|
||||
}
|
||||
|
||||
func deleteSQLDirectoryByPath(directoryPath string) connection.QueryResult {
|
||||
target, _, err := normalizeExistingSQLDirectoryPath(directoryPath)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
if err := os.Remove(target); err != nil {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法删除目录: %v(仅支持删除空目录)", err)}
|
||||
}
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"directoryPath": target}}
|
||||
}
|
||||
|
||||
func renameSQLFileByPath(filePath string, rawName string) connection.QueryResult {
|
||||
source, _, err := normalizeExistingSQLFilePath(filePath)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
name, err := normalizeSQLFileName(rawName)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
target := filepath.Join(filepath.Dir(source), name)
|
||||
if source == target {
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"filePath": target, "name": filepath.Base(target)}}
|
||||
}
|
||||
if _, err := os.Stat(target); err == nil {
|
||||
return connection.QueryResult{Success: false, Message: "目标 SQL 文件已存在"}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法读取目标文件信息: %v", err)}
|
||||
}
|
||||
if err := os.Rename(source, target); err != nil {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法重命名 SQL 文件: %v", err)}
|
||||
}
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"filePath": target, "name": filepath.Base(target)}}
|
||||
}
|
||||
|
||||
func renameSQLDirectoryByPath(directoryPath string, rawName string) connection.QueryResult {
|
||||
source, _, err := normalizeExistingSQLDirectoryPath(directoryPath)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
name, err := normalizeSQLDirectoryName(rawName)
|
||||
if err != nil {
|
||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||
}
|
||||
target := filepath.Join(filepath.Dir(source), name)
|
||||
if source == target {
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"directoryPath": target, "name": filepath.Base(target)}}
|
||||
}
|
||||
if _, err := os.Stat(target); err == nil {
|
||||
return connection.QueryResult{Success: false, Message: "目标目录已存在"}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法读取目标目录信息: %v", err)}
|
||||
}
|
||||
if err := os.Rename(source, target); err != nil {
|
||||
return connection.QueryResult{Success: false, Message: fmt.Sprintf("无法重命名目录: %v", err)}
|
||||
}
|
||||
return connection.QueryResult{Success: true, Data: map[string]interface{}{"directoryPath": target, "name": filepath.Base(target)}}
|
||||
}
|
||||
|
||||
func normalizeDirectoryDialogPath(currentDir string) string {
|
||||
defaultDir := strings.TrimSpace(currentDir)
|
||||
if defaultDir == "" {
|
||||
@@ -140,6 +334,28 @@ func readSQLFileByPath(filePath string) connection.QueryResult {
|
||||
return connection.QueryResult{Success: true, Data: string(content)}
|
||||
}
|
||||
|
||||
func readSQLFileWithMetadataByPath(filePath string) connection.QueryResult {
|
||||
result := readSQLFileByPath(filePath)
|
||||
if !result.Success {
|
||||
return result
|
||||
}
|
||||
if data, ok := result.Data.(map[string]interface{}); ok {
|
||||
return connection.QueryResult{Success: true, Data: data}
|
||||
}
|
||||
selection := strings.TrimSpace(filePath)
|
||||
if abs, err := filepath.Abs(selection); err == nil {
|
||||
selection = abs
|
||||
}
|
||||
return connection.QueryResult{
|
||||
Success: true,
|
||||
Data: map[string]interface{}{
|
||||
"content": result.Data,
|
||||
"filePath": selection,
|
||||
"name": filepath.Base(selection),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func writeSQLFileByPath(filePath string, content string) connection.QueryResult {
|
||||
target := strings.TrimSpace(filePath)
|
||||
if target == "" {
|
||||
@@ -238,9 +454,6 @@ func buildSQLDirectoryEntries(directory string) ([]SQLDirectoryEntry, error) {
|
||||
if childErr != nil {
|
||||
return nil, childErr
|
||||
}
|
||||
if len(children) == 0 {
|
||||
continue
|
||||
}
|
||||
result = append(result, SQLDirectoryEntry{
|
||||
Name: entry.Name(),
|
||||
Path: entryPath,
|
||||
@@ -291,7 +504,7 @@ func (a *App) OpenSQLFile() connection.QueryResult {
|
||||
return connection.QueryResult{Success: false, Message: "已取消"}
|
||||
}
|
||||
|
||||
return readSQLFileByPath(selection)
|
||||
return readSQLFileWithMetadataByPath(selection)
|
||||
}
|
||||
|
||||
func (a *App) SelectSQLDirectory(currentDir string) connection.QueryResult {
|
||||
@@ -347,6 +560,30 @@ func (a *App) WriteSQLFile(filePath string, content string) connection.QueryResu
|
||||
return writeSQLFileByPath(filePath, content)
|
||||
}
|
||||
|
||||
func (a *App) CreateSQLFile(directoryPath string, name string) connection.QueryResult {
|
||||
return createSQLFileInDirectory(directoryPath, name)
|
||||
}
|
||||
|
||||
func (a *App) CreateSQLDirectory(directoryPath string, name string) connection.QueryResult {
|
||||
return createSQLDirectoryInDirectory(directoryPath, name)
|
||||
}
|
||||
|
||||
func (a *App) DeleteSQLFile(filePath string) connection.QueryResult {
|
||||
return deleteSQLFileByPath(filePath)
|
||||
}
|
||||
|
||||
func (a *App) DeleteSQLDirectory(directoryPath string) connection.QueryResult {
|
||||
return deleteSQLDirectoryByPath(directoryPath)
|
||||
}
|
||||
|
||||
func (a *App) RenameSQLFile(filePath string, name string) connection.QueryResult {
|
||||
return renameSQLFileByPath(filePath, name)
|
||||
}
|
||||
|
||||
func (a *App) RenameSQLDirectory(directoryPath string, name string) connection.QueryResult {
|
||||
return renameSQLDirectoryByPath(directoryPath, name)
|
||||
}
|
||||
|
||||
func (a *App) ExportSQLFile(defaultName string, content string) connection.QueryResult {
|
||||
filename, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
||||
Title: "导出 SQL 文件",
|
||||
|
||||
@@ -9,9 +9,13 @@ import (
|
||||
func TestBuildSQLDirectoryEntriesKeepsOnlySQLFilesAndNestedFolders(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
nestedDir := filepath.Join(root, "nested")
|
||||
emptyDir := filepath.Join(root, "empty")
|
||||
if err := os.MkdirAll(nestedDir, 0o755); err != nil {
|
||||
t.Fatalf("MkdirAll returned error: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(emptyDir, 0o755); err != nil {
|
||||
t.Fatalf("MkdirAll empty returned error: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "z-last.sql"), []byte("select 1;"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile sql returned error: %v", err)
|
||||
}
|
||||
@@ -27,17 +31,20 @@ func TestBuildSQLDirectoryEntriesKeepsOnlySQLFilesAndNestedFolders(t *testing.T)
|
||||
t.Fatalf("buildSQLDirectoryEntries returned error: %v", err)
|
||||
}
|
||||
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("expected one folder and one sql file, got %d entries", len(entries))
|
||||
if len(entries) != 3 {
|
||||
t.Fatalf("expected two folders and one sql file, got %d entries", len(entries))
|
||||
}
|
||||
if !entries[0].IsDir || entries[0].Name != "nested" {
|
||||
t.Fatalf("expected nested directory first, got %#v", entries[0])
|
||||
if !entries[0].IsDir || entries[0].Name != "empty" || len(entries[0].Children) != 0 {
|
||||
t.Fatalf("expected empty directory first, got %#v", entries[0])
|
||||
}
|
||||
if len(entries[0].Children) != 1 || entries[0].Children[0].Name != "inner.SQL" {
|
||||
t.Fatalf("expected nested sql child, got %#v", entries[0].Children)
|
||||
if !entries[1].IsDir || entries[1].Name != "nested" {
|
||||
t.Fatalf("expected nested directory second, got %#v", entries[1])
|
||||
}
|
||||
if entries[1].IsDir || entries[1].Name != "z-last.sql" {
|
||||
t.Fatalf("expected top-level sql file second, got %#v", entries[1])
|
||||
if len(entries[1].Children) != 1 || entries[1].Children[0].Name != "inner.SQL" {
|
||||
t.Fatalf("expected nested sql child, got %#v", entries[1].Children)
|
||||
}
|
||||
if entries[2].IsDir || entries[2].Name != "z-last.sql" {
|
||||
t.Fatalf("expected top-level sql file third, got %#v", entries[2])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +82,156 @@ func TestWriteSQLFileByPathRejectsEmptyPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSQLFileInDirectoryCreatesEmptySQLFile(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
|
||||
result := createSQLFileInDirectory(root, "draft")
|
||||
if !result.Success {
|
||||
t.Fatalf("expected sql file create to succeed, got %#v", result)
|
||||
}
|
||||
|
||||
filePath := filepath.Join(root, "draft.sql")
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile returned error: %v", err)
|
||||
}
|
||||
if string(data) != "" {
|
||||
t.Fatalf("expected empty sql file, got %q", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSQLFileInDirectoryRejectsPathTraversalName(t *testing.T) {
|
||||
result := createSQLFileInDirectory(t.TempDir(), "../escape.sql")
|
||||
if result.Success {
|
||||
t.Fatalf("expected path traversal name to fail, got %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSQLDirectoryInDirectoryCreatesEmptyDirectory(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
|
||||
result := createSQLDirectoryInDirectory(root, "reports")
|
||||
if !result.Success {
|
||||
t.Fatalf("expected sql directory create to succeed, got %#v", result)
|
||||
}
|
||||
|
||||
target := filepath.Join(root, "reports")
|
||||
info, err := os.Stat(target)
|
||||
if err != nil {
|
||||
t.Fatalf("Stat returned error: %v", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
t.Fatalf("expected target to be a directory")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSQLDirectoryInDirectoryRejectsPathTraversalName(t *testing.T) {
|
||||
result := createSQLDirectoryInDirectory(t.TempDir(), "../escape")
|
||||
if result.Success {
|
||||
t.Fatalf("expected path traversal directory name to fail, got %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSQLFileByPathRemovesExistingSQLFile(t *testing.T) {
|
||||
filePath := filepath.Join(t.TempDir(), "old.sql")
|
||||
if err := os.WriteFile(filePath, []byte("select 1;"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile returned error: %v", err)
|
||||
}
|
||||
|
||||
result := deleteSQLFileByPath(filePath)
|
||||
if !result.Success {
|
||||
t.Fatalf("expected sql file delete to succeed, got %#v", result)
|
||||
}
|
||||
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected deleted sql file to be gone, stat err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSQLFileByPathRejectsNonSQLFile(t *testing.T) {
|
||||
filePath := filepath.Join(t.TempDir(), "notes.txt")
|
||||
if err := os.WriteFile(filePath, []byte("skip"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile returned error: %v", err)
|
||||
}
|
||||
|
||||
result := deleteSQLFileByPath(filePath)
|
||||
if result.Success {
|
||||
t.Fatalf("expected non sql file delete to fail, got %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSQLDirectoryByPathRemovesEmptyDirectory(t *testing.T) {
|
||||
directoryPath := filepath.Join(t.TempDir(), "old")
|
||||
if err := os.Mkdir(directoryPath, 0o755); err != nil {
|
||||
t.Fatalf("Mkdir returned error: %v", err)
|
||||
}
|
||||
|
||||
result := deleteSQLDirectoryByPath(directoryPath)
|
||||
if !result.Success {
|
||||
t.Fatalf("expected sql directory delete to succeed, got %#v", result)
|
||||
}
|
||||
if _, err := os.Stat(directoryPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected deleted sql directory to be gone, stat err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSQLDirectoryByPathRejectsNonEmptyDirectory(t *testing.T) {
|
||||
directoryPath := filepath.Join(t.TempDir(), "non-empty")
|
||||
if err := os.Mkdir(directoryPath, 0o755); err != nil {
|
||||
t.Fatalf("Mkdir returned error: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(directoryPath, "query.sql"), []byte("select 1;"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile returned error: %v", err)
|
||||
}
|
||||
|
||||
result := deleteSQLDirectoryByPath(directoryPath)
|
||||
if result.Success {
|
||||
t.Fatalf("expected non-empty sql directory delete to fail, got %#v", result)
|
||||
}
|
||||
if _, err := os.Stat(directoryPath); err != nil {
|
||||
t.Fatalf("expected non-empty sql directory to remain, stat err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameSQLFileByPathRenamesWithinSameDirectory(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
source := filepath.Join(root, "old.sql")
|
||||
if err := os.WriteFile(source, []byte("select 1;"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile returned error: %v", err)
|
||||
}
|
||||
|
||||
result := renameSQLFileByPath(source, "new-name")
|
||||
if !result.Success {
|
||||
t.Fatalf("expected sql file rename to succeed, got %#v", result)
|
||||
}
|
||||
target := filepath.Join(root, "new-name.sql")
|
||||
if _, err := os.Stat(target); err != nil {
|
||||
t.Fatalf("expected target sql file, got err=%v", err)
|
||||
}
|
||||
if _, err := os.Stat(source); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected source sql file to be gone, stat err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameSQLDirectoryByPathRenamesWithinSameParent(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
source := filepath.Join(root, "old")
|
||||
if err := os.Mkdir(source, 0o755); err != nil {
|
||||
t.Fatalf("Mkdir returned error: %v", err)
|
||||
}
|
||||
|
||||
result := renameSQLDirectoryByPath(source, "new-name")
|
||||
if !result.Success {
|
||||
t.Fatalf("expected sql directory rename to succeed, got %#v", result)
|
||||
}
|
||||
target := filepath.Join(root, "new-name")
|
||||
if info, err := os.Stat(target); err != nil || !info.IsDir() {
|
||||
t.Fatalf("expected target sql directory, got info=%#v err=%v", info, err)
|
||||
}
|
||||
if _, err := os.Stat(source); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected source sql directory to be gone, stat err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeSQLExportDefaultFilename(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -144,3 +301,29 @@ func TestReadSQLFileByPathReturnsLargeFileMetadata(t *testing.T) {
|
||||
t.Fatalf("expected filePath %q, got %#v", filePath, data["filePath"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadSQLFileWithMetadataByPathReturnsSmallFileContentAndPath(t *testing.T) {
|
||||
filePath := filepath.Join(t.TempDir(), "report.sql")
|
||||
if err := os.WriteFile(filePath, []byte("select 1;"), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile returned error: %v", err)
|
||||
}
|
||||
|
||||
result := readSQLFileWithMetadataByPath(filePath)
|
||||
if !result.Success {
|
||||
t.Fatalf("expected sql file read to succeed, got %#v", result)
|
||||
}
|
||||
|
||||
data, ok := result.Data.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("expected metadata map, got %#v", result.Data)
|
||||
}
|
||||
if data["content"] != "select 1;" {
|
||||
t.Fatalf("expected content, got %#v", data["content"])
|
||||
}
|
||||
if data["filePath"] != filePath {
|
||||
t.Fatalf("expected filePath %q, got %#v", filePath, data["filePath"])
|
||||
}
|
||||
if data["name"] != "report.sql" {
|
||||
t.Fatalf("expected name report.sql, got %#v", data["name"])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user