forked from scylladb/scylla-cluster-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_uda.py
130 lines (115 loc) · 4.87 KB
/
test_uda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright (c) 2022 ScyllaDB
import tempfile
from pathlib import Path
from unittest import TestCase
import yaml
from sdcm.utils.uda import UDA, UDAS
from sdcm.utils.udf import UDF, UDFS
class TestUDA(TestCase):
UDFS = {
"accumulator": UDF(
name="my_acc",
args="(acc tuple<int, int>, val int)",
called_on_null_input_returns="NULL",
return_type="tuple<int, int>",
language="lua",
script="return {acc[1] + val, acc[2] + 1}"
),
"negator": UDF(
name="my_negator",
args="(acc bigint)",
called_on_null_input_returns="NULL",
return_type="bigint",
language="lua",
script="return -acc"
),
"reducer": UDF(
name="sum_reductor",
args="(acc1 bigint, acc2 bigint)",
called_on_null_input_returns="NULL",
return_type="bigint",
language="lua",
script="return acc1 + acc2"
)
}
def test_create_uda_instance(self):
new_uda = UDA(
name="my_uda",
args="int",
return_type="int",
accumulator_udf=self.UDFS["accumulator"],
reduce_udf=self.UDFS["reducer"],
final_udf=self.UDFS["negator"],
initial_condition="(0, 0)"
)
self.assertIsInstance(new_uda, UDA)
def test_get_create_query_string(self):
expected_create_query_string = "CREATE AGGREGATE testing.my_uda(int) " \
"SFUNC my_acc " \
"STYPE tuple<int, int> " \
"REDUCEFUNC sum_reductor " \
"FINALFUNC my_negator " \
"INITCOND (0, 0);"
new_uda = UDA(
name="my_uda",
args="int",
return_type="int",
accumulator_udf=self.UDFS["accumulator"],
reduce_udf=self.UDFS["reducer"],
final_udf=self.UDFS["negator"],
initial_condition="(0, 0)"
)
actual_create_query_string = new_uda.get_create_query_string(ks="testing")
self.assertEqual(actual_create_query_string, expected_create_query_string)
def test_load_uda_from_yaml(self):
expected_create_string = "CREATE AGGREGATE testing.my_uda(int) " \
"SFUNC wasm_plus " \
"STYPE int " \
"REDUCEFUNC wasm_plus " \
"FINALFUNC wasm_simple_return_int " \
"INITCOND (0, 0);"
data = {
"name": "my_uda",
"args": "int",
"return_type": "int",
"accumulator_udf_name": "wasm_plus",
"reduce_udf_name": "wasm_plus",
"final_udf_name": "wasm_simple_return_int",
"initial_condition": "(0, 0)"
}
with tempfile.TemporaryDirectory() as tempdir:
temp_yaml_path = Path(tempdir) / "new.yaml"
with temp_yaml_path.open(mode="w") as outfile:
yaml.safe_dump(data=data, stream=outfile)
uda = UDA.from_yaml(uda_yaml_file_path=str(temp_yaml_path))
create_string = uda.get_create_query_string(ks="testing")
self.assertIsInstance(uda, UDA)
self.assertEqual(expected_create_string, create_string)
self.assertEqual(uda.args, data["args"])
self.assertEqual(uda.return_type, data["return_type"])
self.assertIsInstance(uda.accumulator_udf, UDF)
self.assertEqual(uda.accumulator_udf.name, data["accumulator_udf_name"])
self.assertIsInstance(uda.reduce_udf, UDF)
self.assertEqual(uda.reduce_udf.name, data["reduce_udf_name"])
self.assertIsInstance(uda.final_udf, UDF)
self.assertEqual(uda.final_udf.name, data["final_udf_name"])
self.assertEqual(uda.initial_condition, data["initial_condition"])
def test_load_all_udas(self):
self.assertGreater(len(UDFS.keys()), 1, "UDF count was not greater than 1.")
for uda in UDAS.values():
self.assertTrue(uda.name)
self.assertTrue(uda.args)
self.assertTrue(uda.return_type)
self.assertIsInstance(uda.accumulator_udf, UDF)
self.assertIsInstance(uda.final_udf, UDF)