1
1
import base64
2
2
import logging
3
+ import os
3
4
from datetime import datetime , timezone
4
5
from typing import Sequence , Tuple
5
6
21
22
from mcbackend .test_utils import CheckBehavior , CheckPerformance , make_runmeta
22
23
23
24
try :
24
- client = clickhouse_driver .Client ("localhost" )
25
+ DB_HOST = os .environ .get ("CLICKHOUSE_HOST" , "localhost" )
26
+ DB_PASS = os .environ .get ("CLICKHOUSE_PASS" , "" )
27
+ DB_PORT = os .environ .get ("CLICKHOUSE_PORT" , 9000 )
28
+ DB_KWARGS = dict (host = DB_HOST , port = DB_PORT , password = DB_PASS )
29
+ client = clickhouse_driver .Client (** DB_KWARGS )
25
30
client .execute ("SHOW DATABASES;" )
26
31
HAS_REAL_DB = True
27
32
except :
@@ -51,7 +56,7 @@ def fully_initialized(
51
56
52
57
@pytest .mark .skipif (
53
58
condition = not HAS_REAL_DB ,
54
- reason = "Integration tests need a ClickHouse server on localhost:9000 without authentication ." ,
59
+ reason = "Integration tests need a ClickHouse server." ,
55
60
)
56
61
class TestClickHouseBackendInitialization :
57
62
"""This is separate because ``TestClickHouseBackend.setup_method`` depends on these things."""
@@ -63,12 +68,12 @@ def test_exceptions(self):
63
68
64
69
def test_backend_from_client_object (self ):
65
70
db = "testing_" + hagelkorn .random ()
66
- _client_main = clickhouse_driver .Client ("localhost" )
71
+ _client_main = clickhouse_driver .Client (** DB_KWARGS )
67
72
_client_main .execute (f"CREATE DATABASE { db } ;" )
68
73
69
74
try :
70
75
# When created from a client object, all chains share the client
71
- backend = ClickHouseBackend (client = clickhouse_driver .Client ("localhost" , database = db ))
76
+ backend = ClickHouseBackend (client = clickhouse_driver .Client (** DB_KWARGS , database = db ))
72
77
assert callable (backend ._client_fn )
73
78
run = backend .init_run (make_runmeta ())
74
79
c1 = run .init_chain (0 )
@@ -81,11 +86,11 @@ def test_backend_from_client_object(self):
81
86
82
87
def test_backend_from_client_function (self ):
83
88
db = "testing_" + hagelkorn .random ()
84
- _client_main = clickhouse_driver .Client ("localhost" )
89
+ _client_main = clickhouse_driver .Client (** DB_KWARGS )
85
90
_client_main .execute (f"CREATE DATABASE { db } ;" )
86
91
87
92
def client_fn ():
88
- return clickhouse_driver .Client ("localhost" , database = db )
93
+ return clickhouse_driver .Client (** DB_KWARGS , database = db )
89
94
90
95
try :
91
96
# When created from a client function, each chain has its own client
@@ -108,7 +113,7 @@ def client_fn():
108
113
109
114
@pytest .mark .skipif (
110
115
condition = not HAS_REAL_DB ,
111
- reason = "Integration tests need a ClickHouse server on localhost:9000 without authentication ." ,
116
+ reason = "Integration tests need a ClickHouse server." ,
112
117
)
113
118
class TestClickHouseBackend (CheckBehavior , CheckPerformance ):
114
119
cls_backend = ClickHouseBackend
@@ -118,11 +123,11 @@ class TestClickHouseBackend(CheckBehavior, CheckPerformance):
118
123
def setup_method (self , method ):
119
124
"""Initializes a fresh database just for this test method."""
120
125
self ._db = "testing_" + hagelkorn .random ()
121
- self ._client_main = clickhouse_driver .Client ("localhost" )
126
+ self ._client_main = clickhouse_driver .Client (** DB_KWARGS )
122
127
self ._client_main .execute (f"CREATE DATABASE { self ._db } ;" )
123
- self ._client = clickhouse_driver .Client ("localhost" , database = self ._db )
128
+ self ._client = clickhouse_driver .Client (** DB_KWARGS , database = self ._db )
124
129
self .backend = ClickHouseBackend (
125
- client_fn = lambda : clickhouse_driver .Client ("localhost" , database = self ._db )
130
+ client_fn = lambda : clickhouse_driver .Client (** DB_KWARGS , database = self ._db )
126
131
)
127
132
return
128
133
@@ -197,7 +202,7 @@ def test_create_chain_table(self):
197
202
("scalar" , "UInt16" ),
198
203
("1D" , "Array(Float32)" ),
199
204
("3D" , "Array(Array(Array(Float64)))" ),
200
- ("__stat_accepted" , "UInt8 " ),
205
+ ("__stat_accepted" , "Bool " ),
201
206
]
202
207
pass
203
208
@@ -266,6 +271,30 @@ def test_insert_draw(self):
266
271
numpy .testing .assert_array_equal (v3 , draw ["v3" ])
267
272
pass
268
273
274
+ @pytest .mark .xfail (
275
+ reason = "Batch inserting assumes identical dict composition every time. See #74."
276
+ )
277
+ def test_insert_flaky_stats (self ):
278
+ """Tries to append stats that only sometimes have an entry for a stat."""
279
+ run , chains = fully_initialized (
280
+ self .backend ,
281
+ RunMeta (
282
+ sample_stats = [
283
+ Variable ("always" , "int8" ),
284
+ Variable ("sometimes" , "bool" ),
285
+ ]
286
+ ),
287
+ )
288
+
289
+ chain = chains [0 ]
290
+ chain .append ({}, dict (always = 1 , sometimes = True ))
291
+ chain .append ({}, dict (always = 2 ))
292
+ chain ._commit ()
293
+
294
+ tuple (chain .get_stats ("always" )) == (1 , 2 )
295
+ assert tuple (chain .get_stats ("sometimes" )) == (True , None )
296
+ pass
297
+
269
298
def test_get_row_at (self ):
270
299
run , chains = fully_initialized (
271
300
self .backend ,
0 commit comments