| 315 | } |
| 316 | |
| 317 | func TestListStructure_Integration(t *testing.T) { |
| 318 | list, _ := initList() |
| 319 | defer list.db.Clean() |
| 320 | |
| 321 | // Create a key and use LPush to add some values |
| 322 | key := string(randkv.GetTestKey(1)) |
| 323 | values := [][]byte{randkv.RandomValue(100), randkv.RandomValue(100), randkv.RandomValue(100)} |
| 324 | for _, value := range values { |
| 325 | listErr = list.RPush(key, value, 0) |
| 326 | assert.Nil(t, listErr) |
| 327 | } |
| 328 | |
| 329 | // Use LLen to check the length of the list |
| 330 | tmplen, err := list.LLen(key) |
| 331 | assert.Nil(t, err) |
| 332 | assert.Equal(t, tmplen, len(values)) |
| 333 | |
| 334 | // Use LRange to get all values of the list and check if they are correct |
| 335 | rangeValues, err := list.LRange(key, 0, -1) |
| 336 | assert.Nil(t, err) |
| 337 | bytesRangeValues := make([][]byte, len(rangeValues)) |
| 338 | for i := 0; i < len(rangeValues); i++ { |
| 339 | bytesRangeValues[i] = rangeValues[i].([]byte) |
| 340 | } |
| 341 | |
| 342 | assert.Equal(t, values, bytesRangeValues) |
| 343 | |
| 344 | // Use LRem to remove a value and check if it is properly removed |
| 345 | err = list.LRem(key, 1, values[0]) |
| 346 | assert.Nil(t, err) |
| 347 | rangeValues, err = list.LRange(key, 0, -1) |
| 348 | assert.Nil(t, err) |
| 349 | assert.NotContains(t, rangeValues, values[0]) |
| 350 | |
| 351 | // Use LSet to modify a value and check if it is properly modified |
| 352 | newValue := randkv.RandomValue(100) |
| 353 | err = list.LSet(key, 0, newValue, 0) |
| 354 | assert.Nil(t, err) |
| 355 | rangeValues, err = list.LRange(key, 0, -1) |
| 356 | assert.Nil(t, err) |
| 357 | assert.Contains(t, rangeValues, newValue) |
| 358 | |
| 359 | // Use LTrim to trim the list and check if it is properly trimmed |
| 360 | err = list.LTrim(key, 0, 0) |
| 361 | assert.Nil(t, err) |
| 362 | rangeValues, err = list.LRange(key, 0, -1) |
| 363 | assert.Nil(t, err) |
| 364 | assert.Equal(t, len(rangeValues), 1) |
| 365 | |
| 366 | // Use RPOPLPUSH to move a value to another list and check if it is properly moved |
| 367 | destination := string(randkv.GetTestKey(2)) |
| 368 | err = list.RPOPLPUSH(key, destination, 0) |
| 369 | assert.Nil(t, err) |
| 370 | rangeValues, err = list.LRange(key, 0, -1) |
| 371 | assert.Equal(t, ErrListEmpty, err) |
| 372 | assert.Equal(t, len(rangeValues), 0) |
| 373 | rangeValues, err = list.LRange(destination, 0, -1) |
| 374 | assert.Nil(t, err) |