seed_dump_citus 1.0.2

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.
@@ -0,0 +1,139 @@
1
+ class SeedDumpCitus
2
+ module Environment
3
+
4
+ def dump_using_environment(env = {})
5
+ Rails.application.eager_load!
6
+
7
+ models = retrieve_models(env) - retrieve_models_exclude(env)
8
+
9
+ limit = retrieve_limit_value(env)
10
+ append = retrieve_append_value(env)
11
+ models.each do |model|
12
+ model = model.limit(limit) if limit.present?
13
+
14
+ SeedDumpCitus.dump(model,
15
+ append: append,
16
+ batch_size: retrieve_batch_size_value(env),
17
+ exclude: retrieve_exclude_value(env),
18
+ file: retrieve_file_value(env),
19
+ import: retrieve_import_value(env))
20
+
21
+ append = true # Always append for every model after the first
22
+ # (append for the first model is determined by
23
+ # the APPEND environment variable).
24
+ end
25
+ end
26
+
27
+ private
28
+ # Internal: Array of Strings corresponding to Active Record model class names
29
+ # that should be excluded from the dump.
30
+ ACTIVE_RECORD_INTERNAL_MODELS = ['ActiveRecord::SchemaMigration',
31
+ 'ActiveRecord::InternalMetadata']
32
+
33
+ # Internal: Retrieves an Array of Active Record model class constants to be
34
+ # dumped.
35
+ #
36
+ # If a "MODEL" or "MODELS" environment variable is specified, there will be
37
+ # an attempt to parse the environment variable String by splitting it on
38
+ # commmas and then converting it to constant.
39
+ #
40
+ # Model classes that do not have corresponding database tables or database
41
+ # records will be filtered out, as will model classes internal to Active
42
+ # Record.
43
+ #
44
+ # env - Hash of environment variables from which to parse Active Record
45
+ # model classes. The Hash is not optional but the "MODEL" and "MODELS"
46
+ # keys are optional.
47
+ #
48
+ # Returns the Array of Active Record model classes to be dumped.
49
+ def retrieve_models(env)
50
+ # Parse either the "MODEL" environment variable or the "MODELS"
51
+ # environment variable, with "MODEL" taking precedence.
52
+ models_env = env['MODEL'] || env['MODELS']
53
+
54
+ # If there was a use models environment variable, split it and
55
+ # convert the given model string (e.g. "User") to an actual
56
+ # model constant (e.g. User).
57
+ #
58
+ # If a models environment variable was not given, use descendants of
59
+ # ActiveRecord::Base as the target set of models. This should be all
60
+ # model classes in the project.
61
+ models = if models_env
62
+ models_env.split(',')
63
+ .collect {|x| x.strip.underscore.singularize.camelize.constantize }
64
+ else
65
+ ActiveRecord::Base.descendants
66
+ end
67
+
68
+
69
+ # Filter the set of models to exclude:
70
+ # - The ActiveRecord::SchemaMigration model which is internal to Rails
71
+ # and should not be part of the dumped data.
72
+ # - Models that don't have a corresponding table in the database.
73
+ # - Models whose corresponding database tables are empty.
74
+ filtered_models = models.select do |model|
75
+ !ACTIVE_RECORD_INTERNAL_MODELS.include?(model.to_s) && \
76
+ model.table_exists? && \
77
+ model.exists?
78
+ end
79
+ end
80
+
81
+ # Internal: Returns a Boolean indicating whether the value for the "APPEND"
82
+ # key in the given Hash is equal to the String "true" (ignoring case),
83
+ # false if no value exists.
84
+ def retrieve_append_value(env)
85
+ parse_boolean_value(env['APPEND'])
86
+ end
87
+
88
+ # Internal: Returns a Boolean indicating whether the value for the "IMPORT"
89
+ # key in the given Hash is equal to the String "true" (ignoring case),
90
+ # false if no value exists.
91
+ def retrieve_import_value(env)
92
+ parse_boolean_value(env['IMPORT'])
93
+ end
94
+
95
+ # Internal: Retrieves an Array of Class constants parsed from the value for
96
+ # the "MODELS_EXCLUDE" key in the given Hash, and an empty Array if such
97
+ # key exists.
98
+ def retrieve_models_exclude(env)
99
+ env['MODELS_EXCLUDE'].to_s
100
+ .split(',')
101
+ .collect { |x| x.strip.underscore.singularize.camelize.constantize }
102
+ end
103
+
104
+ # Internal: Retrieves an Integer from the value for the "LIMIT" key in the
105
+ # given Hash, and nil if no such key exists.
106
+ def retrieve_limit_value(env)
107
+ retrieve_integer_value('LIMIT', env)
108
+ end
109
+
110
+ # Internal: Retrieves an Array of Symbols from the value for the "EXCLUDE"
111
+ # key from the given Hash, and nil if no such key exists.
112
+ def retrieve_exclude_value(env)
113
+ env['EXCLUDE'] ? env['EXCLUDE'].split(',').map {|e| e.strip.to_sym} : nil
114
+ end
115
+
116
+ # Internal: Retrieves the value for the "FILE" key from the given Hash, and
117
+ # 'db/seeds.rb' if no such key exists.
118
+ def retrieve_file_value(env)
119
+ env['FILE'] || 'db/seeds.rb'
120
+ end
121
+
122
+ # Internal: Retrieves an Integer from the value for the "BATCH_SIZE" key in
123
+ # the given Hash, and nil if no such key exists.
124
+ def retrieve_batch_size_value(env)
125
+ retrieve_integer_value('BATCH_SIZE', env)
126
+ end
127
+
128
+ # Internal: Retrieves an Integer from the value for the given key in
129
+ # the given Hash, and nil if no such key exists.
130
+ def retrieve_integer_value(key, hash)
131
+ hash[key] ? hash[key].to_i : nil
132
+ end
133
+
134
+ # Internal: Parses a Boolean from the given value.
135
+ def parse_boolean_value(value)
136
+ value.to_s.downcase == 'true'
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,9 @@
1
+ class SeedDumpCitus
2
+ class Railtie < Rails::Railtie
3
+
4
+ rake_tasks do
5
+ load "tasks/seed_dump_citus.rake"
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ namespace :db do
2
+ namespace :seed do
3
+ namespace :citus do
4
+ desc "Dump records from the database into db/seeds.rb"
5
+ task :dump => :environment do
6
+
7
+ SeedDumpCitus.dump_using_environment(ENV)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,74 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: seed_dump_citus 3.3.1 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "seed_dump_citus".freeze
9
+ s.version = "1.0.2"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib".freeze]
13
+ s.authors = ["Rob Halff".freeze, "Ryan Oblak".freeze]
14
+ s.date = "2018-05-08"
15
+ s.description = "Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file".freeze
16
+ s.email = "rroblak@gmail.com".freeze
17
+ s.extra_rdoc_files = [
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".rspec",
22
+ "Gemfile",
23
+ "MIT-LICENSE",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/seed_dump_citus.rb",
28
+ "lib/seed_dump_citus/dump_methods.rb",
29
+ "lib/seed_dump_citus/dump_methods/enumeration.rb",
30
+ "lib/seed_dump_citus/environment.rb",
31
+ "lib/seed_dump_citus/railtie.rb",
32
+ "lib/tasks/seed_dump_citus.rake",
33
+ "seed_dump_citus.gemspec",
34
+ "spec/dump_methods_spec.rb",
35
+ "spec/environment_spec.rb",
36
+ "spec/factories/another_samples.rb",
37
+ "spec/factories/samples.rb",
38
+ "spec/factories/yet_another_samples.rb",
39
+ "spec/helpers.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+ s.homepage = "https://github.com/rroblak/seed_dump_citus".freeze
43
+ s.licenses = ["MIT".freeze]
44
+ s.rubygems_version = "2.7.6".freeze
45
+ s.summary = "{Seed Dumper for Rails}".freeze
46
+
47
+ if s.respond_to? :specification_version then
48
+ s.specification_version = 4
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4"])
52
+ s.add_runtime_dependency(%q<activerecord>.freeze, [">= 4"])
53
+ s.add_development_dependency(%q<byebug>.freeze, ["~> 2.0"])
54
+ s.add_development_dependency(%q<factory_bot>.freeze, ["~> 4.8.2"])
55
+ s.add_development_dependency(%q<activerecord-import>.freeze, ["~> 0.4"])
56
+ s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.0"])
57
+ else
58
+ s.add_dependency(%q<activesupport>.freeze, [">= 4"])
59
+ s.add_dependency(%q<activerecord>.freeze, [">= 4"])
60
+ s.add_dependency(%q<byebug>.freeze, ["~> 2.0"])
61
+ s.add_dependency(%q<factory_bot>.freeze, ["~> 4.8.2"])
62
+ s.add_dependency(%q<activerecord-import>.freeze, ["~> 0.4"])
63
+ s.add_dependency(%q<jeweler>.freeze, ["~> 2.0"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<activesupport>.freeze, [">= 4"])
67
+ s.add_dependency(%q<activerecord>.freeze, [">= 4"])
68
+ s.add_dependency(%q<byebug>.freeze, ["~> 2.0"])
69
+ s.add_dependency(%q<factory_bot>.freeze, ["~> 4.8.2"])
70
+ s.add_dependency(%q<activerecord-import>.freeze, ["~> 0.4"])
71
+ s.add_dependency(%q<jeweler>.freeze, ["~> 2.0"])
72
+ end
73
+ end
74
+
@@ -0,0 +1,176 @@
1
+ require 'spec_helper'
2
+
3
+ describe SeedDumpCitus do
4
+
5
+ def expected_output(include_id = false, id_offset = 0)
6
+ output = "Sample.create!([\n "
7
+
8
+ data = []
9
+ ((1 + id_offset)..(3 + id_offset)).each do |i|
10
+ data << "{#{include_id ? "id: #{i}, " : ''}string: \"string\", text: \"text\", integer: 42, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}"
11
+ end
12
+
13
+ output + data.join(",\n ") + "\n])\n"
14
+ end
15
+
16
+ describe '.dump' do
17
+ before do
18
+ Rails.application.eager_load!
19
+
20
+ create_db
21
+
22
+ FactoryBot.create_list(:sample, 3)
23
+ end
24
+
25
+ context 'without file option' do
26
+ it 'should return the dump of the models passed in' do
27
+ SeedDumpCitus.dump(Sample).should eq(expected_output)
28
+ end
29
+ end
30
+
31
+ context 'with file option' do
32
+ before do
33
+ @filename = Tempfile.new(File.join(Dir.tmpdir, 'foo'), nil)
34
+ end
35
+
36
+ after do
37
+ File.unlink(@filename)
38
+ end
39
+
40
+ it 'should dump the models to the specified file' do
41
+ SeedDumpCitus.dump(Sample, file: @filename)
42
+
43
+ File.open(@filename) { |file| file.read.should eq(expected_output) }
44
+ end
45
+
46
+ context 'with append option' do
47
+ it 'should append to the file rather than overwriting it' do
48
+ SeedDumpCitus.dump(Sample, file: @filename)
49
+ SeedDumpCitus.dump(Sample, file: @filename, append: true)
50
+
51
+ File.open(@filename) { |file| file.read.should eq(expected_output + expected_output) }
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'ActiveRecord relation' do
57
+ it 'should return nil if the count is 0' do
58
+ SeedDumpCitus.dump(EmptyModel).should be(nil)
59
+ end
60
+
61
+ context 'with an order parameter' do
62
+ it 'should dump the models in the specified order' do
63
+ Sample.delete_all
64
+ samples = 3.times {|i| FactoryBot.create(:sample, integer: i) }
65
+
66
+ SeedDumpCitus.dump(Sample.order('integer DESC')).should eq("Sample.create!([\n {string: \"string\", text: \"text\", integer: 2, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {string: \"string\", text: \"text\", integer: 1, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {string: \"string\", text: \"text\", integer: 0, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}\n])\n")
67
+ end
68
+ end
69
+
70
+ context 'without an order parameter' do
71
+ it 'should dump the models sorted by primary key ascending' do
72
+ SeedDumpCitus.dump(Sample).should eq(expected_output)
73
+ end
74
+ end
75
+
76
+ context 'with a limit parameter' do
77
+ it 'should dump the number of models specified by the limit when the limit is smaller than the batch size' do
78
+ expected_output = "Sample.create!([\n {string: \"string\", text: \"text\", integer: 42, float: 3.14, decimal: \"2.72\", datetime: \"1776-07-04 19:14:00\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}\n])\n"
79
+
80
+ SeedDumpCitus.dump(Sample.limit(1)).should eq(expected_output)
81
+ end
82
+
83
+ it 'should dump the number of models specified by the limit when the limit is larger than the batch size but not a multiple of the batch size' do
84
+ Sample.delete_all
85
+ 4.times { FactoryBot.create(:sample) }
86
+
87
+ SeedDumpCitus.dump(Sample.limit(3), batch_size: 2).should eq(expected_output(false, 3))
88
+ end
89
+ end
90
+ end
91
+
92
+ context 'with a batch_size parameter' do
93
+ it 'should not raise an exception' do
94
+ SeedDumpCitus.dump(Sample, batch_size: 100)
95
+ end
96
+
97
+ it 'should not cause records to not be dumped' do
98
+ SeedDumpCitus.dump(Sample, batch_size: 2).should eq(expected_output)
99
+
100
+ SeedDumpCitus.dump(Sample, batch_size: 1).should eq(expected_output)
101
+ end
102
+ end
103
+
104
+ context 'Array' do
105
+ it 'should return the dump of the models passed in' do
106
+ SeedDumpCitus.dump(Sample.all.to_a, batch_size: 2).should eq(expected_output)
107
+ end
108
+
109
+ it 'should return nil if the array is empty' do
110
+ SeedDumpCitus.dump([]).should be(nil)
111
+ end
112
+ end
113
+
114
+ context 'with an exclude parameter' do
115
+ it 'should exclude the specified attributes from the dump' do
116
+ expected_output = "Sample.create!([\n {text: \"text\", integer: 42, decimal: \"2.72\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {text: \"text\", integer: 42, decimal: \"2.72\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false},\n {text: \"text\", integer: 42, decimal: \"2.72\", time: \"2000-01-01 03:15:00\", date: \"1863-11-19\", binary: \"binary\", boolean: false}\n])\n"
117
+
118
+ SeedDumpCitus.dump(Sample, exclude: [:id, :created_at, :updated_at, :string, :float, :datetime]).should eq(expected_output)
119
+ end
120
+ end
121
+
122
+ context 'Range' do
123
+ it 'should dump a class with ranges' do
124
+ expected_output = "RangeSample.create!([\n {range_with_end_included: \"[1,3]\", range_with_end_excluded: \"[1,3)\", positive_infinite_range: \"[1,]\", negative_infinite_range: \"[,1]\", infinite_range: \"[,]\"}\n])\n"
125
+
126
+ SeedDumpCitus.dump([RangeSample.new]).should eq(expected_output)
127
+ end
128
+ end
129
+
130
+ context 'activerecord-import' do
131
+ it 'should dump in the activerecord-import format when import is true' do
132
+ SeedDumpCitus.dump(Sample, import: true, exclude: []).should eq <<-RUBY
133
+ Sample.import([:id, :string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :created_at, :updated_at], [
134
+ [1, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
135
+ [2, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
136
+ [3, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"]
137
+ ])
138
+ RUBY
139
+ end
140
+
141
+ it 'should omit excluded columns if they are specified' do
142
+ SeedDumpCitus.dump(Sample, import: true, exclude: [:id, :created_at, :updated_at]).should eq <<-RUBY
143
+ Sample.import([:string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean], [
144
+ ["string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false],
145
+ ["string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false],
146
+ ["string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false]
147
+ ])
148
+ RUBY
149
+ end
150
+
151
+ context 'should add the params to the output if they are specified' do
152
+ it 'should dump in the activerecord-import format when import is true' do
153
+ SeedDumpCitus.dump(Sample, import: {validate: false }, exclude: []).should eq <<-RUBY
154
+ Sample.import([:id, :string, :text, :integer, :float, :decimal, :datetime, :time, :date, :binary, :boolean, :created_at, :updated_at], [
155
+ [1, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
156
+ [2, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"],
157
+ [3, "string", "text", 42, 3.14, "2.72", "1776-07-04 19:14:00", "2000-01-01 03:15:00", "1863-11-19", "binary", false, "1969-07-20 20:18:00", "1989-11-10 04:20:00"]
158
+ ], validate: false)
159
+ RUBY
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ class RangeSample
167
+ def attributes
168
+ {
169
+ "range_with_end_included" => (1..3),
170
+ "range_with_end_excluded" => (1...3),
171
+ "positive_infinite_range" => (1..Float::INFINITY),
172
+ "negative_infinite_range" => (-Float::INFINITY..1),
173
+ "infinite_range" => (-Float::INFINITY..Float::INFINITY)
174
+ }
175
+ end
176
+ end
@@ -0,0 +1,144 @@
1
+ require 'spec_helper'
2
+
3
+ describe SeedDumpCitus do
4
+ describe '.dump_using_environment' do
5
+ before(:all) do
6
+ create_db
7
+ end
8
+
9
+ before(:each) do
10
+ Rails.application.eager_load!
11
+
12
+ FactoryBot.create(:sample)
13
+ end
14
+
15
+ describe 'APPEND' do
16
+ it "should specify append as true if the APPEND env var is 'true'" do
17
+ SeedDumpCitus.should_receive(:dump).with(anything, include(append: true))
18
+
19
+ SeedDumpCitus.dump_using_environment('APPEND' => 'true')
20
+ end
21
+
22
+ it "should specify append as true if the APPEND env var is 'TRUE'" do
23
+ SeedDumpCitus.should_receive(:dump).with(anything, include(append: true))
24
+
25
+ SeedDumpCitus.dump_using_environment('APPEND' => 'TRUE')
26
+ end
27
+
28
+ it "should specify append as false the first time if the APPEND env var is not 'true' (and true after that)" do
29
+ FactoryBot.create(:another_sample)
30
+
31
+ SeedDumpCitus.should_receive(:dump).with(anything, include(append: false)).ordered
32
+ SeedDumpCitus.should_receive(:dump).with(anything, include(append: true)).ordered
33
+
34
+ SeedDumpCitus.dump_using_environment('APPEND' => 'false')
35
+ end
36
+ end
37
+
38
+ describe 'BATCH_SIZE' do
39
+ it 'should pass along the specified batch size' do
40
+ SeedDumpCitus.should_receive(:dump).with(anything, include(batch_size: 17))
41
+
42
+ SeedDumpCitus.dump_using_environment('BATCH_SIZE' => '17')
43
+ end
44
+
45
+ it 'should pass along a nil batch size if BATCH_SIZE is not specified' do
46
+ SeedDumpCitus.should_receive(:dump).with(anything, include(batch_size: nil))
47
+
48
+ SeedDumpCitus.dump_using_environment
49
+ end
50
+ end
51
+
52
+ describe 'EXCLUDE' do
53
+ it 'should pass along any attributes to be excluded' do
54
+ SeedDumpCitus.should_receive(:dump).with(anything, include(exclude: [:baggins, :saggins]))
55
+
56
+ SeedDumpCitus.dump_using_environment('EXCLUDE' => 'baggins,saggins')
57
+ end
58
+ end
59
+
60
+ describe 'FILE' do
61
+ it 'should pass the FILE parameter to the dump method correctly' do
62
+ SeedDumpCitus.should_receive(:dump).with(anything, include(file: 'blargle'))
63
+
64
+ SeedDumpCitus.dump_using_environment('FILE' => 'blargle')
65
+ end
66
+
67
+ it 'should pass db/seeds.rb as the file parameter if no FILE is specified' do
68
+ SeedDumpCitus.should_receive(:dump).with(anything, include(file: 'db/seeds.rb'))
69
+
70
+ SeedDumpCitus.dump_using_environment
71
+ end
72
+ end
73
+
74
+ describe 'LIMIT' do
75
+ it 'should apply the specified limit to the records' do
76
+ relation_double = double('ActiveRecord relation double')
77
+ Sample.should_receive(:limit).with(5).and_return(relation_double)
78
+
79
+ SeedDumpCitus.should_receive(:dump).with(relation_double, anything)
80
+ SeedDumpCitus.stub(:dump)
81
+
82
+ SeedDumpCitus.dump_using_environment('LIMIT' => '5')
83
+ end
84
+ end
85
+
86
+ ['', 'S'].each do |model_suffix|
87
+ model_env = 'MODEL' + model_suffix
88
+
89
+ describe model_env do
90
+ context "if #{model_env} is not specified" do
91
+ it "should dump all non-empty models" do
92
+ FactoryBot.create(:another_sample)
93
+
94
+ [Sample, AnotherSample].each do |model|
95
+ SeedDumpCitus.should_receive(:dump).with(model, anything)
96
+ end
97
+
98
+ SeedDumpCitus.dump_using_environment
99
+ end
100
+ end
101
+
102
+ context "if #{model_env} is specified" do
103
+ it "should dump only the specified model" do
104
+ FactoryBot.create(:another_sample)
105
+
106
+ SeedDumpCitus.should_receive(:dump).with(Sample, anything)
107
+
108
+ SeedDumpCitus.dump_using_environment(model_env => 'Sample')
109
+ end
110
+
111
+ it "should not dump empty models" do
112
+ SeedDumpCitus.should_not_receive(:dump).with(EmptyModel, anything)
113
+
114
+ SeedDumpCitus.dump_using_environment(model_env => 'EmptyModel, Sample')
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ describe "MODELS_EXCLUDE" do
121
+ it "should dump all non-empty models except the specified models" do
122
+ FactoryBot.create(:another_sample)
123
+
124
+ SeedDumpCitus.should_receive(:dump).with(Sample, anything)
125
+
126
+ SeedDumpCitus.dump_using_environment('MODELS_EXCLUDE' => 'AnotherSample')
127
+ end
128
+ end
129
+
130
+ it 'should run ok without ActiveRecord::SchemaMigration being set (needed for Rails Engines)' do
131
+ schema_migration = ActiveRecord::SchemaMigration
132
+
133
+ ActiveRecord.send(:remove_const, :SchemaMigration)
134
+
135
+ SeedDumpCitus.stub(:dump)
136
+
137
+ begin
138
+ SeedDumpCitus.dump_using_environment
139
+ ensure
140
+ ActiveRecord.const_set(:SchemaMigration, schema_migration)
141
+ end
142
+ end
143
+ end
144
+ end