deep copy items when adding them to database checkers/fixtures

This commit is contained in:
Jim Minter 2020-11-25 13:08:49 -06:00
Родитель dd7e79213b
Коммит 22e0cc5b91
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 0730CBDA10D1A2D3
3 изменённых файлов: 108 добавлений и 10 удалений

Просмотреть файл

@ -29,23 +29,58 @@ func NewChecker() *Checker {
}
func (f *Checker) AddOpenShiftClusterDocuments(docs ...*api.OpenShiftClusterDocument) {
f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docCopy.(*api.OpenShiftClusterDocument))
}
}
func (f *Checker) AddSubscriptionDocuments(docs ...*api.SubscriptionDocument) {
f.subscriptionDocuments = append(f.subscriptionDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.subscriptionDocuments = append(f.subscriptionDocuments, docCopy.(*api.SubscriptionDocument))
}
}
func (f *Checker) AddBillingDocuments(docs ...*api.BillingDocument) {
f.billingDocuments = append(f.billingDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.billingDocuments = append(f.billingDocuments, docCopy.(*api.BillingDocument))
}
}
func (f *Checker) AddAsyncOperationDocuments(docs ...*api.AsyncOperationDocument) {
f.asyncOperationDocuments = append(f.asyncOperationDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.asyncOperationDocuments = append(f.asyncOperationDocuments, docCopy.(*api.AsyncOperationDocument))
}
}
func (f *Checker) AddPortalDocuments(docs ...*api.PortalDocument) {
f.portalDocuments = append(f.portalDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.portalDocuments = append(f.portalDocuments, docCopy.(*api.PortalDocument))
}
}
func (f *Checker) CheckOpenShiftClusters(openShiftClusters *cosmosdb.FakeOpenShiftClusterDocumentClient) (errs []error) {

28
test/database/deepcopy.go Normal file
Просмотреть файл

@ -0,0 +1,28 @@
package database
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"reflect"
"github.com/ugorji/go/codec"
)
var h = &codec.JsonHandle{}
func deepCopy(i interface{}) (interface{}, error) {
var b []byte
err := codec.NewEncoderBytes(&b, h).Encode(i)
if err != nil {
return nil, err
}
i = reflect.New(reflect.ValueOf(i).Elem().Type()).Interface()
err = codec.NewDecoderBytes(b, h).Decode(&i)
if err != nil {
return nil, err
}
return i, nil
}

Просмотреть файл

@ -56,23 +56,58 @@ func (f *Fixture) WithPortal(db database.Portal) *Fixture {
}
func (f *Fixture) AddOpenShiftClusterDocuments(docs ...*api.OpenShiftClusterDocument) {
f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docCopy.(*api.OpenShiftClusterDocument))
}
}
func (f *Fixture) AddSubscriptionDocuments(docs ...*api.SubscriptionDocument) {
f.subscriptionDocuments = append(f.subscriptionDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.subscriptionDocuments = append(f.subscriptionDocuments, docCopy.(*api.SubscriptionDocument))
}
}
func (f *Fixture) AddBillingDocuments(docs ...*api.BillingDocument) {
f.billingDocuments = append(f.billingDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.billingDocuments = append(f.billingDocuments, docCopy.(*api.BillingDocument))
}
}
func (f *Fixture) AddAsyncOperationDocuments(docs ...*api.AsyncOperationDocument) {
f.asyncOperationDocuments = append(f.asyncOperationDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.asyncOperationDocuments = append(f.asyncOperationDocuments, docCopy.(*api.AsyncOperationDocument))
}
}
func (f *Fixture) AddPortalDocuments(docs ...*api.PortalDocument) {
f.portalDocuments = append(f.portalDocuments, docs...)
for _, doc := range docs {
docCopy, err := deepCopy(doc)
if err != nil {
panic(err)
}
f.portalDocuments = append(f.portalDocuments, docCopy.(*api.PortalDocument))
}
}
func (f *Fixture) Create() error {