restforce-db 0.5.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +77 -21
  3. data/lib/restforce/db.rb +6 -0
  4. data/lib/restforce/db/associations/active_record.rb +1 -1
  5. data/lib/restforce/db/attribute_map.rb +5 -1
  6. data/lib/restforce/db/dsl.rb +80 -0
  7. data/lib/restforce/db/initializer.rb +3 -2
  8. data/lib/restforce/db/mapping.rb +20 -77
  9. data/lib/restforce/db/model.rb +7 -4
  10. data/lib/restforce/db/registry.rb +62 -0
  11. data/lib/restforce/db/strategies/always.rb +38 -0
  12. data/lib/restforce/db/strategies/passive.rb +37 -0
  13. data/lib/restforce/db/strategy.rb +25 -0
  14. data/lib/restforce/db/version.rb +1 -1
  15. data/test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_Salesforce_record/{for_a_non-root_mapping → for_a_Passive_strategy}/does_not_create_a_database_record.yml +20 -20
  16. data/test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_Salesforce_record/for_an_Always_strategy/creates_a_matching_database_record.yml +159 -0
  17. data/test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_database_record/{for_a_root_mapping → for_an_Always_strategy}/populates_Salesforce_with_the_new_record.yml +44 -43
  18. data/test/cassettes/{Restforce_DB_Initializer/_run/given_an_existing_Salesforce_record/for_a_root_mapping/creates_a_matching_database_record.yml → Restforce_DB_Strategies_Always/_build_/given_a_Salesforce_record/wants_to_build_a_new_matching_record.yml} +30 -30
  19. data/test/cassettes/Restforce_DB_Strategies_Always/_build_/given_a_Salesforce_record/with_a_corresponding_database_record/does_not_want_to_build_a_new_record.yml +158 -0
  20. data/test/lib/restforce/db/associations/active_record_test.rb +6 -6
  21. data/test/lib/restforce/db/attribute_map_test.rb +5 -1
  22. data/test/lib/restforce/db/dsl_test.rb +80 -0
  23. data/test/lib/restforce/db/initializer_test.rb +8 -8
  24. data/test/lib/restforce/db/mapping_test.rb +7 -17
  25. data/test/lib/restforce/db/model_test.rb +8 -9
  26. data/test/lib/restforce/db/record_types/active_record_test.rb +10 -6
  27. data/test/lib/restforce/db/registry_test.rb +43 -0
  28. data/test/lib/restforce/db/strategies/always_test.rb +38 -0
  29. data/test/lib/restforce/db/strategies/passive_test.rb +17 -0
  30. data/test/lib/restforce/db/strategy_test.rb +19 -0
  31. data/test/support/utilities.rb +7 -9
  32. metadata +17 -5
@@ -13,29 +13,19 @@ describe Restforce::DB::Mapping do
13
13
  column_two: "SF_Field_Two__c",
14
14
  }
15
15
  end
16
+ let(:through) { nil }
16
17
  let!(:mapping) do
17
- Restforce::DB::Mapping.new(
18
- database_model,
19
- salesforce_model,
20
- fields: fields,
21
- associations: associations,
22
- )
23
- end
24
-
25
- describe ".each" do
26
-
27
- # Restforce::DB::Mapping actually implements Enumerable, so we're just
28
- # going with a trivially testable portion of the Enumerable API.
29
- it "yields the registered record types" do
30
- expect(Restforce::DB::Mapping.first).to_equal mapping
18
+ Restforce::DB::Mapping.new(database_model, salesforce_model).tap do |m|
19
+ m.fields = fields
20
+ m.associations = associations
21
+ m.through = through
31
22
  end
32
23
  end
33
24
 
34
25
  describe "#initialize" do
35
26
 
36
- it "adds the mapping to the global collection" do
37
- expect(Restforce::DB::Mapping[database_model]).to_equal [mapping]
38
- expect(Restforce::DB::Mapping[salesforce_model]).to_equal [mapping]
27
+ it "defaults to an initialization strategy of `Always`" do
28
+ expect(mapping.strategy).to_be_instance_of(Restforce::DB::Strategies::Always)
39
29
  end
40
30
  end
41
31
 
@@ -6,12 +6,6 @@ describe Restforce::DB::Model do
6
6
 
7
7
  let(:database_model) { CustomObject }
8
8
  let(:salesforce_model) { "CustomObject__c" }
9
- let(:fields) do
10
- {
11
- name: "Name",
12
- example: "Example_Field__c",
13
- }
14
- end
15
9
 
16
10
  before do
17
11
  database_model.send(:include, Restforce::DB::Model)
@@ -19,11 +13,16 @@ describe Restforce::DB::Model do
19
13
 
20
14
  describe ".sync_with" do
21
15
  before do
22
- database_model.sync_with(salesforce_model, fields: fields)
16
+ database_model.sync_with(salesforce_model) do
17
+ maps(
18
+ name: "Name",
19
+ example: "Example_Field__c",
20
+ )
21
+ end
23
22
  end
24
23
 
25
- it "creates a mapping in Restforce::DB::Mapping" do
26
- expect(Restforce::DB::Mapping[database_model]).to_not_be :empty?
24
+ it "adds a mapping to the global Restforce::DB::Registry" do
25
+ expect(Restforce::DB::Registry[database_model]).to_not_be :empty?
27
26
  end
28
27
  end
29
28
 
@@ -26,6 +26,10 @@ describe Restforce::DB::RecordTypes::ActiveRecord do
26
26
  end
27
27
  let(:instance) { record_type.create!(create_from).record }
28
28
 
29
+ before do
30
+ Restforce::DB::Registry << mapping
31
+ end
32
+
29
33
  it "creates a record in the database from the passed Salesforce record's attributes" do
30
34
  expect(instance.salesforce_id).to_equal salesforce_id
31
35
  expect(instance.name).to_equal attributes[:name]
@@ -39,12 +43,12 @@ describe Restforce::DB::RecordTypes::ActiveRecord do
39
43
  let(:associations) { { user: "Friend__c" } }
40
44
 
41
45
  before do
42
- mapping = Restforce::DB::Mapping.new(
43
- User,
44
- "Contact",
45
- through: "Friend__c",
46
- fields: { email: "Email" },
47
- )
46
+ mapping = Restforce::DB::Mapping.new(User, "Contact").tap do |m|
47
+ m.through = "Friend__c"
48
+ m.fields = { email: "Email" }
49
+ end
50
+ Restforce::DB::Registry << mapping
51
+
48
52
  salesforce_record_type = mapping.salesforce_record_type
49
53
 
50
54
  # Stub out the `#find` method on the record type
@@ -0,0 +1,43 @@
1
+ describe Restforce::DB::Registry do
2
+
3
+ configure!
4
+ mappings!
5
+
6
+ describe ".<<" do
7
+ before do
8
+ Restforce::DB::Registry << mapping
9
+ end
10
+
11
+ it "appends a mapping in the registry under its ActiveRecord class" do
12
+ expect(Restforce::DB::Registry[database_model]).to_equal [mapping]
13
+ end
14
+
15
+ it "appends a mapping in the registry under its Salesforce object type" do
16
+ expect(Restforce::DB::Registry[salesforce_model]).to_equal [mapping]
17
+ end
18
+ end
19
+
20
+ describe ".each" do
21
+ before do
22
+ Restforce::DB::Registry << mapping
23
+ end
24
+
25
+ # Restforce::DB::Registry actually implements Enumerable, so we're just
26
+ # going with a trivially testable portion of the Enumerable API.
27
+ it "yields the registered record types" do
28
+ expect(Restforce::DB::Registry.first).to_equal mapping
29
+ end
30
+ end
31
+
32
+ describe ".clean!" do
33
+ before do
34
+ Restforce::DB::Registry << mapping
35
+ end
36
+
37
+ it "clears out the currently registered mappings" do
38
+ expect(Restforce::DB::Registry.first).to_not_be_nil
39
+ Restforce::DB::Registry.clean!
40
+ expect(Restforce::DB::Registry.first).to_be_nil
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ require_relative "../../../../test_helper"
2
+
3
+ describe Restforce::DB::Strategies::Always do
4
+
5
+ configure!
6
+ mappings!
7
+
8
+ let(:strategy) { Restforce::DB::Strategies::Always.new }
9
+
10
+ it "is not a passive strategy" do
11
+ expect(strategy).to_not_be :passive?
12
+ end
13
+
14
+ describe "#build?", :vcr do
15
+
16
+ describe "given a Salesforce record" do
17
+ let(:salesforce_id) { Salesforce.create! salesforce_model }
18
+ let(:record) { mapping.salesforce_record_type.find(salesforce_id) }
19
+
20
+ it "wants to build a new matching record" do
21
+ expect(strategy.build?(record)).to_equal true
22
+ end
23
+
24
+ describe "with a corresponding database record" do
25
+ before do
26
+ CustomObject.create!(
27
+ salesforce_id: salesforce_id,
28
+ synchronized_at: Time.now,
29
+ )
30
+ end
31
+
32
+ it "does not want to build a new record" do
33
+ expect(strategy.build?(record)).to_equal false
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "../../../../test_helper"
2
+
3
+ describe Restforce::DB::Strategies::Passive do
4
+ let(:strategy) { Restforce::DB::Strategies::Passive.new }
5
+
6
+ it "is a passive strategy" do
7
+ expect(strategy).to_be :passive?
8
+ end
9
+
10
+ describe "#build?" do
11
+
12
+ it "returns false for any record" do
13
+ record = Object.new
14
+ expect(strategy.build?(record)).to_equal false
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ require_relative "../../../test_helper"
2
+
3
+ describe Restforce::DB::Strategy do
4
+
5
+ describe ".for" do
6
+ let(:strategy) { Restforce::DB::Strategy.for(type) }
7
+
8
+ describe ":always" do
9
+ let(:type) { :always }
10
+ it { expect(strategy).to_be_instance_of(Restforce::DB::Strategies::Always) }
11
+ end
12
+
13
+ describe ":always" do
14
+ let(:type) { :passive }
15
+ it { expect(strategy).to_be_instance_of(Restforce::DB::Strategies::Passive) }
16
+ end
17
+ end
18
+
19
+ end
@@ -5,7 +5,7 @@ def configure!
5
5
  end
6
6
 
7
7
  after do
8
- Restforce::DB::Mapping.collection = {}
8
+ Restforce::DB::Registry.clean!
9
9
  DatabaseCleaner.clean
10
10
  Salesforce.clean!
11
11
  end
@@ -20,13 +20,11 @@ def mappings!
20
20
  let(:conditions) { [] }
21
21
  let(:through) { nil }
22
22
  let!(:mapping) do
23
- Restforce::DB::Mapping.new(
24
- database_model,
25
- salesforce_model,
26
- through: through,
27
- fields: fields,
28
- associations: associations,
29
- conditions: conditions,
30
- )
23
+ Restforce::DB::Mapping.new(database_model, salesforce_model).tap do |m|
24
+ m.through = through
25
+ m.conditions = conditions
26
+ m.fields = fields
27
+ m.associations = associations
28
+ end
31
29
  end
32
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restforce-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Horner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -227,6 +227,7 @@ files:
227
227
  - lib/restforce/db/collector.rb
228
228
  - lib/restforce/db/command.rb
229
229
  - lib/restforce/db/configuration.rb
230
+ - lib/restforce/db/dsl.rb
230
231
  - lib/restforce/db/initializer.rb
231
232
  - lib/restforce/db/instances/active_record.rb
232
233
  - lib/restforce/db/instances/base.rb
@@ -236,7 +237,11 @@ files:
236
237
  - lib/restforce/db/record_types/active_record.rb
237
238
  - lib/restforce/db/record_types/base.rb
238
239
  - lib/restforce/db/record_types/salesforce.rb
240
+ - lib/restforce/db/registry.rb
239
241
  - lib/restforce/db/runner.rb
242
+ - lib/restforce/db/strategies/always.rb
243
+ - lib/restforce/db/strategies/passive.rb
244
+ - lib/restforce/db/strategy.rb
240
245
  - lib/restforce/db/synchronizer.rb
241
246
  - lib/restforce/db/tracker.rb
242
247
  - lib/restforce/db/version.rb
@@ -247,9 +252,9 @@ files:
247
252
  - test/cassettes/Restforce_DB_Collector/_run/given_a_Salesforce_record_with_an_associated_database_record/returns_the_attributes_from_both_records.yml
248
253
  - test/cassettes/Restforce_DB_Collector/_run/given_an_existing_Salesforce_record/returns_the_attributes_from_the_Salesforce_record.yml
249
254
  - test/cassettes/Restforce_DB_Collector/_run/given_an_existing_database_record/returns_the_attributes_from_the_database_record.yml
250
- - test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_Salesforce_record/for_a_non-root_mapping/does_not_create_a_database_record.yml
251
- - test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_Salesforce_record/for_a_root_mapping/creates_a_matching_database_record.yml
252
- - test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_database_record/for_a_root_mapping/populates_Salesforce_with_the_new_record.yml
255
+ - test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_Salesforce_record/for_a_Passive_strategy/does_not_create_a_database_record.yml
256
+ - test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_Salesforce_record/for_an_Always_strategy/creates_a_matching_database_record.yml
257
+ - test/cassettes/Restforce_DB_Initializer/_run/given_an_existing_database_record/for_an_Always_strategy/populates_Salesforce_with_the_new_record.yml
253
258
  - test/cassettes/Restforce_DB_Instances_Salesforce/_copy_/updates_the_record_with_the_attributes_from_the_copied_object.yml
254
259
  - test/cassettes/Restforce_DB_Instances_Salesforce/_synced_/when_a_matching_database_record_exists/returns_true.yml
255
260
  - test/cassettes/Restforce_DB_Instances_Salesforce/_synced_/when_no_matching_database_record_exists/returns_false.yml
@@ -262,6 +267,8 @@ files:
262
267
  - test/cassettes/Restforce_DB_RecordTypes_Salesforce/_find/given_a_set_of_mapping_conditions/when_a_record_does_not_meet_the_conditions/does_not_find_the_record.yml
263
268
  - test/cassettes/Restforce_DB_RecordTypes_Salesforce/_find/given_a_set_of_mapping_conditions/when_a_record_meets_the_conditions/finds_the_record.yml
264
269
  - test/cassettes/Restforce_DB_RecordTypes_Salesforce/_find/returns_nil_when_no_matching_record_exists.yml
270
+ - test/cassettes/Restforce_DB_Strategies_Always/_build_/given_a_Salesforce_record/wants_to_build_a_new_matching_record.yml
271
+ - test/cassettes/Restforce_DB_Strategies_Always/_build_/given_a_Salesforce_record/with_a_corresponding_database_record/does_not_want_to_build_a_new_record.yml
265
272
  - test/cassettes/Restforce_DB_Synchronizer/_run/given_a_Salesforce_record_with_an_associated_database_record/updates_the_database_record.yml
266
273
  - test/cassettes/Restforce_DB_Synchronizer/_run/given_a_Salesforce_record_with_an_associated_database_record/updates_the_salesforce_record.yml
267
274
  - test/lib/restforce/db/accumulator_test.rb
@@ -269,6 +276,7 @@ files:
269
276
  - test/lib/restforce/db/attribute_map_test.rb
270
277
  - test/lib/restforce/db/collector_test.rb
271
278
  - test/lib/restforce/db/configuration_test.rb
279
+ - test/lib/restforce/db/dsl_test.rb
272
280
  - test/lib/restforce/db/initializer_test.rb
273
281
  - test/lib/restforce/db/instances/active_record_test.rb
274
282
  - test/lib/restforce/db/instances/salesforce_test.rb
@@ -276,7 +284,11 @@ files:
276
284
  - test/lib/restforce/db/model_test.rb
277
285
  - test/lib/restforce/db/record_types/active_record_test.rb
278
286
  - test/lib/restforce/db/record_types/salesforce_test.rb
287
+ - test/lib/restforce/db/registry_test.rb
279
288
  - test/lib/restforce/db/runner_test.rb
289
+ - test/lib/restforce/db/strategies/always_test.rb
290
+ - test/lib/restforce/db/strategies/passive_test.rb
291
+ - test/lib/restforce/db/strategy_test.rb
280
292
  - test/lib/restforce/db/synchronizer_test.rb
281
293
  - test/lib/restforce/db/tracker_test.rb
282
294
  - test/lib/restforce/db_test.rb