initial-test-data 0.4.4 → 0.5.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: 8e419710d34d05db8e20228b1c73755fe77e027e
4
- data.tar.gz: d5f51ac550b95a1febb1ea3d517850a416801cfd
3
+ metadata.gz: 3416a75583ed319c9dde2c5ece71417c6a3200c0
4
+ data.tar.gz: abff9bae1cda913ac6727b4d9be6b055a6410a7d
5
5
  SHA512:
6
- metadata.gz: 55453a847fb2169969607861fabbd89e6ec2de4faeb8a89ed28e7d72e217cd0c0c38519b3077914f6752422e80f7652bbde4c4db1f3b25c9a911d176df80934a
7
- data.tar.gz: 253a652c13cd309a1e5daa48f5d81415c37aba4d9676fc06801f8f6034f6f4f4d5719e8f1ad8ab0904929837c1a8ca6a66e8cb7f555a76c5b404d5e8a0c27055
6
+ metadata.gz: 8d4eff940bb934e09f8b51e434b0c8800b6c4600940aeb89798d9ac9c8b076f2d4d2598a3946129da14dca38e3d8add8ef9fdf142ec77554cdefd873fbbb03f5
7
+ data.tar.gz: 480ac72a70a852e252d98b7fd2dcb03641d0a7a56e2afddf52fd89715e733cb48cb36b1324fe511ccf81e229cfd821127e90ec0d61d397bd6acd50825715216d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG - initial-test-data
2
2
 
3
+ ## 0.5.0 (February 14, 2016)
4
+
5
+ * Change method name from `load` to `import`.
6
+ * Add test suite.
7
+
3
8
  ## 0.4.4 (February 2, 2016)
4
9
 
5
10
  * Truncate only non-empty tables (optimization).
data/README.md CHANGED
@@ -1,18 +1,24 @@
1
1
  initial-test-data
2
2
  =================
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/initial-test-data.svg)](https://badge.fury.io/rb/initial-test-data)
5
+
4
6
  The `initial-test-data` is a tool to create a text fixture for Rails applications.
5
7
 
6
8
  Overview
7
9
  --------
8
10
 
9
- Although the Rails itself has a standard mechanism
10
- ([Fixtures](http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html)),
11
+ Although the Rails itself has a standard mechanism to initialize the database
12
+ in the _test_ environment
13
+ ([ActiveRecord::FixtureSet](http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html)),
11
14
  it is rather cumbersome to construct a real (often complicated) data structure
12
15
  from YAML files.
13
16
 
14
17
  The `initial-test-data` provides a a way to create a test fixture using Active Record, Factory Girl, etc.
15
18
 
19
+ It also offers utility methods called `store` and `fetch`
20
+ to register and access the initialized data.
21
+
16
22
  Installation
17
23
  ------------
18
24
 
@@ -37,7 +43,7 @@ require File.expand_path('../../config/environment', __FILE__)
37
43
  require 'rails/test_help'
38
44
  require 'initial-test-data'
39
45
 
40
- InitialTestData.load
46
+ InitialTestData.import
41
47
 
42
48
  class ActiveSupport::TestCase
43
49
  include InitialTestData::Utilities
@@ -46,7 +52,7 @@ class ActiveSupport::TestCase
46
52
  end
47
53
  ```
48
54
 
49
- Note that the default value of the first argument of `load` method is `'test'`,
55
+ Note that the default value of the first argument of `import` method is `'test'`,
50
56
  so you can omit it when your test scripts are located in the `test` directory.
51
57
 
52
58
  ### RSpec
@@ -64,7 +70,7 @@ RSpec.configure do |config|
64
70
  config.include InitialTestData::Utilities
65
71
 
66
72
  config.before(:suite) do
67
- InitialTestData.load('spec')
73
+ InitialTestData.import('spec')
68
74
  end
69
75
  end
70
76
  ```
@@ -99,7 +105,7 @@ which has a content like this:
99
105
  - orders
100
106
  ```
101
107
 
102
- ### Options for the `InitialTestData.load` method
108
+ ### Options for the `InitialTestData.import` method
103
109
 
104
110
  #### `except`
105
111
 
@@ -111,7 +117,7 @@ If you want to keep some tables intact, specify the `except` option:
111
117
  ```ruby
112
118
  RSpec.configure do |config|
113
119
  config.before(:suite) do
114
- InitialTestData.load('spec', except: %w(country_names))
120
+ InitialTestData.import('spec', except: %w(country_names))
115
121
  end
116
122
  end
117
123
  ```
@@ -124,7 +130,7 @@ only the specified tables are initialized:
124
130
  ```ruby
125
131
  RSpec.configure do |config|
126
132
  config.before(:suite) do
127
- InitialTestData.load('spec', only: %w(customers products))
133
+ InitialTestData.import('spec', only: %w(customers products))
128
134
  end
129
135
  end
130
136
  ```
@@ -139,12 +145,12 @@ under the `test` directory (or the directory specified by the first argument)
139
145
  and the `app/models` directory.
140
146
 
141
147
  If you want to add monitoring target directories, specify `monitoring`
142
- option to the `InitialTestData.load` method:
148
+ option to the `InitialTestData.import` method:
143
149
 
144
150
  ```ruby
145
151
  RSpec.configure do |config|
146
152
  config.before(:suite) do
147
- InitialTestData.load('spec',
153
+ InitialTestData.import('spec',
148
154
  monitoring: [ 'app/services', 'lib', 'spec/factories' ]
149
155
  end
150
156
  end
@@ -152,6 +158,11 @@ end
152
158
 
153
159
  You should use relative paths from the `Rails.root`.
154
160
 
161
+ #### `quite`
162
+
163
+ Specify `true` to this option in order to suppress the message from the
164
+ `initial-test-data`.
165
+
155
166
  ### Utility methods: `store` and `fetch`
156
167
 
157
168
  The `initial-test-data` provides two utility methods, `store` and `fetch`,
@@ -163,8 +174,13 @@ in order to register an ActiveRecord object by name:
163
174
  ```ruby
164
175
  include InitialTestData::Utilities
165
176
 
177
+ # Using Active Record
166
178
  store(Customer.create(...), :john)
167
179
  store(ShopOwner.create(...), :mike)
180
+
181
+ # Using Factory Girl
182
+ store(create(:customer, ...), :john)
183
+ store(create(:shop_owner, ...), :john)
168
184
  ```
169
185
 
170
186
  Then, you can get this record with `fetch` method in the test scripts:
@@ -1,13 +1,15 @@
1
1
  require 'digest/md5'
2
2
  require 'database_cleaner'
3
- require 'active_support'
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'active_record'
5
+ require 'rails'
4
6
 
5
7
  module InitialTestData
6
8
  DIGEST_TABLE_NAME = '_initial_data_digest'
7
9
  RECORD_IDS = HashWithIndifferentAccess.new
8
10
 
9
11
  class << self
10
- def load(*args)
12
+ def import(*args)
11
13
  @options = args.extract_options!
12
14
  @dir = args[0] || 'test'
13
15
 
@@ -27,6 +29,7 @@ module InitialTestData
27
29
  end
28
30
 
29
31
  if needs_reinitialization
32
+ RECORD_IDS.clear
30
33
  initialize_data
31
34
 
32
35
  digest = klass.first
@@ -44,7 +47,7 @@ module InitialTestData
44
47
  def define_class
45
48
  conn = ActiveRecord::Base.connection
46
49
 
47
- unless conn.table_exists?(DIGEST_TABLE_NAME)
50
+ unless ditest_table_exists?
48
51
  conn.create_table(DIGEST_TABLE_NAME) do |t|
49
52
  t.string :md5_value
50
53
  end
@@ -55,6 +58,13 @@ module InitialTestData
55
58
  end
56
59
  end
57
60
 
61
+ def ditest_table_exists?
62
+ conn = ActiveRecord::Base.connection
63
+ ::Rails::VERSION::MAJOR >= 5 ?
64
+ conn.data_source_exists?(DIGEST_TABLE_NAME) :
65
+ conn.table_exists?(DIGEST_TABLE_NAME)
66
+ end
67
+
58
68
  def generate_md5_digest
59
69
  md5 = Digest::MD5.new
60
70
 
@@ -94,15 +104,15 @@ module InitialTestData
94
104
  table_names.each do |table_name|
95
105
  path = Rails.root.join(@dir, 'initial_data', "#{table_name}.rb")
96
106
  if File.exist?(path)
97
- puts "Creating #{table_name}...."
98
- require path
107
+ puts "Creating #{table_name}...." unless @options[:quiet]
108
+ load path
99
109
  end
100
110
  end
101
111
  else
102
- Dir[Rails.root.join(@dir, 'initial_data', '*.rb')].each do |f|
103
- table_name = f.match(/(\w+)\.rb$/)[1]
104
- puts "Creating #{table_name}...."
105
- require f
112
+ Dir[Rails.root.join(@dir, 'initial_data', '*.rb')].each do |path|
113
+ table_name = path.match(/(\w+)\.rb$/)[1]
114
+ puts "Creating #{table_name}...." unless @options[:quiet]
115
+ load path
106
116
  end
107
117
  end
108
118
  end
@@ -111,7 +121,10 @@ module InitialTestData
111
121
  tables = []
112
122
 
113
123
  conn = ActiveRecord::Base.connection
114
- conn.tables.each do |table|
124
+ data_sources = ::Rails::VERSION::MAJOR >= 5 ?
125
+ conn.data_sources : conn.tables
126
+
127
+ data_sources.each do |table|
115
128
  next if table.in?([ DIGEST_TABLE_NAME, 'schema_migrations' ])
116
129
  if conn.select_one("select * from #{table}")
117
130
  tables << table
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: initial-test-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.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-02-02 00:00:00.000000000 Z
11
+ date: 2016-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -50,6 +50,26 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '6.0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: railties
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '3.0'
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '6.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '6.0'
53
73
  - !ruby/object:Gem::Dependency
54
74
  name: database_cleaner
55
75
  requirement: !ruby/object:Gem::Requirement
@@ -64,6 +84,20 @@ dependencies:
64
84
  - - "~>"
65
85
  - !ruby/object:Gem::Version
66
86
  version: '1.5'
87
+ - !ruby/object:Gem::Dependency
88
+ name: sqlite3
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '1.3'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '1.3'
67
101
  description: initial-test-data provides a way to create a test fixture using Active
68
102
  Record, Factory Girl, etc.
69
103
  email: t-kuroda@oiax.jp