|
| 1 | +package chdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// TestNewSession tests the creation of a new session. |
| 10 | +func TestNewSession(t *testing.T) { |
| 11 | + session, err := NewSession() |
| 12 | + if err != nil { |
| 13 | + t.Fatalf("Failed to create new session: %s", err) |
| 14 | + } |
| 15 | + defer session.Cleanup() |
| 16 | + |
| 17 | + // Check if the session directory exists |
| 18 | + if _, err := os.Stat(session.Path()); os.IsNotExist(err) { |
| 19 | + t.Errorf("Session directory does not exist: %s", session.Path()) |
| 20 | + } |
| 21 | + |
| 22 | + // Check if the session is temporary |
| 23 | + if !session.IsTemp() { |
| 24 | + t.Errorf("Expected session to be temporary") |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// TestSessionClose tests the Close method of the session. |
| 29 | +func TestSessionClose(t *testing.T) { |
| 30 | + session, _ := NewSession() |
| 31 | + defer session.Cleanup() // Cleanup in case Close fails |
| 32 | + |
| 33 | + // Close the session |
| 34 | + session.Close() |
| 35 | + |
| 36 | + // Check if the session directory has been removed |
| 37 | + if _, err := os.Stat(session.Path()); !os.IsNotExist(err) { |
| 38 | + t.Errorf("Session directory should be removed after Close: %s", session.Path()) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// TestSessionCleanup tests the Cleanup method of the session. |
| 43 | +func TestSessionCleanup(t *testing.T) { |
| 44 | + session, _ := NewSession() |
| 45 | + |
| 46 | + // Cleanup the session |
| 47 | + session.Cleanup() |
| 48 | + |
| 49 | + // Check if the session directory has been removed |
| 50 | + if _, err := os.Stat(session.Path()); !os.IsNotExist(err) { |
| 51 | + t.Errorf("Session directory should be removed after Cleanup: %s", session.Path()) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// TestQuery tests the Query method of the session. |
| 56 | +func TestQuery(t *testing.T) { |
| 57 | + path := filepath.Join(os.TempDir(), "chdb_test") |
| 58 | + defer os.RemoveAll(path) |
| 59 | + session, _ := NewSession(path) |
| 60 | + defer session.Cleanup() |
| 61 | + |
| 62 | + session.Query("CREATE DATABASE IF NOT EXISTS testdb; " + |
| 63 | + "CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;") |
| 64 | + |
| 65 | + session.Query("USE testdb; INSERT INTO testtable VALUES (1), (2), (3);") |
| 66 | + |
| 67 | + ret := session.Query("SELECT * FROM testtable;") |
| 68 | + if string(ret.Buf()) != "1\n2\n3\n" { |
| 69 | + t.Errorf("Query result should be 1\n2\n3\n, got %s", string(ret.Buf())) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestSessionPathAndIsTemp(t *testing.T) { |
| 74 | + // Create a new session and check its Path and IsTemp |
| 75 | + session, _ := NewSession() |
| 76 | + defer session.Cleanup() |
| 77 | + |
| 78 | + if session.Path() == "" { |
| 79 | + t.Errorf("Session path should not be empty") |
| 80 | + } |
| 81 | + |
| 82 | + if !session.IsTemp() { |
| 83 | + t.Errorf("Session should be temporary") |
| 84 | + } |
| 85 | + |
| 86 | + // Create a new session with a specific path and check its Path and IsTemp |
| 87 | + path := filepath.Join(os.TempDir(), "chdb_test2") |
| 88 | + defer os.RemoveAll(path) |
| 89 | + session, _ = NewSession(path) |
| 90 | + defer session.Cleanup() |
| 91 | + |
| 92 | + if session.Path() != path { |
| 93 | + t.Errorf("Session path should be %s, got %s", path, session.Path()) |
| 94 | + } |
| 95 | + |
| 96 | + if session.IsTemp() { |
| 97 | + t.Errorf("Session should not be temporary") |
| 98 | + } |
| 99 | +} |
0 commit comments