github.com/polarismesh/polaris@v1.17.8/store/boltdb/config_file_release_history_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package boltdb
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/polarismesh/polaris/common/model"
    28  )
    29  
    30  func mockConfigFileHistory(total int, fileName string) []*model.ConfigFileReleaseHistory {
    31  	ret := make([]*model.ConfigFileReleaseHistory, 0, total)
    32  
    33  	for i := 0; i < total; i++ {
    34  		val := &model.ConfigFileReleaseHistory{
    35  			Name:       fmt.Sprintf("history-%d", i),
    36  			Namespace:  "default",
    37  			Group:      "default",
    38  			FileName:   fmt.Sprintf("history-%d", i),
    39  			Format:     "yaml",
    40  			Content:    fmt.Sprintf("history-%d", i),
    41  			CreateTime: time.Now(),
    42  			ModifyTime: time.Now(),
    43  			Valid:      true,
    44  			Metadata: map[string]string{
    45  				"mock_key": "mock_value",
    46  			},
    47  		}
    48  
    49  		if len(fileName) != 0 {
    50  			val.Name = fileName
    51  		}
    52  
    53  		ret = append(ret, val)
    54  	}
    55  	return ret
    56  }
    57  
    58  func resetHistoryTimeAndIDField(tN time.Time, datas ...*model.ConfigFileReleaseHistory) {
    59  	for i := range datas {
    60  		datas[i].Id = 0
    61  		datas[i].CreateTime = tN
    62  		datas[i].ModifyTime = tN
    63  	}
    64  }
    65  
    66  func Test_configFileReleaseHistoryStore(t *testing.T) {
    67  	t.Run("配置发布历史插入", func(t *testing.T) {
    68  		CreateTableDBHandlerAndRun(t, tblConfigFileReleaseHistory, func(t *testing.T, handler BoltHandler) {
    69  			store := newConfigFileReleaseHistoryStore(handler)
    70  			total := 10
    71  			mockHistories := mockConfigFileHistory(total, "")
    72  
    73  			for i := 0; i < total; i++ {
    74  				if err := store.CreateConfigFileReleaseHistory(mockHistories[i]); err != nil {
    75  					t.Fatal(err)
    76  				}
    77  			}
    78  
    79  			idMap := make(map[uint64]struct{})
    80  
    81  			for i := 0; i < total; i++ {
    82  				mockVal := mockHistories[i]
    83  				val, err := store.GetLatestConfigFileReleaseHistory(mockVal.Namespace, mockVal.Group, mockVal.FileName)
    84  				if err != nil {
    85  					t.Fatal(err)
    86  				}
    87  
    88  				assert.NotNil(t, val)
    89  
    90  				copyVal := *val
    91  				resetHistoryTimeAndIDField(time.Now(), val, mockVal)
    92  				assert.Equal(t, mockVal, val)
    93  
    94  				idMap[copyVal.Id] = struct{}{}
    95  			}
    96  
    97  			assert.Equal(t, total, len(idMap))
    98  		})
    99  	})
   100  }