-
-
Notifications
You must be signed in to change notification settings - Fork 6
Add NewConnectionFromConnString and fix session issues #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
27ee3a9
Remove CGO
auxten 4140837
Fix connectChdb argv and add NewConnectionFromConnString
auxten 10a47fa
Use connect string from --path
auxten 807383e
Use NewConnectionFromConnString
auxten 7aac603
Debug test
auxten 3510647
Add test for NewConnection and NewConnectionFromConnString
auxten 8767563
Ignore *.db
auxten 7072640
Fix Cleanup not closing conn
auxten 7c508dc
Fix NewConnection
auxten 75387d2
Remove read only test
auxten 8eea3ff
Mark NewConnection deprecated
auxten 114587a
Remove debug log
auxten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package chdbpurego | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestNewConnection(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
argc int | ||
argv []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty args", | ||
argc: 0, | ||
argv: []string{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "memory database", | ||
argc: 1, | ||
argv: []string{":memory:"}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
conn, err := NewConnection(tt.argc, tt.argv) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NewConnection() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if conn == nil && !tt.wantErr { | ||
t.Error("NewConnection() returned nil connection without error") | ||
return | ||
} | ||
if conn != nil { | ||
defer conn.Close() | ||
if !conn.Ready() { | ||
t.Error("NewConnection() returned connection that is not ready") | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewConnectionFromConnString(t *testing.T) { | ||
// Create a temporary directory for testing | ||
tmpDir, err := os.MkdirTemp("", "chdb_test_*") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp dir: %v", err) | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
tests := []struct { | ||
name string | ||
connStr string | ||
wantErr bool | ||
checkPath bool | ||
}{ | ||
{ | ||
name: "empty string", | ||
connStr: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "memory database", | ||
connStr: ":memory:", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "memory database with params", | ||
connStr: ":memory:?verbose&log-level=test", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "relative path", | ||
connStr: "test.db", | ||
wantErr: false, | ||
checkPath: true, | ||
}, | ||
{ | ||
name: "file prefix", | ||
connStr: "file:test.db", | ||
wantErr: false, | ||
checkPath: true, | ||
}, | ||
{ | ||
name: "absolute path", | ||
connStr: filepath.Join(tmpDir, "test.db"), | ||
wantErr: false, | ||
checkPath: true, | ||
}, | ||
{ | ||
name: "file prefix with absolute path", | ||
connStr: "file:" + filepath.Join(tmpDir, "test.db"), | ||
wantErr: false, | ||
checkPath: true, | ||
}, | ||
// { | ||
// name: "readonly mode with existing dir", | ||
// connStr: filepath.Join(tmpDir, "readonly.db") + "?mode=ro", | ||
// wantErr: false, | ||
// checkPath: true, | ||
// }, | ||
// { | ||
// name: "readonly mode with non-existing dir", | ||
// connStr: filepath.Join(tmpDir, "new_readonly.db") + "?mode=ro", | ||
// wantErr: true, | ||
// checkPath: true, | ||
// }, | ||
{ | ||
name: "write mode with existing dir", | ||
connStr: filepath.Join(tmpDir, "write.db"), | ||
wantErr: false, | ||
checkPath: true, | ||
}, | ||
{ | ||
name: "write mode with non-existing dir", | ||
connStr: filepath.Join(tmpDir, "new_write.db"), | ||
wantErr: false, | ||
checkPath: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
conn, err := NewConnectionFromConnString(tt.connStr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NewConnectionFromConnString() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if conn == nil && !tt.wantErr { | ||
t.Error("NewConnectionFromConnString() returned nil connection without error") | ||
return | ||
} | ||
if conn != nil { | ||
defer conn.Close() | ||
if !conn.Ready() { | ||
t.Error("NewConnectionFromConnString() returned connection that is not ready") | ||
} | ||
|
||
// Test a simple query to verify the connection works | ||
result, err := conn.Query("SELECT 1", "CSV") | ||
if err != nil { | ||
t.Errorf("Query failed: %v", err) | ||
return | ||
} | ||
if result == nil { | ||
t.Error("Query returned nil result") | ||
return | ||
} | ||
if result.Error() != nil { | ||
t.Errorf("Query result has error: %v", result.Error()) | ||
return | ||
} | ||
if result.String() != "1\n" { | ||
t.Errorf("Query result = %v, want %v", result.String(), "1\n") | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.