|
| 1 | +package component |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestValidateDynamicEntriesExeSQL(t *testing.T) { |
| 9 | + valid := dslWithComponents( |
| 10 | + componentDSL("ExeSQL", map[string]any{ |
| 11 | + "db_type": "mysql", |
| 12 | + "database": "orders", |
| 13 | + "username": "report", |
| 14 | + "host": "10.0.0.5", |
| 15 | + "port": 3306, |
| 16 | + "password": "secret", |
| 17 | + "max_records": 100, |
| 18 | + }), |
| 19 | + ) |
| 20 | + if err := ValidateDynamicEntries(valid); err != nil { |
| 21 | + t.Fatalf("valid ExeSQL DSL rejected: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + // db_type is optional: ExeSQLParam defaults it to "mysql". |
| 25 | + noDBType := dslWithComponents(componentDSL("ExeSQL", map[string]any{ |
| 26 | + "database": "orders", |
| 27 | + "username": "report", |
| 28 | + "host": "10.0.0.5", |
| 29 | + "password": "secret", |
| 30 | + })) |
| 31 | + if err := ValidateDynamicEntries(noDBType); err != nil { |
| 32 | + t.Fatalf("ExeSQL DSL without db_type rejected: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + // Trino connects without credentials. |
| 36 | + trino := dslWithComponents(componentDSL("ExeSQL", map[string]any{ |
| 37 | + "db_type": "trino", |
| 38 | + "database": "catalog", |
| 39 | + "username": "report", |
| 40 | + "host": "10.0.0.5", |
| 41 | + "password": "", |
| 42 | + })) |
| 43 | + if err := ValidateDynamicEntries(trino); err != nil { |
| 44 | + t.Fatalf("valid trino ExeSQL DSL rejected: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + tests := []struct { |
| 48 | + name string |
| 49 | + component map[string]any |
| 50 | + want string |
| 51 | + }{ |
| 52 | + { |
| 53 | + "unsupported db_type", |
| 54 | + componentDSL("ExeSQL", map[string]any{"db_type": "oracle", "database": "d", "username": "u", "host": "h", "password": "p"}), |
| 55 | + "Choose DB type oracle is not supported, it should be in", |
| 56 | + }, |
| 57 | + { |
| 58 | + "non-string db_type", |
| 59 | + componentDSL("ExeSQL", map[string]any{"db_type": 1, "database": "d", "username": "u", "host": "h", "password": "p"}), |
| 60 | + "Choose DB type 1 is not supported, it should be in", |
| 61 | + }, |
| 62 | + { |
| 63 | + "empty database", |
| 64 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "", "username": "u", "host": "h", "password": "p"}), |
| 65 | + "[ExeSQL] Database name does not support empty value", |
| 66 | + }, |
| 67 | + { |
| 68 | + "missing database", |
| 69 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "username": "u", "host": "h", "password": "p"}), |
| 70 | + "[ExeSQL] Database name does not support empty value", |
| 71 | + }, |
| 72 | + { |
| 73 | + "empty username", |
| 74 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "", "host": "h", "password": "p"}), |
| 75 | + "[ExeSQL] database username does not support empty value", |
| 76 | + }, |
| 77 | + { |
| 78 | + "empty host", |
| 79 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "u", "host": " ", "password": "p"}), |
| 80 | + "[ExeSQL] IP Address does not support empty value", |
| 81 | + }, |
| 82 | + { |
| 83 | + "missing password", |
| 84 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "u", "host": "h"}), |
| 85 | + "[ExeSQL] Database password does not support empty value", |
| 86 | + }, |
| 87 | + { |
| 88 | + "zero port", |
| 89 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "u", "host": "h", "password": "p", "port": 0}), |
| 90 | + "[ExeSQL] IP Port 0 not supported, should be positive integer", |
| 91 | + }, |
| 92 | + { |
| 93 | + "fractional port", |
| 94 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "u", "host": "h", "password": "p", "port": 3306.5}), |
| 95 | + "[ExeSQL] IP Port 3306.5 not supported, should be positive integer", |
| 96 | + }, |
| 97 | + { |
| 98 | + "negative max_records", |
| 99 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "u", "host": "h", "password": "p", "max_records": -1}), |
| 100 | + "[ExeSQL] Maximum number of records -1 not supported, should be positive integer", |
| 101 | + }, |
| 102 | + { |
| 103 | + "rag_flow database with default mysql host", |
| 104 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "rag_flow", "username": "u", "host": "ragflow-mysql", "password": "secret"}), |
| 105 | + "[ExeSQL] For the security reason, it does not support database named rag_flow.", |
| 106 | + }, |
| 107 | + { |
| 108 | + "rag_flow database with default mysql password", |
| 109 | + componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "rag_flow", "username": "u", "host": "10.0.0.5", "password": "infini_rag_flow"}), |
| 110 | + "[ExeSQL] For the security reason, it does not support database named rag_flow.", |
| 111 | + }, |
| 112 | + } |
| 113 | + for _, tt := range tests { |
| 114 | + t.Run(tt.name, func(t *testing.T) { |
| 115 | + err := ValidateDynamicEntries(dslWithComponents(tt.component)) |
| 116 | + if err == nil { |
| 117 | + t.Fatalf("expected error containing %q, got nil", tt.want) |
| 118 | + } |
| 119 | + if !strings.Contains(err.Error(), tt.want) { |
| 120 | + t.Fatalf("error %q does not contain %q", err.Error(), tt.want) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments