redisted 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe "References" do
4
+ it "can create a reference_one" do
5
+ class Reference1 < Redisted::Base
6
+ field :name, type: :string
7
+ end
8
+ class ReferenceTestB1 < Redisted::Base
9
+ field :name, type: :string
10
+ references_one :reference1
11
+ end
12
+ a=Reference1.create
13
+ b=ReferenceTestB1.create
14
+ b.reference1=a
15
+ a.name="Test1"
16
+ b.name="Test2"
17
+ b.name.should =="Test2"
18
+ b.reference1.name.should =="Test1"
19
+ end
20
+ it "can create a reference_many" do
21
+ class Reference < Redisted::Base
22
+ field :name, type: :string
23
+ end
24
+ class ReferenceTestB2 < Redisted::Base
25
+ field :name, type: :string
26
+ references_many :reference
27
+ end
28
+ a=Reference.create
29
+ b=Reference.create
30
+ c=Reference.create
31
+ d=ReferenceTestB2.create
32
+ d.references<<a
33
+ d.references.add_one b
34
+ d.references<<c
35
+ a.name="TestA"
36
+ b.name="TestB"
37
+ c.name="TestC"
38
+ d.name="TestD"
39
+ d.name.should =="TestD"
40
+ d.references.size.should ==3
41
+ d.references[0].name.should =="TestA"
42
+ d.references[1].name.should =="TestB"
43
+ d.references[2].name.should =="TestC"
44
+ end
45
+ it "can add and change an object in reference_one" do
46
+ class Reference3 < Redisted::Base
47
+ field :name, type: :string
48
+ end
49
+ class ReferenceTestB3 < Redisted::Base
50
+ field :name, type: :string
51
+ references_one :reference3
52
+ end
53
+ a1=Reference3.create
54
+ a2=Reference3.create
55
+ b=ReferenceTestB3.create
56
+ b.reference3=a1
57
+ a1.name="Test1"
58
+ a2.name="Test2"
59
+ b.name="Test3"
60
+ b.name.should =="Test3"
61
+ b.reference3.name.should =="Test1"
62
+ b.reference3=a2
63
+ b.reference3.name.should =="Test2"
64
+ end
65
+ it "can delete an object in a reference_one" do
66
+ class Reference4 < Redisted::Base
67
+ field :name, type: :string
68
+ end
69
+ class ReferenceTestB4 < Redisted::Base
70
+ field :name, type: :string
71
+ references_one :reference4
72
+ end
73
+ a=Reference4.create
74
+ b=ReferenceTestB4.create
75
+ b.reference4=a
76
+ a.name="Test1"
77
+ b.name="Test2"
78
+ b.name.should =="Test2"
79
+ b.reference4.name.should =="Test1"
80
+ a.destroy
81
+ b.reference4.should be_nil
82
+ end
83
+ it "can delete an object in a reference_many" do
84
+ class ReferenceTestA5 < Redisted::Base
85
+ field :name, type: :string
86
+ end
87
+ class ReferenceTestB5 < Redisted::Base
88
+ field :name, type: :string
89
+ references_many :reference_testa5
90
+ end
91
+ a=ReferenceTestA5.create
92
+ b=ReferenceTestA5.create
93
+ c=ReferenceTestA5.create
94
+ d=ReferenceTestB5.create
95
+ d.reference_testa5s<<a
96
+ d.reference_testa5s<<b
97
+ d.reference_testa5s<<c
98
+ a.name="TestA"
99
+ b.name="TestB"
100
+ c.name="TestC"
101
+ d.name="TestD"
102
+ d.name.should =="TestD"
103
+ d.reference_testa5s.size.should ==3
104
+ d.reference_testa5s[0].name.should =="TestA"
105
+ d.reference_testa5s[1].name.should =="TestB"
106
+ d.reference_testa5s[2].name.should =="TestC"
107
+ b.delete
108
+ d.reference_testa5s.size.should ==2
109
+ d.reference_testa5s[0].name.should =="TestA"
110
+ d.reference_testa5s[1].name.should =="TestC"
111
+ end
112
+ it "can call the reference something different" do
113
+ class Reference6 < Redisted::Base
114
+ field :name, type: :string
115
+ end
116
+ class ReferenceTestB6 < Redisted::Base
117
+ field :name, type: :string
118
+ references_one :reference6, as: :test
119
+ end
120
+ a=Reference6.create
121
+ b=ReferenceTestB6.create
122
+ b.test=a
123
+ a.name="Test1"
124
+ b.name="Test2"
125
+ b.name.should =="Test2"
126
+ b.test.name.should =="Test1"
127
+ end
128
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Scopes" do
4
+ it "can be created" do
5
+ class Scope1 < Redisted::Base
6
+ field :name, type: :string
7
+ field :test_int, type: :integer
8
+ index :odd, includes: ->(elem){(elem.test_int%2)!=0}
9
+ index :even, includes: ->(elem){(elem.test_int%2)==0}
10
+ index :thrice, includes: ->(elem){(elem.test_int%3)==0}
11
+ index :asc, order: ->(elem){elem.test_int},fields: :test_int
12
+ index :desc, order: ->(elem){-elem.test_int},fields: :test_int
13
+
14
+ scope :oddasc,->{by_odd.by_asc}
15
+ scope :evenasc,->{by_even.by_asc}
16
+ scope :oddthrice,->{by_thrice.by_odd}
17
+ end
18
+ a=Scope1.create({test_int: 3})
19
+ b=Scope1.create({test_int: 4})
20
+ c=Scope1.create({test_int: 2})
21
+ d=Scope1.create({test_int: 9})
22
+ e=Scope1.create({test_int: 1})
23
+ f=Scope1.create({test_int: 6})
24
+
25
+ res=Scope1.oddasc.all
26
+ res.size.should ==3
27
+ res[0].id.to_i.should ==e.id
28
+ res[1].id.to_i.should ==a.id
29
+ res[2].id.to_i.should ==d.id
30
+
31
+ res=Scope1.evenasc.all
32
+ res.size.should ==3
33
+ res[0].id.to_i.should ==c.id
34
+ res[1].id.to_i.should ==b.id
35
+ res[2].id.to_i.should ==f.id
36
+
37
+ res=Scope1.oddthrice.by_asc.all
38
+ res.size.should ==2
39
+ res[0].id.to_i.should ==a.id
40
+ res[1].id.to_i.should ==d.id
41
+ res=Scope1.oddthrice.by_desc.all
42
+ res.size.should ==2
43
+ res[0].id.to_i.should ==d.id
44
+ res[1].id.to_i.should ==a.id
45
+ end
46
+ it "allows incomplete filters to work" do
47
+ class Scope2 < Redisted::Base
48
+ field :name, type: :string
49
+ field :test_int, type: :integer
50
+ index :odd, includes: ->(elem){(elem.test_int%2)!=0}
51
+ index :even, includes: ->(elem){(elem.test_int%2)==0}
52
+ index :thrice, includes: ->(elem){(elem.test_int%3)==0}
53
+ index :asc, order: ->(elem){elem.test_int},fields: :test_int
54
+ index :desc, order: ->(elem){-elem.test_int},fields: :test_int
55
+
56
+ scope :oddasc,->{by_odd.by_asc}
57
+ scope :evenasc,->{by_even.by_asc}
58
+ scope :thriceasc,->{by_thrice.by_asc}
59
+ scope :thriceevenasc,->{by_thrice.by_even.by_asc}
60
+ end
61
+ a=Scope2.create({test_int: 3})
62
+ b=Scope2.create({test_int: 4})
63
+ c=Scope2.create({test_int: 2})
64
+ d=Scope2.create({test_int: 9})
65
+ e=Scope2.create({test_int: 1})
66
+ f=Scope2.create({test_int: 6})
67
+
68
+ aaa=Scope2.oddasc
69
+ res=aaa.all
70
+ res.size.should ==3
71
+ res[0].id.to_i.should ==e.id
72
+ res[1].id.to_i.should ==a.id
73
+ res[2].id.to_i.should ==d.id
74
+
75
+ aaa=Scope2.scoped
76
+ res=aaa.thriceasc.all
77
+ res.size.should ==3
78
+ res[0].id.to_i.should ==a.id
79
+ res[1].id.to_i.should ==f.id
80
+ res[2].id.to_i.should ==d.id
81
+
82
+ aaa=Scope2.thriceevenasc
83
+ res=aaa.all
84
+ res.size.should ==1
85
+ res[0].id.to_i.should ==f.id
86
+
87
+ end
88
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Unique Index" do
4
+ it "requires a unique index to have a unique value" do
5
+ class IndexTest1 < Redisted::Base
6
+ field :field1, type: :string
7
+ field :field2, type: :string
8
+ field :name, type: :string
9
+ index :name, unique: true
10
+ end
11
+ a=IndexTest1.create({name: "Test"})
12
+ b=IndexTest1.create
13
+ lambda{
14
+ b.name="Test"
15
+ }.should raise_error
16
+ end
17
+ it "requires a unique index to have a unique value (alternate syntax)" do
18
+ class IndexTest2 < Redisted::Base
19
+ field :field1, type: :string
20
+ field :field2, type: :string
21
+ field :name, type: :string
22
+ index :idxname, unique: :name
23
+ end
24
+ a=IndexTest2.create({name: "Test"})
25
+ b=IndexTest2.create
26
+ lambda{
27
+ b.name="Test"
28
+ }.should raise_error
29
+ end
30
+ it "allows changing a unique index value to a free value, and make the old value free again" do
31
+ class IndexTest3 < Redisted::Base
32
+ field :field1, type: :string
33
+ field :field2, type: :string
34
+ field :name, type: :string
35
+ index :name, unique: true
36
+ end
37
+ a=IndexTest3.create({name: "Test"})
38
+ b=IndexTest3.create
39
+ lambda{
40
+ b.name="Test"
41
+ }.should raise_error
42
+ a.name="Test2"
43
+ lambda{
44
+ b.name="Test"
45
+ }.should_not raise_error
46
+ lambda{
47
+ a.name="Test"
48
+ }.should raise_error
49
+ end
50
+ it "doesn't allow changing a unique index value to a value already in use" do
51
+ class IndexTest4 < Redisted::Base
52
+ field :field1, type: :string
53
+ field :field2, type: :string
54
+ field :name, type: :string
55
+ index :name, unique: name
56
+ end
57
+ a=IndexTest4.create({name: "Test"})
58
+ b=IndexTest4.create({name: "Test2"})
59
+ lambda{
60
+ b.name="Test"
61
+ }.should raise_error
62
+ end
63
+ it "deletes the unique entry when an object is deleted" do
64
+ class IndexTest5 < Redisted::Base
65
+ field :field1, type: :string
66
+ field :field2, type: :string
67
+ field :name, type: :string
68
+ index :name, unique: true
69
+ end
70
+ a=IndexTest5.create({name: "Test"})
71
+ b=IndexTest5.create({name: "Test2"})
72
+ lambda{
73
+ b.name="Test"
74
+ }.should raise_error
75
+ a.delete
76
+ lambda{
77
+ b.name="Test"
78
+ }.should_not raise_error
79
+ end
80
+ it "allows an index to be an array of fields" do
81
+ class IndexTest6 < Redisted::Base
82
+ field :field1, type: :string
83
+ field :field2, type: :string
84
+ field :name, type: :string
85
+ index :special, unique: [:field1,:field2]
86
+ end
87
+ a=IndexTest6.create({field1: "test1",field2: "test2"})
88
+ b=IndexTest6.create({field1: "test1",field2: "test3"})
89
+ lambda{
90
+ b.field1="test2"
91
+ }.should_not raise_error
92
+ lambda{
93
+ b.field1="test2"
94
+ }.should_not raise_error
95
+ lambda{
96
+ b.field1="test1"
97
+ }.should raise_error
98
+ end
99
+ it "disallows two indexes to have the same name" do
100
+ lambda{
101
+ class IndexTest7 < Redisted::Base
102
+ field :field1, type: :string
103
+ field :field2, type: :string
104
+ field :name, type: :string
105
+ index :special, unique: [:field1,:field2]
106
+ index :special, unique: [:field1,:name]
107
+ end
108
+ }.should raise_error
109
+ end
110
+
111
+
112
+
113
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Validations" do
4
+ it "can specify the maximum length of a field" do
5
+ class Validate1 < Redisted::Base
6
+ field :name, type: :string
7
+ field :test_int, type: :integer
8
+ field :provider, type: :string
9
+
10
+ validates_length_of :provider, :maximum=>20
11
+ end
12
+ a=Validate1.create({provider: "TestVal"})
13
+ a.provider="Test2Val"
14
+ lambda{
15
+ a.provider="This is too long...This is too long..."
16
+ }.should raise_error
17
+ a.provider="Test3Val"
18
+ end
19
+ it "can specify the minimum length of a field" do
20
+ class Validate2 < Redisted::Base
21
+ field :name, type: :string
22
+ field :test_int, type: :integer
23
+ field :provider, type: :string
24
+
25
+ validates_length_of :provider, :minimum=>5
26
+ end
27
+ a=Validate1.create({provider: "TestVal"})
28
+ a.provider="Test2Val"
29
+ lambda{
30
+ a.provider="shrt"
31
+ }.should raise_error
32
+ a.provider="Test3Val"
33
+ end
34
+ it "works with a deferred save" do
35
+ class Validate1 < Redisted::Base
36
+ always_cache_until_save
37
+ field :name, type: :string
38
+ field :test_int, type: :integer
39
+ field :provider, type: :string
40
+
41
+ validates_length_of :provider, :maximum=>20
42
+ end
43
+ a=Validate1.create({provider: "TestVal"})
44
+ a.provider="Test2Val"
45
+ a.save
46
+ a.provider="This is too long...This is too long..."
47
+ lambda{
48
+ a.save
49
+ }.should raise_error
50
+ a.provider="Test3Val"
51
+ a.save
52
+ end
53
+ it "can make sure a field is unique", :broken do
54
+ class Validate3 < Redisted::Base
55
+ field :name, type: :string
56
+ field :test_int, type: :integer
57
+ field :provider, type: :string
58
+
59
+ validates_uniqueness_of :name
60
+ end
61
+ a=Validate1.create({name: "Test1"})
62
+ b=Validate1.create
63
+ b.name="Test2"
64
+ lambda{
65
+ b.name="Test1"
66
+ }.should raise_error
67
+ b.name="Test2"
68
+ a.name="Test3"
69
+ b.name="Test1"
70
+ end
71
+ end
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+
4
+ Spork.prefork do
5
+ ENV["RAILS_ENV"] ||= 'test'
6
+ require File.expand_path("../../config/environment", __FILE__)
7
+
8
+ # Requires supporting ruby files with custom matchers and macros, etc,
9
+ # in spec/support/ and its subdirectories.
10
+ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ #config.mock_with :rspec
15
+ config.filter_run_excluding :broken
16
+ config.filter_run :focus => true
17
+ config.run_all_when_everything_filtered = true
18
+ config.before(:each) do
19
+ # Cleanout Redis
20
+ $redis.flushdb
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ Spork.each_run do
27
+
28
+ end
29
+
30
+ # --- Instructions ---
31
+ # Sort the contents of this file into a Spork.prefork and a Spork.each_run
32
+ # block.
33
+ #
34
+ # The Spork.prefork block is run only once when the spork server is started.
35
+ # You typically want to place most of your (slow) initializer code in here, in
36
+ # particular, require'ing any 3rd-party gems that you don't normally modify
37
+ # during development.
38
+ #
39
+ # The Spork.each_run block is run each time you run your specs. In case you
40
+ # need to load files that tend to change during development, require them here.
41
+ # With Rails, your application modules are loaded automatically, so sometimes
42
+ # this block can remain empty.
43
+ #
44
+ # Note: You can modify files loaded *from* the Spork.each_run block without
45
+ # restarting the spork server. However, this file itself will not be reloaded,
46
+ # so if you change any of the code inside the each_run block, you still need to
47
+ # restart the server. In general, if you have non-trivial code in this file,
48
+ # it's advisable to move it into a separate file so you can easily edit it
49
+ # without restarting spork. (For example, with RSpec, you could move
50
+ # non-trivial code into a file spec/support/my_helper.rb, making sure that the
51
+ # spec/support/* files are require'd from inside the each_run block.)
52
+ #
53
+ # Any code that is left outside the two blocks will be run during preforking
54
+ # *and* during each_run -- that's probably not what you want.
55
+ #
56
+ # These instructions should self-destruct in 10 seconds. If they don't, feel
57
+ # free to delete them.
58
+
59
+
60
+
61
+
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redisted
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lee Atchison
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-01 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: &70364879079740 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70364879079740
25
+ description: Rails models based Redis.
26
+ email:
27
+ - lee@leeatchison.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Guardfile
35
+ - README.md
36
+ - Rakefile
37
+ - config/application.rb
38
+ - config/boot.rb
39
+ - config/environment.rb
40
+ - config/redis.rb
41
+ - config/redis.yml
42
+ - index.html
43
+ - lib/redisted.rb
44
+ - lib/redisted/base.rb
45
+ - lib/redisted/delete.rb
46
+ - lib/redisted/errors.rb
47
+ - lib/redisted/fields.rb
48
+ - lib/redisted/getsetattr.rb
49
+ - lib/redisted/index.rb
50
+ - lib/redisted/instantiate.rb
51
+ - lib/redisted/references.rb
52
+ - lib/redisted/relation.rb
53
+ - lib/redisted/version.rb
54
+ - log/development.log
55
+ - log/test.log
56
+ - readme.md
57
+ - redisted.gemspec
58
+ - spec/redisted/callbacks_spec.rb
59
+ - spec/redisted/delete_destroy_spec.rb
60
+ - spec/redisted/fields_spec.rb
61
+ - spec/redisted/filter_index_spec.rb
62
+ - spec/redisted/find_spec.rb
63
+ - spec/redisted/log/test.log
64
+ - spec/redisted/match_index_spec.rb
65
+ - spec/redisted/persisted_fields_spec.rb
66
+ - spec/redisted/recalculate_index_spec.rb
67
+ - spec/redisted/references_spec.rb
68
+ - spec/redisted/scopes_spec.rb
69
+ - spec/redisted/unique_index_spec.rb
70
+ - spec/redisted/validations_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: ''
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project: redisted
92
+ rubygems_version: 1.8.10
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Rails models based on Redis.
96
+ test_files:
97
+ - spec/redisted/callbacks_spec.rb
98
+ - spec/redisted/delete_destroy_spec.rb
99
+ - spec/redisted/fields_spec.rb
100
+ - spec/redisted/filter_index_spec.rb
101
+ - spec/redisted/find_spec.rb
102
+ - spec/redisted/log/test.log
103
+ - spec/redisted/match_index_spec.rb
104
+ - spec/redisted/persisted_fields_spec.rb
105
+ - spec/redisted/recalculate_index_spec.rb
106
+ - spec/redisted/references_spec.rb
107
+ - spec/redisted/scopes_spec.rb
108
+ - spec/redisted/unique_index_spec.rb
109
+ - spec/redisted/validations_spec.rb
110
+ - spec/spec_helper.rb