test_data 0.2.1 → 0.3.1

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
data/lib/test_data.rb CHANGED
@@ -12,20 +12,26 @@ 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
14
  require_relative "test_data/detects_database_emptiness"
15
+ require_relative "test_data/detects_database_existence"
16
+ require_relative "test_data/determines_when_sql_dump_was_made"
17
+ require_relative "test_data/determines_databases_associated_dump_time"
15
18
  require_relative "test_data/dumps_database"
16
19
  require_relative "test_data/error"
17
20
  require_relative "test_data/inserts_test_data"
18
21
  require_relative "test_data/installs_configuration"
19
22
  require_relative "test_data/loads_database_dumps"
20
23
  require_relative "test_data/log"
24
+ require_relative "test_data/manager"
21
25
  require_relative "test_data/railtie"
26
+ require_relative "test_data/records_dump_metadata"
22
27
  require_relative "test_data/save_point"
23
28
  require_relative "test_data/statistics"
24
- require_relative "test_data/manager"
25
29
  require_relative "test_data/truncates_test_data"
26
30
  require_relative "test_data/verifies_configuration"
27
31
  require_relative "test_data/verifies_dumps_are_loadable"
28
32
  require_relative "test_data/version"
33
+ require_relative "test_data/warns_if_dump_is_newer_than_database"
34
+ require_relative "test_data/warns_if_database_is_newer_than_dump"
29
35
  require_relative "generators/test_data/environment_file_generator"
30
36
  require_relative "generators/test_data/initializer_generator"
31
37
  require_relative "generators/test_data/cable_yaml_generator"
@@ -37,25 +43,30 @@ module TestData
37
43
  def self.uninitialize
38
44
  @manager ||= Manager.new
39
45
  @manager.rollback_to_before_data_load
46
+ nil
40
47
  end
41
48
 
42
49
  def self.uses_test_data
43
50
  @manager ||= Manager.new
44
51
  @manager.load
52
+ nil
45
53
  end
46
54
 
47
55
  def self.uses_clean_slate
48
56
  @manager ||= Manager.new
49
57
  @manager.truncate
58
+ nil
50
59
  end
51
60
 
52
61
  def self.uses_rails_fixtures(test_instance)
53
62
  @rails_fixtures_loader ||= CustomLoaders::RailsFixtures.new
54
63
  @manager ||= Manager.new
55
64
  @manager.load_custom_data(@rails_fixtures_loader, test_instance: test_instance)
65
+ nil
56
66
  end
57
67
 
58
68
  def self.insert_test_data_dump
59
69
  InsertsTestData.new.call
70
+ nil
60
71
  end
61
72
  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
@@ -52,6 +86,7 @@ bin/rails test test/integration/transaction_committing_boops_test.rb
52
86
  # Run a test that prevents Rails fixtures for preloading and then loads them in a transaction
53
87
  bin/rails test test/integration/rails_fixtures_override_test.rb
54
88
  bundle exec rspec spec/requests/rails_fixtures_override_spec.rb
89
+ bin/rails test test/integration/fixture_load_count_test.rb
55
90
 
56
91
  # Run a test that forgets to prevent Rails fixtures but then tries to load them in a transaction
57
92
  bin/rails test test/integration/rails_fixtures_double_load_test.rb
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.1
4
+ version: 0.3.1
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-07-30 00:00:00.000000000 Z
11
+ date: 2022-03-27 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
@@ -103,6 +109,7 @@ files:
103
109
  - example/test/integration/better_mode_switching_demo_test.rb
104
110
  - example/test/integration/boops_that_boop_boops_test.rb
105
111
  - example/test/integration/dont_dump_tables_test.rb
112
+ - example/test/integration/fixture_load_count_test.rb
106
113
  - example/test/integration/load_rollback_truncate_test.rb
107
114
  - example/test/integration/migrated_boops_test.rb
108
115
  - example/test/integration/mode_switching_demo_test.rb
@@ -135,6 +142,9 @@ files:
135
142
  - lib/test_data/custom_loaders/abstract_base.rb
136
143
  - lib/test_data/custom_loaders/rails_fixtures.rb
137
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
138
148
  - lib/test_data/dumps_database.rb
139
149
  - lib/test_data/error.rb
140
150
  - lib/test_data/generator_support.rb
@@ -145,12 +155,15 @@ files:
145
155
  - lib/test_data/manager.rb
146
156
  - lib/test_data/railtie.rb
147
157
  - lib/test_data/rake.rb
158
+ - lib/test_data/records_dump_metadata.rb
148
159
  - lib/test_data/save_point.rb
149
160
  - lib/test_data/statistics.rb
150
161
  - lib/test_data/truncates_test_data.rb
151
162
  - lib/test_data/verifies_configuration.rb
152
163
  - lib/test_data/verifies_dumps_are_loadable.rb
153
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
154
167
  - script/reset_example_app
155
168
  - script/test
156
169
  - test_data.gemspec
@@ -175,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
188
  - !ruby/object:Gem::Version
176
189
  version: '0'
177
190
  requirements: []
178
- rubygems_version: 3.2.15
191
+ rubygems_version: 3.3.6
179
192
  signing_key:
180
193
  specification_version: 4
181
194
  summary: Dumps and Loads data for your Rails app's tests