go.ligato.io/vpp-agent/v3@v3.5.0/pkg/models/models_test.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package models_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  
    22  	"go.ligato.io/vpp-agent/v3/pkg/models"
    23  	testmodel "go.ligato.io/vpp-agent/v3/pkg/models/testdata/proto"
    24  )
    25  
    26  func TestKey(t *testing.T) {
    27  	tc := setupTest(t)
    28  	defer tc.teardownTest()
    29  
    30  	tests := []struct {
    31  		name      string
    32  		instance  *testmodel.Basic
    33  		expectKey string
    34  	}{
    35  		{"named",
    36  			&testmodel.Basic{Name: "basic0"},
    37  			"config/module/v1/basic/basic0",
    38  		},
    39  	}
    40  	for _, test := range tests {
    41  		t.Run(test.name, func(t *testing.T) {
    42  			tc.Expect(models.GetKey(test.instance)).To(Equal(test.expectKey))
    43  		})
    44  	}
    45  }
    46  
    47  type testContext struct {
    48  	*GomegaWithT
    49  }
    50  
    51  func setupTest(t *testing.T) *testContext {
    52  	g := NewGomegaWithT(t)
    53  
    54  	ResetDefaultRegistry()
    55  
    56  	basicModel := models.Register(&testmodel.Basic{}, models.Spec{
    57  		Module:  "module",
    58  		Version: "v1",
    59  		Type:    "basic",
    60  	})
    61  	g.Expect(basicModel).ToNot(BeNil())
    62  
    63  	return &testContext{GomegaWithT: g}
    64  }
    65  func (tc *testContext) teardownTest() {}