ampere 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper.rb")
2
+
3
+ describe 'has_many relationships', :has_many => true do
4
+ before :each do
5
+ Redis.new.flushall
6
+ Ampere.connect
7
+
8
+ # These are used by the has_one/belongs_to example below
9
+ class Car < Ampere::Model
10
+ field :make
11
+ field :model
12
+ field :year
13
+
14
+ has_many :passengers
15
+ end
16
+
17
+ class Passenger < Ampere::Model
18
+ field :name
19
+ field :seat
20
+
21
+ belongs_to :car
22
+ end
23
+
24
+ @car = Car.create :make => "Lamborghini",
25
+ :model => "Countach",
26
+ :year => "1974"
27
+ @driver = Passenger.create :name => "Max",
28
+ :seat => "driver"
29
+ @passenger = Passenger.create :name => "Leila",
30
+ :seat => "passenger"
31
+
32
+ end
33
+
34
+ ###
35
+
36
+ it 'should define the necessary methods for has_many relationships' do
37
+ # Attr accessors
38
+ @car.should respond_to(:passengers)
39
+ @car.should respond_to(:"passengers=")
40
+ end
41
+
42
+ it 'should be able to add items to has_many relationships' do
43
+ @car.passengers = @car.passengers + [@driver]
44
+ @car.passengers = @car.passengers + [@passenger]
45
+
46
+ @car.save
47
+ @car.reload
48
+
49
+ @driver.reload
50
+ @passenger.reload
51
+
52
+ @car.passengers.should include(@driver)
53
+ @car.passengers.should include(@passenger)
54
+ end
55
+
56
+ it 'should be able to remove items from has_many relationships' do
57
+ pending
58
+ end
59
+
60
+ it 'should be able to query has_many relationships' do
61
+ pending
62
+ end
63
+
64
+ ###
65
+
66
+ after :all do
67
+ Ampere.disconnect
68
+ Redis.new.flushall
69
+ end
70
+
71
+ end
@@ -0,0 +1,75 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper.rb")
2
+
3
+ describe 'has_one relationships' do
4
+ before :all do
5
+ Redis.new.flushall
6
+ Ampere.connect
7
+
8
+ # These are used by the has_one/belongs_to example below
9
+ class Car < Ampere::Model
10
+ field :make
11
+ field :model
12
+ field :year
13
+
14
+ has_one :engine
15
+ end
16
+
17
+ class Engine < Ampere::Model
18
+ field :displacement
19
+ field :cylinders
20
+ field :configuration
21
+
22
+ belongs_to :car
23
+ end
24
+
25
+ @car = Car.create :make => "Lamborghini",
26
+ :model => "Countach",
27
+ :year => "1974"
28
+ @engine = Engine.create :displacement => "5167",
29
+ :cylinders => "12",
30
+ :configuration => "V"
31
+ end
32
+
33
+ ###
34
+
35
+ it 'can store a relationship to one model instance from another using an attr_accessor' do
36
+ @car.engine_id = @engine.id
37
+ @car.save
38
+ @car.reload
39
+ @engine.reload
40
+ @car.engine_id.should == @engine.id
41
+ @car.engine.should == @engine
42
+
43
+ @car.engine_id = nil
44
+ @car.save
45
+ @car.engine.should be_nil
46
+ end
47
+
48
+ it 'can store a relationship to one model instance from another using custom accessor methods' do
49
+ @car.engine = @engine
50
+ @car.save
51
+ @car.reload
52
+ @engine.reload
53
+ @car.engine_id.should == @engine.id
54
+ @car.engine.should == @engine
55
+
56
+ @car.engine_id = nil
57
+ @car.save
58
+ @car.engine.should be_nil
59
+ end
60
+
61
+ it 'can delete the object related to it in a has_one relationship' do
62
+ @car.engine = @engine
63
+ @car.engine.destroy
64
+ @car.reload
65
+ @car.engine.should be_nil
66
+ Engine.find(@engine.id).should be_nil
67
+ end
68
+
69
+ ###
70
+
71
+ after :all do
72
+ Ampere.disconnect
73
+ Redis.new.flushall
74
+ end
75
+ end
@@ -0,0 +1,62 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")
2
+
3
+ describe 'queries', :queries => true do
4
+ before :each do
5
+ Redis.new.flushall
6
+ Ampere.connect
7
+
8
+ class Motorcycle < Ampere::Model
9
+ field :make
10
+ field :model
11
+ field :year
12
+ end
13
+
14
+ @bike = Motorcycle.create make: "Honda", model: "CB400", year: "1990"
15
+ end
16
+
17
+ ###
18
+
19
+ it 'should be able to update one field atomically' do
20
+ @bike.model.should == "CB400"
21
+ @bike.update_attribute :model, "CB450SC"
22
+ @bike.model.should == "CB450SC"
23
+ @bike.reload.model.should == "CB450SC"
24
+
25
+ @bike.year.should == "1990"
26
+ @bike.update_attribute :year, "1986"
27
+ @bike.year.should == "1986"
28
+ @bike.reload
29
+ @bike.year.should == "1986"
30
+ end
31
+
32
+ it 'should be able to update multiple fields atomically' do
33
+ @bike.update_attributes model: "CB750",
34
+ year: "1996"
35
+ @bike.model.should == "CB750"
36
+ @bike.year.should == "1996"
37
+ @bike.reload.model.should == "CB750"
38
+ @bike.reload.year.should == "1996"
39
+ end
40
+
41
+ it 'should openly refuse to set fields that do not exist' do
42
+ (->{
43
+ @bike.update_attribute :this_field_does_not_exist, "Lorem ipsum dolor sit amet."
44
+ }).should raise_error
45
+ (->{
46
+ # TODO Once this operation is made atomic, this test needs to be updated to also
47
+ # include keys that do exist, in various orders. Even if real fields are included,
48
+ # no fields should be updated if any part of this query would fail. To emulate this
49
+ # behaviour in the meantime, possibly check all keys of attributes hash to see if
50
+ # they exist before performing the queries or synchronize during.
51
+ @bike.update_attributes this_field_does_not_exist: "Ut enim ad minim veniam.",
52
+ neither_does_this_one: "Duis aute irure dolor in."
53
+ }).should raise_error
54
+ end
55
+
56
+ ###
57
+
58
+ after :all do
59
+ Redis.new.flushall
60
+ Ampere.connect
61
+ end
62
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")
2
+
3
+ describe 'Ampere', :ampere => true do
4
+ before :all do
5
+ Ampere.connect
6
+ Redis.new.flushall
7
+ end
8
+
9
+ it 'should be able to connect' do
10
+ Ampere.connected?.should be_true
11
+ Ampere.disconnect
12
+ Ampere.connected?.should be_false
13
+ end
14
+
15
+ # context 'Redis data store', :redis => true do
16
+ # it 'should come with a __guid set' do
17
+ # Redis.new['guid'].should == 0
18
+ # end
19
+ # end
20
+
21
+ after :all do
22
+ Redis.new.flushall
23
+ Ampere.disconnect
24
+ end
25
+ end
26
+
@@ -0,0 +1,71 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")
2
+
3
+ describe 'Collections', :collections => true do
4
+ before :all do
5
+ Redis.new.flushall
6
+ Ampere.connect
7
+
8
+ class President < Ampere::Model
9
+ field :name
10
+ field :party
11
+
12
+ index :party
13
+ end
14
+
15
+ President.create :name => "Millard Fillmore" , :party => "Whig"
16
+ President.create :name => "Ulysses S. Grant" , :party => "Republican"
17
+ President.create :name => "Abraham Lincoln" , :party => "Republican"
18
+ President.create :name => "Franklin D. Roosevelt" , :party => "Democratic"
19
+ President.create :name => "John F. Kennedy" , :party => "Democratic"
20
+ President.create :name => "Jimmy Carter" , :party => "Democratic"
21
+ end
22
+
23
+ ###
24
+
25
+ it 'should be returned by where() queries' do
26
+ democrats = President.where(:party => "Democratic")
27
+ democrats.class.should == Ampere::Collection
28
+ democrats.model.should == President
29
+ democrats.raw_array.length.should == 3
30
+ end
31
+
32
+ it 'should be accessible via [] like an Array' do
33
+ whigs = President.where(:party => "Whig")
34
+ whigs[0].name.should == "Millard Fillmore"
35
+ end
36
+
37
+ it 'should lazily load model instances searched for with indexed fields' do
38
+ whigs = President.where(:party => "Whig")
39
+ whigs.raw_array.first.class.should == String
40
+ whigs[0].name.should == "Millard Fillmore"
41
+ whigs.raw_array.first.class.should == President
42
+ end
43
+
44
+ it 'should return its first() item' do
45
+ republicans = President.where(:party => "Republican")
46
+ republicans.first.name.should == "Ulysses S. Grant"
47
+ end
48
+
49
+ it 'should return its first(n) items' do
50
+ republicans = President.where(:party => "Republican")
51
+ republicans.first(2).map(&:name).should == ["Ulysses S. Grant", "Abraham Lincoln"]
52
+ end
53
+
54
+ it 'should return its last() item' do
55
+ democrats = President.where(:party => "Democratic")
56
+ democrats.last.name.should == "Jimmy Carter"
57
+ end
58
+
59
+ it 'should be convertible to an array' do
60
+ republicans = President.where(:party => "Republican")
61
+ republicans.to_a.map(&:name).should == ["Ulysses S. Grant", "Abraham Lincoln"]
62
+ end
63
+
64
+ ###
65
+
66
+ after :all do
67
+ Ampere.disconnect
68
+ Redis.new.flushall
69
+ end
70
+ end
71
+
@@ -0,0 +1,20 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter '/spec/'
4
+ add_filter '/features/'
5
+ end
6
+
7
+ require 'rspec'
8
+ require 'rspec/autorun'
9
+ require 'shoulda'
10
+
11
+ require 'ampere'
12
+
13
+ # Requires supporting ruby files with custom matchers and macros, etc,
14
+ # in spec/support/ and its subdirectories.
15
+ Dir[File.join(File.dirname(__FILE__), "spec", "support", "**", "*.rb")].each {|f| puts "REQUIRING: #{f}"; require f}
16
+
17
+ # Not used yet
18
+ RSpec.configure do |config|
19
+ config.mock_with :rspec
20
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'ampere'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestAmpere < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ampere
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Max Thom Stahl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: &70174558736960 !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: *70174558736960
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
27
+ requirement: &70174558736420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70174558736420
36
+ - !ruby/object:Gem::Dependency
37
+ name: cucumber
38
+ requirement: &70174558735860 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70174558735860
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &70174558735300 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70174558735300
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &70174558734720 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.6.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70174558734720
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &70174558734140 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70174558734140
80
+ - !ruby/object:Gem::Dependency
81
+ name: rspec
82
+ requirement: &70174558733620 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70174558733620
91
+ - !ruby/object:Gem::Dependency
92
+ name: rdoc
93
+ requirement: &70174558733120 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70174558733120
102
+ description: An ActiveRecord/Mongoid-esque object model for the Redis key/value data
103
+ store.
104
+ email: max@villainousindustri.es
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files:
108
+ - LICENSE.txt
109
+ - README.md
110
+ files:
111
+ - .document
112
+ - .rspec
113
+ - .rvmrc
114
+ - Gemfile
115
+ - Gemfile.lock
116
+ - LICENSE.txt
117
+ - README.md
118
+ - Rakefile
119
+ - VERSION
120
+ - features/ampere.feature
121
+ - features/step_definitions/ampere_steps.rb
122
+ - features/support/env.rb
123
+ - lib/ampere.rb
124
+ - lib/ampere/collection.rb
125
+ - lib/ampere/model.rb
126
+ - spec/models/indices_spec.rb
127
+ - spec/models/model_spec.rb
128
+ - spec/models/queries_spec.rb
129
+ - spec/models/relationships/belongs_to_spec.rb
130
+ - spec/models/relationships/has_many_spec.rb
131
+ - spec/models/relationships/has_one_spec.rb
132
+ - spec/models/updates_spec.rb
133
+ - spec/module/ampere_spec.rb
134
+ - spec/module/collections_spec.rb
135
+ - spec/spec_helper.rb
136
+ - test/helper.rb
137
+ - test/test_ampere.rb
138
+ homepage: http://github.com/mstahl/ampere
139
+ licenses:
140
+ - EPL
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ segments:
152
+ - 0
153
+ hash: -4494108612008764104
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ none: false
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 1.8.10
163
+ signing_key:
164
+ specification_version: 3
165
+ summary: A pure Ruby ORM for Redis.
166
+ test_files: []