tenacity 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.gitignore +6 -0
  2. data/EXTEND.rdoc +79 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +70 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +103 -0
  7. data/Rakefile +96 -0
  8. data/lib/tenacity/associations/belongs_to.rb +28 -0
  9. data/lib/tenacity/associations/has_many.rb +86 -0
  10. data/lib/tenacity/associations/has_one.rb +40 -0
  11. data/lib/tenacity/class_methods.rb +226 -0
  12. data/lib/tenacity/instance_methods.rb +31 -0
  13. data/lib/tenacity/orm_ext/activerecord.rb +116 -0
  14. data/lib/tenacity/orm_ext/couchrest/couchrest_extended_document.rb +45 -0
  15. data/lib/tenacity/orm_ext/couchrest/couchrest_model.rb +46 -0
  16. data/lib/tenacity/orm_ext/couchrest/tenacity_class_methods.rb +52 -0
  17. data/lib/tenacity/orm_ext/couchrest/tenacity_instance_methods.rb +21 -0
  18. data/lib/tenacity/orm_ext/mongo_mapper.rb +107 -0
  19. data/lib/tenacity/version.rb +3 -0
  20. data/lib/tenacity.rb +27 -0
  21. data/tenacity.gemspec +34 -0
  22. data/test/associations/belongs_to_test.rb +119 -0
  23. data/test/associations/has_many_test.rb +184 -0
  24. data/test/associations/has_one_test.rb +77 -0
  25. data/test/core/classmethods_test.rb +53 -0
  26. data/test/fixtures/active_record_car.rb +6 -0
  27. data/test/fixtures/active_record_climate_control_unit.rb +5 -0
  28. data/test/fixtures/active_record_nuts.rb +5 -0
  29. data/test/fixtures/couch_rest_radio.rb +10 -0
  30. data/test/fixtures/mongo_mapper_button.rb +6 -0
  31. data/test/fixtures/mongo_mapper_dashboard.rb +10 -0
  32. data/test/fixtures/mongo_mapper_wheel.rb +9 -0
  33. data/test/helpers/active_record_test_helper.rb +7 -0
  34. data/test/helpers/couch_rest_test_helper.rb +8 -0
  35. data/test/helpers/mongo_mapper_test_helper.rb +3 -0
  36. data/test/orm_ext/activerecord_test.rb +85 -0
  37. data/test/orm_ext/couchrest_test.rb +95 -0
  38. data/test/orm_ext/mongo_mapper_test.rb +93 -0
  39. data/test/test_helper.rb +47 -0
  40. metadata +253 -0
data/tenacity.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/tenacity/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "tenacity"
6
+ s.license = "MIT"
7
+ s.version = Tenacity::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["John Wood"]
10
+ s.email = ["john@johnpwood.net"]
11
+ s.homepage = "http://github.com/jwood/tenacity"
12
+ s.summary = %Q{A ORM independent way of specifying simple relationships between models backed by different databases.}
13
+ s.description = %Q{Tenacity provides an ORM independent way of specifying simple relationships between models backed by different databases.}
14
+
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+
17
+ s.add_runtime_dependency "activesupport", ">= 2.3"
18
+
19
+ s.add_development_dependency "bundler", "~> 1.0.0"
20
+ s.add_development_dependency "rake", "~> 0.8.7"
21
+ s.add_development_dependency "rcov", "~> 0.9.9"
22
+ s.add_development_dependency "shoulda", "~> 2.11.3"
23
+ s.add_development_dependency "mongo_mapper", "~> 0.8.6"
24
+ s.add_development_dependency "bson_ext", "~> 1.1.3"
25
+ s.add_development_dependency "activerecord", "~> 3.0.0"
26
+ s.add_development_dependency "mysql", "~> 2.8.1"
27
+ s.add_development_dependency "couchrest", "~> 1.0.0"
28
+ s.add_development_dependency "couchrest_model"
29
+
30
+ s.files = `git ls-files`.split("\n")
31
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
32
+ s.require_path = 'lib'
33
+ end
34
+
@@ -0,0 +1,119 @@
1
+ require 'test_helper'
2
+
3
+ class BelongsToTest < Test::Unit::TestCase
4
+
5
+ context "A class with a belongs_to association to another class" do
6
+ setup do
7
+ setup_fixtures
8
+ @car = ActiveRecordCar.create
9
+ @wheel = MongoMapperWheel.create(:active_record_car => @car)
10
+ end
11
+
12
+ should "memoize the association" do
13
+ assert_equal @car, @wheel.active_record_car
14
+
15
+ other_car = ActiveRecordCar.create
16
+ assert_equal @car, MongoMapperWheel.find(@wheel.id).active_record_car
17
+ MongoMapperWheel.update(@wheel.id, :active_record_car => other_car)
18
+ assert_equal other_car, MongoMapperWheel.find(@wheel.id).active_record_car
19
+
20
+ assert_equal @car, @wheel.active_record_car
21
+ assert_equal other_car, @wheel.active_record_car(true)
22
+ end
23
+ end
24
+
25
+ context "A MongoMapper class with belongs_to association to an ActiveRecord class" do
26
+ setup do
27
+ setup_fixtures
28
+ @car = ActiveRecordCar.create
29
+ @wheel = MongoMapperWheel.create
30
+ end
31
+
32
+ should "be able to fetch the id of the associated object" do
33
+ @wheel.active_record_car_id = @car.id
34
+ @wheel.save
35
+ assert_equal @car.id, MongoMapperWheel.find(@wheel.id).active_record_car_id.to_i
36
+ end
37
+
38
+ should "be able to load the associated object" do
39
+ @wheel.active_record_car = @car
40
+ @wheel.save
41
+ assert_equal @car.id, MongoMapperWheel.find(@wheel.id).active_record_car_id.to_i
42
+ assert_equal @car, MongoMapperWheel.find(@wheel.id).active_record_car
43
+ end
44
+
45
+ should "be able to load the associated object if all we have is the id" do
46
+ @wheel.active_record_car_id = @car.id
47
+ @wheel.save
48
+ assert_equal @car, MongoMapperWheel.find(@wheel.id).active_record_car
49
+ end
50
+
51
+ should "return nil if no association is set" do
52
+ assert_nil MongoMapperWheel.find(@wheel.id).active_record_car
53
+ end
54
+ end
55
+
56
+ context "An ActiveRecord class with belongs_to association to a MongoMapper class" do
57
+ setup do
58
+ setup_fixtures
59
+ @wheel = MongoMapperWheel.create
60
+ @nut = ActiveRecordNut.create
61
+ end
62
+
63
+ should "be able to fetch the id of the associated object" do
64
+ @nut.mongo_mapper_wheel_id = @wheel.id
65
+ @nut.save
66
+ assert_equal @wheel.id.to_s, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel_id
67
+ end
68
+
69
+ should "be able to load the associated object" do
70
+ @nut.mongo_mapper_wheel = @wheel
71
+ @nut.save
72
+ assert_equal @wheel.id.to_s, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel_id
73
+ assert_equal @wheel, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel
74
+ end
75
+
76
+ should "be be able to load the associated object if all we have is the id" do
77
+ @nut.mongo_mapper_wheel_id = @wheel.id
78
+ @nut.save
79
+ assert_equal @wheel, ActiveRecordNut.find(@nut.id).mongo_mapper_wheel
80
+ end
81
+
82
+ should "return nil if no association is set" do
83
+ assert_nil ActiveRecordNut.find(@nut.id).mongo_mapper_wheel
84
+ end
85
+ end
86
+
87
+ context "A CouchRest class with belongs_to association to a MongoMapper class" do
88
+ setup do
89
+ setup_all_fixtures
90
+ @dashboard = MongoMapperDashboard.create
91
+ @radio = CouchRestRadio.create({})
92
+ end
93
+
94
+ should "be able to fetch the id of the associated object" do
95
+ @radio.mongo_mapper_dashboard_id = @dashboard.id
96
+ @radio.save
97
+ assert_equal @dashboard.id.to_s, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard_id
98
+ end
99
+
100
+ should "be able to load the associated object" do
101
+ @radio.mongo_mapper_dashboard = @dashboard
102
+ @radio.save
103
+ assert_equal @dashboard.id.to_s, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard_id
104
+ assert_equal @dashboard, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard
105
+ end
106
+
107
+ should "be be able to load the associated object if all we have is the id" do
108
+ @radio.mongo_mapper_dashboard_id = @dashboard.id
109
+ @radio.save
110
+ assert_equal @dashboard, CouchRestRadio.find(@radio.id).mongo_mapper_dashboard
111
+ end
112
+
113
+ should "return nil if no association is set" do
114
+ assert_nil CouchRestRadio.find(@radio.id).mongo_mapper_dashboard
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,184 @@
1
+ require 'test_helper'
2
+
3
+ class HasManyTest < Test::Unit::TestCase
4
+
5
+ context "A class with a belongs_to association to another class" do
6
+ setup do
7
+ setup_fixtures
8
+ @car = ActiveRecordCar.create
9
+ @wheels = [MongoMapperWheel.create, MongoMapperWheel.create, MongoMapperWheel.create]
10
+
11
+ @car.mongo_mapper_wheels = @wheels
12
+ @car.save
13
+ end
14
+
15
+ should "memoize the association" do
16
+ assert_equal @wheels, @car.mongo_mapper_wheels
17
+
18
+ other_wheels = [MongoMapperWheel.create, MongoMapperWheel.create, MongoMapperWheel.create]
19
+ assert_equal @wheels, ActiveRecordCar.find(@car.id).mongo_mapper_wheels
20
+ ActiveRecordCar.find(@car.id).update_attribute(:mongo_mapper_wheels, other_wheels)
21
+ assert_equal other_wheels, ActiveRecordCar.find(@car.id).mongo_mapper_wheels
22
+
23
+ assert_equal @wheels, @car.mongo_mapper_wheels
24
+ assert_equal other_wheels, @car.mongo_mapper_wheels(true)
25
+ end
26
+ end
27
+
28
+ context "An ActiveRecord class with a has_many association to a MongoMapper class" do
29
+ setup do
30
+ setup_fixtures
31
+ @car = ActiveRecordCar.create
32
+ @wheel_1 = MongoMapperWheel.create
33
+ @wheel_2 = MongoMapperWheel.create
34
+ @wheel_3 = MongoMapperWheel.create
35
+ end
36
+
37
+ should "be able to set the associated objects by their ids" do
38
+ @car.mongo_mapper_wheel_ids = [@wheel_1.id, @wheel_2.id, @wheel_3.id]
39
+ @car.save
40
+ assert_set_equal [@wheel_1, @wheel_2, @wheel_3], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
41
+ assert_set_equal [@wheel_1.id.to_s, @wheel_2.id.to_s, @wheel_3.id.to_s], ActiveRecordCar.find(@car.id).mongo_mapper_wheel_ids
42
+ end
43
+
44
+ context "that works with associated objects" do
45
+ setup do
46
+ @car.mongo_mapper_wheels = [@wheel_1, @wheel_2, @wheel_3]
47
+ @car.save
48
+ end
49
+
50
+ should "be able to set the associated objects" do
51
+ assert_set_equal [@wheel_1, @wheel_2, @wheel_3], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
52
+ end
53
+
54
+ should "be able to add an associated object using the << operator" do
55
+ wheel_4 = MongoMapperWheel.create
56
+ @car.mongo_mapper_wheels << wheel_4
57
+ @car.save
58
+ assert_set_equal [@wheel_1, @wheel_2, @wheel_3, wheel_4], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
59
+ end
60
+
61
+ should "be able to remove an associated object using the delete method" do
62
+ @car.mongo_mapper_wheels.delete(@wheel_3)
63
+ @car.save
64
+ assert_set_equal [@wheel_1, @wheel_2], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
65
+ end
66
+
67
+ should "be able to clear all associated objects using the clear method" do
68
+ @car.mongo_mapper_wheels.clear
69
+ @car.save
70
+ assert_equal [], ActiveRecordCar.find(@car.id).mongo_mapper_wheels
71
+ end
72
+
73
+ should "return an empty array if the association is not set" do
74
+ car = ActiveRecordCar.create
75
+ assert_set_equal [], ActiveRecordCar.find(car.id).mongo_mapper_wheels
76
+ end
77
+ end
78
+ end
79
+
80
+ context "A MongoMapper class with a has_many association to an ActiveRecord class" do
81
+ setup do
82
+ setup_fixtures
83
+ @wheel = MongoMapperWheel.create
84
+ @nut_1 = ActiveRecordNut.create
85
+ @nut_2 = ActiveRecordNut.create
86
+ @nut_3 = ActiveRecordNut.create
87
+ end
88
+
89
+ should "be able to set the associated objects by their ids" do
90
+ @wheel.active_record_nut_ids = [@nut_1.id, @nut_2.id, @nut_3.id]
91
+ @wheel.save
92
+ assert_set_equal [@nut_1, @nut_2, @nut_3], MongoMapperWheel.find(@wheel.id).active_record_nuts
93
+ assert_set_equal [@nut_1.id.to_s, @nut_2.id.to_s, @nut_3.id.to_s], MongoMapperWheel.find(@wheel.id).active_record_nut_ids
94
+ end
95
+
96
+ context "that works with associated objects" do
97
+ setup do
98
+ @wheel.active_record_nuts = [@nut_1, @nut_2, @nut_3]
99
+ @wheel.save
100
+ end
101
+
102
+ should "be able to set the associated objects" do
103
+ assert_set_equal [@nut_1, @nut_2, @nut_3], MongoMapperWheel.find(@wheel.id).active_record_nuts
104
+ end
105
+
106
+ should "be able to add an associated object using the << operator" do
107
+ nut_4 = ActiveRecordNut.create
108
+ @wheel.active_record_nuts << nut_4
109
+ @wheel.save
110
+ assert_set_equal [@nut_1, @nut_2, @nut_3, nut_4], MongoMapperWheel.find(@wheel.id).active_record_nuts
111
+ end
112
+
113
+ should "be able to remove an associated object using the delete method" do
114
+ @wheel.active_record_nuts.delete(@nut_3)
115
+ @wheel.save
116
+ assert_set_equal [@nut_1, @nut_2], MongoMapperWheel.find(@wheel.id).active_record_nuts
117
+ end
118
+
119
+ should "be able to clear all associated objects using the clear method" do
120
+ @wheel.active_record_nuts.clear
121
+ @wheel.save
122
+ assert_set_equal [], MongoMapperWheel.find(@wheel.id).active_record_nuts
123
+ end
124
+
125
+ should "return an empty array if the association is not set" do
126
+ wheel = MongoMapperWheel.create
127
+ assert_set_equal [], MongoMapperWheel.find(wheel.id).active_record_nuts
128
+ end
129
+ end
130
+ end
131
+
132
+ context "A CouchRest class with a has_many association to a MongoMapper class" do
133
+ setup do
134
+ setup_all_fixtures
135
+ @radio = CouchRestRadio.create({})
136
+ @button_1 = MongoMapperButton.create
137
+ @button_2 = MongoMapperButton.create
138
+ @button_3 = MongoMapperButton.create
139
+ end
140
+
141
+ should "be able to set the associated objects by their ids" do
142
+ @radio.mongo_mapper_button_ids = [@button_1.id, @button_2.id, @button_3.id]
143
+ @radio.save
144
+ assert_set_equal [@button_1, @button_2, @button_3], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
145
+ assert_set_equal [@button_1.id.to_s, @button_2.id.to_s, @button_3.id.to_s], CouchRestRadio.get(@radio.id).mongo_mapper_button_ids
146
+ end
147
+
148
+ context "that works with associated objects" do
149
+ setup do
150
+ @radio.mongo_mapper_buttons = [@button_1, @button_2, @button_3]
151
+ @radio.save
152
+ end
153
+
154
+ should "be able to set the associated objects" do
155
+ assert_set_equal [@button_1, @button_2, @button_3], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
156
+ end
157
+
158
+ should "be able to add an associated object using the << operator" do
159
+ button_4 = MongoMapperButton.create
160
+ @radio.mongo_mapper_buttons << button_4
161
+ @radio.save
162
+ assert_set_equal [@button_1, @button_2, @button_3, button_4], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
163
+ end
164
+
165
+ should "be able to remove an associated object using the delete method" do
166
+ @radio.mongo_mapper_buttons.delete(@button_3)
167
+ @radio.save
168
+ assert_set_equal [@button_1, @button_2], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
169
+ end
170
+
171
+ should "be able to clear all associated objects using the clear method" do
172
+ @radio.mongo_mapper_buttons.clear
173
+ @radio.save
174
+ assert_equal [], CouchRestRadio.get(@radio.id).mongo_mapper_buttons
175
+ end
176
+
177
+ should "return an empty array if the association is not set" do
178
+ radio = CouchRestRadio.create({})
179
+ assert_set_equal [], CouchRestRadio.get(radio.id).mongo_mapper_buttons
180
+ end
181
+ end
182
+ end
183
+
184
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class HasOneTest < Test::Unit::TestCase
4
+
5
+ context "A class with a has_one association to another class" do
6
+ setup do
7
+ setup_fixtures
8
+ @climate_control_unit = ActiveRecordClimateControlUnit.create
9
+ @dashboard = MongoMapperDashboard.create(:active_record_climate_control_unit => @climate_control_unit)
10
+ end
11
+
12
+ should "memoize the association" do
13
+ assert_equal @climate_control_unit, @dashboard.active_record_climate_control_unit
14
+
15
+ other_climate_control_unit = ActiveRecordClimateControlUnit.create
16
+ assert_equal @climate_control_unit, MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
17
+ ActiveRecordClimateControlUnit.update(@climate_control_unit.id, :mongo_mapper_dashboard_id => nil)
18
+ ActiveRecordClimateControlUnit.update(other_climate_control_unit.id, :mongo_mapper_dashboard_id => @dashboard.id)
19
+ assert_equal other_climate_control_unit, MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
20
+
21
+ assert_equal @climate_control_unit, @dashboard.active_record_climate_control_unit
22
+ assert_equal other_climate_control_unit, @dashboard.active_record_climate_control_unit(true)
23
+ end
24
+ end
25
+
26
+ context "A MongoMapper class with a has_one association to an ActiveRecord class" do
27
+ setup do
28
+ setup_fixtures
29
+ @climate_control_unit = ActiveRecordClimateControlUnit.create
30
+ @dashboard = MongoMapperDashboard.create
31
+ end
32
+
33
+ should "be able to set and get the associated object" do
34
+ @dashboard.active_record_climate_control_unit = @climate_control_unit
35
+ assert_equal @climate_control_unit, MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
36
+ end
37
+
38
+ should "return nil if no association is set" do
39
+ assert_nil MongoMapperDashboard.find(@dashboard.id).active_record_climate_control_unit
40
+ end
41
+ end
42
+
43
+ context "An ActiveRecord class with a has_one association to a MongoMapper class" do
44
+ setup do
45
+ setup_fixtures
46
+ @dashboard = MongoMapperDashboard.create
47
+ @car = ActiveRecordCar.create
48
+ end
49
+
50
+ should "be able to set and get the associated object" do
51
+ @car.mongo_mapper_dashboard = @dashboard
52
+ assert_equal @dashboard, ActiveRecordCar.find(@car.id).mongo_mapper_dashboard
53
+ end
54
+
55
+ should "return nil if no association is set" do
56
+ assert_nil ActiveRecordCar.find(@car.id).mongo_mapper_dashboard
57
+ end
58
+ end
59
+
60
+ context "A CouchRest class with a has_one association to a MongoMapper class" do
61
+ setup do
62
+ setup_all_fixtures
63
+ @dashboard = MongoMapperDashboard.create
64
+ @radio = CouchRestRadio.create({})
65
+ end
66
+
67
+ should "be able to set and get the associated object" do
68
+ @dashboard.couch_rest_radio = @radio
69
+ assert_equal @radio, MongoMapperDashboard.find(@dashboard.id).couch_rest_radio
70
+ end
71
+
72
+ should "return nil if no association is set" do
73
+ assert_nil MongoMapperDashboard.find(@dashboard.id).couch_rest_radio
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ class ClassmethodsTest < Test::Unit::TestCase
4
+
5
+ context "A class with a belongs_to :active_record_car association" do
6
+ setup do
7
+ setup_fixtures
8
+ @wheel = MongoMapperWheel.new
9
+ end
10
+
11
+ should("respond to active_record_car") { assert @wheel.respond_to?(:active_record_car) }
12
+ should("respond to active_record_car=") { assert @wheel.respond_to?(:active_record_car=) }
13
+ should("respond to active_record_car_id") { assert @wheel.respond_to?(:active_record_car_id) }
14
+ end
15
+
16
+ context "A class with a has_one :active_record_climate_control_unit association" do
17
+ setup do
18
+ setup_fixtures
19
+ @dashboard = MongoMapperDashboard.new
20
+ end
21
+
22
+ should("respond to active_record_climate_control_unit") { assert @dashboard.respond_to?(:active_record_climate_control_unit) }
23
+ should("respond to active_record_climate_control_unit=") { assert @dashboard.respond_to?(:active_record_climate_control_unit=) }
24
+ end
25
+
26
+ context "A class with a has_many :active_record_nuts association" do
27
+ setup do
28
+ setup_fixtures
29
+ @wheel = MongoMapperWheel.new
30
+ end
31
+
32
+ should("respond to active_record_nuts") { assert @wheel.respond_to?(:active_record_nuts) }
33
+ should("respond to active_record_nuts=") { assert @wheel.respond_to?(:active_record_nuts=) }
34
+ should("respond to active_record_nut_ids") { assert @wheel.respond_to?(:active_record_nut_ids) }
35
+ should("respond to active_record_nut_ids=") { assert @wheel.respond_to?(:active_record_nut_ids=) }
36
+ end
37
+
38
+ context "The object returned by a has_many association" do
39
+ setup do
40
+ setup_fixtures
41
+ @wheel = MongoMapperWheel.new
42
+ @nuts = @wheel.active_record_nuts
43
+ end
44
+
45
+ should("respond to <<") { assert @nuts.respond_to?(:<<) }
46
+ should("respond to delete") { assert @nuts.respond_to?(:delete) }
47
+ should("respond to clear") { assert @nuts.respond_to?(:clear) }
48
+ should("respond to empty?") { assert @nuts.respond_to?(:empty?) }
49
+ should("respond to size") { assert @nuts.respond_to?(:size) }
50
+ end
51
+
52
+ end
53
+
@@ -0,0 +1,6 @@
1
+ class ActiveRecordCar < ActiveRecord::Base
2
+ include Tenacity
3
+
4
+ t_has_many :mongo_mapper_wheels
5
+ t_has_one :mongo_mapper_dashboard
6
+ end
@@ -0,0 +1,5 @@
1
+ class ActiveRecordClimateControlUnit < ActiveRecord::Base
2
+ include Tenacity
3
+
4
+ t_belongs_to :mongo_mapper_dashboard
5
+ end
@@ -0,0 +1,5 @@
1
+ class ActiveRecordNut < ActiveRecord::Base
2
+ include Tenacity
3
+
4
+ t_belongs_to :mongo_mapper_wheel
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'couchrest_model'
2
+
3
+ class CouchRestRadio < CouchRest::Model::Base
4
+ include Tenacity
5
+ use_database COUCH_DB
6
+
7
+ t_belongs_to :mongo_mapper_dashboard
8
+ t_has_many :mongo_mapper_buttons
9
+ end
10
+
@@ -0,0 +1,6 @@
1
+ class MongoMapperButton
2
+ include MongoMapper::Document
3
+ include Tenacity
4
+
5
+ t_belongs_to :couch_rest_radio
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'mongo_mapper'
2
+
3
+ class MongoMapperDashboard
4
+ include MongoMapper::Document
5
+ include Tenacity
6
+
7
+ t_belongs_to :active_record_car
8
+ t_has_one :active_record_climate_control_unit
9
+ t_has_one :couch_rest_radio
10
+ end
@@ -0,0 +1,9 @@
1
+ require 'mongo_mapper'
2
+
3
+ class MongoMapperWheel
4
+ include MongoMapper::Document
5
+ include Tenacity
6
+
7
+ t_belongs_to :active_record_car
8
+ t_has_many :active_record_nuts
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => "mysql",
5
+ :host => "localhost",
6
+ :username => "root",
7
+ :database => "tenacity_test")
@@ -0,0 +1,8 @@
1
+ require 'couchrest_model'
2
+
3
+ COUCHHOST = "http://127.0.0.1:5984"
4
+ TESTDB = 'tenacity-test'
5
+ TEST_SERVER = CouchRest.new
6
+ TEST_SERVER.default_database = TESTDB
7
+ COUCH_DB = TEST_SERVER.database(TESTDB)
8
+
@@ -0,0 +1,3 @@
1
+ require 'mongo_mapper'
2
+
3
+ MongoMapper.database = 'tenacity_test'
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveRecordTest < Test::Unit::TestCase
4
+
5
+ context "The ActiveRecord extension" do
6
+ setup do
7
+ setup_fixtures
8
+ end
9
+
10
+ should "be able to find the object in the database" do
11
+ car = ActiveRecordCar.create
12
+ assert_equal car, ActiveRecordCar._t_find(car.id)
13
+ end
14
+
15
+ should "return nil if the specified object cannot be found in the database" do
16
+ assert_nil ActiveRecordCar._t_find(989782)
17
+ end
18
+
19
+ should "be able to find multiple objects in the database" do
20
+ car = ActiveRecordCar.create
21
+ car_2 = ActiveRecordCar.create
22
+ assert_set_equal [car, car_2], ActiveRecordCar._t_find_bulk([car.id, car_2.id, 989823])
23
+ end
24
+
25
+ should "return an empty array if none of the specified ids could be found in the database" do
26
+ assert_set_equal [], ActiveRecordNut._t_find_bulk([989823, 992111, 989771])
27
+ end
28
+
29
+ should "be able to find the first associate of an object" do
30
+ mongo_mapper_dashboard = MongoMapperDashboard.create
31
+ climate_control_unit = ActiveRecordClimateControlUnit.create(:mongo_mapper_dashboard_id => mongo_mapper_dashboard.id)
32
+ assert_equal climate_control_unit, ActiveRecordClimateControlUnit._t_find_first_by_associate(:mongo_mapper_dashboard_id, mongo_mapper_dashboard.id)
33
+ end
34
+
35
+ should "return nil if unable to find the first associate of an object" do
36
+ assert_nil ActiveRecordClimateControlUnit._t_find_first_by_associate(:mongo_mapper_dashboard_id, 'abc123')
37
+ end
38
+
39
+ should "be able to find all associates of an object" do
40
+ nut_1 = ActiveRecordNut.create(:mongo_mapper_wheel_id => 'abc123')
41
+ nut_2 = ActiveRecordNut.create(:mongo_mapper_wheel_id => 'abc123')
42
+ nut_3 = ActiveRecordNut.create(:mongo_mapper_wheel_id => 'xyz456')
43
+ assert_set_equal [nut_1, nut_2], ActiveRecordNut._t_find_all_by_associate(:mongo_mapper_wheel_id, 'abc123')
44
+ end
45
+
46
+ should "return an empty array able to find the associates of an object" do
47
+ assert_set_equal [], ActiveRecordNut._t_find_all_by_associate(:mongo_mapper_wheel_id, 'abc123')
48
+ end
49
+
50
+ should "be able to reload an object from the database" do
51
+ nut = ActiveRecordNut.create
52
+ nut.mongo_mapper_wheel_id = 'abc123'
53
+ nut.reload
54
+ assert_equal '', nut.mongo_mapper_wheel_id
55
+ end
56
+
57
+ should "be able to clear the associates of a given object" do
58
+ nut = ActiveRecordNut.create
59
+ nut._t_associate_many(:mongo_mapper_wheels, ['abc123', 'def456', 'ghi789'])
60
+ nut.save
61
+ nut._t_clear_associates(:mongo_mapper_wheels)
62
+ assert_set_equal [], nut._t_get_associate_ids(:mongo_mapper_wheels)
63
+ end
64
+
65
+ should "be able to associate many objects with the given object" do
66
+ nut = ActiveRecordNut.create
67
+ nut._t_associate_many(:mongo_mapper_wheels, ['abc123', 'def456', 'ghi789'])
68
+ rows = ActiveRecordNut.connection.execute("select mongo_mapper_wheel_id from active_record_nuts_mongo_mapper_wheels where active_record_nut_id = #{nut.id}")
69
+ ids = []; rows.each { |r| ids << r[0] }; ids
70
+ assert_set_equal ['abc123', 'def456', 'ghi789'], ids
71
+ end
72
+
73
+ should "be able to get the ids of the objects associated with the given object" do
74
+ nut = ActiveRecordNut.create
75
+ nut._t_associate_many(:mongo_mapper_wheels, ['abc123', 'def456', 'ghi789'])
76
+ assert_set_equal ['abc123', 'def456', 'ghi789'], nut._t_get_associate_ids(:mongo_mapper_wheels)
77
+ end
78
+
79
+ should "return an empty array if there are no objects associated with the given object ids" do
80
+ nut = ActiveRecordNut.create
81
+ assert_set_equal [], nut._t_get_associate_ids(:mongo_mapper_wheels)
82
+ end
83
+ end
84
+
85
+ end