|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/internal/commands" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/internal/config" |
| 10 | + smock "github.com/UpCloudLtd/upcloud-cli/internal/mock" |
| 11 | + "github.com/UpCloudLtd/upcloud-cli/internal/output" |
| 12 | + |
| 13 | + "github.com/UpCloudLtd/upcloud-go-api/v4/upcloud" |
| 14 | + "github.com/gemalto/flume" |
| 15 | + "github.com/jedib0t/go-pretty/v6/text" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/mock" |
| 18 | +) |
| 19 | + |
| 20 | +func TestDatabasePlans_SortedHumanOutput(t *testing.T) { |
| 21 | + text.DisableColors() |
| 22 | + plansPg := upcloud.ManagedDatabaseType{ |
| 23 | + ServicePlans: []upcloud.ManagedDatabaseServicePlan{ |
| 24 | + {Plan: "test-plan-5", NodeCount: 3, CoreNumber: 16, MemoryAmount: 128, StorageSize: 2048}, |
| 25 | + {Plan: "test-plan-3", NodeCount: 1, CoreNumber: 2, MemoryAmount: 16, StorageSize: 256}, |
| 26 | + {Plan: "test-plan-1", NodeCount: 1, CoreNumber: 1, MemoryAmount: 8, StorageSize: 2048}, |
| 27 | + {Plan: "test-plan-2", NodeCount: 1, CoreNumber: 2, MemoryAmount: 4, StorageSize: 2048}, |
| 28 | + {Plan: "test-plan-4", NodeCount: 3, CoreNumber: 16, MemoryAmount: 128, StorageSize: 1024}, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + mService := smock.Service{} |
| 33 | + mService.On("GetManagedDatabaseServiceType", mock.Anything).Return(&plansPg, nil) |
| 34 | + |
| 35 | + conf := config.New() |
| 36 | + // force human output |
| 37 | + conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman) |
| 38 | + |
| 39 | + command := commands.BuildCommand(PlansCommand(), nil, conf) |
| 40 | + |
| 41 | + res, err := command.(commands.MultipleArgumentCommand).Execute(commands.NewExecutor(conf, &mService, flume.New("test")), "pg") |
| 42 | + |
| 43 | + assert.Nil(t, err) |
| 44 | + |
| 45 | + buf := bytes.NewBuffer(nil) |
| 46 | + err = output.Render(buf, conf, res) |
| 47 | + output := buf.String() |
| 48 | + |
| 49 | + assert.NoError(t, err) |
| 50 | + |
| 51 | + assert.Less(t, strings.Index(output, "test-plan-1"), strings.Index(output, "test-plan-2")) |
| 52 | + assert.Less(t, strings.Index(output, "test-plan-2"), strings.Index(output, "test-plan-3")) |
| 53 | + assert.Less(t, strings.Index(output, "test-plan-3"), strings.Index(output, "test-plan-4")) |
| 54 | + assert.Less(t, strings.Index(output, "test-plan-4"), strings.Index(output, "test-plan-5")) |
| 55 | +} |
0 commit comments