Repository: pingcap/tidb
File: pkg/meta/meta_test.go (Go)

Code before:
	}
	b, err = json.Marshal(tableInfo)
	require.NoError(t, err)
	require.True(t, meta.IsTableInfoMustLoad(b))

	tableInfo = &model.TableInfo{
		ID:    123,
		State: model.StatePublic,
	}
	b, err = json.Marshal(tableInfo)
	require.NoError(t, err)
	require.False(t, meta.IsTableInfoMustLoad(b))

	tableInfo = &model.TableInfo{
		State: model.StatePublic,
	}
	b, err = json.Marshal(tableInfo)
	require.NoError(t, err)
	require.False(t, meta.IsTableInfoMustLoad(b))
}

func TestIsTableInfoMustLoadSubStringsOrder(t *testing.T) {
	// The order matter!
	// IsTableInfoMustLoad relies on the order of the json marshal result,
	// or the internal of the json marshal in other words.
	// This test cover the invariance, if Go std library changes, we can catch it.
	tableInfo := &model.TableInfo{
		Affinity:       &model.TableAffinityInfo{Level: "s"},
		SoftdeleteInfo: &model.SoftdeleteInfo{Retention: "1h"},
	}
	b, err := json.Marshal(tableInfo)
	require.NoError(t, err)
	expect := `{"id":0,"name":{"O":"","L":""},"charset":"","collate":"","cols":null,"index_info":null,"constraint_info":null,"fk_info":null,"state":0,"pk_is_handle":false,"is_common_handle":false,"common_handle_version":0,"comment":"","auto_inc_id":0,"auto_id_cache":0,"auto_rand_id":0,"max_col_id":0,"max_idx_id":0,"max_fk_id":0,"max_cst_id":0,"update_timestamp":0,"ShardRowIDBits":0,"max_shard_row_id_bits":0,"auto_random_bits":0,"auto_random_range_bits":0,"pre_split_regions":0,"partition":null,"compression":"","view":null,"sequence":null,"Lock":null,"version":0,"tiflash_replica":null,"is_columnar":false,"temp_table_type":0,"cache_table_status":0,"policy_ref_info":null,"stats_options":null,"exchange_partition_info":null,"ttl_info":null,"affinity":{"level":"s"},"softdelete_info":{"retention":"1h"},"revision":0}`
	require.Equal(t, expect, string(b))
}

func TestTableNameExtract(t *testing.T) {
	var tbl model.TableInfo
	tbl.Name = pmodel.NewCIStr(`a`)
	b, err := json.Marshal(tbl)
	require.NoError(t, err)

	nameLRegex := regexp.MustCompile(meta.NameExtractRegexp)
	nameLMatch := nameLRegex.FindStringSubmatch(string(b))
	require.Len(t, nameLMatch, 2)
	require.Equal(t, "a", nameLMatch[1])

	tbl.Name = pmodel.NewCIStr(`"a"`)
	b, err = json.Marshal(tbl)
	require.NoError(t, err)
	nameLMatch = nameLRegex.FindStringSubmatch(string(b))

Review comment (suggestion):
Minor grammar in the test comment: “This test cover the invariance” should be “covers”. Since the test is intentionally brittle to catch `encoding/json` ordering changes, keeping the wording precise helps readability.
```suggestion
	// This test covers the invariance, if Go std library changes, we can catch it.
```

Code after:
	}
	b, err = json.Marshal(tableInfo)
	require.NoError(t, err)
	require.True(t, meta.IsTableInfoMustLoad(b))

	tableInfo = &model.TableInfo{
		ID:    123,
		State: model.StatePublic,
	}
	b, err = json.Marshal(tableInfo)
	require.NoError(t, err)
	require.False(t, meta.IsTableInfoMustLoad(b))

	tableInfo = &model.TableInfo{
		State: model.StatePublic,
	}
	b, err = json.Marshal(tableInfo)
	require.NoError(t, err)
	require.False(t, meta.IsTableInfoMustLoad(b))
}

func TestIsTableInfoMustLoadSubStringsOrder(t *testing.T) {
	// The order matter!
	// IsTableInfoMustLoad relies on the order of the json marshal result,
	// or the internal of the json marshal in other words.
	// This test covers the invariance, if Go std library changes, we can catch it.
	tableInfo := &model.TableInfo{
		Affinity:       &model.TableAffinityInfo{Level: "s"},
		SoftdeleteInfo: &model.SoftdeleteInfo{Retention: "1h"},
	}
	b, err := json.Marshal(tableInfo)
	require.NoError(t, err)
	expect := `{"id":0,"name":{"O":"","L":""},"charset":"","collate":"","cols":null,"index_info":null,"constraint_info":null,"fk_info":null,"state":0,"pk_is_handle":false,"is_common_handle":false,"common_handle_version":0,"comment":"","auto_inc_id":0,"auto_id_cache":0,"auto_rand_id":0,"max_col_id":0,"max_idx_id":0,"max_fk_id":0,"max_cst_id":0,"update_timestamp":0,"ShardRowIDBits":0,"max_shard_row_id_bits":0,"auto_random_bits":0,"auto_random_range_bits":0,"pre_split_regions":0,"partition":null,"compression":"","view":null,"sequence":null,"Lock":null,"version":0,"tiflash_replica":null,"is_columnar":false,"temp_table_type":0,"cache_table_status":0,"policy_ref_info":null,"stats_options":null,"exchange_partition_info":null,"ttl_info":null,"affinity":{"level":"s"},"softdelete_info":{"retention":"1h"},"revision":0}`
	require.Equal(t, expect, string(b))
}

func TestTableNameExtract(t *testing.T) {
	var tbl model.TableInfo
	tbl.Name = pmodel.NewCIStr(`a`)
	b, err := json.Marshal(tbl)
	require.NoError(t, err)

	nameLRegex := regexp.MustCompile(meta.NameExtractRegexp)
	nameLMatch := nameLRegex.FindStringSubmatch(string(b))
	require.Len(t, nameLMatch, 2)
	require.Equal(t, "a", nameLMatch[1])

	tbl.Name = pmodel.NewCIStr(`"a"`)
	b, err = json.Marshal(tbl)
	require.NoError(t, err)
	nameLMatch = nameLRegex.FindStringSubmatch(string(b))