redis-model-extension 0.3.8 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -1
- data/README.markdown +62 -27
- data/Rakefile +0 -6
- data/VERSION +1 -1
- data/lib/redis-model-extension/attributes.rb +38 -0
- data/lib/redis-model-extension/autoincrement_id.rb +38 -0
- data/lib/redis-model-extension/config.rb +67 -0
- data/lib/redis-model-extension/get_find.rb +146 -0
- data/lib/redis-model-extension/initialize.rb +179 -0
- data/lib/redis-model-extension/old_initialize.rb +103 -0
- data/lib/redis-model-extension/redis_key.rb +119 -0
- data/lib/redis-model-extension/save_destroy.rb +104 -0
- data/lib/redis-model-extension/store_keys.rb +34 -0
- data/lib/redis-model-extension/validation.rb +84 -0
- data/lib/redis-model-extension/value_transform.rb +51 -0
- data/lib/redis-model-extension.rb +52 -320
- data/lib/redis-model.rb +14 -0
- data/redis-model-extension.gemspec +40 -6
- data/test/helper.rb +17 -2
- data/test/models.rb +83 -0
- data/test/redis_model_old/test_redis_model_old_config.rb +218 -0
- data/test/redis_model_parts/test_attributes.rb +34 -0
- data/test/redis_model_parts/test_autoincrement_id.rb +86 -0
- data/test/redis_model_parts/test_dynamic_alias.rb +147 -0
- data/test/redis_model_parts/test_get_find.rb +111 -0
- data/test/redis_model_parts/test_hooks.rb +36 -0
- data/test/redis_model_parts/test_initialize.rb +46 -0
- data/test/redis_model_parts/test_redis_key.rb +119 -0
- data/test/redis_model_parts/test_save_destroy.rb +102 -0
- data/test/redis_model_parts/test_validation.rb +72 -0
- data/test/redis_model_parts/test_variable_types.rb +91 -0
- data/test/test_redis-model-extension.rb +38 -200
- data/test/test_string_to_bool.rb +30 -0
- metadata +90 -32
@@ -0,0 +1,119 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'helper'
|
3
|
+
class RedisKeyTest < Test::Unit::TestCase
|
4
|
+
context "Redis key" do
|
5
|
+
setup do
|
6
|
+
RedisModelExtension::Database.redis.flushdb
|
7
|
+
@time = Time.now
|
8
|
+
@args = {
|
9
|
+
"integer" => 12345,
|
10
|
+
:string => "foo",
|
11
|
+
:symbol => :bar,
|
12
|
+
:boolean => true,
|
13
|
+
:array => [1,2,3],
|
14
|
+
:hash => {"foo"=>"bar", "test" => 2},
|
15
|
+
:time => @time,
|
16
|
+
:date => Date.today
|
17
|
+
}
|
18
|
+
@test_model = TestRedisModel.new(@args)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "generate right search key" do
|
22
|
+
@test_model.string = nil
|
23
|
+
assert_equal @test_model.redis_key, "#{TestRedisModel.to_s.underscore}:key:*"
|
24
|
+
assert_equal TestRedisModel.generate_key(@args.merge({:string => nil})), "#{TestRedisModel.to_s.underscore}:key:*"
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
should "generate right key" do
|
29
|
+
assert_equal @test_model.redis_key, "#{TestRedisModel.to_s.underscore}:key:foo"
|
30
|
+
assert_equal TestRedisModel.generate_key(@args), "#{TestRedisModel.to_s.underscore}:key:foo"
|
31
|
+
end
|
32
|
+
|
33
|
+
should "generate right key alias" do
|
34
|
+
assert_equal @test_model.redis_alias_key(:token), "#{TestRedisModel.to_s.underscore}:alias:token:bar"
|
35
|
+
assert_equal TestRedisModel.generate_alias_key(:token, @args), "#{TestRedisModel.to_s.underscore}:alias:token:bar"
|
36
|
+
end
|
37
|
+
|
38
|
+
context "invalid setting" do
|
39
|
+
should "raise ArgumentError on nonexisting field" do
|
40
|
+
assert_raises ArgumentError do
|
41
|
+
class RedisKeyNonExistingKeyRedisModel
|
42
|
+
include RedisModelExtension
|
43
|
+
redis_field :string, :string
|
44
|
+
redis_key :unknown
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
should "raise ArgumentError on using Array as redis key" do
|
50
|
+
assert_raises ArgumentError do
|
51
|
+
class RedisKeyNonExistingKeyRedisModel
|
52
|
+
include RedisModelExtension
|
53
|
+
redis_field :array, :array
|
54
|
+
redis_key :array
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
should "raise ArgumentError on using Hash as redis key" do
|
60
|
+
assert_raises ArgumentError do
|
61
|
+
class RedisKeyNonExistingKeyRedisModel
|
62
|
+
include RedisModelExtension
|
63
|
+
redis_field :array, :array
|
64
|
+
redis_key :array
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
context "normalization" do
|
72
|
+
should "downcase" do
|
73
|
+
class DowncaseRedisModel
|
74
|
+
include RedisModelExtension
|
75
|
+
redis_field :string, :string
|
76
|
+
redis_key :string
|
77
|
+
redis_key_normalize :downcase
|
78
|
+
end
|
79
|
+
model = DowncaseRedisModel.new(:string => "FoO")
|
80
|
+
assert_equal model.redis_key, "#{DowncaseRedisModel.to_s.underscore}:key:foo"
|
81
|
+
end
|
82
|
+
|
83
|
+
should "transliterate" do
|
84
|
+
class TransliterateRedisModel
|
85
|
+
include RedisModelExtension
|
86
|
+
redis_field :string, :string
|
87
|
+
redis_key :string
|
88
|
+
redis_key_normalize :transliterate
|
89
|
+
end
|
90
|
+
model = TransliterateRedisModel.new(:string => "FoOšČ")
|
91
|
+
assert_equal model.redis_key, "#{TransliterateRedisModel.to_s.underscore}:key:FoOsC"
|
92
|
+
end
|
93
|
+
|
94
|
+
should "downcase & transliterate" do
|
95
|
+
class DowncaseTransliterateRedisModel
|
96
|
+
include RedisModelExtension
|
97
|
+
redis_field :string, :string
|
98
|
+
redis_key :string
|
99
|
+
redis_key_normalize :transliterate
|
100
|
+
redis_key_normalize :downcase
|
101
|
+
end
|
102
|
+
model = DowncaseTransliterateRedisModel.new(:string => "FoOšČ")
|
103
|
+
assert_equal model.redis_key, "#{DowncaseTransliterateRedisModel.to_s.underscore}:key:foosc"
|
104
|
+
end
|
105
|
+
|
106
|
+
should "downcase & transliterate in one setting" do
|
107
|
+
class DowncaseTransliterateOneRedisModel
|
108
|
+
include RedisModelExtension
|
109
|
+
redis_field :string, :string
|
110
|
+
redis_key :string
|
111
|
+
redis_key_normalize :transliterate, :downcase
|
112
|
+
end
|
113
|
+
model = DowncaseTransliterateOneRedisModel.new(:string => "FoOšČ")
|
114
|
+
assert_equal model.redis_key, "#{DowncaseTransliterateOneRedisModel.to_s.underscore}:key:foosc"
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'helper'
|
3
|
+
class SaveDestroyTest < Test::Unit::TestCase
|
4
|
+
context "Save, Update & Destroy" do
|
5
|
+
setup do
|
6
|
+
RedisModelExtension::Database.redis.flushdb
|
7
|
+
@time = Time.now
|
8
|
+
@args = {
|
9
|
+
"integer" => 12345,
|
10
|
+
:string => "foo",
|
11
|
+
:symbol => :bar,
|
12
|
+
:boolean => true,
|
13
|
+
:array => [1,2,3],
|
14
|
+
:hash => {"foo"=>"bar", "test" => 2},
|
15
|
+
:time => @time,
|
16
|
+
:date => Date.today,
|
17
|
+
:float => 12.54,
|
18
|
+
}
|
19
|
+
@test_model = TestRedisModel.new(@args)
|
20
|
+
end
|
21
|
+
|
22
|
+
context "create" do
|
23
|
+
should "be saved" do
|
24
|
+
assert test_model = TestRedisModel.create(@args), "should be saved"
|
25
|
+
assert !test_model.errors.any?, "no erros"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "saving" do
|
30
|
+
setup do
|
31
|
+
@test_model.save
|
32
|
+
end
|
33
|
+
|
34
|
+
should "be saved and then change of variable included in key should rename it in redis!" do
|
35
|
+
assert_equal RedisModelExtension::Database.redis.keys("*").size, 2 #including key and alias
|
36
|
+
@test_model.symbol = :changed_symbol
|
37
|
+
@test_model.string = "change_of_string"
|
38
|
+
@test_model.save
|
39
|
+
assert_equal RedisModelExtension::Database.redis.keys("*").size, 2 #including key and alias
|
40
|
+
end
|
41
|
+
|
42
|
+
should "have same elements after get" do
|
43
|
+
@getted_model = TestRedisModel.get(@args)
|
44
|
+
assert_equal @getted_model.integer, @test_model.integer
|
45
|
+
assert_equal @getted_model.string, @test_model.string
|
46
|
+
assert_equal @getted_model.symbol, @test_model.symbol
|
47
|
+
assert_equal @getted_model.boolean, @test_model.boolean
|
48
|
+
end
|
49
|
+
|
50
|
+
should "have same elements after get and to_arg" do
|
51
|
+
@getted_model = TestRedisModel.get(@args)
|
52
|
+
assert_same_elements @getted_model.to_arg.to_json.split(","), @args.to_json.split(",")
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
context "updating" do
|
58
|
+
setup do
|
59
|
+
@new_args = {:integer => 123457, :string => "bar", :symbol => :foo, :boolean => false}
|
60
|
+
end
|
61
|
+
|
62
|
+
should "change attributes" do
|
63
|
+
@test_model.update @new_args
|
64
|
+
assert_equal @test_model.integer, @new_args[:integer]
|
65
|
+
assert_equal @test_model.string, @new_args[:string]
|
66
|
+
assert_equal @test_model.symbol, @new_args[:symbol]
|
67
|
+
assert_equal @test_model.boolean, @new_args[:boolean]
|
68
|
+
end
|
69
|
+
|
70
|
+
should "ignore unknown attributes and other normaly update" do
|
71
|
+
@test_model.update @new_args.merge(:unknown => "attribute")
|
72
|
+
assert_equal @test_model.integer, @new_args[:integer]
|
73
|
+
assert_equal @test_model.string, @new_args[:string]
|
74
|
+
assert_equal @test_model.symbol, @new_args[:symbol]
|
75
|
+
assert_equal @test_model.boolean, @new_args[:boolean]
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
context "destroy" do
|
81
|
+
|
82
|
+
setup do
|
83
|
+
@test_model.save
|
84
|
+
@redis_key = @test_model.redis_key
|
85
|
+
end
|
86
|
+
|
87
|
+
should "remove key and aliases" do
|
88
|
+
before = RedisModelExtension::Database.redis.keys("*").size
|
89
|
+
@test_model.destroy!
|
90
|
+
assert_equal RedisModelExtension::Database.redis.keys("*").size, before-2 #including key and alias
|
91
|
+
end
|
92
|
+
|
93
|
+
should "not be found" do
|
94
|
+
assert test = TestRedisModel.get(@args)
|
95
|
+
test.destroy!
|
96
|
+
assert_equal TestRedisModel.exists?(@args), false, "Should not exists"
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'helper'
|
3
|
+
class ValidationTest < Test::Unit::TestCase
|
4
|
+
context "Validation" do
|
5
|
+
setup do
|
6
|
+
RedisModelExtension::Database.redis.flushdb
|
7
|
+
@time = Time.now
|
8
|
+
@args = {
|
9
|
+
"integer" => 12345,
|
10
|
+
:string => "foo",
|
11
|
+
:symbol => :bar,
|
12
|
+
:boolean => true,
|
13
|
+
:array => [1,2,3],
|
14
|
+
:hash => {"foo"=>"bar", "test" => 2},
|
15
|
+
:time => @time,
|
16
|
+
:date => Date.today
|
17
|
+
}
|
18
|
+
@test_model = TestRedisModel.new(@args)
|
19
|
+
end
|
20
|
+
|
21
|
+
context "class" do
|
22
|
+
|
23
|
+
should "validate redis_key" do
|
24
|
+
assert TestRedisModel.valid_key?(:string => "test")
|
25
|
+
assert !TestRedisModel.valid_key?(:string => nil)
|
26
|
+
assert !TestRedisModel.valid_key?()
|
27
|
+
end
|
28
|
+
|
29
|
+
should "validate redis_alias_key" do
|
30
|
+
assert TestRedisModel.valid_alias_key?(:token, :symbol => :test)
|
31
|
+
assert !TestRedisModel.valid_alias_key?(:token, :symbol => nil)
|
32
|
+
assert !TestRedisModel.valid_alias_key?(:token)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
context "instance" do
|
38
|
+
|
39
|
+
should "validate alias key" do
|
40
|
+
assert @test_model.valid_alias_key?(:token)
|
41
|
+
end
|
42
|
+
|
43
|
+
should "validate key" do
|
44
|
+
assert @test_model.valid_key?
|
45
|
+
end
|
46
|
+
|
47
|
+
should "return errors after valid?" do
|
48
|
+
test = TestRedisModel.new()
|
49
|
+
assert !test.valid?, "shouldn't be valid"
|
50
|
+
assert_equal test.errors.messages.size, 2, "should have 2 errors (2 required fields)"
|
51
|
+
assert_equal test.error.messages.size, 2, "error should be asliased to errors"
|
52
|
+
end
|
53
|
+
|
54
|
+
should "return errors and be aliased to error" do
|
55
|
+
test = TestRedisModel.new()
|
56
|
+
assert !test.valid?, "shouldn't be valid"
|
57
|
+
assert_equal test.error, test.errors
|
58
|
+
end
|
59
|
+
|
60
|
+
should "not raise exeption on invalid initialize" do
|
61
|
+
assert_nothing_raised { TestRedisModel.new() }
|
62
|
+
end
|
63
|
+
|
64
|
+
should "return false on save" do
|
65
|
+
test_model = TestRedisModel.new()
|
66
|
+
assert !test_model.save, "return false on save"
|
67
|
+
assert test_model.errors.any?, "have any error"
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'helper'
|
3
|
+
class VariableTypesTest < Test::Unit::TestCase
|
4
|
+
context "Definition & Types of variables" do
|
5
|
+
setup do
|
6
|
+
RedisModelExtension::Database.redis.flushdb
|
7
|
+
|
8
|
+
@time = Time.now
|
9
|
+
@args = {
|
10
|
+
"integer" => 12345,
|
11
|
+
:string => "foo",
|
12
|
+
:symbol => :bar,
|
13
|
+
:boolean => true,
|
14
|
+
:array => [1,2,3],
|
15
|
+
:hash => {"foo"=>"bar", "test" => 2},
|
16
|
+
:time => @time,
|
17
|
+
:date => Date.today,
|
18
|
+
:float => 12.43,
|
19
|
+
}
|
20
|
+
@test_model = TestRedisModel.new(@args)
|
21
|
+
@test_model_partial = TestRedisModel.new(:integer => 12345, :string => "foo")
|
22
|
+
end
|
23
|
+
|
24
|
+
context "on invalid type" do
|
25
|
+
should "return not modified value" do
|
26
|
+
assert_equal @test_model.value_transform("Test", :unknown_type), "Test", "Value transform"
|
27
|
+
assert_equal @test_model.value_parse("Test", :unknown_type), "Test", "Value parse"
|
28
|
+
assert_equal @test_model.value_to_redis(:unknown_field_name, "Test"), "Test", "Value to redis"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
should "get valid arguments" do
|
33
|
+
assert_equal @test_model.integer, 12345
|
34
|
+
assert_equal @test_model.string, "foo"
|
35
|
+
assert_equal @test_model.symbol, :bar
|
36
|
+
assert_equal @test_model.boolean, true
|
37
|
+
assert_equal @test_model.array, [1,2,3]
|
38
|
+
assert_equal @test_model.hash, {"foo"=>"bar", "test" => 2}
|
39
|
+
assert_equal @test_model.time, @time
|
40
|
+
assert_equal @test_model.date, Date.today
|
41
|
+
assert_equal @test_model.float, 12.43
|
42
|
+
end
|
43
|
+
|
44
|
+
should "return default value when value is nil" do
|
45
|
+
assert_equal @test_model_partial.symbol, :default
|
46
|
+
end
|
47
|
+
|
48
|
+
should "return valid exists?" do
|
49
|
+
assert_equal @test_model.integer?, true
|
50
|
+
assert_equal @test_model.string?, true
|
51
|
+
assert_equal @test_model.symbol?, true
|
52
|
+
assert_equal @test_model.boolean?, true
|
53
|
+
assert_equal @test_model.array?, true
|
54
|
+
assert_equal @test_model.hash?, true
|
55
|
+
assert_equal @test_model.time?, true
|
56
|
+
assert_equal @test_model.date?, true
|
57
|
+
assert_equal @test_model.float?, true
|
58
|
+
|
59
|
+
assert_equal @test_model_partial.integer?, true
|
60
|
+
assert_equal @test_model_partial.string?, true
|
61
|
+
assert_equal @test_model_partial.symbol?, true, "should be set by default value"
|
62
|
+
assert_equal @test_model_partial.boolean?, false
|
63
|
+
assert_equal @test_model_partial.hash?, false
|
64
|
+
assert_equal @test_model_partial.array?, false
|
65
|
+
assert_equal @test_model_partial.time?, false
|
66
|
+
assert_equal @test_model_partial.date?, false
|
67
|
+
assert_equal @test_model_partial.float?, false
|
68
|
+
end
|
69
|
+
|
70
|
+
should "be assign new values" do
|
71
|
+
@test_model.integer = 54321
|
72
|
+
@test_model.string = "bar"
|
73
|
+
@test_model.symbol = :foo
|
74
|
+
@test_model.boolean = false
|
75
|
+
@test_model.array = [4,5,6]
|
76
|
+
@test_model.hash = {"bar" => "foo"}
|
77
|
+
@test_model.time = @time-100
|
78
|
+
@test_model.date = Date.today-10
|
79
|
+
@test_model.float = 25.43
|
80
|
+
assert_equal @test_model.integer, 54321
|
81
|
+
assert_equal @test_model.string, "bar"
|
82
|
+
assert_equal @test_model.symbol, :foo
|
83
|
+
assert_equal @test_model.boolean, false
|
84
|
+
assert_equal @test_model.array, [4,5,6]
|
85
|
+
assert_equal @test_model.hash, {"bar" => "foo"}
|
86
|
+
assert_equal @test_model.time, @time-100
|
87
|
+
assert_equal @test_model.date, Date.today-10
|
88
|
+
assert_equal @test_model.float, 25.43
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -1,186 +1,11 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
require 'helper'
|
3
3
|
class RedisModelTest < Test::Unit::TestCase
|
4
|
-
context "
|
5
|
-
setup do
|
6
|
-
RedisModelExtension::Database.redis.flushdb
|
7
|
-
|
8
|
-
class TestRedisModel
|
9
|
-
REDIS_MODEL_CONF = {
|
10
|
-
:fields => {
|
11
|
-
:integer => :to_i,
|
12
|
-
:boolean => :to_bool,
|
13
|
-
:string => :to_s,
|
14
|
-
:symbol => :to_sym,
|
15
|
-
},
|
16
|
-
:required => [:integer, :string],
|
17
|
-
:redis_key => [:string],
|
18
|
-
:redis_aliases => {
|
19
|
-
:token => [:symbol]
|
20
|
-
}
|
21
|
-
}
|
22
|
-
include RedisModel
|
23
|
-
initialize_redis_model_methods REDIS_MODEL_CONF
|
24
|
-
end
|
25
|
-
@args = {:integer => 12345, :string => "foo", :symbol => :bar, :boolean => true}
|
26
|
-
@test_model = TestRedisModel.new(@args)
|
27
|
-
@test_model_partial = TestRedisModel.new(:integer => 12345, :string => "foo")
|
28
|
-
end
|
29
|
-
|
30
|
-
context "define methods" do
|
31
|
-
should "be accessible" do
|
32
|
-
assert @test_model.respond_to?(:integer)
|
33
|
-
assert @test_model.respond_to?(:boolean)
|
34
|
-
assert @test_model.respond_to?(:string)
|
35
|
-
assert @test_model.respond_to?(:symbol)
|
36
|
-
end
|
37
|
-
|
38
|
-
should "get valid arguments" do
|
39
|
-
assert_equal @test_model.integer, 12345
|
40
|
-
assert_equal @test_model.string, "foo"
|
41
|
-
assert_equal @test_model.symbol, :bar
|
42
|
-
assert_equal @test_model.boolean, true
|
43
|
-
end
|
44
|
-
|
45
|
-
should "return valid exists?" do
|
46
|
-
assert_equal @test_model.integer?, true
|
47
|
-
assert_equal @test_model.string?, true
|
48
|
-
assert_equal @test_model.symbol?, true
|
49
|
-
assert_equal @test_model.boolean?, true
|
50
|
-
|
51
|
-
assert_equal @test_model_partial.integer?, true
|
52
|
-
assert_equal @test_model_partial.string?, true
|
53
|
-
assert_equal @test_model_partial.symbol?, false
|
54
|
-
assert_equal @test_model_partial.boolean?, false
|
55
|
-
end
|
56
|
-
|
57
|
-
should "be assign new values" do
|
58
|
-
@test_model.integer = 54321
|
59
|
-
@test_model.string = "bar"
|
60
|
-
@test_model.symbol = :foo
|
61
|
-
@test_model.boolean = false
|
62
|
-
assert_equal @test_model.integer, 54321
|
63
|
-
assert_equal @test_model.string, "bar"
|
64
|
-
assert_equal @test_model.symbol, :foo
|
65
|
-
assert_equal @test_model.boolean, false
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context "redis key" do
|
70
|
-
should "generate right key" do
|
71
|
-
assert_equal @test_model.redis_key, "#{TestRedisModel.to_s.underscore}:key:foo"
|
72
|
-
assert_equal TestRedisModel.generate_key(@args), "#{TestRedisModel.to_s.underscore}:key:foo"
|
73
|
-
end
|
74
|
-
should "generate right key alias" do
|
75
|
-
assert_equal @test_model.redis_alias_key(:token), "#{TestRedisModel.to_s.underscore}:alias:token:bar"
|
76
|
-
assert_equal TestRedisModel.generate_alias_key(:token, @args), "#{TestRedisModel.to_s.underscore}:alias:token:bar"
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context "after initialize" do
|
81
|
-
should "clear input arguments" do
|
82
|
-
test_model = TestRedisModel.new(@args.merge({:foor => :bar, :not_in_fields => "foo"}))
|
83
|
-
assert_same_elements test_model.args, @args
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
context "validation" do
|
88
|
-
|
89
|
-
should "return errors after valid?" do
|
90
|
-
test = TestRedisModel.new()
|
91
|
-
assert !test.valid?, "shouldn't be valid"
|
92
|
-
assert_equal test.errors.size, 2, "should have 2 errors (2 required fields)"
|
93
|
-
end
|
94
|
-
|
95
|
-
should "be able to add custom error (ex. in initialize)" do
|
96
|
-
test = TestRedisModel.new()
|
97
|
-
test.error << "my custom error"
|
98
|
-
assert !test.valid?, "shouldn't be valid"
|
99
|
-
assert_equal test.errors.size, 3, "should have 3 errors (2 required fields + 1 custom error)"
|
100
|
-
assert_equal test.error.size, test.errors.size, "error and errors should be same (only as backup)"
|
101
|
-
end
|
102
|
-
|
103
|
-
should "not raise exeption on invalid initialize" do
|
104
|
-
assert_nothing_raised { TestRedisModel.new() }
|
105
|
-
end
|
106
|
-
|
107
|
-
should "raise exeption on save" do
|
108
|
-
test_model = TestRedisModel.new()
|
109
|
-
assert_raises ArgumentError do
|
110
|
-
test_model.save
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "updating" do
|
116
|
-
setup do
|
117
|
-
@new_args = {:integer => 123457, :string => "bar", :symbol => :foo, :boolean => false}
|
118
|
-
end
|
4
|
+
context "Redis Model" do
|
119
5
|
|
120
|
-
should "change attributes" do
|
121
|
-
@test_model.update @new_args
|
122
|
-
assert_equal @test_model.integer, @new_args[:integer]
|
123
|
-
assert_equal @test_model.string, @new_args[:string]
|
124
|
-
assert_equal @test_model.symbol, @new_args[:symbol]
|
125
|
-
assert_equal @test_model.boolean, @new_args[:boolean]
|
126
|
-
end
|
127
|
-
|
128
|
-
should "ignore unknown attributes and other normaly update" do
|
129
|
-
@test_model.update @new_args.merge(:unknown => "attribute")
|
130
|
-
assert_equal @test_model.integer, @new_args[:integer]
|
131
|
-
assert_equal @test_model.string, @new_args[:string]
|
132
|
-
assert_equal @test_model.symbol, @new_args[:symbol]
|
133
|
-
assert_equal @test_model.boolean, @new_args[:boolean]
|
134
|
-
end
|
135
|
-
|
136
|
-
end
|
137
|
-
|
138
|
-
context "saving" do
|
139
|
-
setup do
|
140
|
-
@test_model.save
|
141
|
-
end
|
142
|
-
|
143
|
-
should "be saved and then change of variable included in key should rename it in redis!" do
|
144
|
-
assert_equal RedisModelExtension::Database.redis.keys("*").size, 2 #including key and alias
|
145
|
-
@test_model.string = "change_of_strging"
|
146
|
-
@test_model.save
|
147
|
-
assert_equal RedisModelExtension::Database.redis.keys("*").size, 2 #including key and alias
|
148
|
-
end
|
149
|
-
|
150
|
-
should "have same elements after get" do
|
151
|
-
@getted_model = TestRedisModel.get(@args)
|
152
|
-
assert_equal @getted_model.integer, @test_model.integer
|
153
|
-
assert_equal @getted_model.string, @test_model.string
|
154
|
-
assert_equal @getted_model.symbol, @test_model.symbol
|
155
|
-
assert_equal @getted_model.boolean, @test_model.boolean
|
156
|
-
end
|
157
|
-
|
158
|
-
should "have same elements after get and to_arg" do
|
159
|
-
@getted_model = TestRedisModel.get(@args)
|
160
|
-
assert_equal @getted_model.to_arg, @args
|
161
|
-
end
|
162
|
-
|
163
|
-
context "alias" do
|
164
|
-
should "be getted by alias" do
|
165
|
-
@getted_model = TestRedisModel.get_by_alias(:token ,@args)
|
166
|
-
assert_equal @getted_model.integer, @test_model.integer
|
167
|
-
assert_equal @getted_model.string, @test_model.string
|
168
|
-
assert_equal @getted_model.symbol, @test_model.symbol
|
169
|
-
assert_equal @getted_model.boolean, @test_model.boolean
|
170
|
-
end
|
171
|
-
|
172
|
-
should "be getted after change in alias" do
|
173
|
-
getted_model = TestRedisModel.get_by_alias(:token ,@args)
|
174
|
-
getted_model.symbol = "Test_token"
|
175
|
-
getted_model.save
|
176
|
-
assert_equal getted_model.integer, TestRedisModel.get_by_alias(:token ,:symbol => "Test_token").integer
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
6
|
context "without rejected nil values on save" do
|
182
7
|
should "save nil values" do
|
183
|
-
class
|
8
|
+
class NilTestOldRedisModel
|
184
9
|
REDIS_MODEL_CONF = {
|
185
10
|
:fields => {
|
186
11
|
:integer => :to_i,
|
@@ -190,34 +15,47 @@ class RedisModelTest < Test::Unit::TestCase
|
|
190
15
|
:redis_key => [:string],
|
191
16
|
:redis_aliases => {},
|
192
17
|
:reject_nil_values => false
|
193
|
-
|
194
|
-
|
195
|
-
|
18
|
+
}
|
19
|
+
include RedisModel
|
20
|
+
initialize_redis_model_methods REDIS_MODEL_CONF
|
196
21
|
end
|
197
|
-
args = {integer: 100, string: "test"}
|
198
|
-
nil_test_model = NilTestRedisModel.new(args)
|
199
22
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
assert_equal founded.integer, 100
|
208
|
-
assert_equal founded.string, "test"
|
23
|
+
class NilTestRedisModel
|
24
|
+
include RedisModelExtension
|
25
|
+
redis_field :integer, :integer
|
26
|
+
redis_field :string, :string
|
27
|
+
redis_key :string
|
28
|
+
redis_save_fields_with_nil true
|
29
|
+
end
|
209
30
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
31
|
+
[NilTestOldRedisModel, NilTestRedisModel].each do |klass|
|
32
|
+
args = {integer: 100, string: "test"}
|
33
|
+
nil_test_model = klass.new(args)
|
34
|
+
|
35
|
+
#on initialize
|
36
|
+
assert_equal nil_test_model.integer, 100, "For #{klass} should have integer = 100"
|
37
|
+
assert_equal nil_test_model.string, "test", "For #{klass} should have string = test"
|
38
|
+
nil_test_model.save
|
39
|
+
|
40
|
+
#after find
|
41
|
+
founded = klass.get(args)
|
42
|
+
assert_equal founded.integer, 100, "After get for #{klass} should have integer = 100"
|
43
|
+
assert_equal founded.string, "test", "After get for #{klass} should have string = test"
|
44
|
+
|
45
|
+
#set integer to nil
|
46
|
+
founded.integer = nil
|
47
|
+
assert_equal founded.integer, nil, "After get and save for #{klass} should have integer = nil"
|
48
|
+
#perform save
|
49
|
+
founded.save
|
50
|
+
|
51
|
+
#after second find
|
52
|
+
founded = klass.get(args)
|
53
|
+
assert_equal founded.integer, nil, "After 2nd get for #{klass} should have integer = nil"
|
54
|
+
assert_equal founded.string, "test", "After 2nd get for #{klass} should have string = test"
|
55
|
+
end
|
215
56
|
|
216
|
-
#after second find
|
217
|
-
founded = NilTestRedisModel.get(args)
|
218
|
-
assert_equal founded.integer, nil
|
219
|
-
assert_equal founded.string, "test"
|
220
57
|
end
|
221
58
|
end
|
59
|
+
|
222
60
|
end
|
223
61
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'helper'
|
3
|
+
class StringToBoolTest < Test::Unit::TestCase
|
4
|
+
context "String to_bool" do
|
5
|
+
|
6
|
+
should "return true" do
|
7
|
+
assert "true".to_bool, "true"
|
8
|
+
assert "1".to_bool, "1"
|
9
|
+
assert "t".to_bool, "t"
|
10
|
+
assert "y".to_bool, "y"
|
11
|
+
assert "yes".to_bool, "yes"
|
12
|
+
end
|
13
|
+
|
14
|
+
should "return false" do
|
15
|
+
assert ! "false".to_bool, "false"
|
16
|
+
assert ! "0".to_bool, "0"
|
17
|
+
assert ! "f".to_bool, "f"
|
18
|
+
assert ! "no".to_bool, "no"
|
19
|
+
assert ! "n".to_bool, "n"
|
20
|
+
assert ! "".to_bool, "blank string"
|
21
|
+
end
|
22
|
+
|
23
|
+
should "raise exception on unknown" do
|
24
|
+
assert_raises ArgumentError do
|
25
|
+
"unknown string".to_bool
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|