db_seed_dump 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+
3
+ describe SeedDump do
4
+ describe '.dump_using_environment' do
5
+ before(:all) do
6
+ create_db
7
+ end
8
+
9
+ before do
10
+ Rails.application.eager_load!
11
+
12
+ FactoryBot.create(:sample)
13
+ end
14
+
15
+ describe 'INSERT_ALL' do
16
+ it "specifies insert_all as true if the INSERT_ALL env var is 'true'" do
17
+ described_class.should_receive(:dump).with(anything, include(insert_all: true))
18
+
19
+ described_class.dump_using_environment('INSERT_ALL' => 'true')
20
+ end
21
+ end
22
+
23
+ describe 'DUMP_ALL' do
24
+ it 'sets empty array if dump all is true' do
25
+ described_class.should_receive(:dump).with(anything, include(exclude: []))
26
+
27
+ described_class.dump_using_environment('DUMP_ALL' => 'true')
28
+ end
29
+ end
30
+
31
+ describe 'APPEND' do
32
+ it "specifies append as true if the APPEND env var is 'true'" do
33
+ described_class.should_receive(:dump).with(anything, include(append: true))
34
+
35
+ described_class.dump_using_environment('APPEND' => 'true')
36
+ end
37
+
38
+ it "specifies append as true if the APPEND env var is 'TRUE'" do
39
+ described_class.should_receive(:dump).with(anything, include(append: true))
40
+
41
+ described_class.dump_using_environment('APPEND' => 'TRUE')
42
+ end
43
+
44
+ it "specifies append as false the first time if the APPEND env var is not 'true' (and true after that)" do
45
+ FactoryBot.create(:another_sample)
46
+
47
+ described_class.should_receive(:dump).with(anything, include(append: false)).ordered
48
+ described_class.should_receive(:dump).with(anything, include(append: true)).ordered
49
+
50
+ described_class.dump_using_environment('APPEND' => 'false')
51
+ end
52
+ end
53
+
54
+ describe 'BATCH_SIZE' do
55
+ it 'passes along the specified batch size' do
56
+ described_class.should_receive(:dump).with(anything, include(batch_size: 17))
57
+
58
+ described_class.dump_using_environment('BATCH_SIZE' => '17')
59
+ end
60
+
61
+ it 'passes along a nil batch size if BATCH_SIZE is not specified' do
62
+ described_class.should_receive(:dump).with(anything, include(batch_size: nil))
63
+
64
+ described_class.dump_using_environment
65
+ end
66
+ end
67
+
68
+ describe 'EXCLUDE' do
69
+ it 'passes along any attributes to be excluded' do
70
+ described_class.should_receive(:dump).with(anything, include(exclude: %i[baggins saggins]))
71
+
72
+ described_class.dump_using_environment('EXCLUDE' => 'baggins,saggins')
73
+ end
74
+ end
75
+
76
+ describe 'FILE' do
77
+ it 'passes the FILE parameter to the dump method correctly' do
78
+ described_class.should_receive(:dump).with(anything, include(file: 'blargle'))
79
+
80
+ described_class.dump_using_environment('FILE' => 'blargle')
81
+ end
82
+
83
+ it 'passes db/seeds.rb as the file parameter if no FILE is specified' do
84
+ described_class.should_receive(:dump).with(anything, include(file: 'db/seeds.rb'))
85
+
86
+ described_class.dump_using_environment
87
+ end
88
+ end
89
+
90
+ describe 'LIMIT' do
91
+ it 'applies the specified limit to the records' do
92
+ relation_double = double('ActiveRecord relation double')
93
+ Sample.should_receive(:limit).with(5).and_return(relation_double)
94
+
95
+ described_class.should_receive(:dump).with(relation_double, anything)
96
+ described_class.stub(:dump)
97
+
98
+ described_class.dump_using_environment('LIMIT' => '5')
99
+ end
100
+ end
101
+
102
+ ['', 'S'].each do |model_suffix|
103
+ model_env = "MODEL#{model_suffix}"
104
+
105
+ describe model_env do
106
+ context "if #{model_env} is not specified" do
107
+ it 'dumps all non-empty models' do
108
+ FactoryBot.create(:another_sample)
109
+
110
+ [Sample, AnotherSample].each do |model|
111
+ SeedDump.should_receive(:dump).with(model, anything)
112
+ end
113
+
114
+ SeedDump.dump_using_environment
115
+ end
116
+ end
117
+
118
+ context "if #{model_env} is specified" do
119
+ it 'dumps only the specified model' do
120
+ FactoryBot.create(:another_sample)
121
+
122
+ SeedDump.should_receive(:dump).with(Sample, anything)
123
+
124
+ SeedDump.dump_using_environment(model_env => 'Sample')
125
+ end
126
+
127
+ it 'does not dump empty models' do
128
+ SeedDump.should_not_receive(:dump).with(EmptyModel, anything)
129
+
130
+ SeedDump.dump_using_environment(model_env => 'EmptyModel, Sample')
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ describe 'MODELS_EXCLUDE' do
137
+ it 'dumps all non-empty models except the specified models' do
138
+ FactoryBot.create(:another_sample)
139
+
140
+ described_class.should_receive(:dump).with(Sample, anything)
141
+
142
+ described_class.dump_using_environment('MODELS_EXCLUDE' => 'AnotherSample')
143
+ end
144
+ end
145
+
146
+ it 'runs ok without ActiveRecord::SchemaMigration being set (needed for Rails Engines)' do
147
+ schema_migration = ActiveRecord::SchemaMigration
148
+
149
+ ActiveRecord.send(:remove_const, :SchemaMigration)
150
+
151
+ described_class.stub(:dump)
152
+
153
+ begin
154
+ described_class.dump_using_environment
155
+ ensure
156
+ ActiveRecord.const_set(:SchemaMigration, schema_migration)
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,14 @@
1
+ FactoryBot.define do
2
+ factory :another_sample do
3
+ string { 'string' }
4
+ text { 'text' }
5
+ integer { 42 }
6
+ float { 3.14 }
7
+ decimal { 2.72 }
8
+ datetime { DateTime.parse('July 4, 1776 7:14pm UTC') }
9
+ time { Time.parse('3:15am UTC') }
10
+ date { Date.parse('November 19, 1863') }
11
+ binary { 'binary' }
12
+ boolean { false }
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ FactoryBot.define do
2
+ factory :sample do
3
+ string { 'string' }
4
+ text { 'text' }
5
+ integer { 42 }
6
+ float { 3.14 }
7
+ decimal { 2.72 }
8
+ datetime { DateTime.parse('July 4, 1776 7:14pm UTC') }
9
+ time { Time.parse('3:15am UTC') }
10
+ date { Date.parse('November 19, 1863') }
11
+ binary { 'binary' }
12
+ boolean { false }
13
+ created_at { DateTime.parse('July 20, 1969 20:18 UTC') }
14
+ updated_at { DateTime.parse('November 10, 1989 4:20 UTC') }
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ FactoryBot.define do
2
+ factory :yet_another_sample do
3
+ string { 'string' }
4
+ text { 'text' }
5
+ integer { 42 }
6
+ float { 3.14 }
7
+ decimal { 2.72 }
8
+ datetime { DateTime.parse('July 4, 1776 7:14pm UTC') }
9
+ time { Time.parse('3:15am UTC') }
10
+ date { Date.parse('November 19, 1863') }
11
+ binary { 'binary' }
12
+ boolean { false }
13
+ end
14
+ end
data/spec/helpers.rb ADDED
@@ -0,0 +1,92 @@
1
+ # Mock Rails.application.eager_load! and define some
2
+ # Rails models for use in specs.
3
+ class Rails
4
+ def self.application
5
+ self
6
+ end
7
+
8
+ def self.eager_load!
9
+ @already_called ||= false
10
+
11
+ unless @already_called
12
+ Object.const_set('Sample', Class.new(ActiveRecord::Base))
13
+
14
+ Object.const_set('AnotherSample', Class.new(ActiveRecord::Base))
15
+
16
+ Object.const_set('YetAnotherSample', Class.new(ActiveRecord::Base))
17
+
18
+ Object.const_set('NoTableModel', Class.new(ActiveRecord::Base))
19
+
20
+ Object.const_set('EmptyModel', Class.new(ActiveRecord::Base))
21
+
22
+ @already_called = true
23
+ end
24
+ end
25
+
26
+ def self.env
27
+ 'test'
28
+ end
29
+ end
30
+
31
+ module Helpers
32
+ def create_db
33
+ ActiveRecord::Migration.verbose = false
34
+
35
+ ActiveRecord::Schema.define(version: 1) do
36
+ create_table 'samples', force: true do |t|
37
+ t.string 'string'
38
+ t.text 'text'
39
+ t.integer 'integer'
40
+ t.float 'float'
41
+ t.decimal 'decimal'
42
+ t.datetime 'datetime'
43
+ t.time 'time'
44
+ t.date 'date'
45
+ t.binary 'binary'
46
+ t.boolean 'boolean'
47
+ t.datetime 'created_at', null: false
48
+ t.datetime 'updated_at', null: false
49
+ end
50
+
51
+ create_table 'another_samples', force: true do |t|
52
+ t.string 'string'
53
+ t.text 'text'
54
+ t.integer 'integer'
55
+ t.float 'float'
56
+ t.decimal 'decimal'
57
+ t.datetime 'datetime'
58
+ t.time 'time'
59
+ t.date 'date'
60
+ t.binary 'binary'
61
+ t.boolean 'boolean'
62
+ t.datetime 'created_at', null: false
63
+ t.datetime 'updated_at', null: false
64
+ end
65
+
66
+ create_table 'yet_another_samples', force: true do |t|
67
+ t.string 'string'
68
+ t.text 'text'
69
+ t.integer 'integer'
70
+ t.float 'float'
71
+ t.decimal 'decimal'
72
+ t.datetime 'datetime'
73
+ t.time 'time'
74
+ t.date 'date'
75
+ t.binary 'binary'
76
+ t.boolean 'boolean'
77
+ t.datetime 'created_at', null: false
78
+ t.datetime 'updated_at', null: false
79
+ end
80
+
81
+ create_table 'empty_models', force: true
82
+ end
83
+ end
84
+
85
+ def load_sample_data
86
+ Rails.application.eager_load!
87
+
88
+ Sample.create!
89
+
90
+ ChildSample.create!
91
+ end
92
+ end
@@ -0,0 +1,36 @@
1
+ require 'seed_dump'
2
+
3
+ require 'active_support'
4
+ require 'active_record'
5
+
6
+ require 'tempfile'
7
+
8
+ require 'byebug'
9
+
10
+ require 'database_cleaner'
11
+ require 'factory_bot'
12
+
13
+ require './spec/helpers'
14
+
15
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
16
+
17
+ FactoryBot.find_definitions
18
+
19
+ RSpec.configure do |config|
20
+ config.order = 'random'
21
+
22
+ config.include Helpers
23
+
24
+ config.before(:suite) do
25
+ DatabaseCleaner.strategy = :transaction
26
+ DatabaseCleaner.clean_with(:truncation)
27
+ end
28
+
29
+ config.before do
30
+ DatabaseCleaner.start
31
+ end
32
+
33
+ config.after do
34
+ DatabaseCleaner.clean
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_seed_dump
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Halff
8
+ - Ryan Oblak
9
+ - Nick Flueckiger
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2021-10-02 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '4'
29
+ - !ruby/object:Gem::Dependency
30
+ name: activesupport
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '4'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '4'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rgl
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: activerecord-import
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '0.4'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '0.4'
71
+ - !ruby/object:Gem::Dependency
72
+ name: byebug
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '11.0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '11.0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: factory_bot
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: 4.8.2
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: 4.8.2
99
+ - !ruby/object:Gem::Dependency
100
+ name: rubocop
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: rubocop-performance
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: rubocop-rspec
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: jeweler
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '2.0'
148
+ type: :development
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - "~>"
153
+ - !ruby/object:Gem::Version
154
+ version: '2.0'
155
+ description: Dump (parts) of your database to db/seeds.rb to get a headstart creating
156
+ a meaningful seeds.rb file
157
+ email: nick.flueckiger@renuo.ch
158
+ executables:
159
+ - fastcheck
160
+ extensions: []
161
+ extra_rdoc_files:
162
+ - README.md
163
+ files:
164
+ - ".rspec"
165
+ - ".rubocop.yml"
166
+ - ".rubocop_todo.yml"
167
+ - Gemfile
168
+ - MIT-LICENSE
169
+ - README.md
170
+ - Rakefile
171
+ - VERSION
172
+ - bin/fastcheck
173
+ - db_seed_dump.gemspec
174
+ - lib/seed_dump.rb
175
+ - lib/seed_dump/dependency_unwrangler.rb
176
+ - lib/seed_dump/dump_methods.rb
177
+ - lib/seed_dump/dump_methods/enumeration.rb
178
+ - lib/seed_dump/environment.rb
179
+ - lib/seed_dump/railtie.rb
180
+ - lib/tasks/seed_dump.rake
181
+ - spec/dump_methods_spec.rb
182
+ - spec/environment_spec.rb
183
+ - spec/factories/another_samples.rb
184
+ - spec/factories/samples.rb
185
+ - spec/factories/yet_another_samples.rb
186
+ - spec/helpers.rb
187
+ - spec/spec_helper.rb
188
+ homepage: https://github.com/reno/seed_dump
189
+ licenses:
190
+ - MIT
191
+ metadata: {}
192
+ post_install_message:
193
+ rdoc_options: []
194
+ require_paths:
195
+ - lib
196
+ required_ruby_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ requirements: []
207
+ rubygems_version: 3.2.22
208
+ signing_key:
209
+ specification_version: 4
210
+ summary: "{Seed Dumper for Rails}"
211
+ test_files: []