|
| 1 | +// Copyright 2024 The PipeCD 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 planpreview |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/pipe-cd/pipecd/pkg/model" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestSortResults(t *testing.T) { |
| 25 | + t.Parallel() |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + input []*model.ApplicationPlanPreviewResult |
| 30 | + expected []*model.ApplicationPlanPreviewResult |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "sort by name, kind", |
| 34 | + input: []*model.ApplicationPlanPreviewResult{ |
| 35 | + {ApplicationName: "appB", ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 36 | + {ApplicationName: "appB", ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 37 | + {ApplicationName: "appA", ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 38 | + {ApplicationName: "appA", ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 39 | + }, |
| 40 | + expected: []*model.ApplicationPlanPreviewResult{ |
| 41 | + {ApplicationName: "appA", ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 42 | + {ApplicationName: "appA", ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 43 | + {ApplicationName: "appB", ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 44 | + {ApplicationName: "appB", ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "sort by name, env, kind", |
| 49 | + input: []*model.ApplicationPlanPreviewResult{ |
| 50 | + {ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 51 | + {ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 52 | + {ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "prd"}, ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 53 | + {ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 54 | + }, |
| 55 | + expected: []*model.ApplicationPlanPreviewResult{ |
| 56 | + {ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 57 | + {ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "prd"}, ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 58 | + {ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES}, |
| 59 | + {ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_CLOUDRUN}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, tt := range tests { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + t.Parallel() |
| 67 | + sortResults(tt.input) |
| 68 | + require.Equal(t, tt.expected, tt.input) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments