-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
162 lines (131 loc) · 4.2 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package kece
import (
"testing"
)
func TestClient(t *testing.T) {
client := &Client{ID: "001"}
cm := &ClientMessage{Client: client}
t.Run("should success with command GET, DEL, or AUTH with valid message", func(t *testing.T) {
cm.Message = []byte("GET 1")
err := cm.ValidateMessage()
if err != nil {
t.Errorf("error validate client message with GET command %s", err.Error())
}
})
t.Run("should error with command GET, DEL, or AUTH with invalid message", func(t *testing.T) {
cm.Message = []byte("DEL 1 bla")
err := cm.ValidateMessage()
if err == nil {
t.Errorf("error validate client message with DEL command")
}
})
t.Run("should success with command SET with valid message", func(t *testing.T) {
cm.Message = []byte("SET 1 wury")
err := cm.ValidateMessage()
if err != nil {
t.Errorf("error validate client message with SET command %s", err.Error())
}
})
t.Run("should error with command SET with invalid message", func(t *testing.T) {
cm.Message = []byte("SET 1 wury yanto")
err := cm.ValidateMessage()
if err == nil {
t.Errorf("error validate client message with SET command")
}
})
t.Run("should success with command SET with many spaces", func(t *testing.T) {
cm.Message = []byte("SET 1 agung")
err := cm.ValidateMessage()
if err != nil {
t.Errorf("error validate client message with SET command %s", err.Error())
}
})
t.Run("should error with command SET with invalid args", func(t *testing.T) {
cm.Message = []byte(`SET k "agung dwi p'`)
err := cm.ValidateMessage()
if err == nil {
t.Errorf("error validate client message with SET command")
}
})
t.Run("should success with command SET with JSON string", func(t *testing.T) {
cm.Message = []byte(`SET adp {"id": 1, "name": "agung dwi prasetyo"}`)
err := cm.ValidateMessage()
if err != nil {
t.Errorf("error validate client message with SET command for JSON data with spaces %s", err.Error())
}
if string(cm.Value) != `{"id": 1, "name": "agung dwi prasetyo"}` {
t.Errorf("value is not equal")
}
})
t.Run("should success with command SET with a string", func(t *testing.T) {
cm.Message = []byte(`SET v "this is value stored to kece"`)
err := cm.ValidateMessage()
if err != nil {
t.Errorf("error validate client message with SET command for string data %s", err.Error())
}
if string(cm.Value) != `this is value stored to kece` {
t.Errorf("value is not equal")
}
})
t.Run("should error with command SET with invalid value", func(t *testing.T) {
cm.Message = []byte(`SET v "test'`)
err := cm.ValidateMessage()
if err == nil {
t.Errorf("error validate client message with SET command for invalid value")
}
})
}
func TestIsValidValue(t *testing.T) {
tests := []struct {
name string
args string
wantValue string
wantExpired int
wantError bool
}{
{
name: "Testcase #1: Positive (store json string)",
args: `{"field": "this is value from field a"}`,
wantError: false,
wantValue: `{"field": "this is value from field a"}`,
},
{
name: "Testcase #2: Positive (store a string with spaces)",
args: `"this is value"`,
wantError: false,
wantValue: `this is value`,
},
{
name: "Testcase #3: Positive (store a string with spaces)",
args: `'this is value'`,
wantError: false,
wantValue: `this is value`,
},
{
name: "Testcase #4: Positive (store a string with expired value)",
args: `'this is value with lifetime 20 seconds' 20`,
wantError: false,
wantValue: `this is value with lifetime 20 seconds`,
wantExpired: 20,
},
{
name: "Testcase #5: Negative (invalid sign)",
args: `"this is value'`,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, expired, err := processingValue(tt.args)
if (err != nil) != tt.wantError {
t.Errorf("error: processingValue() = %v, want %v", err, tt.wantError)
}
if err == nil && value != tt.wantValue {
t.Errorf("value: processingValue() = %v, want %v", value, tt.wantValue)
}
if err == nil && expired != tt.wantExpired {
t.Errorf("expired: processingValue() = %v, want %v", expired, tt.wantExpired)
}
})
}
}