def test_non_schema_relevant_query(
        self,
        producer,
        test_position,
        save_position,
        external_patches,
        schema_event_handler,
        mock_schema_tracker_cursor,
        non_schema_relevant_query_event,
        mock_dump_handler
    ):
        schema_event_handler.handle_event(non_schema_relevant_query_event, test_position)
        assert external_patches.execute_query.call_count == 1

        if 'CREATE DATABASE' in non_schema_relevant_query_event.query:
            expected_schema = None
        else:
            expected_schema = non_schema_relevant_query_event.schema

        assert external_patches.execute_query.call_args_list == [
            mock.call(
                query=non_schema_relevant_query_event.query,
                database_name=expected_schema
            )
        ]
        # We should flush and save state before
        assert producer.flush.call_count == 1
        assert save_position.call_count == 1
        # And after
        assert external_patches.upsert_global_event_state.call_count == 1
Esempio n. 2
0
    def test_handle_event_rename_table(self, producer, test_position,
                                       save_position, external_patches,
                                       rename_table_schema_event,
                                       schema_wrapper_mock,
                                       mock_db_connections, stats_counter,
                                       mock_create_dump, mock_persist_dump):
        schema_event_handler = SchemaEventHandler(
            db_connections=mock_db_connections,
            producer=producer,
            schema_wrapper=schema_wrapper_mock,
            stats_counter=stats_counter,
            register_dry_run=False,
        )
        schema_event_handler.handle_event(rename_table_schema_event,
                                          test_position)

        assert producer.flush.call_count == 1
        assert save_position.call_count == 1

        assert schema_wrapper_mock.reset_cache.call_count == 1

        assert external_patches.execute_query.call_count == 1
        assert external_patches.execute_query.call_args_list == [
            mock.call(
                query=rename_table_schema_event.query,
                database_name=rename_table_schema_event.schema,
            )
        ]

        assert external_patches.upsert_global_event_state.call_count == 1
    def test_handle_event_rename_table(
        self,
        producer,
        test_position,
        save_position,
        external_patches,
        rename_table_schema_event,
        schema_wrapper_mock,
        mock_db_connections,
        stats_counter,
        mock_dump_handler
    ):
        schema_event_handler = SchemaEventHandler(
            db_connections=mock_db_connections,
            producer=producer,
            schema_wrapper=schema_wrapper_mock,
            stats_counter=stats_counter,
            register_dry_run=False,
        )
        schema_event_handler.handle_event(rename_table_schema_event, test_position)

        assert producer.flush.call_count == 1
        assert save_position.call_count == 1

        assert schema_wrapper_mock.reset_cache.call_count == 1

        assert external_patches.execute_query.call_count == 1
        assert external_patches.execute_query.call_args_list == [
            mock.call(
                query=rename_table_schema_event.query,
                database_name=rename_table_schema_event.schema,
            )
        ]

        assert external_patches.upsert_global_event_state.call_count == 1
Esempio n. 4
0
 def _assert_query_skipped(self, schema_event_handler, query_event,
                           test_position, external_patches, producer,
                           stats_counter, mock_persist_dump):
     schema_event_handler.handle_event(query_event, test_position)
     assert external_patches.execute_query.call_count == 0
     assert producer.flush.call_count == 0
     assert mock_persist_dump.call_count == 0
     if stats_counter:
         assert stats_counter.increment.call_count == 0
Esempio n. 5
0
 def test_filter_out_wrong_schema(self, producer, test_position,
                                  save_position, external_patches,
                                  schema_event_handler,
                                  alter_table_schema_event,
                                  mock_create_dump, mock_persist_dump):
     external_patches.database_config.return_value = ['fake_schema']
     schema_event_handler.handle_event(alter_table_schema_event,
                                       test_position)
     assert external_patches.populate_schema_cache.call_count == 0
     assert external_patches.upsert_global_event_state.call_count == 0
    def _setup_handle_event_alter_table(
        self,
        namespace,
        producer,
        stats_counter,
        test_position,
        save_position,
        external_patches,
        schema_event_handler,
        schematizer_client,
        alter_table_schema_event,
        show_create_result_initial,
        show_create_result_after_alter,
        mock_schema_tracker_cursor,
        table_with_schema_changes,
        alter_table_schema_store_response,
        test_schema,
        mock_create_dump,
        mock_persist_dump
    ):
        """Integration test the things that need to be called for handling an
           event with an alter table hence many mocks.
        """
        schema_event_handler.schema_wrapper.schematizer_client = schematizer_client
        schematizer_client.register_schema_from_mysql_stmts.return_value = \
            alter_table_schema_store_response
        new_create_table_stmt = show_create_result_after_alter.query
        mysql_statements = {
            "old_create_table_stmt": show_create_result_initial.query,
            "alter_table_stmt": alter_table_schema_event.query,
        }
        external_patches.get_show_create_statement.side_effect = [
            show_create_result_initial,
            show_create_result_after_alter
        ]

        schema_event_handler.handle_event(alter_table_schema_event, test_position)
        self.check_external_calls(
            namespace,
            schematizer_client,
            producer,
            alter_table_schema_event,
            mock_schema_tracker_cursor,
            table_with_schema_changes,
            schema_event_handler,
            new_create_table_stmt,
            alter_table_schema_store_response,
            external_patches,
            test_schema,
            mock_create_dump,
            mock_persist_dump,
            mysql_statements=mysql_statements
        )
        assert producer.flush.call_count == 1
        assert save_position.call_count == 1
 def _assert_query_skipped(
     self,
     schema_event_handler,
     query_event,
     test_position,
     external_patches,
     producer,
     stats_counter,
 ):
     schema_event_handler.handle_event(query_event, test_position)
     assert external_patches.execute_query.call_count == 0
     assert producer.flush.call_count == 0
     if stats_counter:
         assert stats_counter.increment.call_count == 0
 def test_filter_out_wrong_schema(
     self,
     producer,
     test_position,
     save_position,
     external_patches,
     schema_event_handler,
     alter_table_schema_event,
     mock_dump_handler
 ):
     external_patches.database_config.return_value = ['fake_schema']
     schema_event_handler.handle_event(alter_table_schema_event, test_position)
     assert external_patches.populate_schema_cache.call_count == 0
     assert external_patches.upsert_global_event_state.call_count == 0
Esempio n. 9
0
 def test_incomplete_transaction(self, producer, save_position,
                                 test_position, external_patches,
                                 schema_event_handler,
                                 alter_table_schema_event,
                                 show_create_result_initial,
                                 mock_dump_handler):
     external_patches.get_show_create_statement.side_effect = [
         show_create_result_initial, Exception
     ]
     with pytest.raises(Exception):
         schema_event_handler.handle_event(alter_table_schema_event,
                                           test_position)
     assert external_patches.upsert_global_event_state.call_count == 0
     assert producer.flush.call_count == 1
     assert save_position.call_count == 1
 def test_incomplete_transaction(
     self,
     producer,
     save_position,
     test_position,
     external_patches,
     schema_event_handler,
     alter_table_schema_event,
     show_create_result_initial,
     mock_dump_handler
 ):
     external_patches.get_show_create_statement.side_effect = [
         show_create_result_initial,
         Exception
     ]
     with pytest.raises(Exception):
         schema_event_handler.handle_event(alter_table_schema_event, test_position)
     assert external_patches.upsert_global_event_state.call_count == 0
     assert producer.flush.call_count == 1
     assert save_position.call_count == 1
Esempio n. 11
0
    def _setup_handle_event_alter_table(
            self, namespace, producer, stats_counter, test_position,
            save_position, external_patches, schema_event_handler,
            schematizer_client, alter_table_schema_event,
            show_create_result_initial, show_create_result_after_alter,
            mock_schema_tracker_cursor, table_with_schema_changes,
            alter_table_schema_store_response, test_schema, mock_create_dump,
            mock_persist_dump):
        """Integration test the things that need to be called for handling an
           event with an alter table hence many mocks.
        """
        schema_event_handler.schema_wrapper.schematizer_client = schematizer_client
        schematizer_client.register_schema_from_mysql_stmts.return_value = \
            alter_table_schema_store_response
        new_create_table_stmt = show_create_result_after_alter.query
        mysql_statements = {
            "old_create_table_stmt": show_create_result_initial.query,
            "alter_table_stmt": alter_table_schema_event.query,
        }
        external_patches.get_show_create_statement.side_effect = [
            show_create_result_initial, show_create_result_after_alter
        ]

        schema_event_handler.handle_event(alter_table_schema_event,
                                          test_position)
        self.check_external_calls(namespace,
                                  schematizer_client,
                                  producer,
                                  alter_table_schema_event,
                                  mock_schema_tracker_cursor,
                                  table_with_schema_changes,
                                  schema_event_handler,
                                  new_create_table_stmt,
                                  alter_table_schema_store_response,
                                  external_patches,
                                  test_schema,
                                  mock_create_dump,
                                  mock_persist_dump,
                                  mysql_statements=mysql_statements)
        assert producer.flush.call_count == 1
        assert save_position.call_count == 1
Esempio n. 12
0
    def test_non_schema_relevant_query(self, producer, test_position,
                                       save_position, external_patches,
                                       schema_event_handler,
                                       mock_schema_tracker_cursor,
                                       non_schema_relevant_query_event,
                                       mock_create_dump, mock_persist_dump):
        schema_event_handler.handle_event(non_schema_relevant_query_event,
                                          test_position)
        assert external_patches.execute_query.call_count == 1

        if 'CREATE DATABASE' in non_schema_relevant_query_event.query:
            expected_schema = None
        else:
            expected_schema = non_schema_relevant_query_event.schema

        assert external_patches.execute_query.call_args_list == [
            mock.call(query=non_schema_relevant_query_event.query,
                      database_name=expected_schema)
        ]
        # We should flush and save state before
        assert producer.flush.call_count == 1
        assert save_position.call_count == 1
        # And after
        assert external_patches.upsert_global_event_state.call_count == 1