test.py 11.1 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 7
from external_sources import SourceMySQL, SourceClickHouse, SourceFile, SourceExecutableCache, SourceExecutableHashed
from external_sources import SourceMongo, SourceHTTP, SourceHTTPS
A
alesapin 已提交
8 9 10 11 12

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))

FIELDS = {
    "simple": [
A
alesapin 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
        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 已提交
28
        Field("ParentKeyField", "UInt64", default_value_for_get=444, hierarchical=True)
A
alesapin 已提交
29 30
    ],
    "complex": [
A
alesapin 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        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 已提交
47 48 49 50 51 52
    ],
    "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 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66
        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 已提交
67 68 69 70 71 72
    ]

}

LAYOUTS = [
    Layout("hashed"),
A
alesapin 已提交
73
    Layout("cache"),
A
alesapin 已提交
74 75 76 77 78 79 80
    Layout("flat"),
    Layout("complex_key_hashed"),
    Layout("complex_key_cache"),
    Layout("range_hashed")
]

SOURCES = [
C
comunodi 已提交
81 82 83 84 85 86 87 88 89
    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 已提交
90 91 92 93 94 95 96
]

DICTIONARIES = []

cluster = None
node = None

C
comunodi 已提交
97

A
alesapin 已提交
98 99 100 101 102 103 104 105 106 107 108
def setup_module(module):
    global DICTIONARIES
    global cluster
    global node

    dict_configs_path = os.path.join(SCRIPT_DIR, 'configs/dictionaries')
    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 已提交
109
            if source.compatible_with_layout(layout):
C
comunodi 已提交
110
                structure = DictionaryStructure(layout, FIELDS[layout.layout_type])
A
alesapin 已提交
111
                dict_name = source.name + "_" + layout.name
C
comunodi 已提交
112
                dict_path = os.path.join(dict_configs_path, dict_name + '.xml')
A
alesapin 已提交
113 114 115 116 117
                dictionary = Dictionary(dict_name, structure, source, dict_path, "table_" + dict_name)
                dictionary.generate_config()
                DICTIONARIES.append(dictionary)
            else:
                print "Source", source.name, "incompatible with layout", layout.name
A
alesapin 已提交
118 119 120 121 122

    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'))
C
comunodi 已提交
123
    node = cluster.add_instance('node', main_configs=main_configs, with_mysql=True, with_mongo=True)
124
    cluster.add_instance('clickhouse1')
A
alesapin 已提交
125

C
comunodi 已提交
126

A
alesapin 已提交
127 128 129 130 131 132
@pytest.fixture(scope="module")
def started_cluster():
    try:
        cluster.start()
        for dictionary in DICTIONARIES:
            print "Preparing", dictionary.name
A
alesapin 已提交
133
            dictionary.prepare_source(cluster)
A
alesapin 已提交
134 135 136 137 138 139 140 141 142 143
            print "Prepared"

        yield cluster

    finally:
        cluster.shutdown()


def test_simple_dictionaries(started_cluster):
    fields = FIELDS["simple"]
C
comunodi 已提交
144 145 146 147 148 149 150 151 152
    data = [
        Row(fields,
            [1, 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, 0]),
        Row(fields,
            [2, 3, 4, 5, 6, -7, -8,
             -9, -10, '550e8400-e29b-41d4-a716-446655440002',
             '1978-06-28', '1986-02-28 23:42:25', 'hello', 21.543, 3222154213.4, 1]),
A
alesapin 已提交
153 154 155 156 157 158 159
    ]

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

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

A
alesapin 已提交
161 162 163 164 165
    queries_with_answers = []
    for dct in simple_dicts:
        for row in data:
            for field in fields:
                if not field.is_key:
A
alesapin 已提交
166 167 168 169 170 171 172 173
                    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 已提交
174 175 176 177 178 179 180 181 182 183 184
        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 已提交
185 186 187

    for query, answer in queries_with_answers:
        print query
A
alesapin 已提交
188 189
        if isinstance(answer, list):
            answer = str(answer).replace(' ', '')
A
alesapin 已提交
190 191
        assert node.query(query) == str(answer) + '\n'

C
comunodi 已提交
192

A
alesapin 已提交
193 194
def test_complex_dictionaries(started_cluster):
    fields = FIELDS["complex"]
C
comunodi 已提交
195 196 197 198 199 200 201 202 203 204 205
    data = [
        Row(fields,
            [1, 'world', 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]),
        Row(fields,
            [2, 'qwerty2', 52, 2345, 6544, 9191991, -2,
             -717, -81818, -92929, '550e8400-e29b-41d4-a716-446655440007',
             '1975-09-28', '2000-02-28 23:33:24',
             'my', 255.543, 3332221.44]),
A
alesapin 已提交
206 207
    ]

C
comunodi 已提交
208
    complex_dicts = [d for d in DICTIONARIES if d.structure.layout.layout_type == "complex"]
A
alesapin 已提交
209 210 211 212
    for dct in complex_dicts:
        dct.load_data(data)

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

A
alesapin 已提交
214 215 216 217 218
    queries_with_answers = []
    for dct in complex_dicts:
        for row in data:
            for field in fields:
                if not field.is_key:
A
alesapin 已提交
219 220 221 222 223 224 225 226
                    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 已提交
227 228 229 230 231

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

C
comunodi 已提交
232

A
alesapin 已提交
233 234
def test_ranged_dictionaries(started_cluster):
    fields = FIELDS["ranged"]
C
comunodi 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247
    data = [
        Row(fields,
            [1, '2019-02-10', '2019-02-01', '2019-02-28',
             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]),
        Row(fields,
            [2, '2019-04-10', '2019-04-01', '2019-04-28',
             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]),
A
alesapin 已提交
248 249
    ]

C
comunodi 已提交
250
    ranged_dicts = [d for d in DICTIONARIES if d.structure.layout.layout_type == "ranged"]
A
alesapin 已提交
251 252 253 254 255 256 257 258 259 260
    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 已提交
261 262
                    for query in dct.get_select_get_queries(field, row):
                        queries_with_answers.append((query, row.get_value_by_name(field.name)))
A
alesapin 已提交
263 264 265 266

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