|
| 1 | +import base64 |
1 | 2 | import datetime |
2 | 3 | import json |
3 | 4 | import uuid |
@@ -1227,3 +1228,47 @@ def test_password_masking(started_cluster): |
1227 | 1228 | == "CREATE DICTIONARY default.mongodb_dictionary_password_masking (`_id` String) PRIMARY KEY _id SOURCE(MONGODB(HOST \\'127.0.0.1\\' PORT 27017 USER \\'testuser\\' PASSWORD \\'[HIDDEN]\\' DB \\'example\\' COLLECTION \\'test_clickhouse\\' OPTIONS \\'ssl=true\\')) LIFETIME(MIN 0 MAX 0) LAYOUT(FLAT())\n" |
1228 | 1229 | ) |
1229 | 1230 | node.query("DROP DICTIONARY IF EXISTS mongodb_dictionary_password_masking;") |
| 1231 | + |
| 1232 | + |
| 1233 | +def test_json_serialization(started_cluster): |
| 1234 | + mongo_connection = get_mongo_connection(started_cluster) |
| 1235 | + db = mongo_connection["test"] |
| 1236 | + db.command("dropAllUsersFromDatabase") |
| 1237 | + db.command("createUser", "root", pwd=mongo_pass, roles=["readWrite"]) |
| 1238 | + json_serialization_table = db["json_serialization_table"] |
| 1239 | + |
| 1240 | + date = datetime.datetime.strptime("2025-05-17 13:14:15", "%Y-%m-%d %H:%M:%S") |
| 1241 | + |
| 1242 | + def create_dataset(mongo, level) -> dict: |
| 1243 | + return { |
| 1244 | + "type_string": "Type string", |
| 1245 | + "type_oid": bson.ObjectId("60f7e65e16b1c1d1c8a2b6b3") if mongo else "60f7e65e16b1c1d1c8a2b6b3", |
| 1246 | + "type_binary": bson.Binary(b"binarydata", subtype=0) if mongo else base64.b64encode(b"binarydata").decode(), |
| 1247 | + "type_bool": True, |
| 1248 | + "type_int32": 123, |
| 1249 | + "type_int64": bson.int64.Int64(2**63 - 1) if mongo else int(2**63 - 1), |
| 1250 | + "type_double": float(3.141592653589793238), |
| 1251 | + "type_date": date if mongo else date.strftime("%Y-%m-%d %H:%M:%S"), |
| 1252 | + "type_timestamp": bson.timestamp.Timestamp(date, 1) if mongo else date.strftime("%Y-%m-%d %H:%M:%S"), |
| 1253 | + "type_document": {"nested_doc": create_dataset(mongo, level - 1)} if level > 0 else {}, |
| 1254 | + "type_array": [create_dataset(mongo, level - 1)] if level > 0 else [], |
| 1255 | + "type_regex": bson.regex.Regex(r"^pattern.*$", "i") if mongo else {"^pattern.*$": "i"}, |
| 1256 | + "type_null": None, |
| 1257 | + } |
| 1258 | + |
| 1259 | + json_serialization_table.insert_one({"dataset": create_dataset(True, 10)}) |
| 1260 | + node = started_cluster.instances["node"] |
| 1261 | + node.query( |
| 1262 | + f""" |
| 1263 | + CREATE OR REPLACE TABLE json_serialization_table( |
| 1264 | + dataset String |
| 1265 | + ) ENGINE = MongoDB('mongo1:27017', 'test', 'json_serialization_table', 'root', '{mongo_pass}') |
| 1266 | + """ |
| 1267 | + ) |
| 1268 | + |
| 1269 | + assert node.query(f"SELECT COUNT() FROM json_serialization_table") == "1\n" |
| 1270 | + assert (node.query(f"SELECT dataset FROM json_serialization_table")[:-1] |
| 1271 | + == json.dumps(create_dataset(False, 10), separators=(',', ':'))) |
| 1272 | + |
| 1273 | + node.query("DROP TABLE json_serialization_table") |
| 1274 | + json_serialization_table.drop() |
0 commit comments