initial-test-data 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33493c61601d245c3dfe070e612a3ef7ba272c12
4
+ data.tar.gz: 6cfd76859a9a8a07473d39b7c0b10ff4eb26a0ee
5
+ SHA512:
6
+ metadata.gz: 097420351592a5a1cce9ae6bf44f493c427d098cfe04dc0f9858d801cafa7a858d19ef54d3a4f9fb5f0d5a59fda9f0ee141272b5b0bca2777576f2d0f83bf329
7
+ data.tar.gz: 53071d19f084cd3638c7ca4b89e26e386ffc2dc4beeb6781aefabfc004aaf3413949aeea96aba08620873df4f7b6b274dd109ab2643bdfa6130567f030d2ce45
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Tsutomu Kuroda
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ initial-test-data
2
+ =================
3
+
4
+ The `initial-test-data` is a tool to create a text fixture for Rails applications.
5
+
6
+ Overview
7
+ --------
8
+
9
+ Although the Rails itself has a standard mechanism
10
+ ([Fixtures](http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html)),
11
+ it is rather cumbersome to construct a real (often complicated) data structure
12
+ from YAML files.
13
+
14
+ The `initial-test-data` provides a a way to create a test fixture using Active Record, Factory Girl, etc.
15
+
16
+ Installation
17
+ ------------
18
+
19
+ Add the following line to `Gemfile`:
20
+
21
+ ```ruby
22
+ gem 'initial-test-data', group: :test
23
+ ```
24
+
25
+ Run `bin/bundle install` on the terminal.
26
+
27
+ Configuration of test framework
28
+ -------------------------------
29
+
30
+ ### MiniTest
31
+
32
+ To be written.
33
+
34
+ ### RSpec
35
+
36
+ Edit `spec/rails_helper.rb` like this:
37
+
38
+ ```ruby
39
+ ENV["RAILS_ENV"] ||= 'test'
40
+ require 'spec_helper'
41
+ require File.expand_path("../../config/environment", __FILE__)
42
+ require 'rspec/rails'
43
+ require 'initial-test-data'
44
+
45
+ RSpec.configure do |config|
46
+ config.before(:suite) do
47
+ InitialTestData.load('spec')
48
+ end
49
+ end
50
+ ```
51
+
52
+ Usage
53
+ -----
54
+
55
+ ### Synopsis
56
+
57
+ * Create `initial_data` subdirectory in the `test` or `spec` directory.
58
+ * Put any ruby scripts on this file.
59
+ * Run your test suite.
60
+
61
+ ### Caching data
62
+
63
+ The `initial-test-data` generates the md5 digest of all ruby scripts in
64
+ the `initial_data` directory and stores it to the `_initial_data_digest`
65
+ table of the test database.
66
+
67
+ If the generated md5 digest is equal to the previously stored value,
68
+ data initializing process will be skipped.
69
+
70
+ ### Load Order
71
+
72
+ The files in the `initial_data` directory are loaded in alphabetical order.
73
+ If you control the load order, you should make a YAML file called `_index.yml`,
74
+ which has a content like this:
75
+
76
+ ```yaml
77
+ - products
78
+ - customers
79
+ - orders
80
+ ```
81
+
82
+ Example
83
+ -------
84
+
85
+ ### MiniTest and Active Record
86
+
87
+ To be written
88
+
89
+ ### RSpec, Capybara and Factory Girl
90
+
91
+ ```ruby
92
+ # spec/initial_data/customers.rb
93
+
94
+ include FactoryGirl::Syntax::Methods
95
+
96
+ 0.upto(9) do |n|
97
+ create(:customer, email: "test#{n}@example.com")
98
+ end
99
+
100
+ # spec/features/manage_customers_spec.rb
101
+
102
+ require 'rails_helper'
103
+
104
+ feature 'Manage customers' do
105
+ let(:customer) { Customer.find_by(email: 'test0@example.com') }
106
+
107
+ scenario 'Change the name of a customer' do
108
+ visit root_path
109
+
110
+ find('table.customers td', text: customer.email)
111
+ .find(:xpath, '..').click_link('Edit')
112
+
113
+ fill_in 'Given Name', with: 'John'
114
+ fill_in 'Family Name', with: 'Doe'
115
+ click_button 'Update'
116
+
117
+ customer.reload
118
+ expect(customer.given_name).to eq('John')
119
+ expect(customer.family_name).to eq('Doe')
120
+ end
121
+ end
122
+ ```
@@ -0,0 +1 @@
1
+ require 'initial-test-data/initial_test_data'
@@ -0,0 +1,73 @@
1
+ require 'digest/md5'
2
+ require 'database_cleaner'
3
+
4
+ class InitialTestData
5
+ DIGEST_TABLE_NAME = '_initial_data_digest'
6
+
7
+ class << self
8
+ def load(dir = 'test')
9
+ @dir = dir.to_s
10
+
11
+ klass = define_class
12
+
13
+ digest_path = Rails.root.join('tmp', 'initial_data.md5')
14
+
15
+ md5_digest = generate_md5_digest
16
+ md5_digest_cache = klass.first.try(:md5_value)
17
+
18
+ unless md5_digest == md5_digest_cache
19
+ initialize_data
20
+ digest ||= klass.new
21
+ digest.md5_value = md5_digest
22
+ digest.save
23
+ end
24
+ end
25
+
26
+ private
27
+ def define_class
28
+ conn = ActiveRecord::Base.connection
29
+
30
+ unless conn.table_exists?(DIGEST_TABLE_NAME)
31
+ conn.create_table(DIGEST_TABLE_NAME) do |t|
32
+ t.string :md5_value
33
+ end
34
+ end
35
+
36
+ Class.new(ActiveRecord::Base) do
37
+ self.table_name = DIGEST_TABLE_NAME
38
+ end
39
+ end
40
+
41
+ def generate_md5_digest
42
+ md5 = Digest::MD5.new
43
+ Dir[Rails.root.join(@dir, 'initial_data', '*.rb')].each do |f|
44
+ md5.update File.new(f).read
45
+ end
46
+
47
+ md5.hexdigest
48
+ end
49
+
50
+ def initialize_data
51
+ DatabaseCleaner.strategy = :truncation
52
+ DatabaseCleaner.clean
53
+
54
+ yaml_path = Rails.root.join(@dir, 'initial_data', '_index.yml')
55
+ if File.exist?(yaml_path)
56
+ table_names = YAML.load_file(yaml_path)
57
+ table_names.each do |table_name|
58
+ path = Rails.root.join(@dir, 'initial_data', "#{table_name}.rb")
59
+ if File.exist?(path)
60
+ puts "Creating #{table_name}...."
61
+ require path
62
+ end
63
+ end
64
+ else
65
+ Dir[Rails.root.join(@dir, 'initial_data', '*.rb')].each do |f|
66
+ table_name = f.match(/(\w+)\.rb$/)[1]
67
+ puts "Creating #{table_name}...."
68
+ require f
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: initial-test-data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tsutomu KURODA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: database_cleaner
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ description: initial-test-data provides a way to create a test fixture using Active
28
+ Record, Factory Girl, etc.
29
+ email: t-kuroda@oiax.jp
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - lib/initial-test-data.rb
37
+ - lib/initial-test-data/initial_test_data.rb
38
+ homepage: https://github.com/oiax/initial-test-data
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '2.0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.5.1
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Test fixture creation tool
62
+ test_files: []