test_data 0.2.2 → 0.3.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,32 @@
1
+ module TestData
2
+ class WarnsIfDatabaseIsNewerThanDump
3
+ def initialize
4
+ @config = TestData.config
5
+ @determines_when_sql_dump_was_made = DeterminesWhenSqlDumpWasMade.new
6
+ @determines_databases_associated_dump_time = DeterminesDatabasesAssociatedDumpTime.new
7
+ end
8
+
9
+ def call
10
+ return unless Rails.env.test_data?
11
+ sql_dumped_at = @determines_when_sql_dump_was_made.call
12
+ database_dumped_at = @determines_databases_associated_dump_time.call
13
+
14
+ if database_dumped_at.present? &&
15
+ sql_dumped_at.present? &&
16
+ database_dumped_at > sql_dumped_at
17
+ TestData.log.warn <<~MSG
18
+ Your local test_data database '#{@config.database_name}' is associated
19
+ with a SQL dump that was NEWER than the current dumps located in
20
+ '#{File.dirname(@config.data_dump_path)}':
21
+
22
+ SQL Dump: #{sql_dumped_at.localtime}
23
+ Database: #{database_dumped_at.localtime}
24
+
25
+ If you're not intentionally resetting your local test_data database to an earlier
26
+ version, you may want to take a closer look before taking any destructive actions.
27
+
28
+ MSG
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ module TestData
2
+ class WarnsIfDumpIsNewerThanDatabase
3
+ def initialize
4
+ @config = TestData.config
5
+ @determines_when_sql_dump_was_made = DeterminesWhenSqlDumpWasMade.new
6
+ @determines_databases_associated_dump_time = DeterminesDatabasesAssociatedDumpTime.new
7
+ end
8
+
9
+ def call
10
+ return unless Rails.env.test_data?
11
+ sql_dumped_at = @determines_when_sql_dump_was_made.call
12
+ database_dumped_at = @determines_databases_associated_dump_time.call
13
+
14
+ if sql_dumped_at.present? &&
15
+ database_dumped_at.present? &&
16
+ sql_dumped_at > database_dumped_at
17
+ TestData.log.warn <<~MSG
18
+ The SQL dumps in '#{File.dirname(@config.data_dump_path)}' were created
19
+ after your local test_data database '#{@config.database_name}' was last dumped.
20
+
21
+ SQL Dump: #{sql_dumped_at.localtime}
22
+ Database: #{database_dumped_at.localtime}
23
+
24
+ To avoid potential data loss, you may want to consider dropping '#{@config.database_name}'
25
+ and loading the SQL dumps before making changes to the test data in this database
26
+ or performing another dump.
27
+
28
+ To do this, kill any processes with RAILS_ENV=test_data and then run:
29
+
30
+ $ bin/rake test_data:reinitialize
31
+
32
+ MSG
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,42 @@
1
+ module TestData
2
+ module Wrap
3
+ class WebpackerConfig
4
+ def initialize
5
+ @user_config_path = Pathname.new("#{TestData.config.pwd}/config/webpacker.yml")
6
+ end
7
+
8
+ def relative_user_config_path
9
+ @user_config_path.relative_path_from(Rails.root)
10
+ end
11
+
12
+ def no_user_config_exists?
13
+ !@user_config_path.readable?
14
+ end
15
+
16
+ def user_config
17
+ load_yaml(@user_config_path)
18
+ end
19
+
20
+ def webpacker_gem_spec_loaded?
21
+ !!Gem.loaded_specs["webpacker"]
22
+ end
23
+
24
+ def builtin_config
25
+ webpacker_path = Gem.loaded_specs["webpacker"].full_gem_path
26
+ load_yaml(File.join(webpacker_path, "lib/install/config/webpacker.yml"))
27
+ end
28
+
29
+ def required_entries_missing_from_test_data_config
30
+ missing_keys = builtin_config["development"].keys - user_config["test_data"].keys
31
+ builtin_config["development"].slice(*missing_keys).presence
32
+ end
33
+
34
+ private
35
+
36
+ def load_yaml(path)
37
+ YAML.load_file(path)
38
+ rescue
39
+ end
40
+ end
41
+ end
42
+ end
data/lib/test_data.rb CHANGED
@@ -11,21 +11,28 @@ require_relative "test_data/configurators/secrets_yaml"
11
11
  require_relative "test_data/configurators/webpacker_yaml"
12
12
  require_relative "test_data/custom_loaders/abstract_base"
13
13
  require_relative "test_data/custom_loaders/rails_fixtures"
14
+ require_relative "test_data/wrap/webpacker_config"
14
15
  require_relative "test_data/detects_database_emptiness"
16
+ require_relative "test_data/detects_database_existence"
17
+ require_relative "test_data/determines_when_sql_dump_was_made"
18
+ require_relative "test_data/determines_databases_associated_dump_time"
15
19
  require_relative "test_data/dumps_database"
16
20
  require_relative "test_data/error"
17
21
  require_relative "test_data/inserts_test_data"
18
22
  require_relative "test_data/installs_configuration"
19
23
  require_relative "test_data/loads_database_dumps"
20
24
  require_relative "test_data/log"
25
+ require_relative "test_data/manager"
21
26
  require_relative "test_data/railtie"
27
+ require_relative "test_data/records_dump_metadata"
22
28
  require_relative "test_data/save_point"
23
29
  require_relative "test_data/statistics"
24
- require_relative "test_data/manager"
25
30
  require_relative "test_data/truncates_test_data"
26
31
  require_relative "test_data/verifies_configuration"
27
32
  require_relative "test_data/verifies_dumps_are_loadable"
28
33
  require_relative "test_data/version"
34
+ require_relative "test_data/warns_if_dump_is_newer_than_database"
35
+ require_relative "test_data/warns_if_database_is_newer_than_dump"
29
36
  require_relative "generators/test_data/environment_file_generator"
30
37
  require_relative "generators/test_data/initializer_generator"
31
38
  require_relative "generators/test_data/cable_yaml_generator"
@@ -37,25 +44,30 @@ module TestData
37
44
  def self.uninitialize
38
45
  @manager ||= Manager.new
39
46
  @manager.rollback_to_before_data_load
47
+ nil
40
48
  end
41
49
 
42
50
  def self.uses_test_data
43
51
  @manager ||= Manager.new
44
52
  @manager.load
53
+ nil
45
54
  end
46
55
 
47
56
  def self.uses_clean_slate
48
57
  @manager ||= Manager.new
49
58
  @manager.truncate
59
+ nil
50
60
  end
51
61
 
52
62
  def self.uses_rails_fixtures(test_instance)
53
63
  @rails_fixtures_loader ||= CustomLoaders::RailsFixtures.new
54
64
  @manager ||= Manager.new
55
65
  @manager.load_custom_data(@rails_fixtures_loader, test_instance: test_instance)
66
+ nil
56
67
  end
57
68
 
58
69
  def self.insert_test_data_dump
59
70
  InsertsTestData.new.call
71
+ nil
60
72
  end
61
73
  end
data/script/test CHANGED
@@ -28,11 +28,45 @@ bin/rails test test/integration/better_mode_switching_demo_test.rb
28
28
  bin/rails test test/integration/parallel_boops_with_fixtures_test.rb
29
29
  bin/rails test test/integration/parallel_boops_without_fixtures_test.rb
30
30
 
31
+ # Verify the out-of-date database warning system works (step 1)
32
+ mkdir -p tmp/dump-backup-1
33
+ cp test/support/test_data/* tmp/dump-backup-1
34
+
31
35
  # Test using the test_data env to interactively create test data
32
36
  RAILS_ENV=test_data rails runner "5.times { Boop.create! }"
33
37
  bin/rake test_data:dump
34
38
  bin/rails test test/integration/updated_boops_test.rb
35
39
 
40
+ # Verify the out-of-date database warning system works (step 2)
41
+ mkdir -p tmp/dump-backup-2
42
+ cp test/support/test_data/* tmp/dump-backup-2
43
+ cp tmp/dump-backup-1/* test/support/test_data
44
+ set +e
45
+ result=`yes "n" | bin/rake test_data:reinitialize 2>&1`
46
+ set -e
47
+ if ! echo "$result" | grep -q "\[test_data:warn\] Your local test_data database 'example_test_data' is associated"; then
48
+ echo "Expected to be warned that the SQL dump was older than the database, but there was no warning."
49
+ exit 1
50
+ fi
51
+ TEST_DATA_CONFIRM=true bin/rake test_data:reinitialize
52
+ cp tmp/dump-backup-2/* test/support/test_data
53
+ if ! RAILS_ENV=test_data bin/rails runner "" 2>&1 | grep -q "\[test_data:warn\] The SQL dumps in 'test/support/test_data' were created"; then
54
+ echo "Expected to be warned that the SQL dump was newer than the database, but there was no warning."
55
+ exit 1
56
+ fi
57
+ set +e
58
+ result=`yes "n" | bin/rake test_data:reinitialize 2>&1`
59
+ set -e
60
+ if echo "$result" | grep -q "\[test_data:warn\] Your local test_data database 'example_test_data' is associated"; then
61
+ echo "Expected NOT to be warned that the SQL dump was older than the database, but a warning was printed."
62
+ exit 1
63
+ fi
64
+ TEST_DATA_CONFIRM=true bin/rake test_data:reinitialize
65
+ if RAILS_ENV=test_data bin/rails runner "" 2>&1 | grep -q "\[test_data:warn\]"; then
66
+ echo "Expected NOT to be warned that the SQL dump was newer than the database, but a warning was printed."
67
+ exit 1
68
+ fi
69
+
36
70
  # Test a migration being added and run and an out-of-date dump being loaded
37
71
  cp ../test/fixtures/20210418220133_add_beep_to_boops.rb db/migrate
38
72
  cp ../test/fixtures/20210624180810_create_pants.rb db/migrate
data/test_data.gemspec CHANGED
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ["lib"]
25
25
 
26
- spec.add_dependency "railties", "~> 6.0"
26
+ spec.add_dependency "railties", ">= 6.0", "< 8.0"
27
27
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Searls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-03 00:00:00.000000000 Z
11
+ date: 2022-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '6.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '6.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.0'
27
33
  description:
28
34
  email:
29
35
  - searls@gmail.com
@@ -136,6 +142,9 @@ files:
136
142
  - lib/test_data/custom_loaders/abstract_base.rb
137
143
  - lib/test_data/custom_loaders/rails_fixtures.rb
138
144
  - lib/test_data/detects_database_emptiness.rb
145
+ - lib/test_data/detects_database_existence.rb
146
+ - lib/test_data/determines_databases_associated_dump_time.rb
147
+ - lib/test_data/determines_when_sql_dump_was_made.rb
139
148
  - lib/test_data/dumps_database.rb
140
149
  - lib/test_data/error.rb
141
150
  - lib/test_data/generator_support.rb
@@ -146,12 +155,16 @@ files:
146
155
  - lib/test_data/manager.rb
147
156
  - lib/test_data/railtie.rb
148
157
  - lib/test_data/rake.rb
158
+ - lib/test_data/records_dump_metadata.rb
149
159
  - lib/test_data/save_point.rb
150
160
  - lib/test_data/statistics.rb
151
161
  - lib/test_data/truncates_test_data.rb
152
162
  - lib/test_data/verifies_configuration.rb
153
163
  - lib/test_data/verifies_dumps_are_loadable.rb
154
164
  - lib/test_data/version.rb
165
+ - lib/test_data/warns_if_database_is_newer_than_dump.rb
166
+ - lib/test_data/warns_if_dump_is_newer_than_database.rb
167
+ - lib/test_data/wrap/webpacker_config.rb
155
168
  - script/reset_example_app
156
169
  - script/test
157
170
  - test_data.gemspec
@@ -176,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
189
  - !ruby/object:Gem::Version
177
190
  version: '0'
178
191
  requirements: []
179
- rubygems_version: 3.2.15
192
+ rubygems_version: 3.3.6
180
193
  signing_key:
181
194
  specification_version: 4
182
195
  summary: Dumps and Loads data for your Rails app's tests