package rulesengine import ( "context" "testing" "github.com/stretchr/testify/assert" ) type mockDS struct { DeleteCommandFunc func(ctx context.Context, name string) (DeleteResult, error) AddCommandsFunc func(ctx context.Context, names []string) (AddNameResult, error) DeletePrivilegeFunc func(ctx context.Context, name string) (DeleteResult, error) AddPrivilegesFunc func(ctx context.Context, names []string) (AddNameResult, error) Dataset } func (m mockDS) DeleteCommand(ctx context.Context, name string) (DeleteResult, error) { return m.DeleteCommandFunc(ctx, name) } func (m mockDS) AddCommands(ctx context.Context, names []string) (AddNameResult, error) { return m.AddCommandsFunc(ctx, names) } func (m mockDS) DeletePrivilege(ctx context.Context, name string) (DeleteResult, error) { return m.DeletePrivilegeFunc(ctx, name) } func (m mockDS) AddPrivileges(ctx context.Context, names []string) (AddNameResult, error) { return m.AddPrivilegesFunc(ctx, names) } func TestDeleteCommand(t *testing.T) { t.Parallel() tests := map[string]struct { name string assertErr assert.ErrorAssertionFunc }{ "Empty name": { name: "", assertErr: ErrorEqualMsg("empty command name"), }, "Valid name": { name: "test", assertErr: assert.NoError, }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() ds := mockDS{ DeleteCommandFunc: func(_ context.Context, _ string) (DeleteResult, error) { return DeleteResult{}, nil }, } reng := New(ds) _, err := reng.DeleteCommand(context.Background(), tc.name) tc.assertErr(t, err) }) } } func TestAddCommands(t *testing.T) { t.Parallel() tests := map[string]struct { commands []PostCommandPayload assertErr assert.ErrorAssertionFunc }{ "Empty command list": { commands: []PostCommandPayload{}, assertErr: ErrorEqualMsg("empty command list"), }, "Valid command list": { commands: []PostCommandPayload{{Name: "test"}}, assertErr: assert.NoError, }, "Invalid command": { commands: []PostCommandPayload{{Name: ""}}, assertErr: ErrorEqualMsg("invalid command at 0: empty command name"), }, "Multiple invalid commands": { commands: []PostCommandPayload{{Name: ""}, {Name: ""}}, assertErr: ErrorEqualMsg("invalid command at 0: empty command name\ninvalid command at 1: empty command name"), }, "Too many commands": { commands: func() []PostCommandPayload { commands := make([]PostCommandPayload, 501) for i := 0; i < maxCommands+1; i++ { commands[i] = PostCommandPayload{Name: "test"} } return commands }(), assertErr: ErrorEqualMsg("total number of commands 501 exceeds max 500"), }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() ds := mockDS{ AddCommandsFunc: func(_ context.Context, _ []string) (AddNameResult, error) { return AddNameResult{}, nil }, } reng := New(ds) _, err := reng.AddCommands(context.Background(), tc.commands) tc.assertErr(t, err) }) } }