|
| 1 | +package backend |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestDictBackendKeyStorage(t *testing.T) { |
| 10 | + backend := Dict{} |
| 11 | + backend.Start() |
| 12 | + |
| 13 | + if err := backend.Store("key", "val"); err != nil { |
| 14 | + t.Errorf("Store(\"key\") got unexpected error: %s", err) |
| 15 | + } |
| 16 | + |
| 17 | + if val, err := backend.Get("key"); err != nil { |
| 18 | + t.Errorf("Get(\"key\") got unexpected error: %s", err) |
| 19 | + } else if val != "val" { |
| 20 | + t.Errorf("Get(\"key\") expected:%q actual:%q", "val", val) |
| 21 | + } |
| 22 | + |
| 23 | + if val, err := backend.Get("notkey"); err == nil { |
| 24 | + t.Errorf("Get(\"notkey\") expected error:%q instead got value:%q", errors.New("no key found"), val) |
| 25 | + } else if !strings.Contains(err.Error(), "no key found") { |
| 26 | + t.Errorf("Get(\"notkey\") expected error:%q actual:%q", "no key found", err.Error()) |
| 27 | + } |
| 28 | + |
| 29 | + if err := backend.Store("key", "newval"); err != nil { |
| 30 | + t.Errorf("Store(\"key\") unexpected expected error:%s", err) |
| 31 | + } |
| 32 | + |
| 33 | + if err := backend.Store("key", "newval"); err != nil { |
| 34 | + t.Errorf("Store(\"key\") unexpected expected error:%s", err) |
| 35 | + } |
| 36 | + |
| 37 | + if val, err := backend.Get("key"); err != nil { |
| 38 | + t.Errorf("Get(\"key\") got unexpected error: %s", err) |
| 39 | + } else if val != "newval" { |
| 40 | + t.Errorf("Get(\"key\") expected:%q actual:%q", "newval", val) |
| 41 | + } |
| 42 | + |
| 43 | + if val := backend.Delete("key"); val != true { |
| 44 | + t.Errorf("Delete(\"key\") expected success") |
| 45 | + } |
| 46 | + |
| 47 | + if val := backend.Delete("key"); val != false { |
| 48 | + t.Errorf("Delete(\"key\") expected failure") |
| 49 | + } |
| 50 | + |
| 51 | + if val := backend.Delete("notkey"); val != false { |
| 52 | + t.Errorf("Delete(\"key\") expected failure") |
| 53 | + } |
| 54 | +} |
0 commit comments