add test coverage for Args validation and Validate method

Signed-off-by: samay43 <samayrbhat43@gmail.com>
This commit is contained in:
samay43
2026-08-13 10:57:54 +05:30
parent b731710e81
commit fa3f5737c8
+110
View File
@@ -449,3 +449,113 @@ func TestCreateCommand(t *testing.T) {
assert.NoError(t, e)
})
}
func TestCreateCommand_Args(t *testing.T) {
testCases := []struct {
name string
args []string
fromSchedule string
expectError bool
}{
{
name: "should error when no name and no from-schedule",
args: []string{},
expectError: true,
},
{
name: "should pass when a valid name is provided",
args: []string{"my-backup"},
expectError: false,
},
{
name: "should error when the name is not a valid DNS1123 subdomain",
args: []string{"Invalid_Name!"},
expectError: true,
},
{
name: "should pass with no name when from-schedule is set",
args: []string{},
fromSchedule: "daily-backup",
expectError: false,
},
{
name: "should error when more than one arg is given",
args: []string{"name1", "name2"},
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
f := &factorymocks.Factory{}
cmd := NewCreateCommand(f, "")
if tc.fromSchedule != "" {
err := cmd.Flags().Set("from-schedule", tc.fromSchedule)
assert.NoError(t, err)
}
err := cmd.Args(cmd, tc.args)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestCreateOptions_Validate(t *testing.T) {
testCases := []struct {
name string
optName string
fromSchedule string
args []string
expectError bool
}{
{
name: "should error when no name and no from-schedule",
optName: "",
args: []string{},
expectError: true,
},
{
name: "should pass with a valid name and no from-schedule",
optName: "my-backup",
args: []string{"my-backup"},
expectError: false,
},
{
name: "should error when name is invalid, regardless of from-schedule",
optName: "Invalid_Name!",
fromSchedule: "daily-backup",
args: []string{"Invalid_Name!"},
expectError: true,
},
{
name: "should pass when from-schedule is set and no name given",
optName: "",
fromSchedule: "daily-backup",
args: []string{},
expectError: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
f := &factorymocks.Factory{}
cmd := NewCreateCommand(f, "")
o := NewCreateOptions()
o.Name = tc.optName
o.FromSchedule = tc.fromSchedule
err := o.Validate(cmd, tc.args, f)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}