# Copyright 2023 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

## disable schema sync and automatic flush
=> DBGInvoke __enable_schema_sync_service('false')


## clean up
=> DBGInvoke __drop_tidb_table(default, test)
=> DBGInvoke __refresh_schemas()


## create a DeltaMerge table
=> DBGInvoke __mock_tidb_table(default, test, 'col_1 String')
=> DBGInvoke __refresh_mapped_table_schema(default, test)
=> DBGInvoke query_mapped('select tidb_database,tidb_name,engine from system.tables where database=''\$d'' and name=''\$t''', default, test)
┌─database─┬─name─┬─engine─────┐
│ default  │ test │ DeltaMerge │
└──────────┴──────┴────────────┘

=> DBGInvoke __put_region(4, 0, 100, default, test)
=> DBGInvoke __put_region(5, 100, 150, default, test)

## insert some data
=> DBGInvoke __raft_insert_row(default, test, 4, 51, 'test51')
=> DBGInvoke __try_flush_region(4)
=> DBGInvoke query_mapped('select * from \$d.\$t', default, test)
┌─col_1──┬─_tidb_rowid─┐
│ test51 │          51 │
└────────┴─────────────┘
=> DBGInvoke __raft_insert_row(default, test, 5, 103, 'test103')
=> DBGInvoke __try_flush_region(5)
=> DBGInvoke query_mapped('select * from \$d.\$t', default, test)
┌─col_1───┬─_tidb_rowid─┐
│ test51  │          51 │
│ test103 │         103 │
└─────────┴─────────────┘

## remove region 5
=> DBGInvoke __remove_region(5)
=> DBGInvoke query_mapped('select * from \$d.\$t', default, test)
┌─col_1──┬─_tidb_rowid─┐
│ test51 │          51 │
└────────┴─────────────┘

## remove region 4 and ensure that no data left
=> DBGInvoke __remove_region(4)
=> DBGInvoke query_mapped('select count() as cnt from \$d.\$t', default, test)
┌─cnt─┐
│   0 │
└─────┘


## clean up
=> DBGInvoke __drop_tidb_table(default, test)
=> DBGInvoke __refresh_schemas()
