|
| 1 | +package chdbpurego |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestNewConnection(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + argc int |
| 13 | + argv []string |
| 14 | + wantErr bool |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "empty args", |
| 18 | + argc: 0, |
| 19 | + argv: []string{}, |
| 20 | + wantErr: false, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "memory database", |
| 24 | + argc: 1, |
| 25 | + argv: []string{":memory:"}, |
| 26 | + wantErr: false, |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + for _, tt := range tests { |
| 31 | + t.Run(tt.name, func(t *testing.T) { |
| 32 | + conn, err := NewConnection(tt.argc, tt.argv) |
| 33 | + if (err != nil) != tt.wantErr { |
| 34 | + t.Errorf("NewConnection() error = %v, wantErr %v", err, tt.wantErr) |
| 35 | + return |
| 36 | + } |
| 37 | + if conn == nil && !tt.wantErr { |
| 38 | + t.Error("NewConnection() returned nil connection without error") |
| 39 | + return |
| 40 | + } |
| 41 | + if conn != nil { |
| 42 | + defer conn.Close() |
| 43 | + if !conn.Ready() { |
| 44 | + t.Error("NewConnection() returned connection that is not ready") |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestNewConnectionFromConnString(t *testing.T) { |
| 52 | + // Create a temporary directory for testing |
| 53 | + tmpDir, err := os.MkdirTemp("", "chdb_test_*") |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 56 | + } |
| 57 | + defer os.RemoveAll(tmpDir) |
| 58 | + |
| 59 | + tests := []struct { |
| 60 | + name string |
| 61 | + connStr string |
| 62 | + wantErr bool |
| 63 | + checkPath bool |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "empty string", |
| 67 | + connStr: "", |
| 68 | + wantErr: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "memory database", |
| 72 | + connStr: ":memory:", |
| 73 | + wantErr: false, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "memory database with params", |
| 77 | + connStr: ":memory:?verbose&log-level=test", |
| 78 | + wantErr: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "relative path", |
| 82 | + connStr: "test.db", |
| 83 | + wantErr: false, |
| 84 | + checkPath: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "file prefix", |
| 88 | + connStr: "file:test.db", |
| 89 | + wantErr: false, |
| 90 | + checkPath: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "absolute path", |
| 94 | + connStr: filepath.Join(tmpDir, "test.db"), |
| 95 | + wantErr: false, |
| 96 | + checkPath: true, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "file prefix with absolute path", |
| 100 | + connStr: "file:" + filepath.Join(tmpDir, "test.db"), |
| 101 | + wantErr: false, |
| 102 | + checkPath: true, |
| 103 | + }, |
| 104 | + // { |
| 105 | + // name: "readonly mode with existing dir", |
| 106 | + // connStr: filepath.Join(tmpDir, "readonly.db") + "?mode=ro", |
| 107 | + // wantErr: false, |
| 108 | + // checkPath: true, |
| 109 | + // }, |
| 110 | + // { |
| 111 | + // name: "readonly mode with non-existing dir", |
| 112 | + // connStr: filepath.Join(tmpDir, "new_readonly.db") + "?mode=ro", |
| 113 | + // wantErr: true, |
| 114 | + // checkPath: true, |
| 115 | + // }, |
| 116 | + { |
| 117 | + name: "write mode with existing dir", |
| 118 | + connStr: filepath.Join(tmpDir, "write.db"), |
| 119 | + wantErr: false, |
| 120 | + checkPath: true, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "write mode with non-existing dir", |
| 124 | + connStr: filepath.Join(tmpDir, "new_write.db"), |
| 125 | + wantErr: false, |
| 126 | + checkPath: true, |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + // Create a directory with read-only permissions for permission testing |
| 131 | + readOnlyDir := filepath.Join(tmpDir, "readonly_dir") |
| 132 | + if err := os.MkdirAll(readOnlyDir, 0555); err != nil { |
| 133 | + t.Fatalf("Failed to create read-only directory: %v", err) |
| 134 | + } |
| 135 | + tests = append(tests, struct { |
| 136 | + name string |
| 137 | + connStr string |
| 138 | + wantErr bool |
| 139 | + checkPath bool |
| 140 | + }{ |
| 141 | + name: "write mode with read-only dir", |
| 142 | + connStr: filepath.Join(readOnlyDir, "test.db"), |
| 143 | + wantErr: true, |
| 144 | + checkPath: true, |
| 145 | + }) |
| 146 | + |
| 147 | + for _, tt := range tests { |
| 148 | + t.Run(tt.name, func(t *testing.T) { |
| 149 | + conn, err := NewConnectionFromConnString(tt.connStr) |
| 150 | + if (err != nil) != tt.wantErr { |
| 151 | + t.Errorf("NewConnectionFromConnString() error = %v, wantErr %v", err, tt.wantErr) |
| 152 | + return |
| 153 | + } |
| 154 | + if conn == nil && !tt.wantErr { |
| 155 | + t.Error("NewConnectionFromConnString() returned nil connection without error") |
| 156 | + return |
| 157 | + } |
| 158 | + if conn != nil { |
| 159 | + defer conn.Close() |
| 160 | + if !conn.Ready() { |
| 161 | + t.Error("NewConnectionFromConnString() returned connection that is not ready") |
| 162 | + } |
| 163 | + |
| 164 | + // Test a simple query to verify the connection works |
| 165 | + result, err := conn.Query("SELECT 1", "CSV") |
| 166 | + if err != nil { |
| 167 | + t.Errorf("Query failed: %v", err) |
| 168 | + return |
| 169 | + } |
| 170 | + if result == nil { |
| 171 | + t.Error("Query returned nil result") |
| 172 | + return |
| 173 | + } |
| 174 | + if result.Error() != nil { |
| 175 | + t.Errorf("Query result has error: %v", result.Error()) |
| 176 | + return |
| 177 | + } |
| 178 | + if result.String() != "1\n" { |
| 179 | + t.Errorf("Query result = %v, want %v", result.String(), "1\n") |
| 180 | + } |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
0 commit comments