remote_factory_girl 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 568879d0cb0a73b0a0b4cfbc8d24a7e55db06052
4
- data.tar.gz: 050ebe624b42a226271972e4174d635750a1b998
3
+ metadata.gz: 29a342e578e2499f6d8cd58bfc6d52b669b62d4f
4
+ data.tar.gz: 3eef162182966244ea79a39b0d7daaca444198ca
5
5
  SHA512:
6
- metadata.gz: 7b9105b90699050925579b0e0dc4fd9b7af917f6c6073b1f90798ab9c9ac030627b12f5d7169adf05677b984e8cdc0541a8ffb1af3c1923fac5a0f1f789d2311
7
- data.tar.gz: 6d1c20f5438cb6eebdb16f019415843c628ff4c43267c9c85a422e1f55eb27521fb2bc9a407ea5f3bb62482070b477598127a91fa854074c04ffab9709af5f92
6
+ metadata.gz: 72b597be3b6f6798478bd2e9f15b2408ff8726bc8c5d670a322871a806e0bd6cbbc4bcf3f739391c2ae7e790d1e84665fdd36bd2105f214e23d0dbf6f5c55200
7
+ data.tar.gz: 7b8c81a70d02f0c6569ce6e64ca7cb0a4e94debb928d77c61a0ac29a791835cffc8edb000a5ba921dd362086dd635ef20c1b07d2f05cc3b0201e78ba2d3a4f29
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ### 1.1.0
2
+ 1. Configure and create test data in multiple "home"/"remote" applications
3
+ 2. Remove RemoteFactoryGirl.reset. Instead use, RemoteFactoryGirl.remotes_config.reset
1
4
  ### 1.0.2 - Aug. 19, 2014
2
5
  1. Update specs to use Rspec '3.0.0' syntax
3
6
 
@@ -1,5 +1,6 @@
1
1
  require "remote_factory_girl/version"
2
2
  require 'remote_factory_girl/config'
3
+ require 'remote_factory_girl/remotes_config'
3
4
  require 'remote_factory_girl/http'
4
5
  require 'remote_factory_girl/config_applier'
5
6
  require 'remote_factory_girl/hash_to_dot'
@@ -23,32 +24,35 @@ module RemoteFactoryGirl
23
24
  end
24
25
  end
25
26
 
26
- def self.configure(config = Config, &block)
27
+ def self.configure(remote_name = remotes_config.default_remote_name, config = Config, &block)
27
28
  configuration = Config.new
28
29
  yield(configuration)
29
- self.config = configuration
30
+ remotes_config.remotes[remote_name] = configuration
30
31
  end
31
32
 
32
33
  def self.factories(params = {}, http = Http)
33
- factory = RemoteFactoryGirl.new(config)
34
+ config_for_remote = config(remotes_config.current_remote)
35
+ factory = RemoteFactoryGirl.new(config_for_remote)
34
36
  factory.factories(params, http).to_hash
35
37
  end
36
38
 
37
39
  def self.create(factory_name, attributes = {}, config_applier = ConfigApplier, http = Http)
38
- factory = RemoteFactoryGirl.new(config)
40
+ config_for_remote = config(remotes_config.current_remote)
41
+ factory = RemoteFactoryGirl.new(config_for_remote)
39
42
  factory.create(factory_name, attributes, http)
40
- config_applier.apply_config(factory.response.to_hash, config.to_hash)
43
+ config_applier.apply_config(factory.response.to_hash, config_for_remote.to_hash)
41
44
  end
42
45
 
43
- def self.config
44
- @config
46
+ def self.with_remote(remote_name = remotes_config.default_remote_name)
47
+ remotes_config.current_remote = remote_name
48
+ self
45
49
  end
46
50
 
47
- def self.config=(config)
48
- @config = config
51
+ def self.config(remote_name = remotes_config.default_remote_name)
52
+ remotes_config.remotes[remote_name]
49
53
  end
50
54
 
51
- def self.reset(config = Config.new)
52
- self.config = config
55
+ def self.remotes_config
56
+ @remotes_config ||= RemotesConfig.new
53
57
  end
54
58
  end
@@ -0,0 +1,30 @@
1
+ require 'remote_factory_girl/config'
2
+
3
+ module RemoteFactoryGirl
4
+ class RemotesConfig
5
+
6
+ attr_writer :current_remote
7
+ attr_accessor :remotes
8
+
9
+ DEFAULT_REMOTE_NAME = :default
10
+
11
+ alias_method :to_hash, :remotes
12
+
13
+ def initialize
14
+ @remotes = {}
15
+ end
16
+
17
+ def current_remote
18
+ @current_remote || default_remote_name
19
+ end
20
+
21
+ def default_remote_name
22
+ DEFAULT_REMOTE_NAME
23
+ end
24
+
25
+ def reset(config = Config.new)
26
+ self.current_remote = default_remote_name
27
+ self.remotes = { default_remote_name => config}
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module RemoteFactoryGirl
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,8 +1,9 @@
1
1
  require 'remote_factory_girl'
2
+ require 'spec_helper'
2
3
 
3
4
  describe RemoteFactoryGirl do
4
5
 
5
- before { RemoteFactoryGirl.reset }
6
+ before { RemoteFactoryGirl.remotes_config.reset }
6
7
 
7
8
  describe 'configuration' do
8
9
  it 'should be configured with correct defaults' do
@@ -49,6 +50,34 @@ describe RemoteFactoryGirl do
49
50
  RemoteFactoryGirl.config.https = true
50
51
  expect(RemoteFactoryGirl.config.https).to eq true
51
52
  end
53
+
54
+ context 'when configuring multiple remotes' do
55
+
56
+ before do
57
+ configure_remote_factory_girl(remote_name: :travis,
58
+ host: 'localhost',
59
+ port: 3000,
60
+ end_point: '/remote_factory_girl/travis/home')
61
+ configure_remote_factory_girl(remote_name: :casey,
62
+ host: 'over_the_rainbow',
63
+ port: 6000,
64
+ end_point: '/remote_factory_girl/casey/home')
65
+ end
66
+
67
+ it 'should return configuration for remote "travis"' do
68
+ expect(RemoteFactoryGirl.config(:travis).home).to eq({:host => 'localhost',
69
+ :port => 3000,
70
+ :end_point => '/remote_factory_girl/travis/home'})
71
+ expect(RemoteFactoryGirl.config(:travis).return_with_root).to eq(true)
72
+ expect(RemoteFactoryGirl.config(:travis).return_response_as).to eq(:as_hash)
73
+ end
74
+
75
+ it 'should return configuration for remote "casey"' do
76
+ expect(RemoteFactoryGirl.config(:casey).home).to eq({:host => 'over_the_rainbow',
77
+ :port => 6000,
78
+ :end_point => '/remote_factory_girl/casey/home'})
79
+ end
80
+ end
52
81
  end
53
82
 
54
83
  describe 'errors' do
@@ -71,15 +100,114 @@ describe RemoteFactoryGirl do
71
100
  end
72
101
 
73
102
  describe '.factories' do
103
+ context 'when multiple remotes are configured' do
74
104
 
75
- before { RemoteFactoryGirl.config.home[:host] = 'localhost' }
105
+ before do
106
+ configure_remote_factory_girl(remote_name: :travis,
107
+ host: 'localhost',
108
+ port: 3000,
109
+ end_point: '/remote_factory_girl/travis/home')
110
+ configure_remote_factory_girl(remote_name: :casey,
111
+ host: 'over_the_rainbow',
112
+ port: 6000,
113
+ end_point: '/remote_factory_girl/casey/home')
114
+ end
76
115
 
77
- it 'should return all factories available' do
78
- expect(RemoteFactoryGirl.factories).to match_array(['user', 'user_admin'])
116
+ context 'for remote "travis"' do
117
+ it 'should return all factories available' do
118
+ expect(RemoteFactoryGirl.with_remote(:travis).factories).to match_array(['user', 'user_admin'])
119
+ end
120
+
121
+ it 'should be configured to send HTTP request to remote "travis"' do
122
+ remote_factory_girl = RemoteFactoryGirl.with_remote(:travis)
123
+
124
+ expect(remote_factory_girl).to receive(:factories) do
125
+ expect(
126
+ remote_factory_girl.remotes_config.current_remote
127
+ ).to eq(:travis)
128
+ expect(
129
+ remote_factory_girl.config(:travis).home_url
130
+ ).to eq('http://localhost:3000/remote_factory_girl/travis/home')
131
+ end
132
+ remote_factory_girl.factories
133
+ end
134
+ end
135
+
136
+ context 'for remote "casey"' do
137
+ it 'should return all factories available' do
138
+ expect(RemoteFactoryGirl.with_remote(:casey).factories).to match_array(['user', 'user_admin'])
139
+ end
140
+
141
+ it 'should be configured to send HTTP request to remote "casey"' do
142
+ remote_factory_girl = RemoteFactoryGirl.with_remote(:casey)
143
+
144
+ expect(remote_factory_girl).to receive(:factories) do
145
+ expect(
146
+ remote_factory_girl.remotes_config.current_remote
147
+ ).to eq(:casey)
148
+ expect(
149
+ remote_factory_girl.config(:casey).home_url
150
+ ).to eq('http://over_the_rainbow:6000/remote_factory_girl/casey/home')
151
+ end
152
+ remote_factory_girl.factories
153
+ end
154
+ end
155
+ end
156
+
157
+ context 'when configured with remote "default"' do
158
+
159
+ before do
160
+ configure_remote_factory_girl(host: 'not_configured_with_name',
161
+ port: 9000)
162
+ end
163
+
164
+ it 'should return all factories available' do
165
+ expect(RemoteFactoryGirl.factories).to match_array(['user', 'user_admin'])
166
+ end
167
+
168
+ it 'should be configured to send HTTP request to remote "default"' do
169
+ remote_factory_girl = RemoteFactoryGirl
170
+
171
+ expect(remote_factory_girl).to receive(:factories) do
172
+ expect(
173
+ remote_factory_girl.remotes_config.current_remote
174
+ ).to eq(:default)
175
+ expect(
176
+ remote_factory_girl.config.home_url
177
+ ).to eq('http://not_configured_with_name:9000/remote_factory_girl/home')
178
+ end
179
+ remote_factory_girl.factories
180
+ end
79
181
  end
80
182
  end
81
183
 
82
184
  describe '.create' do
185
+ context 'when configured with multiple remotes' do
186
+ before do
187
+ configure_remote_factory_girl(remote_name: :travis,
188
+ host: 'localhost',
189
+ port: 3000,
190
+ end_point: '/remote_factory_girl/travis/home',
191
+ return_with_root: false)
192
+ configure_remote_factory_girl(remote_name: :casey,
193
+ host: 'over_the_rainbow',
194
+ port: 6000,
195
+ end_point: '/remote_factory_girl/casey/home',
196
+ return_response_as: :dot_notation,
197
+ return_with_root: false)
198
+
199
+ end
200
+
201
+ it 'should be able to create a factory with "travis" remote' do
202
+ user = RemoteFactoryGirl.with_remote(:travis).create(:user)
203
+ expect(user['first_name']).to eq('Sam')
204
+ end
205
+
206
+ it 'should be able to create a factory with "casey" remote' do
207
+ user = RemoteFactoryGirl.with_remote(:casey).create(:user)
208
+ expect(user.first_name).to eq('Sam')
209
+ end
210
+ end
83
211
 
84
212
  describe 'default .home' do
85
213
 
@@ -109,11 +237,10 @@ describe RemoteFactoryGirl do
109
237
  end
110
238
 
111
239
  it 'should not return root hash key and should return an object that responds to dot notation' do
112
- RemoteFactoryGirl.configure do |config|
113
- config.home = { :host => 'localhost' }
114
- config.return_response_as = :dot_notation
115
- config.return_with_root = false
116
- end
240
+ configure_remote_factory_girl(host: 'localhost',
241
+ port: 3000,
242
+ return_response_as: :dot_notation,
243
+ return_with_root: false)
117
244
  user = RemoteFactoryGirl.create(:user)
118
245
  expect(user.first_name).to eq('Sam')
119
246
  end
@@ -1,36 +1,146 @@
1
1
  require 'remote_factory_girl'
2
+ require 'spec_helper'
2
3
 
3
4
  describe RemoteFactoryGirl do
5
+ describe '#create' do
4
6
 
5
- let(:config) { double('config', :to_hash => {}) }
7
+ let(:http) { double('RemoteFactoryGirl::Http') }
8
+ let(:factory_attributes) { { :first_name => 'Sam' } }
6
9
 
7
- describe '#create' do
8
- it 'should send a post request' do
9
- http = double('RemoteFactoryGirl::Http')
10
- attributes = { :first_name => 'Sam' }
11
- expect(http).to receive(:post).with(config, {:factory => 'user', :attributes => { :first_name => 'Sam'}})
12
- RemoteFactoryGirl::RemoteFactoryGirl.new(config).create('user', attributes, http)
10
+ context 'when remote configuration does not specify a remote name' do
11
+
12
+ before do
13
+ configure_remote_factory_girl(host: 'localhost',
14
+ port: 3000)
15
+ end
16
+
17
+ it "should send a post request to remote" do
18
+ config = RemoteFactoryGirl.config
19
+ expect(http).to receive(:post).with(config, {:factory => :user, :attributes => factory_attributes})
20
+ RemoteFactoryGirl::RemoteFactoryGirl.new(config).create(:user, factory_attributes, http)
21
+ end
22
+ end
23
+
24
+ context 'when multiple remotes are configured' do
25
+ before do
26
+ configure_remote_factory_girl(remote_name: :travis,
27
+ host: 'localhost',
28
+ port: 3000,
29
+ end_point: '/remote_factory_girl/travis/home')
30
+ configure_remote_factory_girl(remote_name: :casey,
31
+ host: 'over_the_rainbow',
32
+ port: 6000,
33
+ end_point: '/remote_factory_girl/casey/home')
34
+ end
35
+
36
+ it "should be configured to send HTTP requests to 'travis' remote" do
37
+ remote_config_travis = RemoteFactoryGirl.config(:travis)
38
+ remote_factory_girl = RemoteFactoryGirl::RemoteFactoryGirl.new(remote_config_travis)
39
+
40
+ expect(remote_factory_girl).to receive(:create).with(:user, factory_attributes, http) do |_, _, _|
41
+ expect(
42
+ remote_factory_girl.config.home_url
43
+ ).to eq('http://localhost:3000/remote_factory_girl/travis/home')
44
+ end
45
+
46
+ remote_factory_girl.create(:user, factory_attributes, http)
47
+ end
48
+
49
+ it "should be configured to send HTTP requests to 'casey' remote" do
50
+ remote_config_casey = RemoteFactoryGirl.config(:casey)
51
+ remote_factory_girl = RemoteFactoryGirl::RemoteFactoryGirl.new(remote_config_casey)
52
+
53
+ expect(remote_factory_girl).to receive(:create).with(:user, factory_attributes, http) do |_, _, _|
54
+ expect(
55
+ remote_factory_girl.config.home_url
56
+ ).to eq('http://over_the_rainbow:6000/remote_factory_girl/casey/home')
57
+ end
58
+
59
+ remote_factory_girl.create(:user, factory_attributes, http)
60
+ end
13
61
  end
14
62
  end
15
63
 
16
64
  xit 'should be able to configure with a block' do
65
+ # TODO: Remove
17
66
  pending
18
67
  end
19
68
 
20
- describe '.config' do
21
- it 'should be able to set and get config' do
22
- config = double('config')
23
- RemoteFactoryGirl.config = config
24
- expect(RemoteFactoryGirl.config).to equal(config)
69
+ describe '.reset' do
70
+ context 'when multiple remotes are configured' do
71
+
72
+ before do
73
+ configure_remote_factory_girl(remote_name: :travis,
74
+ host: 'localhost',
75
+ port: 3000,
76
+ end_point: '/remote_factory_girl/travis/home')
77
+ configure_remote_factory_girl(remote_name: :casey,
78
+ host: 'over_the_rainbow',
79
+ port: 6000,
80
+ end_point: '/remote_factory_girl/casey/home')
81
+
82
+ end
83
+
84
+ it 'should be able to reset the configuration' do
85
+ RemoteFactoryGirl.remotes_config.reset
86
+ expect(RemoteFactoryGirl.remotes_config.to_hash.keys).to eq([:default])
87
+ end
88
+ end
89
+
90
+ context 'when only one remote configuration and does not specify a remote name' do
91
+ before do
92
+ configure_remote_factory_girl(host: 'not_configured_with_name',
93
+ port: 9000)
94
+ end
95
+
96
+ it 'should be able to reset the configuration' do
97
+ RemoteFactoryGirl.remotes_config.reset
98
+ expect(RemoteFactoryGirl.remotes_config.to_hash.keys).to eq([:default])
99
+ end
25
100
  end
26
101
  end
27
102
 
28
- describe '.reset' do
29
- it 'should be able to reset the configuration' do
30
- config = double('config')
31
- RemoteFactoryGirl.config = config
32
- RemoteFactoryGirl.reset(double('config'))
33
- expect(RemoteFactoryGirl.config).to_not equal(config)
103
+ describe '.factories' do
104
+ context 'when multiple remotes are configured' do
105
+
106
+ let(:http) { double('RemoteFactoryGirl::Http') }
107
+
108
+ before do
109
+ configure_remote_factory_girl(remote_name: :travis,
110
+ host: 'localhost',
111
+ port: 3000,
112
+ end_point: '/remote_factory_girl/travis/home')
113
+ configure_remote_factory_girl(remote_name: :casey,
114
+ host: 'over_the_rainbow',
115
+ port: 6000,
116
+ end_point: '/remote_factory_girl/casey/home')
117
+ end
118
+
119
+ it "should be configured to send HTTP requests to 'travis' remote" do
120
+ remote_config_travis = RemoteFactoryGirl.config(:travis)
121
+ remote_factory_girl = RemoteFactoryGirl::RemoteFactoryGirl.new(remote_config_travis)
122
+
123
+ expect(remote_factory_girl).to receive(:factories).with({}, http) do |_, _|
124
+ expect(
125
+ remote_factory_girl.config.home_url
126
+ ).to eq('http://localhost:3000/remote_factory_girl/travis/home')
127
+ end
128
+
129
+ remote_factory_girl.factories({}, http)
130
+ end
131
+
132
+ it "should be configured to send HTTP requests to 'casey' remote" do
133
+ remote_config_casey = RemoteFactoryGirl.config(:casey)
134
+ remote_factory_girl = RemoteFactoryGirl::RemoteFactoryGirl.new(remote_config_casey)
135
+
136
+ expect(remote_factory_girl).to receive(:factories).with({}, http) do |_, _|
137
+ expect(
138
+ remote_factory_girl.config.home_url
139
+ ).to eq('http://over_the_rainbow:6000/remote_factory_girl/casey/home')
140
+ end
141
+
142
+ remote_factory_girl.factories({}, http)
143
+ end
34
144
  end
35
145
  end
36
146
 
@@ -0,0 +1,42 @@
1
+ require 'remote_factory_girl/remotes_config.rb'
2
+
3
+ describe RemoteFactoryGirl::Config do
4
+
5
+ let(:remotes_config) { RemoteFactoryGirl::RemotesConfig.new }
6
+ let(:home_1_config) { double('config') }
7
+ let(:home_2_config) { double('config') }
8
+ let(:default_remote_name) { RemoteFactoryGirl::RemotesConfig::DEFAULT_REMOTE_NAME }
9
+
10
+ describe '#initialize' do
11
+ context 'default configuration' do
12
+ it 'should be configured with correct defaults' do
13
+ expect(remotes_config.remotes).to eq({})
14
+ end
15
+ end
16
+ end
17
+
18
+ context 'adding a remote' do
19
+ before do
20
+ remotes_config.remotes[:home_1] = home_1_config
21
+ end
22
+
23
+ it 'should return configuration for home_1' do
24
+ expect(remotes_config.remotes[:home_1]).to eq(home_1_config)
25
+ end
26
+ end
27
+
28
+ describe '#reset' do
29
+
30
+ let(:reset_config) { double('config') }
31
+
32
+ before do
33
+ remotes_config.remotes[:home_1] = home_1_config
34
+ remotes_config.remotes[:home_2] = home_2_config
35
+ remotes_config.reset(reset_config)
36
+ end
37
+
38
+ it 'should return an empty hash' do
39
+ expect(remotes_config.to_hash).to eq({:default => reset_config })
40
+ end
41
+ end
42
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,3 +6,35 @@ require 'remote_factory_girl'
6
6
  RSpec.configure do |config|
7
7
 
8
8
  end
9
+
10
+ def configure_remote_factory_girl(remote_name: nil,
11
+ host: nil,
12
+ port: nil,
13
+ end_point: '/remote_factory_girl/home',
14
+ return_response_as: :as_hash,
15
+ return_with_root: true,
16
+ return_as_active_resource: false,
17
+ https: false)
18
+ if remote_name.nil?
19
+ RemoteFactoryGirl.configure do |config|
20
+ config.home = {:host => host,
21
+ :port => port,
22
+ :end_point => end_point }
23
+ config.return_response_as = return_response_as
24
+ config.return_with_root = return_with_root
25
+ config.return_as_active_resource = return_as_active_resource
26
+ config.https = https
27
+ end
28
+
29
+ else
30
+ RemoteFactoryGirl.configure(remote_name) do |config|
31
+ config.home = {:host => host,
32
+ :port => port,
33
+ :end_point => end_point }
34
+ config.return_response_as = return_response_as
35
+ config.return_with_root = return_with_root
36
+ config.return_as_active_resource = return_as_active_resource
37
+ config.https = https
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote_factory_girl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tdouce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-19 00:00:00.000000000 Z
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - lib/remote_factory_girl/hash_to_dot.rb
89
89
  - lib/remote_factory_girl/http.rb
90
90
  - lib/remote_factory_girl/json_to_active_resource.rb
91
+ - lib/remote_factory_girl/remotes_config.rb
91
92
  - lib/remote_factory_girl/version.rb
92
93
  - remote_factory_girl.gemspec
93
94
  - spec/integration/remote_factory_girl_spec.rb
@@ -97,6 +98,7 @@ files:
97
98
  - spec/models/remote_factory_girl/http_spec.rb
98
99
  - spec/models/remote_factory_girl/json_to_active_resource_spec.rb
99
100
  - spec/models/remote_factory_girl_spec.rb
101
+ - spec/models/remotes_config_spec.rb
100
102
  - spec/spec_helper.rb
101
103
  - wiki.md
102
104
  homepage: https://github.com/tdouce/remote_factory_girl
@@ -131,4 +133,5 @@ test_files:
131
133
  - spec/models/remote_factory_girl/http_spec.rb
132
134
  - spec/models/remote_factory_girl/json_to_active_resource_spec.rb
133
135
  - spec/models/remote_factory_girl_spec.rb
136
+ - spec/models/remotes_config_spec.rb
134
137
  - spec/spec_helper.rb