-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathhash_test.rb
143 lines (117 loc) · 4.26 KB
/
hash_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# frozen_string_literal: true
require "test_helper"
require "active_support/core_ext/integer"
class HashTest < ActiveSupport::TestCase
setup { @hash = Kredis.hash "myhash" }
test "[] reading" do
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal "value2", @hash["key2"]
assert_equal "value3", @hash[:key3]
assert_nil @hash["key"]
end
test "[]= assigment" do
@hash[:key] = :value
@hash[:key2] = "value2"
assert_equal({ "key" => "value", "key2" => "value2" }, @hash.to_h)
end
test "update" do
@hash.update(key: :value)
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal({ "key" => "value", "key2" => "value2", "key3" => "value3" }, @hash.to_h)
end
test "values_at" do
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal %w[ value2 value3 ], @hash.values_at("key2", "key3")
end
test "delete" do
@hash.update(key: :value)
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal({ "key" => "value", "key2" => "value2", "key3" => "value3" }, @hash.to_h)
@hash.delete("key")
assert_equal({ "key2" => "value2", "key3" => "value3" }, @hash.to_h)
@hash.delete("key2", "key3")
assert_equal({}, @hash.to_h)
end
test "entries" do
@hash.update(key: :value)
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal({ "key" => "value", "key2" => "value2", "key3" => "value3" }, @hash.entries)
assert_equal @hash.to_h, @hash.entries
end
test "keys" do
@hash.update(key: :value)
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal %w[ key key2 key3 ], @hash.keys
end
test "values" do
@hash.update(key: :value)
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal %w[ value value2 value3 ], @hash.values
end
test "typed as integer" do
@hash = Kredis.hash "myhash", typed: :integer
@hash.update(space_invaders: 100, pong: 42)
assert_equal %w[ space_invaders pong ], @hash.keys
assert_equal [ 100, 42 ], @hash.values
assert_equal 100, @hash[:space_invaders]
assert_equal 42, @hash["pong"]
assert_equal({ "space_invaders" => 100, "pong" => 42 }, @hash.to_h)
end
test "remove" do
@hash.update("key2" => "value2")
assert_equal "value2", @hash["key2"]
@hash.remove
assert_equal({}, @hash.to_h)
end
test "clear" do
@hash.update("key2" => "value2")
assert_equal "value2", @hash["key2"]
@hash.clear
assert_equal({}, @hash.to_h)
end
test "exists?" do
assert_not @hash.exists?
@hash[:key] = :value
assert @hash.exists?
end
test "default value" do
@hash = Kredis.hash "myhash", typed: :integer, default: { space_invaders: "100", pong: "42" }
assert_equal({ "space_invaders" => 100, "pong" => 42 }, @hash.to_h)
assert_equal(%w[ space_invaders pong ], @hash.keys)
assert_equal([ 100, 42 ], @hash.values)
assert_equal(100, @hash["space_invaders"])
assert_equal([ 100, 42 ], @hash.values_at("space_invaders", "pong"))
end
test "update with default" do
@hash = Kredis.hash "myhash", typed: :integer, default: { space_invaders: "100", pong: "42" }
@hash.update(ping: "54")
assert_equal(%w[ space_invaders pong ping ], @hash.keys)
end
test "[]= with default" do
@hash = Kredis.hash "myhash", typed: :integer, default: { space_invaders: "100", pong: "42" }
@hash[:ping] = "54"
assert_equal(%w[ space_invaders pong ping ], @hash.keys)
end
test "delete with default" do
@hash = Kredis.hash "myhash", typed: :integer, default: { space_invaders: "100", pong: "42" }
@hash.delete(:pong)
assert_equal(%w[ space_invaders ], @hash.keys)
end
test "default via proc" do
@hash = Kredis.hash "myhash", typed: :integer, default: ->() { { space_invaders: "100", pong: "42" } }
assert_equal({ "space_invaders" => 100, "pong" => 42 }, @hash.to_h)
end
test "handles nil values gracefully" do
@hash.update("key" => nil, "key2" => "value2")
assert_nil @hash["key"]
assert_equal "value2", @hash["key2"]
end
test "support ttl" do
@hash = Kredis.hash "myhash", expires_in: 1.second
@hash[:key] = :value
@hash.update("key2" => "value2", "key3" => "value3")
assert_equal "value", @hash[:key]
sleep 1.1
assert_equal [], @hash.keys
end
end