|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | + |
| 4 | +""" |
| 5 | +Streaming SQL DDL tests: CREATE TABLE, DROP TABLE, SHOW TABLES, |
| 6 | +SHOW CREATE TABLE, CREATE STREAMING TABLE. |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +class TestShowTables: |
| 11 | + |
| 12 | + def test_show_tables_empty(self, fs_instance): |
| 13 | + """SHOW TABLES returns success on a fresh instance.""" |
| 14 | + fs_instance.start() |
| 15 | + resp = fs_instance.execute_sql("SHOW TABLES") |
| 16 | + assert resp.status_code < 400 |
| 17 | + |
| 18 | + def test_show_streaming_tables_empty(self, fs_instance): |
| 19 | + """SHOW STREAMING TABLES returns success on a fresh instance.""" |
| 20 | + fs_instance.start() |
| 21 | + resp = fs_instance.execute_sql("SHOW STREAMING TABLES") |
| 22 | + assert resp.status_code < 400 |
| 23 | + |
| 24 | + |
| 25 | +class TestCreateTable: |
| 26 | + |
| 27 | + def test_create_source_table(self, fs_instance): |
| 28 | + """CREATE TABLE with connector options registers a source table.""" |
| 29 | + fs_instance.start() |
| 30 | + |
| 31 | + create_sql = """ |
| 32 | + CREATE TABLE test_source ( |
| 33 | + id BIGINT, |
| 34 | + name VARCHAR, |
| 35 | + event_time TIMESTAMP, |
| 36 | + WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND |
| 37 | + ) WITH ( |
| 38 | + 'connector' = 'kafka', |
| 39 | + 'topic' = 'test-topic', |
| 40 | + 'bootstrap.servers' = 'localhost:9092', |
| 41 | + 'format' = 'json' |
| 42 | + ) |
| 43 | + """ |
| 44 | + resp = fs_instance.execute_sql(create_sql) |
| 45 | + assert resp.status_code < 400 |
| 46 | + |
| 47 | + show_resp = fs_instance.execute_sql("SHOW TABLES") |
| 48 | + assert show_resp.status_code < 400 |
| 49 | + |
| 50 | + def test_create_duplicate_table_fails(self, fs_instance): |
| 51 | + """Creating the same table twice should return an error.""" |
| 52 | + fs_instance.start() |
| 53 | + |
| 54 | + create_sql = """ |
| 55 | + CREATE TABLE dup_table ( |
| 56 | + id BIGINT |
| 57 | + ) WITH ( |
| 58 | + 'connector' = 'kafka', |
| 59 | + 'topic' = 'dup-topic', |
| 60 | + 'bootstrap.servers' = 'localhost:9092', |
| 61 | + 'format' = 'json' |
| 62 | + ) |
| 63 | + """ |
| 64 | + resp1 = fs_instance.execute_sql(create_sql) |
| 65 | + assert resp1.status_code < 400 |
| 66 | + |
| 67 | + resp2 = fs_instance.execute_sql(create_sql) |
| 68 | + assert resp2.status_code >= 400 |
| 69 | + |
| 70 | + |
| 71 | +class TestDropTable: |
| 72 | + |
| 73 | + def test_drop_existing_table(self, fs_instance): |
| 74 | + """DROP TABLE removes a previously created table.""" |
| 75 | + fs_instance.start() |
| 76 | + |
| 77 | + fs_instance.execute_sql(""" |
| 78 | + CREATE TABLE to_drop ( |
| 79 | + id BIGINT |
| 80 | + ) WITH ( |
| 81 | + 'connector' = 'kafka', |
| 82 | + 'topic' = 'drop-topic', |
| 83 | + 'bootstrap.servers' = 'localhost:9092', |
| 84 | + 'format' = 'json' |
| 85 | + ) |
| 86 | + """) |
| 87 | + |
| 88 | + resp = fs_instance.execute_sql("DROP TABLE to_drop") |
| 89 | + assert resp.status_code < 400 |
| 90 | + |
| 91 | + def test_drop_nonexistent_table_fails(self, fs_instance): |
| 92 | + """DROP TABLE on a non-existent table returns an error.""" |
| 93 | + fs_instance.start() |
| 94 | + resp = fs_instance.execute_sql("DROP TABLE no_such_table") |
| 95 | + assert resp.status_code >= 400 |
| 96 | + |
| 97 | + def test_drop_if_exists_nonexistent_succeeds(self, fs_instance): |
| 98 | + """DROP TABLE IF EXISTS on a non-existent table should succeed.""" |
| 99 | + fs_instance.start() |
| 100 | + resp = fs_instance.execute_sql("DROP TABLE IF EXISTS no_such_table") |
| 101 | + assert resp.status_code < 400 |
| 102 | + |
| 103 | + |
| 104 | +class TestShowCreateTable: |
| 105 | + |
| 106 | + def test_show_create_table(self, fs_instance): |
| 107 | + """SHOW CREATE TABLE returns DDL for an existing table.""" |
| 108 | + fs_instance.start() |
| 109 | + |
| 110 | + fs_instance.execute_sql(""" |
| 111 | + CREATE TABLE show_me ( |
| 112 | + id BIGINT, |
| 113 | + value VARCHAR |
| 114 | + ) WITH ( |
| 115 | + 'connector' = 'kafka', |
| 116 | + 'topic' = 'show-topic', |
| 117 | + 'bootstrap.servers' = 'localhost:9092', |
| 118 | + 'format' = 'json' |
| 119 | + ) |
| 120 | + """) |
| 121 | + |
| 122 | + resp = fs_instance.execute_sql("SHOW CREATE TABLE show_me") |
| 123 | + assert resp.status_code < 400 |
| 124 | + |
| 125 | + def test_show_create_nonexistent_fails(self, fs_instance): |
| 126 | + """SHOW CREATE TABLE on a missing table returns an error.""" |
| 127 | + fs_instance.start() |
| 128 | + resp = fs_instance.execute_sql("SHOW CREATE TABLE ghost_table") |
| 129 | + assert resp.status_code >= 400 |
| 130 | + |
| 131 | + |
| 132 | +class TestSqlErrorHandling: |
| 133 | + |
| 134 | + def test_invalid_sql_syntax(self, fs_instance): |
| 135 | + """Malformed SQL returns an error status.""" |
| 136 | + fs_instance.start() |
| 137 | + resp = fs_instance.execute_sql("NOT VALID SQL AT ALL") |
| 138 | + assert resp.status_code >= 400 |
| 139 | + |
| 140 | + def test_empty_sql(self, fs_instance): |
| 141 | + """Empty SQL string returns an error status.""" |
| 142 | + fs_instance.start() |
| 143 | + resp = fs_instance.execute_sql("") |
| 144 | + assert resp.status_code >= 400 |
0 commit comments