data_seeder 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -98
  3. data/app/models/data_seeder/seed_file.rb +54 -14
  4. data/lib/data_seeder.rb +30 -10
  5. data/lib/data_seeder/config.rb +39 -17
  6. data/lib/data_seeder/loader.rb +80 -85
  7. data/lib/data_seeder/loader/csv.rb +2 -1
  8. data/lib/data_seeder/loader/txt.rb +1 -6
  9. data/lib/data_seeder/loader/yaml.rb +1 -1
  10. data/lib/data_seeder/version.rb +1 -1
  11. data/test/dummy/app/models/app_error_data_seeder.rb +10 -8
  12. data/test/dummy/app/models/country.rb +0 -12
  13. data/test/dummy/db/development.sqlite3 +0 -0
  14. data/test/dummy/db/seed.test/{bar.err → bar_err/bar.err} +0 -0
  15. data/test/dummy/db/seed.test/countries_csv/countries.cfg +9 -0
  16. data/test/dummy/db/seed.test/countries_csv/countries.csv +249 -0
  17. data/test/dummy/db/seed.test/countries_txt/countries.cfg +6 -0
  18. data/test/dummy/db/seed.test/{countries.txt → countries_txt/countries.txt} +0 -0
  19. data/test/dummy/db/seed.test/{foo.err → foo_err/foo.err} +0 -0
  20. data/test/dummy/db/seed.test/{states.csv → states_csv/states.csv} +0 -0
  21. data/test/dummy/db/seed.test/states_json/states.cfg +1 -0
  22. data/test/dummy/db/seed.test/{states.json → states_json/states.json} +0 -1
  23. data/test/dummy/db/seed.test/states_txt/states.cfg +6 -0
  24. data/test/dummy/db/seed.test/{states.txt → states_txt/states.txt} +0 -1
  25. data/test/dummy/db/seed.test/states_yml/states.cfg +1 -0
  26. data/test/dummy/db/seed.test/{states.yml → states_yml/states.yml} +0 -1
  27. data/test/dummy/db/seed.test/{zulu.err → zulu_err/zulu.err} +0 -0
  28. data/test/dummy/db/test.sqlite3 +0 -0
  29. data/test/dummy/log/development.log +126 -0
  30. data/test/dummy/log/test.log +90945 -0
  31. data/test/models/data_seeder_test.rb +50 -30
  32. metadata +33 -20
  33. data/lib/data_seeder/logger.rb +0 -15
@@ -3,8 +3,8 @@ require 'test_helper'
3
3
  describe DataSeeder, :model do
4
4
  describe 'when run with txt files' do
5
5
  before do
6
- @name = 'test.txt'
7
- @seed_dir = setup_seed_dir(@name, 'countries.txt', 'states.txt')
6
+ @name = 'test.txt'
7
+ @seed_dirs = setup_seed_dirs(@name, 'countries_txt', 'states_txt')
8
8
  end
9
9
 
10
10
  after do
@@ -12,18 +12,18 @@ describe DataSeeder, :model do
12
12
  end
13
13
 
14
14
  it 'should load seed files' do
15
- modify_seed_file(@name, 'states.txt') do |body|
15
+ modify_seed_file(@name, 'states_txt/states.txt') do |body|
16
16
  body.sub('KY Kentucky', 'KV Kentucky').sub('VT Vermont', 'VT Vermount')
17
17
  end
18
- DataSeeder.run(seed_dir: @seed_dir)
18
+ DataSeeder.run(seed_dirs: @seed_dirs)
19
19
  assert_equal 50, State.count
20
20
  assert_equal 'United States', Country.find_by(code: 'US').try(:name)
21
21
  assert_equal 'Kentucky', State.find_by(code: 'KV').try(:name)
22
22
  assert_equal 'Vermount', State.find_by(code: 'VT').try(:name)
23
- modify_seed_file(@name, 'states.txt') do |body|
23
+ modify_seed_file(@name, 'states_txt/states.txt') do |body|
24
24
  body.sub('KV Kentucky', 'KY Kentucky').sub('VT Vermount', 'VT Vermont')
25
25
  end
26
- DataSeeder.run(seed_dir: @seed_dir)
26
+ DataSeeder.run
27
27
  assert_equal 50, State.count
28
28
  assert_equal 'Kentucky', State.find_by(code: 'KY').try(:name)
29
29
  assert_equal 'Vermont', State.find_by(code: 'VT').try(:name)
@@ -34,9 +34,10 @@ describe DataSeeder, :model do
34
34
  %w(csv json yml).each do |loader_type|
35
35
  describe "when run with #{loader_type} files" do
36
36
  before do
37
- @name = "test.#{loader_type}"
38
- @file = "states.#{loader_type}"
39
- @seed_dir = setup_seed_dir(@name, @file)
37
+ @name = "test.#{loader_type}"
38
+ @dir = "states_#{loader_type}"
39
+ @file = "#{@dir}/states.#{loader_type}"
40
+ @seed_dirs = setup_seed_dirs(@name, @dir)
40
41
  end
41
42
 
42
43
  after do
@@ -47,13 +48,13 @@ describe DataSeeder, :model do
47
48
  modify_seed_file(@name, @file) do |body|
48
49
  body.sub('Alaska', 'Alaskaska')
49
50
  end
50
- DataSeeder.run(seed_dir: @seed_dir)
51
+ DataSeeder.run(seed_dirs: @seed_dirs)
51
52
  assert_equal 50, State.count
52
53
  assert_equal 'Alaskaska', State.find_by(code: 'AK').try(:name)
53
54
  modify_seed_file(@name, @file) do |body|
54
55
  body.sub('Alaskaska', 'Alaska')
55
56
  end
56
- DataSeeder.run(seed_dir: @seed_dir)
57
+ DataSeeder.run(seed_dirs: @seed_dirs)
57
58
  assert_equal 50, State.count
58
59
  assert_equal 'Alaska', State.find_by(code: 'AK').try(:name)
59
60
  end
@@ -62,9 +63,9 @@ describe DataSeeder, :model do
62
63
 
63
64
  describe 'when run with use_line_number_as_id' do
64
65
  before do
65
- @name = "test.use_line_number_as_id"
66
- @file = "states.csv"
67
- @seed_dir = setup_seed_dir(@name, @file)
66
+ @name = "test.use_line_number_as_id"
67
+ @dir = "states_csv"
68
+ @seed_dirs = setup_seed_dirs(@name, @dir)
68
69
  end
69
70
 
70
71
  after do
@@ -72,21 +73,39 @@ describe DataSeeder, :model do
72
73
  end
73
74
 
74
75
  it 'should use the line number as the id' do
75
- modify_seed_file(@name, 'states.csv') do |body|
76
+ modify_seed_file(@name, 'states_csv/states.csv') do |body|
76
77
  # Remove the id column
77
78
  body.gsub(/^.*?,/,'')
78
79
  end
79
- File.open(seed_file_name(@name, 'states.cfg'), 'w') {|f| f.write '{ use_line_number_as_id: true }'}
80
- DataSeeder.run(seed_dir: @seed_dir)
80
+ File.open(seed_file_name(@name, 'states_csv/states.cfg'), 'w') {|f| f.write '{ use_line_number_as_id: true }'}
81
+ DataSeeder.run(seed_dirs: @seed_dirs)
81
82
  assert_equal 'AK', State.find(1).code
82
83
  assert_equal 'WY', State.find(50).code
83
84
  end
84
85
  end
85
86
 
87
+ describe 'when run with postprocess config' do
88
+ before do
89
+ @name = "test.postprocess"
90
+ @dir = "countries_csv"
91
+ @seed_dirs = setup_seed_dirs(@name, @dir)
92
+ end
93
+
94
+ after do
95
+ cleanup_seed_dir(@name)
96
+ end
97
+
98
+ it 'should allow the postprocess config to modify attributes' do
99
+ DataSeeder.run(seed_dirs: @seed_dirs)
100
+ assert_equal 'United States', Country.find_by(code: 'US').try(:name)
101
+ end
102
+ end
103
+
104
+
86
105
  describe 'when run with a custom loader' do
87
106
  before do
88
- @name = 'test.custom'
89
- @seed_dir = setup_seed_dir(@name, 'states.txt', 'foo.err', 'bar.err')
107
+ @name = 'test.custom'
108
+ @seed_dirs = setup_seed_dirs(@name, 'states_txt', 'foo_err', 'bar_err')
90
109
  end
91
110
 
92
111
  after do
@@ -94,10 +113,10 @@ describe DataSeeder, :model do
94
113
  end
95
114
 
96
115
  it 'should load seed files' do
97
- modify_seed_file(@name, 'states.txt') do |body|
116
+ modify_seed_file(@name, 'states_txt/states.txt') do |body|
98
117
  body.sub('KY Kentucky', 'KV Kentucky').sub('VT Vermont', 'VT Vermount')
99
118
  end
100
- DataSeeder.run(seed_dir: @seed_dir, loaders: {'err' => AppErrorDataSeeder.new})
119
+ DataSeeder.run(seed_dirs: @seed_dirs, loaders: {'err' => AppErrorDataSeeder})
101
120
  assert_equal 50, State.count
102
121
  assert_equal 'Kentucky', State.find_by(code: 'KV').try(:name)
103
122
  assert_equal 'Vermount', State.find_by(code: 'VT').try(:name)
@@ -108,13 +127,13 @@ describe DataSeeder, :model do
108
127
  assert 3, bar.app_errors.count
109
128
  assert_equal 'Error message for B1', bar.app_errors.find_by(code: 'B1').try(:message)
110
129
 
111
- modify_seed_file(@name, 'states.txt') do |body|
130
+ modify_seed_file(@name, 'states_txt/states.txt') do |body|
112
131
  body.sub('KV Kentucky', 'KY Kentucky').sub('VT Vermount', 'VT Vermont')
113
132
  end
114
- modify_seed_file(@name, 'bar.err') do |body|
133
+ modify_seed_file(@name, 'bar_err/bar.err') do |body|
115
134
  body.sub('B1 Error message for B1', 'C1 Error message for C1')
116
135
  end
117
- DataSeeder.run(seed_dir: @seed_dir, loaders: {'err' => AppErrorDataSeeder.new})
136
+ DataSeeder.run(seed_dirs: @seed_dirs, loaders: {'err' => AppErrorDataSeeder})
118
137
  assert_equal 50, State.count
119
138
  assert_equal 'Kentucky', State.find_by(code: 'KY').try(:name)
120
139
  assert_equal 'Vermont', State.find_by(code: 'VT').try(:name)
@@ -127,8 +146,9 @@ describe DataSeeder, :model do
127
146
  assert_nil bar.app_errors.find_by(code: 'B1')
128
147
  assert_equal 'Error message for C1', bar.app_errors.find_by(code: 'C1').try(:message)
129
148
 
130
- FileUtils.cp(Rails.root.join('db', 'seed.test', 'zulu.err'), @seed_dir)
131
- DataSeeder.run(seed_dir: @seed_dir, loaders: {'err' => AppErrorDataSeeder.new})
149
+ FileUtils.cp_r(Rails.root.join('db', 'seed.test', 'zulu_err'), seed_dir_name(@name))
150
+ DataSeeder.config.add_seed_dir(seed_file_name(@name, 'zulu_err'))
151
+ DataSeeder.run
132
152
  assert_equal 50, State.count
133
153
  assert_equal 3, App.count
134
154
  zulu = App.find_by(name: 'zulu')
@@ -139,13 +159,13 @@ describe DataSeeder, :model do
139
159
  end
140
160
 
141
161
 
142
- def setup_seed_dir(name, *files)
162
+ def setup_seed_dirs(name, *dirs)
143
163
  dir_name = seed_dir_name(name)
144
164
  FileUtils.mkdir_p(dir_name)
145
- files.each do |file|
146
- FileUtils.cp(Rails.root.join('db', 'seed.test', file), dir_name)
165
+ return dirs.map do |dir|
166
+ FileUtils.cp_r(Rails.root.join('db', 'seed.test', dir), dir_name)
167
+ "#{dir_name}/#{dir}"
147
168
  end
148
- return dir_name
149
169
  end
150
170
 
151
171
  def cleanup_seed_dir(name)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_seeder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Pardee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-27 00:00:00.000000000 Z
11
+ date: 2016-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -43,7 +43,6 @@ files:
43
43
  - lib/data_seeder/loader/json.rb
44
44
  - lib/data_seeder/loader/txt.rb
45
45
  - lib/data_seeder/loader/yaml.rb
46
- - lib/data_seeder/logger.rb
47
46
  - lib/data_seeder/version.rb
48
47
  - test/dummy/Rakefile
49
48
  - test/dummy/app/models/app.rb
@@ -63,19 +62,26 @@ files:
63
62
  - test/dummy/config/environments/test.rb
64
63
  - test/dummy/config/initializers/backtrace_silencers.rb
65
64
  - test/dummy/config/initializers/inflections.rb
65
+ - test/dummy/db/development.sqlite3
66
66
  - test/dummy/db/migrate/20150313022149_create_countries.rb
67
67
  - test/dummy/db/migrate/20150313022228_create_states.rb
68
68
  - test/dummy/db/migrate/20150313172634_create_apps.rb
69
69
  - test/dummy/db/migrate/20150313172719_create_app_errors.rb
70
70
  - test/dummy/db/schema.rb
71
- - test/dummy/db/seed.test/bar.err
72
- - test/dummy/db/seed.test/countries.txt
73
- - test/dummy/db/seed.test/foo.err
74
- - test/dummy/db/seed.test/states.csv
75
- - test/dummy/db/seed.test/states.json
76
- - test/dummy/db/seed.test/states.txt
77
- - test/dummy/db/seed.test/states.yml
78
- - test/dummy/db/seed.test/zulu.err
71
+ - test/dummy/db/seed.test/bar_err/bar.err
72
+ - test/dummy/db/seed.test/countries_csv/countries.cfg
73
+ - test/dummy/db/seed.test/countries_csv/countries.csv
74
+ - test/dummy/db/seed.test/countries_txt/countries.cfg
75
+ - test/dummy/db/seed.test/countries_txt/countries.txt
76
+ - test/dummy/db/seed.test/foo_err/foo.err
77
+ - test/dummy/db/seed.test/states_csv/states.csv
78
+ - test/dummy/db/seed.test/states_json/states.cfg
79
+ - test/dummy/db/seed.test/states_json/states.json
80
+ - test/dummy/db/seed.test/states_txt/states.cfg
81
+ - test/dummy/db/seed.test/states_txt/states.txt
82
+ - test/dummy/db/seed.test/states_yml/states.cfg
83
+ - test/dummy/db/seed.test/states_yml/states.yml
84
+ - test/dummy/db/seed.test/zulu_err/zulu.err
79
85
  - test/dummy/db/test.sqlite3
80
86
  - test/dummy/log/development.log
81
87
  - test/dummy/log/test.log
@@ -109,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
115
  version: '0'
110
116
  requirements: []
111
117
  rubyforge_project:
112
- rubygems_version: 2.4.5
118
+ rubygems_version: 2.5.1
113
119
  signing_key:
114
120
  specification_version: 4
115
121
  summary: Seed your Rails database
@@ -131,19 +137,26 @@ test_files:
131
137
  - test/dummy/config/initializers/backtrace_silencers.rb
132
138
  - test/dummy/config/initializers/inflections.rb
133
139
  - test/dummy/config.ru
140
+ - test/dummy/db/development.sqlite3
134
141
  - test/dummy/db/migrate/20150313022149_create_countries.rb
135
142
  - test/dummy/db/migrate/20150313022228_create_states.rb
136
143
  - test/dummy/db/migrate/20150313172634_create_apps.rb
137
144
  - test/dummy/db/migrate/20150313172719_create_app_errors.rb
138
145
  - test/dummy/db/schema.rb
139
- - test/dummy/db/seed.test/bar.err
140
- - test/dummy/db/seed.test/countries.txt
141
- - test/dummy/db/seed.test/foo.err
142
- - test/dummy/db/seed.test/states.csv
143
- - test/dummy/db/seed.test/states.json
144
- - test/dummy/db/seed.test/states.txt
145
- - test/dummy/db/seed.test/states.yml
146
- - test/dummy/db/seed.test/zulu.err
146
+ - test/dummy/db/seed.test/bar_err/bar.err
147
+ - test/dummy/db/seed.test/countries_csv/countries.cfg
148
+ - test/dummy/db/seed.test/countries_csv/countries.csv
149
+ - test/dummy/db/seed.test/countries_txt/countries.cfg
150
+ - test/dummy/db/seed.test/countries_txt/countries.txt
151
+ - test/dummy/db/seed.test/foo_err/foo.err
152
+ - test/dummy/db/seed.test/states_csv/states.csv
153
+ - test/dummy/db/seed.test/states_json/states.cfg
154
+ - test/dummy/db/seed.test/states_json/states.json
155
+ - test/dummy/db/seed.test/states_txt/states.cfg
156
+ - test/dummy/db/seed.test/states_txt/states.txt
157
+ - test/dummy/db/seed.test/states_yml/states.cfg
158
+ - test/dummy/db/seed.test/states_yml/states.yml
159
+ - test/dummy/db/seed.test/zulu_err/zulu.err
147
160
  - test/dummy/db/test.sqlite3
148
161
  - test/dummy/log/development.log
149
162
  - test/dummy/log/test.log
@@ -1,15 +0,0 @@
1
- module DataSeeder
2
- class Logger < ::Logger
3
- attr_accessor :verbose
4
-
5
- def initialize
6
- super($stdout)
7
- @verbose = true
8
- self.formatter = ->(severity, datetime, progname, msg) { "#{msg}\n" }
9
- end
10
-
11
- def info(arg='', &block)
12
- super if @verbose
13
- end
14
- end
15
- end