remotable 0.3.0 → 0.4.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,8 +6,8 @@ require "rr"
6
6
 
7
7
  class BespokeTest < ActiveSupport::TestCase
8
8
  include RR::Adapters::TestUnit
9
-
10
-
9
+
10
+
11
11
  teardown do
12
12
  def model.new_resource
13
13
  BespokeResource.new
@@ -16,61 +16,61 @@ class BespokeTest < ActiveSupport::TestCase
16
16
  nil
17
17
  end
18
18
  end
19
-
20
-
21
-
19
+
20
+
21
+
22
22
  # ========================================================================= #
23
23
  # Finding #
24
24
  # ========================================================================= #
25
-
25
+
26
26
  test "should be able to find resources by different attributes" do
27
27
  new_tenant_slug = "not_found"
28
28
  assert_equal 0, BespokeTenant.where(:slug => new_tenant_slug).count,
29
29
  "There's not supposed to be a BespokeTenant with the slug #{new_tenant_slug}."
30
-
30
+
31
31
  def model.find_by(attribute, value)
32
32
  BespokeResource.new(:slug => value)
33
33
  end
34
-
34
+
35
35
  new_tenant = BespokeTenant.find_by_slug(new_tenant_slug)
36
36
  assert_not_nil new_tenant, "A remote @tenant was not found with the slug #{new_tenant_slug.inspect}"
37
37
  end
38
-
38
+
39
39
  test "should create a record locally when fetching a new remote resource" do
40
40
  new_tenant_slug = "17"
41
41
  assert_equal 0, BespokeTenant.where(:slug => new_tenant_slug).count,
42
42
  "There's not supposed to be a BespokeTenant with the slug #{new_tenant_slug}."
43
-
43
+
44
44
  def model.find_by(attribute, value)
45
45
  BespokeResource.new(:slug => value)
46
46
  end
47
-
47
+
48
48
  assert_difference "BespokeTenant.count", +1 do
49
49
  new_tenant = BespokeTenant.find_by_slug(new_tenant_slug)
50
50
  assert_not_nil new_tenant, "A remote tenant was not found with the id #{new_tenant_slug.inspect}"
51
51
  end
52
52
  end
53
-
53
+
54
54
  test "if a resource is neither local nor remote, raise an exception with the bang method" do
55
55
  new_tenant_slug = "not_found3"
56
56
  assert_equal 0, BespokeTenant.where(:slug => new_tenant_slug).count,
57
57
  "There's not supposed to be a BespokeTenant with the slug #{new_tenant_slug}."
58
-
58
+
59
59
  assert_raises ActiveRecord::RecordNotFound do
60
60
  BespokeTenant.find_by_slug!(new_tenant_slug)
61
61
  end
62
62
  end
63
-
64
-
65
-
63
+
64
+
65
+
66
66
  # ========================================================================= #
67
67
  # Updating #
68
68
  # ========================================================================= #
69
-
69
+
70
70
  test "should update a record remotely when updating one locally" do
71
71
  @tenant = Factory(:bespoke_tenant)
72
72
  new_name = "Totally Wonky"
73
-
73
+
74
74
  # RemoteTenant.run_simulation do |s|
75
75
  # s.show(@tenant.remote_id, {
76
76
  # :id => @tenant.remote_id,
@@ -78,89 +78,89 @@ class BespokeTest < ActiveSupport::TestCase
78
78
  # :church_name => @tenant.name
79
79
  # })
80
80
  # s.update(@tenant.remote_id)
81
- #
81
+ #
82
82
  # @tenant.nosync = false
83
83
  # @tenant.name = "Totally Wonky"
84
84
  # assert_equal true, @tenant.any_remote_changes?
85
- #
85
+ #
86
86
  # @tenant.save!
87
- #
87
+ #
88
88
  # pending "Not sure how to test that an update happened"
89
89
  # end
90
90
  end
91
-
91
+
92
92
  test "should fail to update a record locally when failing to update one remotely" do
93
93
  @tenant = Factory(:bespoke_tenant, :nosync => false)
94
-
94
+
95
95
  def model.find_by(attribute, value)
96
96
  BespokeResource.new(attribute => value)
97
97
  end
98
-
98
+
99
99
  def resource.save
100
100
  false
101
101
  end
102
-
102
+
103
103
  def resource.errors
104
104
  {:name => ["is already taken"]}
105
105
  end
106
-
106
+
107
107
  @tenant.name = "Totally Wonky"
108
108
  assert_raises(ActiveRecord::RecordInvalid) do
109
109
  @tenant.save!
110
110
  end
111
111
  assert_equal ["is already taken"], @tenant.errors[:name]
112
112
  end
113
-
114
-
115
-
116
-
113
+
114
+
115
+
116
+
117
117
  # ========================================================================= #
118
118
  # Creating #
119
119
  # ========================================================================= #
120
-
120
+
121
121
  test "should create a record remotely when creating one locally" do
122
122
  @tenant = BespokeTenant.new({
123
123
  :slug => "brand_new",
124
124
  :name => "Brand New"
125
125
  })
126
-
126
+
127
127
  assert_nil @tenant.remote_resource
128
128
  @tenant.save!
129
129
  assert_not_nil @tenant.remote_resource
130
130
  end
131
-
131
+
132
132
  test "should fail to create a record locally when failing to create one remotely" do
133
133
  @tenant = BespokeTenant.new({
134
134
  :slug => "brand_new",
135
135
  :name => "Brand New"
136
136
  })
137
-
137
+
138
138
  def model.new_resource
139
139
  resource = BespokeResource.new
140
140
  def resource.save; false; end
141
141
  def resource.errors; {:name => ["is already taken"]}; end
142
142
  resource
143
143
  end
144
-
144
+
145
145
  assert_raises(ActiveRecord::RecordInvalid) do
146
146
  @tenant.save!
147
147
  end
148
-
148
+
149
149
  assert_equal ["is already taken"], @tenant.errors[:name]
150
150
  end
151
-
152
-
153
-
151
+
152
+
153
+
154
154
  # ========================================================================= #
155
155
  # Destroying #
156
156
  # ========================================================================= #
157
-
157
+
158
158
  test "should destroy a record remotely when destroying one locally" do
159
159
  @tenant = Factory(:bespoke_tenant, :nosync => false)
160
160
  mock(resource).destroy { true }
161
161
  @tenant.destroy
162
162
  end
163
-
163
+
164
164
  test "should fail to destroy a record locally when failing to destroy one remotely" do
165
165
  @tenant = Factory(:bespoke_tenant, :nosync => false)
166
166
  mock(resource).destroy { raise StandardError }
@@ -168,30 +168,30 @@ class BespokeTest < ActiveSupport::TestCase
168
168
  @tenant.destroy
169
169
  end
170
170
  end
171
-
171
+
172
172
  test "should delete a local record when a remote record has been deleted" do
173
173
  @tenant = Factory(:bespoke_tenant, :expires_at => 1.year.ago)
174
-
174
+
175
175
  def model.find_by(remote_attr, value)
176
176
  nil
177
177
  end
178
-
178
+
179
179
  assert_difference "BespokeTenant.count", -1 do
180
180
  @tenant = BespokeTenant.find_by_slug(@tenant.slug)
181
181
  assert_equal nil, @tenant
182
182
  end
183
183
  end
184
-
185
-
186
-
184
+
185
+
186
+
187
187
  private
188
-
188
+
189
189
  def model
190
190
  BespokeTenant.remote_model
191
191
  end
192
-
192
+
193
193
  def resource
194
194
  @tenant.remote_resource
195
195
  end
196
-
196
+
197
197
  end
@@ -4,27 +4,27 @@ require "support/active_resource"
4
4
 
5
5
 
6
6
  class NoSyncTest < ActiveSupport::TestCase
7
-
8
-
9
-
7
+
8
+
9
+
10
10
  test "nosync? should be false by default" do
11
11
  assert_equal false, Tenant.new.nosync?
12
12
  end
13
-
13
+
14
14
  test "nosync? should be true if remotable is turned off globally" do
15
15
  Remotable.nosync do
16
16
  assert_equal true, Tenant.new.nosync?
17
17
  assert_equal true, RemoteWithoutKey.new.nosync?
18
18
  end
19
19
  end
20
-
20
+
21
21
  test "nosync? should be true if remotable is turned off for the model" do
22
22
  Tenant.nosync do
23
23
  assert_equal true, Tenant.new.nosync?
24
24
  assert_equal false, RemoteWithoutKey.new.nosync?
25
25
  end
26
26
  end
27
-
27
+
28
28
  test "nosync? should be false if syncing is resumed temporarily on a model that prevents it by default" do
29
29
  Tenant.nosync!
30
30
  assert_equal true, Tenant.new.nosync?
@@ -33,10 +33,10 @@ class NoSyncTest < ActiveSupport::TestCase
33
33
  end
34
34
  assert_equal true, Tenant.new.nosync?
35
35
  end
36
-
36
+
37
37
  test "nosync? should take the value further up the chain if a model's value is temporarily cleared" do
38
38
  assert_not_nil Tenant.remote_model
39
-
39
+
40
40
  Remotable.nosync!
41
41
  Tenant.nosync(false) do
42
42
  Tenant.nosync(nil) do
@@ -48,22 +48,22 @@ class NoSyncTest < ActiveSupport::TestCase
48
48
  end
49
49
  assert_equal true, Tenant.nosync?
50
50
  end
51
-
52
-
53
-
51
+
52
+
53
+
54
54
  # ========================================================================= #
55
55
  # Finding #
56
56
  # ========================================================================= #
57
-
57
+
58
58
  test "should do nothing if a tenant is expired" do
59
59
  tenant = Factory(:tenant, :expires_at => 1.year.ago)
60
-
60
+
61
61
  Remotable.nosync do
62
62
  result = Tenant.find_by_remote_id(tenant.remote_id)
63
63
  assert_equal tenant, result
64
64
  end
65
65
  end
66
-
67
-
68
-
66
+
67
+
68
+
69
69
  end
@@ -5,49 +5,49 @@ require "support/null"
5
5
 
6
6
  class NullRemoteTest < ActiveSupport::TestCase
7
7
  include RR::Adapters::TestUnit
8
-
9
-
10
-
8
+
9
+
10
+
11
11
  # ========================================================================= #
12
12
  # Finding #
13
13
  # ========================================================================= #
14
-
14
+
15
15
  test "should do nothing if a tenant is expired" do
16
16
  tenant = Factory(:null_test_tenant, expires_at: 2.days.ago)
17
17
  result = NullTestTenant.find_by_slug(tenant.slug)
18
18
  assert_equal tenant, result
19
19
  end
20
-
20
+
21
21
  test "should raise an exception with the bang method if resource isn't found locally" do
22
22
  new_tenant_slug = "not_found3"
23
23
  assert_equal 0, NullTestTenant.where(:slug => new_tenant_slug).count,
24
24
  "There's not supposed to be a NullTestTenant with the slug #{new_tenant_slug}."
25
-
25
+
26
26
  assert_raises ActiveRecord::RecordNotFound do
27
27
  NullTestTenant.find_by_slug!(new_tenant_slug)
28
28
  end
29
29
  end
30
-
31
-
32
-
30
+
31
+
32
+
33
33
  # ========================================================================= #
34
34
  # Updating #
35
35
  # ========================================================================= #
36
-
36
+
37
37
  test "should update a record locally without any interference" do
38
38
  tenant = Factory(:null_test_tenant)
39
39
  new_name = "Totally Wonky"
40
-
40
+
41
41
  tenant.name = new_name
42
42
  tenant.save!
43
43
  end
44
-
45
-
46
-
44
+
45
+
46
+
47
47
  # ========================================================================= #
48
48
  # Creating #
49
49
  # ========================================================================= #
50
-
50
+
51
51
  test "should create a record locally without any interference" do
52
52
  tenant = NullTestTenant.create!({
53
53
  :slug => "brand_new",
@@ -55,19 +55,19 @@ class NullRemoteTest < ActiveSupport::TestCase
55
55
  })
56
56
  assert_not_nil tenant.remote_resource
57
57
  end
58
-
59
-
60
-
58
+
59
+
60
+
61
61
  # ========================================================================= #
62
62
  # Destroying #
63
63
  # ========================================================================= #
64
-
64
+
65
65
  test "should destroy a record locally without any interference" do
66
66
  tenant = Factory(:null_test_tenant)
67
67
  tenant.nosync = false
68
68
  tenant.destroy
69
69
  end
70
-
71
-
72
-
70
+
71
+
72
+
73
73
  end
@@ -4,50 +4,50 @@ require "support/bespoke"
4
4
 
5
5
 
6
6
  class RemotableTest < ActiveSupport::TestCase
7
-
8
-
9
-
7
+
8
+
9
+
10
10
  # ========================================================================= #
11
11
  # Keys #
12
12
  # ========================================================================= #
13
-
13
+
14
14
  test "should consider :id to be the remote key if none is specified" do
15
15
  assert_equal :id, RemoteWithoutKey.remote_key
16
16
  assert_equal :remote_id, RemoteWithoutKey.local_key
17
17
  end
18
-
18
+
19
19
  test "should use a different remote_key if one is supplied" do
20
20
  assert_equal :slug, RemoteWithKey.remote_key
21
21
  assert_equal :slug, RemoteWithKey.local_key
22
22
  end
23
-
23
+
24
24
  test "should be able to generate paths for with different attributes" do
25
25
  assert_equal "by_slug/value", Tenant.remote_path_for(:slug, "value")
26
26
  assert_equal "by_nombre/value", Tenant.remote_path_for(:name, "value")
27
27
  end
28
-
28
+
29
29
  test "should be able to generate paths for composite keys" do
30
30
  assert_equal "groups/5/tenants/test", RemoteWithCompositeKey.remote_path_for([:group_id, :slug], [5, "test"])
31
31
  end
32
-
33
-
34
-
32
+
33
+
34
+
35
35
  # ========================================================================= #
36
36
  # Temporary remote models #
37
37
  # ========================================================================= #
38
-
38
+
39
39
  test "should support temporary models" do
40
40
  assert_nil BespokeTenant.find_by_slug("404")
41
41
  assert_not_nil BespokeTenant.with_remote_model(BespokeModel2.new) { BespokeTenant.find_by_slug("404") }
42
42
  assert_nil BespokeTenant.find_by_slug("405")
43
43
  end
44
-
44
+
45
45
  test "should support temporary models and chainable syntax" do
46
46
  assert_nil BespokeTenant.find_by_slug("404")
47
47
  assert_not_nil BespokeTenant.with_remote_model(BespokeModel2.new).find_by_slug("404")
48
48
  assert_nil BespokeTenant.find_by_slug("405")
49
49
  end
50
-
50
+
51
51
  test "should support setting remote_model to nil" do
52
52
  Tenant.with_remote_model(nil) do
53
53
  Tenant.remote_model
@@ -56,9 +56,9 @@ class RemotableTest < ActiveSupport::TestCase
56
56
  assert_not_nil Tenant.find_by_name("Test 1"), "new tenant was not found"
57
57
  end
58
58
  end
59
-
60
-
61
-
59
+
60
+
61
+
62
62
  # ========================================================================= #
63
63
  # Edge cases #
64
64
  # #
@@ -68,7 +68,7 @@ class RemotableTest < ActiveSupport::TestCase
68
68
  # Remotable doesn't break when it tries to look up a remote record #
69
69
  # with a nil key. #
70
70
  # ========================================================================= #
71
-
71
+
72
72
  test "should not break when refreshing a record which does not have a foreign key" do
73
73
  tenant = Tenant.nosync { Tenant.create!(
74
74
  :name => "Test 1",
@@ -76,67 +76,67 @@ class RemotableTest < ActiveSupport::TestCase
76
76
  :remote_id => nil,
77
77
  :expires_at => 1.day.ago ) }
78
78
  assert_equal nil, tenant.remote_id
79
-
79
+
80
80
  # Fetching this tenant, Remotable will want to
81
81
  # refresh it, but it shouldn't because it can't.
82
82
  assert_not_nil Tenant.where(:slug => "test-1").first, "A tenant with the slug \"test-1\" was not found"
83
83
  end
84
-
84
+
85
85
  test "should not break when updating a record which does not have a foreign key" do
86
86
  tenant = Tenant.nosync { Tenant.create!(
87
87
  :name => "Test 1",
88
88
  :slug => "test-1",
89
89
  :remote_id => nil ) }
90
90
  assert_equal nil, tenant.remote_id
91
-
91
+
92
92
  # Updating this tenatn, Remotable will want to
93
93
  # sync it, but it shouldn't because it can't.
94
94
  assert_equal true, tenant.update_attributes(:name => "Test 2"), "The tenant was not updated (errors: #{tenant.errors.full_messages.join(", ")})"
95
95
  end
96
-
97
-
98
-
96
+
97
+
98
+
99
99
  # ========================================================================= #
100
100
  # Finders #
101
101
  # ========================================================================= #
102
-
102
+
103
103
  test "should create expected finders" do
104
104
  assert_equal true, Tenant.respond_to?(:find_by_name)
105
105
  assert_equal true, Tenant.respond_to?(:find_by_slug)
106
106
  assert_equal true, RemoteWithoutKey.respond_to?(:find_by_id)
107
107
  assert_equal true, RemoteWithCompositeKey.respond_to?(:find_by_group_id_and_slug)
108
108
  end
109
-
109
+
110
110
  test "should recognize a finder method with a single key" do
111
111
  method_details = RemoteWithKey.recognize_remote_finder_method(:find_by_slug)
112
112
  assert_not_equal false, method_details
113
113
  assert_equal :slug, method_details[:remote_key]
114
114
  end
115
-
115
+
116
116
  test "should recognize a finder method with a composite key" do
117
117
  method_details = RemoteWithCompositeKey.recognize_remote_finder_method(:find_by_group_id_and_slug)
118
118
  assert_not_equal false, method_details
119
119
  assert_equal [:group_id, :slug], method_details[:remote_key]
120
120
  end
121
-
122
-
123
-
121
+
122
+
123
+
124
124
  # ========================================================================= #
125
125
  # Validating Models #
126
126
  # ========================================================================= #
127
-
127
+
128
128
  test "should raise an exception if a remote model does not respond to all class methods" do
129
129
  class Example1 < ActiveRecord::Base; self.table_name = "tenants"; end
130
130
  class RemoteModel1; def self.find_by(*args); end; end
131
131
  assert_raise(Remotable::InvalidRemoteModel) { Example1.remote_model RemoteModel1 }
132
132
  end
133
-
133
+
134
134
  test "should raise an exception if a remote resource does not respond to all instance methods" do
135
135
  class Example2 < ActiveRecord::Base; self.table_name = "tenants"; end
136
136
  class RemoteModel2; def self.new_resource; Object.new; end; end
137
137
  assert_raise(Remotable::InvalidRemoteModel) { Example2.remote_model RemoteModel2 }
138
138
  end
139
-
139
+
140
140
  test "should not raise an exception if remote models are not being validated" do
141
141
  Remotable.without_validation do
142
142
  class Example4 < ActiveRecord::Base; self.table_name = "tenants"; end
@@ -144,11 +144,11 @@ class RemotableTest < ActiveSupport::TestCase
144
144
  assert_nothing_raised { Example4.remote_model RemoteModel4 }
145
145
  end
146
146
  end
147
-
147
+
148
148
  test "should not raise an exception if a remote model responds to all required methods" do
149
149
  class Example3 < ActiveRecord::Base; self.table_name = "tenants"; end
150
150
  assert_nothing_raised { Example3.remote_model BespokeModel.new }
151
151
  end
152
-
153
-
152
+
153
+
154
154
  end