Skip to content

Commit

Permalink
do proper list copy
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebob committed Jun 21, 2023
1 parent f507fa8 commit fb75409
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@ func TestCopy(t *testing.T) {
s.CheckGet(t, "existingkey", "value")
})

t.Run("list", func(t *testing.T) {
must1(t, c, "LPUSH", "l1", "original")
must1(t, c, "COPY", "l1", "l2")
mustOK(t, c, "LSET", "l1", "0", "modified")
s.CheckList(t, "l1", "modified")
s.CheckList(t, "l2", "original")
})

t.Run("destination db", func(t *testing.T) {
s.Set("akey1", "value")
must1(t, c, "COPY", "akey1", "akey2", "DB", "2")
Expand Down
9 changes: 9 additions & 0 deletions integration/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@ func TestCopy(t *testing.T) {
c.Do("HGET", "temp2", "oslo")
})

t.Run("list set", func(t *testing.T) {
c.Do("LPUSH", "list", "aap", "noot", "mies")
c.Do("COPY", "list", "list2")
c.Do("TYPE", "list2")
c.Do("LSET", "list", "1", "vuur")
c.Do("LRANGE", "list", "0", "-1")
c.Do("LRANGE", "list2", "0", "-1")
})

t.Run("list", func(t *testing.T) {
c.Do("LPUSH", "list", "aap", "noot", "mies")
c.Do("COPY", "list", "list2")
Expand Down
8 changes: 7 additions & 1 deletion miniredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func (m *Miniredis) copy(
case "hash":
destDB.hashKeys[dst] = copyHashKey(srcDB.hashKeys[src])
case "list":
destDB.listKeys[dst] = srcDB.listKeys[src]
destDB.listKeys[dst] = copyListKey(srcDB.listKeys[src])
case "set":
destDB.setKeys[dst] = copySetKey(srcDB.setKeys[src])
case "zset":
Expand Down Expand Up @@ -705,6 +705,12 @@ func copyHashKey(orig hashKey) hashKey {
return cpy
}

func copyListKey(orig listKey) listKey {
cpy := make(listKey, len(orig))
copy(cpy, orig)
return cpy
}

func copySetKey(orig setKey) setKey {
cpy := setKey{}
for k, v := range orig {
Expand Down

0 comments on commit fb75409

Please sign in to comment.