test.py 16.3 KB
Newer Older
A
alesapin 已提交
1 2 3 4 5
import pytest
import os

from helpers.cluster import ClickHouseCluster
from dictionary import Field, Row, Dictionary, DictionaryStructure, Layout
C
comunodi 已提交
6
from external_sources import SourceMySQL, SourceClickHouse, SourceFile, SourceExecutableCache, SourceExecutableHashed
7
from external_sources import SourceMongo, SourceHTTP, SourceHTTPS, SourceRedis
A
alesapin 已提交
8
import math
A
alesapin 已提交
9 10

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
11
dict_configs_path = os.path.join(SCRIPT_DIR, 'configs/dictionaries')
A
alesapin 已提交
12 13 14

FIELDS = {
    "simple": [
A
alesapin 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
        Field("KeyField", 'UInt64', is_key=True, default_value_for_get=9999999),
        Field("UInt8_", 'UInt8', default_value_for_get=55),
        Field("UInt16_", 'UInt16', default_value_for_get=66),
        Field("UInt32_", 'UInt32', default_value_for_get=77),
        Field("UInt64_", 'UInt64', default_value_for_get=88),
        Field("Int8_", 'Int8', default_value_for_get=-55),
        Field("Int16_", 'Int16', default_value_for_get=-66),
        Field("Int32_", 'Int32', default_value_for_get=-77),
        Field("Int64_", 'Int64', default_value_for_get=-88),
        Field("UUID_", 'UUID', default_value_for_get='550e8400-0000-0000-0000-000000000000'),
        Field("Date_", 'Date', default_value_for_get='2018-12-30'),
        Field("DateTime_", 'DateTime', default_value_for_get='2018-12-30 00:00:00'),
        Field("String_", 'String', default_value_for_get='hi'),
        Field("Float32_", 'Float32', default_value_for_get=555.11),
        Field("Float64_", 'Float64', default_value_for_get=777.11),
A
alesapin 已提交
30
        Field("ParentKeyField", "UInt64", default_value_for_get=444, hierarchical=True)
A
alesapin 已提交
31 32
    ],
    "complex": [
A
alesapin 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        Field("KeyField1", 'UInt64', is_key=True, default_value_for_get=9999999),
        Field("KeyField2", 'String', is_key=True, default_value_for_get='xxxxxxxxx'),
        Field("UInt8_", 'UInt8', default_value_for_get=55),
        Field("UInt16_", 'UInt16', default_value_for_get=66),
        Field("UInt32_", 'UInt32', default_value_for_get=77),
        Field("UInt64_", 'UInt64', default_value_for_get=88),
        Field("Int8_", 'Int8', default_value_for_get=-55),
        Field("Int16_", 'Int16', default_value_for_get=-66),
        Field("Int32_", 'Int32', default_value_for_get=-77),
        Field("Int64_", 'Int64', default_value_for_get=-88),
        Field("UUID_", 'UUID', default_value_for_get='550e8400-0000-0000-0000-000000000000'),
        Field("Date_", 'Date', default_value_for_get='2018-12-30'),
        Field("DateTime_", 'DateTime', default_value_for_get='2018-12-30 00:00:00'),
        Field("String_", 'String', default_value_for_get='hi'),
        Field("Float32_", 'Float32', default_value_for_get=555.11),
        Field("Float64_", 'Float64', default_value_for_get=777.11),
A
alesapin 已提交
49 50 51 52 53 54
    ],
    "ranged": [
        Field("KeyField1", 'UInt64', is_key=True),
        Field("KeyField2", 'Date', is_range_key=True),
        Field("StartDate", 'Date', range_hash_type='min'),
        Field("EndDate", 'Date', range_hash_type='max'),
A
alesapin 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
        Field("UInt8_", 'UInt8', default_value_for_get=55),
        Field("UInt16_", 'UInt16', default_value_for_get=66),
        Field("UInt32_", 'UInt32', default_value_for_get=77),
        Field("UInt64_", 'UInt64', default_value_for_get=88),
        Field("Int8_", 'Int8', default_value_for_get=-55),
        Field("Int16_", 'Int16', default_value_for_get=-66),
        Field("Int32_", 'Int32', default_value_for_get=-77),
        Field("Int64_", 'Int64', default_value_for_get=-88),
        Field("UUID_", 'UUID', default_value_for_get='550e8400-0000-0000-0000-000000000000'),
        Field("Date_", 'Date', default_value_for_get='2018-12-30'),
        Field("DateTime_", 'DateTime', default_value_for_get='2018-12-30 00:00:00'),
        Field("String_", 'String', default_value_for_get='hi'),
        Field("Float32_", 'Float32', default_value_for_get=555.11),
        Field("Float64_", 'Float64', default_value_for_get=777.11),
A
alesapin 已提交
69
    ]
70 71 72 73 74
}

VALUES = {
    "simple": [
        [1, 22, 333, 4444, 55555, -6, -77,
75 76
            -888, -999, '550e8400-e29b-41d4-a716-446655440003',
            '1973-06-28', '1985-02-28 23:43:25', 'hello', 22.543, 3332154213.4, 0],
77
        [2, 3, 4, 5, 6, -7, -8,
78 79
            -9, -10, '550e8400-e29b-41d4-a716-446655440002',
            '1978-06-28', '1986-02-28 23:42:25', 'hello', 21.543, 3222154213.4, 1]
80 81 82
    ],
    "complex": [
        [1, 'world', 22, 333, 4444, 55555, -6,
83 84 85
             -77, -888, -999, '550e8400-e29b-41d4-a716-446655440003',
             '1973-06-28', '1985-02-28 23:43:25',
             'hello', 22.543, 3332154213.4],
86
        [2, 'qwerty2', 52, 2345, 6544, 9191991, -2,
87 88 89
            -717, -81818, -92929, '550e8400-e29b-41d4-a716-446655440007',
            '1975-09-28', '2000-02-28 23:33:24',
            'my', 255.543, 3332221.44]
A
alesapin 已提交
90

91 92 93
    ],
    "ranged": [
        [1, '2019-02-10', '2019-02-01', '2019-02-28',
94 95 96 97
            22, 333, 4444, 55555, -6, -77, -888, -999,
            '550e8400-e29b-41d4-a716-446655440003',
            '1973-06-28', '1985-02-28 23:43:25', 'hello',
            22.543, 3332154213.4],
98
        [2, '2019-04-10', '2019-04-01', '2019-04-28',
99 100 101 102
            11, 3223, 41444, 52515, -65, -747, -8388, -9099,
            '550e8400-e29b-41d4-a716-446655440004',
            '1973-06-29', '2002-02-28 23:23:25', '!!!!',
            32.543, 3332543.4]
103
    ]
A
alesapin 已提交
104 105
}

106 107


A
alesapin 已提交
108 109
LAYOUTS = [
    Layout("hashed"),
A
alesapin 已提交
110
    Layout("cache"),
A
alesapin 已提交
111 112 113 114 115 116 117
    Layout("flat"),
    Layout("complex_key_hashed"),
    Layout("complex_key_cache"),
    Layout("range_hashed")
]

SOURCES = [
C
comunodi 已提交
118 119 120 121 122 123 124 125 126
    SourceMongo("MongoDB", "localhost", "27018", "mongo1", "27017", "root", "clickhouse"),
    SourceMySQL("MySQL", "localhost", "3308", "mysql1", "3306", "root", "clickhouse"),
    SourceClickHouse("RemoteClickHouse", "localhost", "9000", "clickhouse1", "9000", "default", ""),
    SourceClickHouse("LocalClickHouse", "localhost", "9000", "node", "9000", "default", ""),
    SourceFile("File", "localhost", "9000", "node", "9000", "", ""),
    SourceExecutableHashed("ExecutableHashed", "localhost", "9000", "node", "9000", "", ""),
    SourceExecutableCache("ExecutableCache", "localhost", "9000", "node", "9000", "", ""),
    SourceHTTP("SourceHTTP", "localhost", "9000", "clickhouse1", "9000", "", ""),
    SourceHTTPS("SourceHTTPS", "localhost", "9000", "clickhouse1", "9000", "", ""),
A
alesapin 已提交
127 128 129 130
]

DICTIONARIES = []

131 132 133 134 135 136 137 138
# Key-value dictionaries with onle one possible field for key
SOURCES_KV = [
    SourceRedis("RedisSimple", "localhost", "6380", "redis1", "6379", "", "", storage_type="simple"),
    SourceRedis("RedisHash", "localhost", "6380", "redis1", "6379", "", "", storage_type="hash_map"),
]

DICTIONARIES_KV = []

A
alesapin 已提交
139 140 141
cluster = None
node = None

142 143 144 145 146 147 148 149 150
def get_dict(source, layout, fields, suffix_name=''):
    global dict_configs_path

    structure = DictionaryStructure(layout, fields)
    dict_name = source.name + "_" + layout.name + '_' + suffix_name
    dict_path = os.path.join(dict_configs_path, dict_name + '.xml')
    dictionary = Dictionary(dict_name, structure, source, dict_path, "table_" + dict_name, fields)
    dictionary.generate_config()
    return dictionary
C
comunodi 已提交
151

A
alesapin 已提交
152 153 154 155
def setup_module(module):
    global DICTIONARIES
    global cluster
    global node
156
    global dict_configs_path
A
alesapin 已提交
157 158 159 160 161 162

    for f in os.listdir(dict_configs_path):
        os.remove(os.path.join(dict_configs_path, f))

    for layout in LAYOUTS:
        for source in SOURCES:
A
alesapin 已提交
163
            if source.compatible_with_layout(layout):
A
alesapin 已提交
164
                DICTIONARIES.append(get_dict(source, layout, FIELDS[layout.layout_type]))
A
alesapin 已提交
165 166
            else:
                print "Source", source.name, "incompatible with layout", layout.name
A
alesapin 已提交
167

168 169 170 171 172 173 174 175 176 177
    for layout in LAYOUTS:
        field_keys = list(filter(lambda x: x.is_key, FIELDS[layout.layout_type]))
        for source in SOURCES_KV:
            if not source.compatible_with_layout(layout):
                print "Source", source.name, "incompatible with layout", layout.name
                continue

            for field in FIELDS[layout.layout_type]:
                if not (field.is_key or field.is_range or field.is_range_key):
                    DICTIONARIES_KV.append(get_dict(source, layout, field_keys + [field], field.name))
A
alesapin 已提交
178 179 180 181 182

    main_configs = []
    for fname in os.listdir(dict_configs_path):
        main_configs.append(os.path.join(dict_configs_path, fname))
    cluster = ClickHouseCluster(__file__, base_configs_dir=os.path.join(SCRIPT_DIR, 'configs'))
183
    node = cluster.add_instance('node', main_configs=main_configs, with_mysql=True, with_mongo=True, with_redis=True)
184
    cluster.add_instance('clickhouse1')
A
alesapin 已提交
185

C
comunodi 已提交
186

A
alesapin 已提交
187 188 189 190
@pytest.fixture(scope="module")
def started_cluster():
    try:
        cluster.start()
191
        for dictionary in DICTIONARIES + DICTIONARIES_KV:
A
alesapin 已提交
192
            print "Preparing", dictionary.name
A
alesapin 已提交
193
            dictionary.prepare_source(cluster)
A
alesapin 已提交
194 195 196 197 198 199 200 201
            print "Prepared"

        yield cluster

    finally:
        cluster.shutdown()


A
alesapin 已提交
202 203 204 205 206 207 208 209 210
def get_dictionaries(fold, total_folds, all_dicts):
    chunk_len = int(math.ceil(len(all_dicts) / float(total_folds)))
    if chunk_len * fold >= len(all_dicts):
        return []
    return all_dicts[fold * chunk_len : (fold + 1) * chunk_len]


@pytest.mark.parametrize("fold", list(range(10)))
def test_simple_dictionaries(started_cluster, fold):
A
alesapin 已提交
211
    fields = FIELDS["simple"]
212 213
    values = VALUES["simple"]
    data = [Row(fields, vals) for vals in values]
A
alesapin 已提交
214

A
alesapin 已提交
215 216 217 218
    all_simple_dicts = [d for d in DICTIONARIES if d.structure.layout.layout_type == "simple"]
    simple_dicts = get_dictionaries(fold, 10, all_simple_dicts)

    print "Length of dicts:", len(simple_dicts)
A
alesapin 已提交
219 220 221 222
    for dct in simple_dicts:
        dct.load_data(data)

    node.query("system reload dictionaries")
A
alesapin 已提交
223

A
alesapin 已提交
224 225 226 227 228
    queries_with_answers = []
    for dct in simple_dicts:
        for row in data:
            for field in fields:
                if not field.is_key:
A
alesapin 已提交
229 230 231 232 233 234 235 236
                    for query in dct.get_select_get_queries(field, row):
                        queries_with_answers.append((query, row.get_value_by_name(field.name)))

                    for query in dct.get_select_has_queries(field, row):
                        queries_with_answers.append((query, 1))

                    for query in dct.get_select_get_or_default_queries(field, row):
                        queries_with_answers.append((query, field.default_value_for_get))
A
alesapin 已提交
237 238 239 240 241 242 243 244 245 246 247
        for query in dct.get_hierarchical_queries(data[0]):
            queries_with_answers.append((query, [1]))

        for query in dct.get_hierarchical_queries(data[1]):
            queries_with_answers.append((query, [2, 1]))

        for query in dct.get_is_in_queries(data[0], data[1]):
            queries_with_answers.append((query, 0))

        for query in dct.get_is_in_queries(data[1], data[0]):
            queries_with_answers.append((query, 1))
A
alesapin 已提交
248 249 250

    for query, answer in queries_with_answers:
        print query
A
alesapin 已提交
251 252
        if isinstance(answer, list):
            answer = str(answer).replace(' ', '')
A
alesapin 已提交
253 254
        assert node.query(query) == str(answer) + '\n'

C
comunodi 已提交
255

A
alesapin 已提交
256 257
def test_complex_dictionaries(started_cluster):
    fields = FIELDS["complex"]
258 259
    values = VALUES["complex"]
    data = [Row(fields, vals) for vals in values]
A
alesapin 已提交
260

C
comunodi 已提交
261
    complex_dicts = [d for d in DICTIONARIES if d.structure.layout.layout_type == "complex"]
A
alesapin 已提交
262 263 264 265
    for dct in complex_dicts:
        dct.load_data(data)

    node.query("system reload dictionaries")
A
alesapin 已提交
266

A
alesapin 已提交
267 268 269 270 271
    queries_with_answers = []
    for dct in complex_dicts:
        for row in data:
            for field in fields:
                if not field.is_key:
A
alesapin 已提交
272 273 274 275 276 277 278 279
                    for query in dct.get_select_get_queries(field, row):
                        queries_with_answers.append((query, row.get_value_by_name(field.name)))

                    for query in dct.get_select_has_queries(field, row):
                        queries_with_answers.append((query, 1))

                    for query in dct.get_select_get_or_default_queries(field, row):
                        queries_with_answers.append((query, field.default_value_for_get))
A
alesapin 已提交
280 281 282 283 284

    for query, answer in queries_with_answers:
        print query
        assert node.query(query) == str(answer) + '\n'

C
comunodi 已提交
285

A
alesapin 已提交
286 287
def test_ranged_dictionaries(started_cluster):
    fields = FIELDS["ranged"]
288 289
    values = VALUES["ranged"]
    data = [Row(fields, vals) for vals in values]
A
alesapin 已提交
290

C
comunodi 已提交
291
    ranged_dicts = [d for d in DICTIONARIES if d.structure.layout.layout_type == "ranged"]
A
alesapin 已提交
292 293 294 295 296 297 298 299 300 301
    for dct in ranged_dicts:
        dct.load_data(data)

    node.query("system reload dictionaries")

    queries_with_answers = []
    for dct in ranged_dicts:
        for row in data:
            for field in fields:
                if not field.is_key and not field.is_range:
A
alesapin 已提交
302 303
                    for query in dct.get_select_get_queries(field, row):
                        queries_with_answers.append((query, row.get_value_by_name(field.name)))
A
alesapin 已提交
304 305 306 307

    for query, answer in queries_with_answers:
        print query
        assert node.query(query) == str(answer) + '\n'
308 309


A
alesapin 已提交
310 311
@pytest.mark.parametrize("fold", list(range(10)))
def test_key_value_simple_dictionaries(started_cluster, fold):
312 313 314 315
    fields = FIELDS["simple"]
    values = VALUES["simple"]
    data = [Row(fields, vals) for vals in values]

A
alesapin 已提交
316 317
    all_simple_dicts = [d for d in DICTIONARIES_KV if d.structure.layout.layout_type == "simple"]
    simple_dicts = get_dictionaries(fold, 10, all_simple_dicts)
318 319 320 321 322 323 324 325

    for dct in simple_dicts:
        queries_with_answers = []
        local_data = []
        for row in data:
            local_fields = dct.get_fields()
            local_values = [row.get_value_by_name(field.name) for field in local_fields if row.has_field(field.name)]
            local_data.append(Row(local_fields, local_values))
A
alesapin 已提交
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
        dct.load_data(local_data)

        node.query("system reload dictionary {}".format(dct.name))

        print 'name: ', dct.name

        for row in local_data:
            print dct.get_fields()
            for field in dct.get_fields():
                print field.name, field.is_key
                if not field.is_key:
                    for query in dct.get_select_get_queries(field, row):
                        queries_with_answers.append((query, row.get_value_by_name(field.name)))

                    for query in dct.get_select_has_queries(field, row):
                        queries_with_answers.append((query, 1))

                    for query in dct.get_select_get_or_default_queries(field, row):
                        queries_with_answers.append((query, field.default_value_for_get))

        if dct.structure.has_hierarchy:
            for query in dct.get_hierarchical_queries(data[0]):
                queries_with_answers.append((query, [1]))

            for query in dct.get_hierarchical_queries(data[1]):
                queries_with_answers.append((query, [2, 1]))

            for query in dct.get_is_in_queries(data[0], data[1]):
                queries_with_answers.append((query, 0))

            for query in dct.get_is_in_queries(data[1], data[0]):
                queries_with_answers.append((query, 1))

        for query, answer in queries_with_answers:
            print query
            if isinstance(answer, list):
                answer = str(answer).replace(' ', '')
            assert node.query(query) == str(answer) + '\n'


def test_key_value_complex_dictionaries(started_cluster):
    fields = FIELDS["complex"]
    values = VALUES["complex"]
    data = [Row(fields, vals) for vals in values]

    complex_dicts = [d for d in DICTIONARIES if d.structure.layout.layout_type == "complex"]
    for dct in complex_dicts:
        dct.load_data(data)

    node.query("system reload dictionaries")

    for dct in complex_dicts:
        queries_with_answers = []
        local_data = []
        for row in data:
            local_fields = dct.get_fields()
            local_values = [row.get_value_by_name(field.name) for field in local_fields if row.has_field(field.name)]
            local_data.append(Row(local_fields, local_values))
A
alesapin 已提交
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
        dct.load_data(local_data)

        node.query("system reload dictionary {}".format(dct.name))

        for row in local_data:
            for field in dct.get_fields():
                if not field.is_key:
                    for query in dct.get_select_get_queries(field, row):
                        queries_with_answers.append((query, row.get_value_by_name(field.name)))

                    for query in dct.get_select_has_queries(field, row):
                        queries_with_answers.append((query, 1))

                    for query in dct.get_select_get_or_default_queries(field, row):
                        queries_with_answers.append((query, field.default_value_for_get))

    for query, answer in queries_with_answers:
        print query
        assert node.query(query) == str(answer) + '\n'