github.com/sacloud/iaas-api-go@v1.12.0/helper/query/server_plan_test.go (about) 1 // Copyright 2016-2022 The sacloud/iaas-api-go Authors 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 query 16 17 import ( 18 "context" 19 "errors" 20 "testing" 21 22 "github.com/sacloud/iaas-api-go" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestFindServerPlan(t *testing.T) { 27 cases := []struct { 28 msg string 29 in *FindServerPlanRequest 30 finder ServerPlanFinder 31 out *iaas.ServerPlan 32 err error 33 }{ 34 { 35 msg: "finder returns unexpected error", 36 in: nil, 37 finder: &dummyServerPlanFinder{ 38 err: errors.New("dummy"), 39 }, 40 out: nil, 41 err: errors.New("dummy"), 42 }, 43 { 44 msg: "finder returns multiple result", 45 in: &FindServerPlanRequest{ 46 CPU: 1, 47 }, 48 finder: &dummyServerPlanFinder{ 49 plans: []*iaas.ServerPlan{ 50 {ID: 1}, 51 {ID: 2}, 52 }, 53 }, 54 out: &iaas.ServerPlan{ID: 1}, 55 err: nil, 56 }, 57 { 58 msg: "with nil find parameter", 59 in: &FindServerPlanRequest{ 60 CPU: 1, 61 }, 62 finder: &dummyServerPlanFinder{ 63 plans: []*iaas.ServerPlan{ 64 {ID: 1}, 65 }, 66 }, 67 out: &iaas.ServerPlan{ID: 1}, 68 err: nil, 69 }, 70 { 71 msg: "with nil find parameter", 72 in: &FindServerPlanRequest{ 73 CPU: 1, 74 }, 75 finder: &dummyServerPlanFinder{ 76 plans: []*iaas.ServerPlan{ 77 {ID: 1}, 78 }, 79 }, 80 out: &iaas.ServerPlan{ID: 1}, 81 err: nil, 82 }, 83 { 84 msg: "plan not found", 85 in: nil, 86 finder: &dummyServerPlanFinder{ 87 plans: []*iaas.ServerPlan{}, 88 }, 89 out: nil, 90 err: errors.New("server plan not found"), 91 }, 92 } 93 94 for _, tc := range cases { 95 res, err := FindServerPlan(context.Background(), tc.finder, "tk1v", tc.in) 96 require.Equal(t, tc.err, err, tc.msg) 97 require.Equal(t, tc.out, res, tc.msg) 98 } 99 }