initial-test-data 0.3.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +95 -5
- data/lib/initial-test-data/initial_test_data.rb +26 -4
- data/lib/initial-test-data/utilities.rb +33 -0
- data/lib/initial-test-data.rb +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f858811a806c2fc042d1396bd6390e4c05e2e6dc
|
4
|
+
data.tar.gz: 900a063128080d53afbbf8238b6deb959816dc7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 586696349dc870ea98209f3a5a7da6eb4f48abe7910b3814e71738c28d510c51a0442b62e51c7c5c37f64869a6d2a2243c1c4a28ed6a5c9ceaa1b8c5f3e8060e
|
7
|
+
data.tar.gz: e247db303eb1b98f9e2d8adccf07534b6e4deddcfe5d4031a4e7187d4868f94d9b67ecfe8cb76ab70f44a9c49113655b2424ac19182308187d704a12be079506
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -29,7 +29,25 @@ Configuration of test framework
|
|
29
29
|
|
30
30
|
### MiniTest
|
31
31
|
|
32
|
-
|
32
|
+
Edit `test/test_helper.rb` like this:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
ENV['RAILS_ENV'] ||= 'test'
|
36
|
+
require File.expand_path('../../config/environment', __FILE__)
|
37
|
+
require 'rails/test_help'
|
38
|
+
require 'initial-test-data'
|
39
|
+
|
40
|
+
InitialTestData.load
|
41
|
+
|
42
|
+
class ActiveSupport::TestCase
|
43
|
+
include InitialTestData::Utilities
|
44
|
+
|
45
|
+
# (snip)
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Note that the default value of the first argument of `load` method is `'test'`,
|
50
|
+
so you can omit it when your test scripts are located in the `test` directory.
|
33
51
|
|
34
52
|
### RSpec
|
35
53
|
|
@@ -43,6 +61,8 @@ require 'rspec/rails'
|
|
43
61
|
require 'initial-test-data'
|
44
62
|
|
45
63
|
RSpec.configure do |config|
|
64
|
+
config.include InitialTestData::Utilities
|
65
|
+
|
46
66
|
config.before(:suite) do
|
47
67
|
InitialTestData.load('spec')
|
48
68
|
end
|
@@ -110,19 +130,87 @@ option to the `InitialTestData.load` method:
|
|
110
130
|
```ruby
|
111
131
|
RSpec.configure do |config|
|
112
132
|
config.before(:suite) do
|
113
|
-
InitialTestData.load('spec',
|
133
|
+
InitialTestData.load('spec',
|
134
|
+
monitoring: [ 'app/services', 'lib', 'spec/factories' ]
|
114
135
|
end
|
115
136
|
end
|
116
137
|
```
|
117
138
|
|
118
139
|
You should use relative paths from the `Rails.root`.
|
119
140
|
|
141
|
+
### Utility methods: `store` and `fetch`
|
142
|
+
|
143
|
+
The `initial-test-data` provides two utility methods, `store` and `fetch`,
|
144
|
+
to make it easy to refer the initialized records in your tests.
|
145
|
+
|
146
|
+
You can use the `store` method within the initialization scripts
|
147
|
+
in order to register an ActiveRecord object by name:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
include InitialTestData::Utilities
|
151
|
+
|
152
|
+
store(Customer.create(...), :john)
|
153
|
+
store(ShopOwner.create(...), :mike)
|
154
|
+
```
|
155
|
+
|
156
|
+
Then, you can get this record with `fetch` method in the test scripts:
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
customer = fetch(:customer, :john)
|
160
|
+
show_owner = fetch(:shop_owner, :mike)
|
161
|
+
```
|
162
|
+
|
163
|
+
The fetch method treats `:john` and `"john"` as the same key.
|
164
|
+
|
165
|
+
Note that the `initial-test-data` creates a YAML file
|
166
|
+
named `initial_data_record_ids.yml` in the `tmp` directory
|
167
|
+
to track the primary key values of registered records.
|
168
|
+
Please do not remove or tamper it.
|
169
|
+
|
120
170
|
Example
|
121
171
|
-------
|
122
172
|
|
123
173
|
### MiniTest and Active Record
|
124
174
|
|
125
|
-
|
175
|
+
```ruby
|
176
|
+
# test/initial_data/customers.rb
|
177
|
+
|
178
|
+
include InitialTestData::Utilities
|
179
|
+
|
180
|
+
0.upto(9) do |n|
|
181
|
+
c = Customer.create(
|
182
|
+
email: "test#{n}@example.com",
|
183
|
+
given_name: 'John',
|
184
|
+
family_name: 'Doe'
|
185
|
+
)
|
186
|
+
store(c, "test#{n}")
|
187
|
+
end
|
188
|
+
|
189
|
+
# test/integration/manage_customers_test.rb
|
190
|
+
|
191
|
+
require 'test_helper'
|
192
|
+
|
193
|
+
class ManageCustomersTest < ActionDispatch::IntegrationTest
|
194
|
+
test "Change the name of a customer" do
|
195
|
+
customer = fetch(:customer, :test0)
|
196
|
+
|
197
|
+
get "/customers/#{customer.id}/edit"
|
198
|
+
assert_response :success
|
199
|
+
|
200
|
+
patch "/customers/#{customer.id}",
|
201
|
+
customer: { given_name: 'Mike', family_name: 'Smith' }
|
202
|
+
assert_redirected_to [ assigns(:customer) ]
|
203
|
+
|
204
|
+
follow_redirect!
|
205
|
+
assert_select "h1", "Listing Customers"
|
206
|
+
|
207
|
+
customer.reload
|
208
|
+
|
209
|
+
assert_equal 'Mike', customer.given_name
|
210
|
+
assert_equal 'Smith', customer.family_name
|
211
|
+
end
|
212
|
+
end
|
213
|
+
```
|
126
214
|
|
127
215
|
### RSpec, Capybara and Factory Girl
|
128
216
|
|
@@ -130,9 +218,11 @@ To be written
|
|
130
218
|
# spec/initial_data/customers.rb
|
131
219
|
|
132
220
|
include FactoryGirl::Syntax::Methods
|
221
|
+
include InitialTestData::Utilities
|
133
222
|
|
134
223
|
0.upto(9) do |n|
|
135
|
-
create(:customer, email: "test#{n}@example.com")
|
224
|
+
c = create(:customer, email: "test#{n}@example.com")
|
225
|
+
store(c, "test#{n}")
|
136
226
|
end
|
137
227
|
|
138
228
|
# spec/features/manage_customers_spec.rb
|
@@ -140,7 +230,7 @@ end
|
|
140
230
|
require 'rails_helper'
|
141
231
|
|
142
232
|
feature 'Manage customers' do
|
143
|
-
let(:customer) {
|
233
|
+
let(:customer) { fetch(:customer, :test0) }
|
144
234
|
|
145
235
|
scenario 'Change the name of a customer' do
|
146
236
|
visit root_path
|
@@ -1,8 +1,10 @@
|
|
1
1
|
require 'digest/md5'
|
2
2
|
require 'database_cleaner'
|
3
|
+
require 'active_support'
|
3
4
|
|
4
|
-
|
5
|
+
module InitialTestData
|
5
6
|
DIGEST_TABLE_NAME = '_initial_data_digest'
|
7
|
+
RECORD_IDS = HashWithIndifferentAccess.new
|
6
8
|
|
7
9
|
class << self
|
8
10
|
def load(*args)
|
@@ -11,18 +13,30 @@ class InitialTestData
|
|
11
13
|
|
12
14
|
klass = define_class
|
13
15
|
|
14
|
-
digest_path = Rails.root.join('tmp', 'initial_data.md5')
|
15
|
-
|
16
16
|
md5_digest = generate_md5_digest
|
17
17
|
md5_digest_cache = klass.first.try(:md5_value)
|
18
18
|
|
19
|
-
|
19
|
+
needs_reinitialization = true
|
20
|
+
record_ids_path = Rails.root.join('tmp', 'initial_data_record_ids.yml')
|
21
|
+
if File.exists?(record_ids_path) && md5_digest == md5_digest_cache
|
22
|
+
begin
|
23
|
+
RECORD_IDS.merge! YAML.load_file(record_ids_path)
|
24
|
+
needs_reinitialization = false
|
25
|
+
rescue SyntaxError
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if needs_reinitialization
|
20
30
|
initialize_data
|
21
31
|
|
22
32
|
digest = klass.first
|
23
33
|
digest ||= klass.new
|
24
34
|
digest.md5_value = md5_digest
|
25
35
|
digest.save
|
36
|
+
|
37
|
+
File.open(record_ids_path, 'w') do |f|
|
38
|
+
f.write RECORD_IDS.to_yaml
|
39
|
+
end
|
26
40
|
end
|
27
41
|
end
|
28
42
|
|
@@ -92,5 +106,13 @@ class InitialTestData
|
|
92
106
|
end
|
93
107
|
end
|
94
108
|
end
|
109
|
+
|
110
|
+
|
111
|
+
def save_record_ids
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
def load_record_ids
|
116
|
+
end
|
95
117
|
end
|
96
118
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module InitialTestData
|
4
|
+
module Utilities
|
5
|
+
def store(record, name)
|
6
|
+
model_name = record.class.model_name.singular
|
7
|
+
record_name = name.to_s
|
8
|
+
|
9
|
+
RECORD_IDS[model_name] ||= HashWithIndifferentAccess.new
|
10
|
+
|
11
|
+
if RECORD_IDS[model_name].has_key?(record_name)
|
12
|
+
raise "The key '#{record_name}' already exists " +
|
13
|
+
"for #{record.class.model_name.name}."
|
14
|
+
elsif !record.kind_of?(ActiveRecord::Base)
|
15
|
+
raise "Given object is not an instance of ActiveRecord::Base."
|
16
|
+
elsif record.new_record?
|
17
|
+
raise "Given record is not persisted yet."
|
18
|
+
else
|
19
|
+
RECORD_IDS[model_name][record_name] = record.id
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def fetch(model_name, name)
|
24
|
+
if RECORD_IDS[model_name].kind_of?(Hash) &&
|
25
|
+
RECORD_IDS[model_name][name]
|
26
|
+
klass = model_name.to_s.camelize.constantize
|
27
|
+
klass.find RECORD_IDS[model_name][name]
|
28
|
+
else
|
29
|
+
raise "No record is registered with the key '#{model_name}' and '#{name}'."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/initial-test-data.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: initial-test-data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tsutomu KURODA
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01
|
11
|
+
date: 2016-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: database_cleaner
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -36,6 +50,7 @@ files:
|
|
36
50
|
- README.md
|
37
51
|
- lib/initial-test-data.rb
|
38
52
|
- lib/initial-test-data/initial_test_data.rb
|
53
|
+
- lib/initial-test-data/utilities.rb
|
39
54
|
homepage: https://github.com/oiax/initial-test-data
|
40
55
|
licenses:
|
41
56
|
- MIT
|