Skip to content

Commit

Permalink
Add some test
Browse files Browse the repository at this point in the history
  • Loading branch information
heka1024 committed Mar 3, 2024
1 parent 6eaab52 commit 4b69e05
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class Person
kredis_list :names_with_custom_key_via_lambda, key: ->(p) { "person:#{p.id}:names_customized" }
kredis_list :names_with_custom_key_via_method, key: :generate_key
kredis_list :names_with_default_via_lambda, default: ->(p) { [ "Random", p.name ] }
kredis_list :names_with_ttl, expires_in: 1.second
kredis_unique_list :skills, limit: 2
kredis_unique_list :skills_with_default_via_lambda, default: ->(p) { [ "Random", "Random", p.name ] }
kredis_unique_list :skills_with_ttl, expires_in: 1.second
kredis_ordered_set :reading_list, limit: 2
kredis_flag :special
kredis_flag :temporary_special, expires_in: 1.second
Expand All @@ -34,6 +36,7 @@ class Person
kredis_slots :meetings, available: 3
kredis_set :vacations
kredis_set :vacations_with_default_via_lambda, default: ->(p) { JSON.parse(p.vacation_destinations).map { |location| location["city"] } }
kredis_set :vacations_with_ttl, expires_in: 1.second
kredis_json :settings
kredis_json :settings_with_default_via_lambda, default: ->(p) { JSON.parse(p.anthropometry).merge(eye_color: p.eye_color) }
kredis_counter :amount
Expand Down Expand Up @@ -148,6 +151,14 @@ class AttributesTest < ActiveSupport::TestCase
assert_equal %w[ Random Jason ], Kredis.redis.lrange("people:8:names_with_default_via_lambda", 0, -1)
end

test "list with ttl" do
@person.names_with_ttl.append(%w[ david kasper ])
assert_equal %w[ david kasper ], @person.names_with_ttl.elements

sleep 1.1
assert_equal [], @person.names_with_ttl.elements
end

test "unique list" do
@person.skills.prepend(%w[ trolling photography ])
@person.skills.prepend("racing")
Expand All @@ -160,6 +171,14 @@ class AttributesTest < ActiveSupport::TestCase
assert_equal %w[ Random Jason ], Kredis.redis.lrange("people:8:skills_with_default_via_lambda", 0, -1)
end

test "unique list with ttl" do
@person.skills_with_ttl.prepend(%w[ trolling photography ])
assert_equal %w[ trolling photography ].to_set, @person.skills_with_ttl.elements.to_set

sleep 1.1
assert_equal [], @person.skills_with_ttl.elements
end

test "ordered set" do
@person.reading_list.prepend(%w[ rework shapeup remote ])
assert_equal %w[ remote shapeup ], @person.reading_list.elements
Expand Down Expand Up @@ -324,6 +343,14 @@ class AttributesTest < ActiveSupport::TestCase
assert_equal [ "Paris" ], Kredis.redis.smembers("people:8:vacations_with_default_via_lambda")
end

test "set with ttl" do
@person.vacations_with_ttl.add "paris"
assert_equal [ "paris" ], @person.vacations_with_ttl.members

sleep 1.1
assert_equal [], @person.vacations_with_ttl.members
end

test "json" do
@person.settings.value = { "color" => "red", "count" => 2 }
assert_equal({ "color" => "red", "count" => 2 }, @person.settings.value)
Expand Down

0 comments on commit 4b69e05

Please sign in to comment.