Skip to content

improve Kredis::Types::List #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
class Kredis::Types::List < Kredis::Types::Proxying
prepend Kredis::DefaultValues

proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del
proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :llen

attr_accessor :typed

def elements
strings_to_types(lrange(0, -1) || [], typed)
slice(0, -1)
end
alias to_a elements

def slice(start = 0, stop = -1)
strings_to_types(lrange(start, stop) || [], typed)
end

def remove(*elements)
types_to_strings(elements, typed).each { |element| lrem 0, element }
end
Expand All @@ -29,10 +33,20 @@ def clear
del
end

def first(n = nil)
n ? slice(0, n - 1) : slice(0, 0).first
end

def last(n = nil)
n ? lrange(-n, -1) : lrange(-1, -1).first
n ? slice(-n, -1) : slice(-1, -1).first
end

def size
llen
end

alias length size

private
def set_default
append default
Expand Down
70 changes: 61 additions & 9 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
class ListTest < ActiveSupport::TestCase
setup { @list = Kredis.list "mylist" }

test "elements" do
@list.clear
@list.append %w[ 1 2 3 ]
assert_equal %w[ 1 2 3 ], @list.elements
end

test "append" do
@list.append(%w[ 1 2 3 ])
@list << 4
Expand Down Expand Up @@ -43,6 +49,16 @@ class ListTest < ActiveSupport::TestCase
assert_equal [], @list.elements
end

test "first" do
@list.prepend(%w[ 1 2 3 ])
assert_equal "3", @list.first
end

test "first(n)" do
@list.prepend(%w[ 1 2 3 ])
assert_equal %w[ 3 2 ], @list.first(2)
end

test "last" do
@list.append(%w[ 1 2 3 ])
assert_equal "3", @list.last
Expand All @@ -53,14 +69,11 @@ class ListTest < ActiveSupport::TestCase
assert_equal %w[ 2 3 ], @list.last(2)
end

test "typed as datetime" do
@list = Kredis.list "mylist", typed: :datetime

@list.append [ 1.day.from_now.midnight.in_time_zone("Pacific Time (US & Canada)"), 2.days.from_now.midnight.in_time_zone("UTC") ]
assert_equal [ 1.day.from_now.midnight, 2.days.from_now.midnight ], @list.elements

@list.remove(2.days.from_now.midnight)
assert_equal [ 1.day.from_now.midnight ], @list.elements
test "size" do
@list.clear
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would assert_equal 0, @list.size here rather than having a separate test for size after removal.

assert_equal 0, @list.size
@list.append(%w[ 1 2 3 ])
assert_equal 3, @list.size
end

test "exists?" do
Expand All @@ -76,7 +89,6 @@ class ListTest < ActiveSupport::TestCase
assert_equal %w[ 2 3 ], @list.elements
end


test "default" do
@list = Kredis.list "mylist", default: %w[ 1 2 3 ]

Expand Down Expand Up @@ -132,3 +144,43 @@ class ListTest < ActiveSupport::TestCase
assert_equal [ 0, 1, 2, 3, 4, 10, 20, 30 ], Kredis.list("mylist", typed: :integer).to_a.sort
end
end

class TypedListTest < ActiveSupport::TestCase
setup { @list = Kredis.list "mylist.typed", typed: :integer }

test "elements" do
@list.clear
@list.append 1, 2, 3
assert_equal [ 1, 2, 3 ], @list.elements
end

test "first" do
@list.prepend 1, 2, 3
assert_equal 3, @list.first
end

test "first(n)" do
@list.prepend 3, 2, 1
assert_equal [ 1, 2 ], @list.first(2)
end

test "last" do
@list.append 1, 2, 3
assert_equal 3, @list.last
end

test "last(n)" do
@list.append 1, 2, 3
assert_equal [ 2, 3 ], @list.last(2)
end

test "typed as datetime" do
@list = Kredis.list "mylist", typed: :datetime

@list.append [ 1.day.from_now.midnight.in_time_zone("Pacific Time (US & Canada)"), 2.days.from_now.midnight.in_time_zone("UTC") ]
assert_equal [ 1.day.from_now.midnight, 2.days.from_now.midnight ], @list.elements

@list.remove(2.days.from_now.midnight)
assert_equal [ 1.day.from_now.midnight ], @list.elements
end
end