11import base64
22import logging
3+ import os
34from datetime import datetime , timezone
45from typing import Sequence , Tuple
56
2122from mcbackend .test_utils import CheckBehavior , CheckPerformance , make_runmeta
2223
2324try :
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 )
2530 client .execute ("SHOW DATABASES;" )
2631 HAS_REAL_DB = True
2732except :
@@ -51,7 +56,7 @@ def fully_initialized(
5156
5257@pytest .mark .skipif (
5358 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." ,
5560)
5661class TestClickHouseBackendInitialization :
5762 """This is separate because ``TestClickHouseBackend.setup_method`` depends on these things."""
@@ -63,12 +68,12 @@ def test_exceptions(self):
6368
6469 def test_backend_from_client_object (self ):
6570 db = "testing_" + hagelkorn .random ()
66- _client_main = clickhouse_driver .Client ("localhost" )
71+ _client_main = clickhouse_driver .Client (** DB_KWARGS )
6772 _client_main .execute (f"CREATE DATABASE { db } ;" )
6873
6974 try :
7075 # 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 ))
7277 assert callable (backend ._client_fn )
7378 run = backend .init_run (make_runmeta ())
7479 c1 = run .init_chain (0 )
@@ -81,11 +86,11 @@ def test_backend_from_client_object(self):
8186
8287 def test_backend_from_client_function (self ):
8388 db = "testing_" + hagelkorn .random ()
84- _client_main = clickhouse_driver .Client ("localhost" )
89+ _client_main = clickhouse_driver .Client (** DB_KWARGS )
8590 _client_main .execute (f"CREATE DATABASE { db } ;" )
8691
8792 def client_fn ():
88- return clickhouse_driver .Client ("localhost" , database = db )
93+ return clickhouse_driver .Client (** DB_KWARGS , database = db )
8994
9095 try :
9196 # When created from a client function, each chain has its own client
@@ -108,7 +113,7 @@ def client_fn():
108113
109114@pytest .mark .skipif (
110115 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." ,
112117)
113118class TestClickHouseBackend (CheckBehavior , CheckPerformance ):
114119 cls_backend = ClickHouseBackend
@@ -118,11 +123,11 @@ class TestClickHouseBackend(CheckBehavior, CheckPerformance):
118123 def setup_method (self , method ):
119124 """Initializes a fresh database just for this test method."""
120125 self ._db = "testing_" + hagelkorn .random ()
121- self ._client_main = clickhouse_driver .Client ("localhost" )
126+ self ._client_main = clickhouse_driver .Client (** DB_KWARGS )
122127 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 )
124129 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 )
126131 )
127132 return
128133
@@ -197,7 +202,7 @@ def test_create_chain_table(self):
197202 ("scalar" , "UInt16" ),
198203 ("1D" , "Array(Float32)" ),
199204 ("3D" , "Array(Array(Array(Float64)))" ),
200- ("__stat_accepted" , "UInt8 " ),
205+ ("__stat_accepted" , "Bool " ),
201206 ]
202207 pass
203208
@@ -266,6 +271,30 @@ def test_insert_draw(self):
266271 numpy .testing .assert_array_equal (v3 , draw ["v3" ])
267272 pass
268273
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+
269298 def test_get_row_at (self ):
270299 run , chains = fully_initialized (
271300 self .backend ,
0 commit comments