+
+
+class TestSetupSQL:
+
+ @pytest.fixture(autouse=True)
+ def init_env(self, temp_db, tmp_path, def_config, sql_preprocessor_cfg):
+ def_config.lib_dir.sql = tmp_path / 'sql'
+ def_config.lib_dir.sql.mkdir()
+
+ self.config = def_config
+
+
+ def write_sql(self, fname, content):
+ (self.config.lib_dir.sql / fname).write_text(content)
+
+
+ @pytest.mark.parametrize("reverse", [True, False])
+ def test_create_tables(self, temp_db_conn, temp_db_cursor, reverse):
+ self.write_sql('tables.sql',
+ """CREATE FUNCTION test() RETURNS bool
+ AS $$ SELECT {{db.reverse_only}} $$ LANGUAGE SQL""")
+
+ database_import.create_tables(temp_db_conn, self.config, reverse)
+
+ temp_db_cursor.scalar('SELECT test()') == reverse
+
+
+ def test_create_table_triggers(self, temp_db_conn, temp_db_cursor):
+ self.write_sql('table-triggers.sql',
+ """CREATE FUNCTION test() RETURNS TEXT
+ AS $$ SELECT 'a'::text $$ LANGUAGE SQL""")
+
+ database_import.create_table_triggers(temp_db_conn, self.config)
+
+ temp_db_cursor.scalar('SELECT test()') == 'a'
+
+
+ def test_create_partition_tables(self, temp_db_conn, temp_db_cursor):
+ self.write_sql('partition-tables.src.sql',
+ """CREATE FUNCTION test() RETURNS TEXT
+ AS $$ SELECT 'b'::text $$ LANGUAGE SQL""")
+
+ database_import.create_partition_tables(temp_db_conn, self.config)
+
+ temp_db_cursor.scalar('SELECT test()') == 'b'
+
+
+ @pytest.mark.parametrize("drop", [True, False])
+ @pytest.mark.asyncio
+ async def test_create_search_indices(self, temp_db_conn, temp_db_cursor, drop):
+ self.write_sql('indices.sql',
+ """CREATE FUNCTION test() RETURNS bool
+ AS $$ SELECT {{drop}} $$ LANGUAGE SQL""")
+
+ await database_import.create_search_indices(temp_db_conn, self.config, drop)
+
+ temp_db_cursor.scalar('SELECT test()') == drop