-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package client | ||
|
||
import ( | ||
context "context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/gojuno/minimock" | ||
"github.com/namreg/godown/internal/api" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClient_Rpop(t *testing.T) { | ||
mc := minimock.NewController(t) | ||
defer mc.Finish() | ||
|
||
tests := []struct { | ||
name string | ||
arg string | ||
expectCommand string | ||
mockResponse *api.ExecuteCommandResponse | ||
mockErr error | ||
wantResult ScalarResult | ||
}{ | ||
{ | ||
name: "could_not_execute_command", | ||
arg: "key", | ||
expectCommand: "RPOP key", | ||
mockErr: errors.New("something went wrong"), | ||
wantResult: ScalarResult{ | ||
err: errors.New("could not execute command: something went wrong"), | ||
}, | ||
}, | ||
{ | ||
name: "server_responds_with_error", | ||
arg: "key", | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.ErrCommandReply, | ||
Item: "internal server error", | ||
}, | ||
wantResult: ScalarResult{err: errors.New("internal server error")}, | ||
}, | ||
{ | ||
name: "server_responds_with_nil", | ||
arg: "key", | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.NilCommandReply, | ||
}, | ||
wantResult: ScalarResult{}, | ||
}, | ||
{ | ||
name: "server_responds_with_string", | ||
arg: "key", | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.StringCommandReply, | ||
Item: "value", | ||
}, | ||
wantResult: ScalarResult{val: stringToPtr("value")}, | ||
}, | ||
{ | ||
name: "server_responds_with_unexpected_reply", | ||
arg: "key", | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.OkCommandReply, | ||
}, | ||
wantResult: ScalarResult{err: errors.New("unexpected reply: OK")}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mock := NewexecutorMock(mc) | ||
mock.ExecuteCommandMock. | ||
Expect(context.Background(), &api.ExecuteCommandRequest{Command: tt.expectCommand}). | ||
Return(tt.mockResponse, tt.mockErr) | ||
|
||
cl := Client{executor: mock} | ||
|
||
res := cl.RPop(tt.arg) | ||
assert.Equal(t, tt.wantResult, res) | ||
}) | ||
} | ||
} | ||
|
||
func TestClient_RpopWithContext(t *testing.T) { | ||
mc := minimock.NewController(t) | ||
defer mc.Finish() | ||
|
||
type args struct { | ||
ctx context.Context | ||
key string | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
expectCtx context.Context | ||
expectCommand string | ||
mockResponse *api.ExecuteCommandResponse | ||
mockErr error | ||
wantResult ScalarResult | ||
}{ | ||
{ | ||
name: "could_not_execute_command", | ||
args: args{ctx: context.Background(), key: "key"}, | ||
expectCtx: context.Background(), | ||
expectCommand: "RPOP key", | ||
mockErr: errors.New("something went wrong"), | ||
wantResult: ScalarResult{ | ||
err: errors.New("could not execute command: something went wrong"), | ||
}, | ||
}, | ||
{ | ||
name: "server_responds_with_error", | ||
args: args{ctx: context.Background(), key: "key"}, | ||
expectCtx: context.Background(), | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.ErrCommandReply, | ||
Item: "internal server error", | ||
}, | ||
wantResult: ScalarResult{err: errors.New("internal server error")}, | ||
}, | ||
{ | ||
name: "server_responds_with_nil", | ||
args: args{ctx: context.Background(), key: "key"}, | ||
expectCtx: context.Background(), | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.NilCommandReply, | ||
}, | ||
wantResult: ScalarResult{}, | ||
}, | ||
{ | ||
name: "server_responds_with_string", | ||
args: args{ctx: context.Background(), key: "key"}, | ||
expectCtx: context.Background(), | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.StringCommandReply, | ||
Item: "value", | ||
}, | ||
wantResult: ScalarResult{val: stringToPtr("value")}, | ||
}, | ||
{ | ||
name: "server_responds_with_unexpected_reply", | ||
args: args{ctx: context.Background(), key: "key"}, | ||
expectCtx: context.Background(), | ||
expectCommand: "RPOP key", | ||
mockResponse: &api.ExecuteCommandResponse{ | ||
Reply: api.OkCommandReply, | ||
}, | ||
wantResult: ScalarResult{err: errors.New("unexpected reply: OK")}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mock := NewexecutorMock(mc) | ||
mock.ExecuteCommandMock. | ||
Expect(tt.expectCtx, &api.ExecuteCommandRequest{Command: tt.expectCommand}). | ||
Return(tt.mockResponse, tt.mockErr) | ||
|
||
cl := Client{executor: mock} | ||
|
||
res := cl.RPopWithContext(tt.args.ctx, tt.args.key) | ||
assert.Equal(t, tt.wantResult, res) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package command | ||
|
||
import "github.com/namreg/godown/internal/storage" | ||
|
||
// Rpop is a RPOP command. | ||
type Rpop struct { | ||
strg dataStore | ||
} | ||
|
||
// Name returns a command name. | ||
// Implements Name method of Command interface. | ||
func (c *Rpop) Name() string { | ||
return "RPOP" | ||
} | ||
|
||
// Help returns a help message for the command. | ||
// Implements Help method of Command interface. | ||
func (c *Rpop) Help() string { | ||
return `Usage: RPOP key | ||
Removes and returns the last element of the list stored at key.` | ||
} | ||
|
||
// Execute excutes a command with the given arguments. | ||
// Implements Execute method of Command interface. | ||
func (c *Rpop) Execute(args ...string) Reply { | ||
if len(args) != 1 { | ||
return ErrReply{Value: ErrWrongArgsNumber} | ||
} | ||
|
||
var popped string | ||
|
||
setter := func(old *storage.Value) (*storage.Value, error) { | ||
if old == nil { | ||
return nil, nil | ||
} | ||
|
||
if old.Type() != storage.ListDataType { | ||
return nil, ErrWrongTypeOp | ||
} | ||
|
||
list := old.Data().([]string) | ||
popped, list = list[len(list)-1], list[:len(list)-1] | ||
|
||
if len(list) == 0 { | ||
return nil, nil | ||
} | ||
|
||
return storage.NewList(list), nil | ||
} | ||
|
||
if err := c.strg.Put(storage.Key(args[0]), setter); err != nil { | ||
return ErrReply{Value: err} | ||
} | ||
|
||
if popped == "" { | ||
return NilReply{} | ||
} | ||
return StringReply{Value: popped} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gojuno/minimock" | ||
|
||
"github.com/namreg/godown/internal/storage" | ||
"github.com/namreg/godown/internal/storage/memory" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRpop_Name(t *testing.T) { | ||
cmd := new(Rpop) | ||
assert.Equal(t, "RPOP", cmd.Name()) | ||
} | ||
|
||
func TestRpop_Help(t *testing.T) { | ||
cmd := new(Rpop) | ||
expected := `Usage: RPOP key | ||
Removes and returns the last element of the list stored at key.` | ||
assert.Equal(t, expected, cmd.Help()) | ||
} | ||
|
||
func TestRpop_Execute(t *testing.T) { | ||
expired := storage.NewList([]string{"val1", "val2"}) | ||
expired.SetTTL(time.Now().Add(-1 * time.Second)) | ||
|
||
strg := memory.New(map[storage.Key]*storage.Value{ | ||
"string": storage.NewString("string"), | ||
"list": storage.NewList([]string{"val1", "val2"}), | ||
"expired": expired, | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
want Reply | ||
}{ | ||
{"ok", []string{"list"}, StringReply{Value: "val2"}}, | ||
{"expired_key", []string{"expired"}, NilReply{}}, | ||
{"not_existing_key", []string{"not_existing_key"}, NilReply{}}, | ||
{"wrong_type_op", []string{"string"}, ErrReply{Value: ErrWrongTypeOp}}, | ||
{"wrong_args_number/1", []string{}, ErrReply{Value: ErrWrongArgsNumber}}, | ||
{"wrong_args_number/2", []string{"list", "0"}, ErrReply{Value: ErrWrongArgsNumber}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cmd := Rpop{strg: strg} | ||
res := cmd.Execute(tt.args...) | ||
assert.Equal(t, tt.want, res) | ||
}) | ||
} | ||
} | ||
|
||
func TestRpop_Execute_StorageErr(t *testing.T) { | ||
mc := minimock.NewController(t) | ||
defer mc.Finish() | ||
|
||
err := errors.New("error") | ||
|
||
strg := NewdataStoreMock(mc) | ||
strg.PutMock.Return(err) | ||
|
||
cmd := Rpop{strg: strg} | ||
|
||
res := cmd.Execute("list") | ||
assert.Equal(t, ErrReply{Value: err}, res) | ||
} | ||
|
||
func TestRpop_Execute_DelEmptyList(t *testing.T) { | ||
strg := memory.New(map[storage.Key]*storage.Value{ | ||
"list": storage.NewList([]string{"val1"}), | ||
}) | ||
|
||
cmd := Rpop{strg: strg} | ||
_ = cmd.Execute("list") | ||
|
||
items, err := strg.All() | ||
assert.NoError(t, err) | ||
|
||
value, ok := items["list"] | ||
assert.Nil(t, value) | ||
assert.False(t, ok) | ||
} |