|
| 1 | +package packagecmds |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/jfrog/jfrog-cli-core/v2/plugins/components" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestBuildPackageRequestPayload(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + arguments []string |
| 14 | + expectedKey string |
| 15 | + expectedType string |
| 16 | + expectedName string |
| 17 | + expectedVersions []string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Valid package request with single version", |
| 21 | + arguments: []string{"my-app", "npm", "my-package", "1.0.0"}, |
| 22 | + expectedKey: "my-app", |
| 23 | + expectedType: "npm", |
| 24 | + expectedName: "my-package", |
| 25 | + expectedVersions: []string{"1.0.0"}, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "Valid package request with multiple versions", |
| 29 | + arguments: []string{"my-app", "npm", "my-package", "1.0.0,1.1.0 , 1.2.0"}, |
| 30 | + expectedKey: "my-app", |
| 31 | + expectedType: "npm", |
| 32 | + expectedName: "my-package", |
| 33 | + expectedVersions: []string{"1.0.0", "1.1.0", "1.2.0"}, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Package request without versions", |
| 37 | + arguments: []string{"my-app", "npm", "my-package"}, |
| 38 | + expectedKey: "my-app", |
| 39 | + expectedType: "npm", |
| 40 | + expectedName: "my-package", |
| 41 | + expectedVersions: nil, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + ctx := &components.Context{ |
| 48 | + Arguments: tt.arguments, |
| 49 | + } |
| 50 | + |
| 51 | + result, err := BuildPackageRequestPayload(ctx) |
| 52 | + |
| 53 | + assert.NoError(t, err) |
| 54 | + assert.NotNil(t, result) |
| 55 | + assert.Equal(t, tt.expectedKey, result.ApplicationKey) |
| 56 | + assert.Equal(t, tt.expectedType, result.Type) |
| 57 | + assert.Equal(t, tt.expectedName, result.Name) |
| 58 | + assert.Equal(t, tt.expectedVersions, result.Versions) |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
0 commit comments