1
1
package chdb
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"path/filepath"
6
7
"testing"
7
8
)
8
9
9
- // TestNewSession tests the creation of a new session.
10
- func TestNewSession (t * testing.T ) {
11
- session , err := NewSession ()
10
+ var (
11
+ session * Session
12
+ )
13
+
14
+ func globalSetup () error {
15
+ sess , err := NewSession ()
12
16
if err != nil {
13
- t . Fatalf ( "Failed to create new session: %s" , err )
17
+ return err
14
18
}
15
- defer session .Cleanup ()
19
+ session = sess
20
+ return nil
21
+ }
22
+
23
+ func globalTeardown () {
24
+ session .Cleanup ()
25
+ session .Close ()
26
+ }
27
+
28
+ func TestMain (m * testing.M ) {
29
+ if err := globalSetup (); err != nil {
30
+ fmt .Println ("Global setup failed:" , err )
31
+ os .Exit (1 )
32
+ }
33
+ // Run all tests.
34
+ exitCode := m .Run ()
35
+
36
+ // Global teardown: clean up any resources here.
37
+ globalTeardown ()
38
+
39
+ // Exit with the code returned by m.Run().
40
+ os .Exit (exitCode )
41
+ }
42
+
43
+ // TestNewSession tests the creation of a new session.
44
+ func TestNewSession (t * testing.T ) {
16
45
17
46
// Check if the session directory exists
18
47
if _ , err := os .Stat (session .Path ()); os .IsNotExist (err ) {
@@ -25,59 +54,30 @@ func TestNewSession(t *testing.T) {
25
54
}
26
55
}
27
56
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
57
+ // This test is currently flaky because of this: https://github.com/chdb-io/chdb/pull/299/commits/91b0aedd8c17e74a4bb213e885d89cc9a77c99ad
58
+ func TestQuery (t * testing.T ) {
32
59
33
- // Close the session
34
- session .Close ()
60
+ // time.Sleep(time.Second * 5)
35
61
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 () )
62
+ _ , err := session . Query ( "CREATE TABLE IF NOT EXISTS TestQuery (id UInt32) ENGINE = MergeTree() ORDER BY id;" )
63
+ if err != nil {
64
+ t .Fatal ( err )
39
65
}
40
- }
41
66
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 ()
67
+ _ , err = session .Query ("INSERT INTO TestQuery VALUES (1), (2), (3);" )
68
+ if err != nil {
69
+ t .Fatal (err )
70
+ }
71
+ ret , err := session .Query ("SELECT * FROM TestQuery;" )
72
+ if err != nil {
73
+ t .Fatalf ("Query failed: %s" , err )
74
+ }
48
75
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 ())
76
+ if string (ret .Buf ()) != "1\n 2\n 3\n " {
77
+ t .Fatalf ("Query result should be 1\n 2\n 3\n , got %s" , string (ret .Buf ()))
52
78
}
53
79
}
54
80
55
- // This test is currently flaky because of this: https://github.com/chdb-io/chdb/pull/299/commits/91b0aedd8c17e74a4bb213e885d89cc9a77c99ad
56
- // func TestQuery(t *testing.T) {
57
-
58
- // session, _ := NewSession()
59
- // defer session.Cleanup()
60
- // // time.Sleep(time.Second * 5)
61
-
62
- // _, err := session.Query("CREATE TABLE IF NOT EXISTS TestQuery (id UInt32) ENGINE = MergeTree() ORDER BY id;")
63
- // if err != nil {
64
- // t.Fatal(err)
65
- // }
66
-
67
- // _, err = session.Query("INSERT INTO TestQuery VALUES (1), (2), (3);")
68
- // if err != nil {
69
- // t.Fatal(err)
70
- // }
71
- // ret, err := session.Query("SELECT * FROM TestQuery;")
72
- // if err != nil {
73
- // t.Fatalf("Query failed: %s", err)
74
- // }
75
-
76
- // if string(ret.Buf()) != "1\n2\n3\n" {
77
- // t.Fatalf("Query result should be 1\n2\n3\n, got %s", string(ret.Buf()))
78
- // }
79
- // }
80
-
81
81
func TestSessionPathAndIsTemp (t * testing.T ) {
82
82
// Create a new session and check its Path and IsTemp
83
83
session , _ := NewSession ()
@@ -105,3 +105,30 @@ func TestSessionPathAndIsTemp(t *testing.T) {
105
105
t .Errorf ("Session should not be temporary" )
106
106
}
107
107
}
108
+
109
+ // TestSessionClose tests the Close method of the session.
110
+ func TestSessionClose (t * testing.T ) {
111
+ session , _ := NewSession ()
112
+ defer session .Cleanup () // Cleanup in case Close fails
113
+
114
+ // Close the session
115
+ session .Close ()
116
+
117
+ // Check if the session directory has been removed
118
+ if _ , err := os .Stat (session .Path ()); ! os .IsNotExist (err ) {
119
+ t .Errorf ("Session directory should be removed after Close: %s" , session .Path ())
120
+ }
121
+ }
122
+
123
+ // TestSessionCleanup tests the Cleanup method of the session.
124
+ func TestSessionCleanup (t * testing.T ) {
125
+ session , _ := NewSession ()
126
+
127
+ // Cleanup the session
128
+ session .Cleanup ()
129
+
130
+ // Check if the session directory has been removed
131
+ if _ , err := os .Stat (session .Path ()); ! os .IsNotExist (err ) {
132
+ t .Errorf ("Session directory should be removed after Cleanup: %s" , session .Path ())
133
+ }
134
+ }
0 commit comments