|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "HelixDB/common" |
| 5 | + "errors" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestExists(t *testing.T) { |
| 11 | + _, _ = Set(common.Cmd{Name: "SET", Args: []string{"ex_key1", "value1"}}) |
| 12 | + _, _ = Set(common.Cmd{Name: "SET", Args: []string{"ex_key2", "value2"}}) |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + command common.Cmd |
| 16 | + want []byte |
| 17 | + wantErr error |
| 18 | + }{ |
| 19 | + // Single existing key |
| 20 | + {common.Cmd{Name: "EXISTS", Args: []string{"ex_key1"}}, common.RespInteger(1), nil}, |
| 21 | + // Single non-existent key |
| 22 | + {common.Cmd{Name: "EXISTS", Args: []string{"no_such_key"}}, common.RespInteger(0), nil}, |
| 23 | + // Multiple keys, all exist |
| 24 | + {common.Cmd{Name: "EXISTS", Args: []string{"ex_key1", "ex_key2"}}, common.RespInteger(2), nil}, |
| 25 | + // Multiple keys, some missing |
| 26 | + {common.Cmd{Name: "EXISTS", Args: []string{"ex_key1", "no_such_key"}}, common.RespInteger(1), nil}, |
| 27 | + // Duplicate key counts multiple times |
| 28 | + {common.Cmd{Name: "EXISTS", Args: []string{"ex_key1", "ex_key1"}}, common.RespInteger(2), nil}, |
| 29 | + // Wrong number of arguments |
| 30 | + {common.Cmd{Name: "EXISTS"}, common.RespError("wrong number of arguments for 'exists' command"), common.ErrWrongNumberOfArgs}, |
| 31 | + } |
| 32 | + for _, test := range tests { |
| 33 | + if got, gotErr := Exists(test.command); !reflect.DeepEqual(got, test.want) || !errors.Is(gotErr, test.wantErr) { |
| 34 | + t.Errorf("Exists(%v) = %v, %v; want %v, %v", test.command, got, gotErr, test.want, test.wantErr) |
| 35 | + } |
| 36 | + } |
| 37 | +} |
0 commit comments