Skip to content

Commit 1ea27ac

Browse files
committed
feat(testing tool): add DEFINE/UNDEFINE statement support for ksqldb-testing-tool
1 parent 6aaee5f commit 1ea27ac

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

docs/how-to-guides/test-an-app.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ The following are the supported KSQL statements in `run-ksql-test`:
114114
- `UNSET`
115115
- `INSERT INTO`
116116
- `INSERT VALUES`
117+
- `DEFINE`
118+
- `UNDEFINE`
117119

118120
There are also four test only statements for verifying data.
119121

ksqldb-testing-tool/src/main/java/io/confluent/ksql/tools/test/SqlTestExecutor.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@
4646
import io.confluent.ksql.parser.tree.AssertValues;
4747
import io.confluent.ksql.parser.tree.CreateAsSelect;
4848
import io.confluent.ksql.parser.tree.CreateSource;
49+
import io.confluent.ksql.parser.tree.DefineVariable;
4950
import io.confluent.ksql.parser.tree.DropStatement;
5051
import io.confluent.ksql.parser.tree.InsertValues;
5152
import io.confluent.ksql.parser.tree.SetProperty;
53+
import io.confluent.ksql.parser.tree.UndefineVariable;
5254
import io.confluent.ksql.parser.tree.UnsetProperty;
5355
import io.confluent.ksql.properties.PropertyOverrider;
5456
import io.confluent.ksql.query.QueryId;
@@ -124,6 +126,7 @@ public class SqlTestExecutor implements Closeable {
124126
private KafkaTopicClient topicClient;
125127
private Path tmpFolder;
126128
private final Map<String, Object> overrides;
129+
private final Map<String, String> variables;
127130
private final Map<QueryId, DriverAndProperties> drivers;
128131

129132
// populated during execution to handle the expected exception
@@ -204,6 +207,7 @@ public void onDeregister(final QueryMetadata query) {
204207
this.formatInjector = new DefaultFormatInjector();
205208
this.topicClient = requireNonNull(topicClient, "topicClient");
206209
this.overrides = new HashMap<>();
210+
this.variables = new HashMap<>();
207211
this.drivers = drivers;
208212
this.tmpFolder = requireNonNull(tmpFolder, "tmpFolder");
209213
}
@@ -242,7 +246,7 @@ private void doAssert(final AssertStatement statement) {
242246
}
243247

244248
private void execute(final ParsedStatement parsedStatement) {
245-
final PreparedStatement<?> engineStatement = engine.prepare(parsedStatement);
249+
final PreparedStatement<?> engineStatement = engine.prepare(parsedStatement, variables);
246250
final ConfiguredStatement<?> configured = ConfiguredStatement
247251
.of(engineStatement, SessionConfig.of(config, overrides));
248252

@@ -257,6 +261,12 @@ private void execute(final ParsedStatement parsedStatement) {
257261
} else if (engineStatement.getStatement() instanceof UnsetProperty) {
258262
PropertyOverrider.unset((ConfiguredStatement<UnsetProperty>) configured, overrides);
259263
return;
264+
} else if (engineStatement.getStatement() instanceof DefineVariable variableStatement) {
265+
variables.put(variableStatement.getVariableName(), variableStatement.getVariableValue());
266+
return;
267+
} else if (engineStatement.getStatement() instanceof UndefineVariable variableStatement) {
268+
variables.remove(variableStatement.getVariableName());
269+
return;
260270
}
261271

262272
final ConfiguredStatement<?> injected = formatInjector.inject(configured);

ksqldb-testing-tool/src/test/resources/sql-test-runner/test1.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ CREATE OR REPLACE STREAM bar AS SELECT id, col1 + 1 as col1 FROM foo;
8585
INSERT INTO foo (rowtime, id, col1) VALUES (1, 1, 1);
8686
ASSERT VALUES bar (rowtime, id, col1) VALUES (1, 1, 2);
8787

88+
----------------------------------------------------------------------------------------------------
89+
--@test: basic test with `DEFINE` statement
90+
----------------------------------------------------------------------------------------------------
91+
DEFINE someVar='bar';
92+
CREATE STREAM foo (id INT KEY, col1 VARCHAR) WITH (kafka_topic='foo', value_format='JSON');
93+
94+
INSERT INTO foo (rowtime, id, col1) VALUES (1, 1, 'val-${someVar}');
95+
ASSERT VALUES foo (rowtime, id, col1) VALUES (1, 1, 'val-bar');
96+
97+
----------------------------------------------------------------------------------------------------
98+
--@test: basic test with `UNDEFINE` statement
99+
----------------------------------------------------------------------------------------------------
100+
DEFINE someVar='bar';
101+
UNDEFINE someVar;
102+
CREATE STREAM foo (id INT KEY, col1 VARCHAR) WITH (kafka_topic='foo', value_format='JSON');
103+
104+
INSERT INTO foo (rowtime, id, col1) VALUES (1, 1, 'val-${someVar}');
105+
ASSERT VALUES foo (rowtime, id, col1) VALUES (1, 1, 'val-');
106+
88107
----------------------------------------------------------------------------------------------------
89108
--@test: bad assert statement should fail
90109

0 commit comments

Comments
 (0)