test_data 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +13 -13
- data/README.md +30 -28
- data/example/Gemfile.lock +1 -1
- data/lib/test_data/detects_database_existence.rb +19 -0
- data/lib/test_data/determines_databases_associated_dump_time.rb +13 -0
- data/lib/test_data/determines_when_sql_dump_was_made.rb +24 -0
- data/lib/test_data/dumps_database.rb +3 -0
- data/lib/test_data/railtie.rb +4 -0
- data/lib/test_data/rake.rb +39 -9
- data/lib/test_data/records_dump_metadata.rb +9 -0
- data/lib/test_data/version.rb +1 -1
- data/lib/test_data/warns_if_database_is_newer_than_dump.rb +32 -0
- data/lib/test_data/warns_if_dump_is_newer_than_database.rb +36 -0
- data/lib/test_data.rb +12 -1
- data/script/test +34 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c9bcdeaebcbad6163fb495b327408b64f7d85f90efd7ab171275ba4fd61e484
|
4
|
+
data.tar.gz: c08433086b9cfcb57e88e275e63f8600083945afdd738962a952e94573b95f77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e69305fb469eab8714569cea4b57fb6a3b9050ea97065230ef86afa27e8c089d094b75673515348e31f8ec989a61924fb7924d70c21fdbd31decdef8e490459e
|
7
|
+
data.tar.gz: 1592fccf3753f33c74f449ad2debe029806778383fd07f089c9bb67e49861e5a3885d04497720dd04525511182ca8b87ff6774c90837845204f210d85f3db270
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# 0.3.0
|
2
|
+
|
3
|
+
- Add a `test_data:reinitialize` task that will delete the `test_data` database
|
4
|
+
if necessary before invoking `test_data:initialize`
|
5
|
+
- Warn if re-initializing and the local database appears to have been dumped
|
6
|
+
or loaded from a dump that is newer than the dumps on disk
|
7
|
+
- Add a warning on app load if the dumps on disk appear
|
8
|
+
newer than the local `test_data` database
|
9
|
+
|
1
10
|
# 0.2.2
|
2
11
|
|
3
12
|
- Improve performance of Rails fixtures being repeatedly loaded by changing the
|
data/Gemfile.lock
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
test_data (0.
|
4
|
+
test_data (0.3.0)
|
5
5
|
railties (~> 6.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actionpack (6.1.4)
|
11
|
-
actionview (= 6.1.4)
|
12
|
-
activesupport (= 6.1.4)
|
10
|
+
actionpack (6.1.4.1)
|
11
|
+
actionview (= 6.1.4.1)
|
12
|
+
activesupport (= 6.1.4.1)
|
13
13
|
rack (~> 2.0, >= 2.0.9)
|
14
14
|
rack-test (>= 0.6.3)
|
15
15
|
rails-dom-testing (~> 2.0)
|
16
16
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
-
actionview (6.1.4)
|
18
|
-
activesupport (= 6.1.4)
|
17
|
+
actionview (6.1.4.1)
|
18
|
+
activesupport (= 6.1.4.1)
|
19
19
|
builder (~> 3.1)
|
20
20
|
erubi (~> 1.4)
|
21
21
|
rails-dom-testing (~> 2.0)
|
22
22
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
23
|
-
activesupport (6.1.4)
|
23
|
+
activesupport (6.1.4.1)
|
24
24
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
25
|
i18n (>= 1.6, < 2)
|
26
26
|
minitest (>= 5.1)
|
@@ -34,13 +34,13 @@ GEM
|
|
34
34
|
erubi (1.10.0)
|
35
35
|
i18n (1.8.10)
|
36
36
|
concurrent-ruby (~> 1.0)
|
37
|
-
loofah (2.
|
37
|
+
loofah (2.12.0)
|
38
38
|
crass (~> 1.0.2)
|
39
39
|
nokogiri (>= 1.5.9)
|
40
40
|
method_source (1.0.0)
|
41
41
|
mini_portile2 (2.6.1)
|
42
42
|
minitest (5.14.4)
|
43
|
-
nokogiri (1.12.
|
43
|
+
nokogiri (1.12.4)
|
44
44
|
mini_portile2 (~> 2.6.1)
|
45
45
|
racc (~> 1.4)
|
46
46
|
parallel (1.20.1)
|
@@ -56,11 +56,11 @@ GEM
|
|
56
56
|
rails-dom-testing (2.0.3)
|
57
57
|
activesupport (>= 4.2.0)
|
58
58
|
nokogiri (>= 1.6)
|
59
|
-
rails-html-sanitizer (1.
|
59
|
+
rails-html-sanitizer (1.4.2)
|
60
60
|
loofah (~> 2.3)
|
61
|
-
railties (6.1.4)
|
62
|
-
actionpack (= 6.1.4)
|
63
|
-
activesupport (= 6.1.4)
|
61
|
+
railties (6.1.4.1)
|
62
|
+
actionpack (= 6.1.4.1)
|
63
|
+
activesupport (= 6.1.4.1)
|
64
64
|
method_source
|
65
65
|
rake (>= 0.13)
|
66
66
|
thor (~> 1.0)
|
data/README.md
CHANGED
@@ -61,6 +61,7 @@ we have reckoned with to this point.
|
|
61
61
|
* [test_data:configure](#test_dataconfigure)
|
62
62
|
* [test_data:verify_config](#test_dataverify_config)
|
63
63
|
* [test_data:initialize](#test_datainitialize)
|
64
|
+
* [test_data:reinitialize](#test_datareinitialize)
|
64
65
|
* [test_data:dump](#test_datadump)
|
65
66
|
* [test_data:load](#test_dataload)
|
66
67
|
* [test_data:create_database](#test_datacreate_database)
|
@@ -430,23 +431,21 @@ to update your test data. Here's how to do it:
|
|
430
431
|
1. Be sure there's nothing in your local `test_data` database that you added
|
431
432
|
intentionally and forgot to dump, because it's about to be erased
|
432
433
|
|
433
|
-
2. Run `rake test_data:
|
434
|
+
2. Run `rake test_data:reinitialize` drop and recreate the `test_data`
|
435
|
+
database and load the latest SQL dumps into it
|
434
436
|
|
435
|
-
3. Run
|
436
|
-
the latest SQL dumps into it
|
437
|
+
3. Run any pending migrations with `RAILS_ENV=test_data bin/rake db:migrate`
|
437
438
|
|
438
|
-
4.
|
439
|
-
|
440
|
-
5. If you need to create any additional data, start up the server
|
439
|
+
4. If you need to create any additional data, start up the server
|
441
440
|
(`RAILS_ENV=test_data bin/rails s`), just like in [Step
|
442
441
|
2](#step-2-create-some-test-data)
|
443
442
|
|
444
|
-
|
443
|
+
5. Export your newly-updated `test_data` database with `rake test_data:dump`
|
445
444
|
|
446
|
-
|
445
|
+
6. Ensure your tests are passing and then commit the resulting SQL files
|
447
446
|
|
448
447
|
* If the local `test_data` database is already up-to-date with the current SQL
|
449
|
-
dumps, follow steps **
|
448
|
+
dumps, follow steps **3 through 6** above
|
450
449
|
|
451
450
|
It's important to keep in mind that your test data SQL dumps are a shared,
|
452
451
|
generated resource among your team (just like a `structure.sql` or `schema.rb`
|
@@ -664,6 +663,18 @@ your seed file. Specifically:
|
|
664
663
|
* Otherwise, it invokes the task `db:schema:load` and `db:seed` (similar to
|
665
664
|
Rails' built-in `db:setup` task)
|
666
665
|
|
666
|
+
### test_data:reinitialize
|
667
|
+
|
668
|
+
This task is designed for the situation where you may already have a `test_data`
|
669
|
+
database created and simply want to drop it and replace it with whatever dumps
|
670
|
+
are in the `test/support/test_data` directory.
|
671
|
+
|
672
|
+
Dropping the database requires confirmation, either interactively or by setting
|
673
|
+
the environment variable `TEST_DATA_CONFIRM`. It will additionally warn you in
|
674
|
+
the event that the local database appears to be newer than the dumps on disk
|
675
|
+
that would replace it. From there, this task behaves the same way as `rake
|
676
|
+
test_data:initialize`.
|
677
|
+
|
667
678
|
### test_data:dump
|
668
679
|
|
669
680
|
This task is designed to be run after you've created or updated your test data
|
@@ -775,8 +786,8 @@ There are two additional things to keep in mind if using this method:
|
|
775
786
|
|
776
787
|
1. Using this feature requires that you've first invoked
|
777
788
|
[TestData.prevent_rails_fixtures_from_loading_automatically!](#testdataprevent_rails_fixtures_from_loading_automatically)
|
778
|
-
to override Rails' default behavior
|
779
|
-
started running
|
789
|
+
before your tests have started running to override Rails' default behavior
|
790
|
+
before any of your tests have loaded or started running
|
780
791
|
|
781
792
|
2. Because the method depends on Rails' fixture caching mechanism, it must be
|
782
793
|
passed an instance of the running test class (e.g.
|
@@ -1227,20 +1238,9 @@ result in a significantly slower overall test suite. Instead, if you group tests
|
|
1227
1238
|
that use the same type of test data together (e.g. by separating them into
|
1228
1239
|
separate suites), you might find profound speed gains.
|
1229
1240
|
|
1230
|
-
To illustrate
|
1231
|
-
and 5 that
|
1232
|
-
|
1233
|
-
the only operation needed by the second call will be a rollback to the savepoint
|
1234
|
-
taken after the test data was loaded. If, however, a `uses_test_data` test is
|
1235
|
-
followed by a `uses_rails_fixtures` test, then a lot more work is required:
|
1236
|
-
first a rollback, then the truncation of the test data, then a load of the
|
1237
|
-
fixtures followed by creation of a new savepoint—which would in tunr be undone
|
1238
|
-
again if the _next_ test happened to call `uses_test_data`. Switching between
|
1239
|
-
tests that use different sources of test data can cause significant unnecessary
|
1240
|
-
thrashing.
|
1241
|
-
|
1242
|
-
To illustrate the above, if all of these tests ran in random order (the
|
1243
|
-
default), you might see:
|
1241
|
+
To illustrate this, suppose you had 5 tests that relied on your `test_data` data
|
1242
|
+
and 5 that relied on Rails fixtures. If all of these tests ran in random order
|
1243
|
+
(the default), you might see the following behavior at run-time:
|
1244
1244
|
|
1245
1245
|
```
|
1246
1246
|
$ bin/rails test test/example_test.rb
|
@@ -1272,9 +1272,11 @@ data into sub-suites that are run serially, on e after the other.
|
|
1272
1272
|
[minitest-suite](https://github.com/testdouble/minitest-suite) to accomplish
|
1273
1273
|
exactly this. Just declare something like `suite :test_data` or `suite
|
1274
1274
|
:fixtures` at the top of each test class
|
1275
|
-
* If you're using RSpec, the
|
1276
|
-
|
1277
|
-
|
1275
|
+
* If you're using RSpec, the [suite option combined with a custom
|
1276
|
+
ordering](https://gist.github.com/myronmarston/8fea012b9eb21b637335bb29069bce6b)
|
1277
|
+
can accomplish this. You might also consider using
|
1278
|
+
[tags](https://relishapp.com/rspec/rspec-core/v/3-10/docs/command-line/tag-option)
|
1279
|
+
to organize your tests by type, but you'll likely have to
|
1278
1280
|
run a separate CLI invocation for each to avoid the tests from being
|
1279
1281
|
interleaved
|
1280
1282
|
|
data/example/Gemfile.lock
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
module TestData
|
2
|
+
class DetectsDatabaseExistence
|
3
|
+
def initialize
|
4
|
+
@config = TestData.config
|
5
|
+
end
|
6
|
+
|
7
|
+
def call
|
8
|
+
rows = ActiveRecord::Base.connection.execute <<~SQL
|
9
|
+
select datname database_name
|
10
|
+
from pg_catalog.pg_database
|
11
|
+
SQL
|
12
|
+
rows.any? { |row|
|
13
|
+
row["database_name"] == @config.database_name
|
14
|
+
}
|
15
|
+
rescue ActiveRecord::NoDatabaseError
|
16
|
+
false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module TestData
|
2
|
+
class DeterminesDatabasesAssociatedDumpTime
|
3
|
+
def call
|
4
|
+
if (last_dumped_at = ActiveRecord::InternalMetadata.find_by(key: "test_data:last_dumped_at")&.value)
|
5
|
+
Time.parse(last_dumped_at)
|
6
|
+
end
|
7
|
+
rescue ActiveRecord::StatementInvalid
|
8
|
+
# This will be raised if the DB exists but hasn't been migrated/schema-loaded
|
9
|
+
rescue ActiveRecord::NoDatabaseError
|
10
|
+
# This will be raised if the DB doesn't exist yet, which we don't need to warn about
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module TestData
|
2
|
+
class DeterminesWhenSqlDumpWasMade
|
3
|
+
def initialize
|
4
|
+
@config = TestData.config
|
5
|
+
end
|
6
|
+
|
7
|
+
def call
|
8
|
+
if (last_dumped_at = find_last_dumped_value)
|
9
|
+
Time.zone.parse(last_dumped_at)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def find_last_dumped_value
|
16
|
+
return unless File.exist?(@config.non_test_data_dump_path)
|
17
|
+
File.open(@config.non_test_data_dump_path, "r").each_line do |line|
|
18
|
+
if (match = line.match(/INSERT INTO public\.ar_internal_metadata VALUES \('test_data:last_dumped_at', '([^']*)'/))
|
19
|
+
return match[1]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -6,9 +6,12 @@ module TestData
|
|
6
6
|
class DumpsDatabase
|
7
7
|
def initialize
|
8
8
|
@config = TestData.config
|
9
|
+
@records_dump_metadata = RecordsDumpMetadata.new
|
9
10
|
end
|
10
11
|
|
11
12
|
def call
|
13
|
+
@records_dump_metadata.call
|
14
|
+
|
12
15
|
dump(
|
13
16
|
type: :schema,
|
14
17
|
database_name: @config.database_name,
|
data/lib/test_data/railtie.rb
CHANGED
data/lib/test_data/rake.rb
CHANGED
@@ -10,17 +10,18 @@ def run_in_test_data_env(task_name)
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def create_database_or_else_blow_up_if_its_not_empty!
|
13
|
+
raise unless Rails.env.test_data?
|
14
|
+
|
15
|
+
unless TestData::DetectsDatabaseExistence.new.call
|
16
|
+
Rake::Task["test_data:create_database"].invoke
|
17
|
+
end
|
18
|
+
|
13
19
|
unless TestData::DetectsDatabaseEmptiness.new.empty?
|
14
20
|
raise TestData::Error.new("Database '#{TestData.config.database_name}' already exists and is not empty. To re-initialize it, drop it first (e.g. `rake test_data:drop_database`)")
|
15
21
|
end
|
16
|
-
rescue TestData::Error => e
|
17
|
-
raise e
|
18
|
-
rescue
|
19
|
-
# Only (anticipated) cause for raise here is DB did not exist
|
20
|
-
Rake::Task["test_data:create_database"].invoke
|
21
22
|
end
|
22
23
|
|
23
|
-
desc "Verifies test_data environment looks good"
|
24
|
+
desc "Verifies that the test_data environment looks good"
|
24
25
|
task "test_data:verify_config" do
|
25
26
|
TestData.log.with_plain_writer do
|
26
27
|
config = TestData::VerifiesConfiguration.new.call
|
@@ -34,12 +35,12 @@ task "test_data:verify_config" do
|
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
|
-
desc "
|
38
|
+
desc "Installs default configuration files and snippets"
|
38
39
|
task "test_data:configure" do
|
39
40
|
TestData::InstallsConfiguration.new.call
|
40
41
|
end
|
41
42
|
|
42
|
-
desc "
|
43
|
+
desc "Initializes test_data's interactive database"
|
43
44
|
task "test_data:initialize" => ["test_data:verify_config", :environment] do
|
44
45
|
next run_in_test_data_env("test_data:initialize") if wrong_env?
|
45
46
|
|
@@ -59,7 +60,36 @@ task "test_data:initialize" => ["test_data:verify_config", :environment] do
|
|
59
60
|
MSG
|
60
61
|
end
|
61
62
|
|
62
|
-
desc "
|
63
|
+
desc "Re-initializes test_data's interactive database (by dropping and reloading it)"
|
64
|
+
task "test_data:reinitialize" => ["test_data:verify_config", :environment] do
|
65
|
+
next run_in_test_data_env("test_data:reinitialize") if wrong_env?
|
66
|
+
|
67
|
+
# Take caution only if the test_data database exists
|
68
|
+
if TestData::DetectsDatabaseExistence.new.call
|
69
|
+
TestData::WarnsIfDatabaseIsNewerThanDump.new.call
|
70
|
+
|
71
|
+
unless ENV["TEST_DATA_CONFIRM"].present?
|
72
|
+
confirmed = if $stdin.isatty
|
73
|
+
puts "This will DROP test_data database '#{TestData.config.database_name}'. Are you sure you want to re-initialize it? [yN]"
|
74
|
+
$stdin.gets.chomp.downcase.start_with?("y")
|
75
|
+
else
|
76
|
+
puts "'#{TestData.config.database_name}' exists. Set TEST_DATA_CONFIRM=true to drop the database and re-initialize it."
|
77
|
+
false
|
78
|
+
end
|
79
|
+
|
80
|
+
unless confirmed
|
81
|
+
puts "Exiting without making any changes"
|
82
|
+
exit 1
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
Rake::Task["test_data:drop_database"].invoke
|
87
|
+
end
|
88
|
+
|
89
|
+
Rake::Task["test_data:initialize"].invoke
|
90
|
+
end
|
91
|
+
|
92
|
+
desc "Initializes test_data Rails environment & configure database"
|
63
93
|
task "test_data:install" => ["test_data:configure", "test_data:initialize"]
|
64
94
|
|
65
95
|
desc "Dumps the interactive test_data database"
|
data/lib/test_data/version.rb
CHANGED
@@ -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
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
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-
|
11
|
+
date: 2021-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -136,6 +136,9 @@ files:
|
|
136
136
|
- lib/test_data/custom_loaders/abstract_base.rb
|
137
137
|
- lib/test_data/custom_loaders/rails_fixtures.rb
|
138
138
|
- lib/test_data/detects_database_emptiness.rb
|
139
|
+
- lib/test_data/detects_database_existence.rb
|
140
|
+
- lib/test_data/determines_databases_associated_dump_time.rb
|
141
|
+
- lib/test_data/determines_when_sql_dump_was_made.rb
|
139
142
|
- lib/test_data/dumps_database.rb
|
140
143
|
- lib/test_data/error.rb
|
141
144
|
- lib/test_data/generator_support.rb
|
@@ -146,12 +149,15 @@ files:
|
|
146
149
|
- lib/test_data/manager.rb
|
147
150
|
- lib/test_data/railtie.rb
|
148
151
|
- lib/test_data/rake.rb
|
152
|
+
- lib/test_data/records_dump_metadata.rb
|
149
153
|
- lib/test_data/save_point.rb
|
150
154
|
- lib/test_data/statistics.rb
|
151
155
|
- lib/test_data/truncates_test_data.rb
|
152
156
|
- lib/test_data/verifies_configuration.rb
|
153
157
|
- lib/test_data/verifies_dumps_are_loadable.rb
|
154
158
|
- lib/test_data/version.rb
|
159
|
+
- lib/test_data/warns_if_database_is_newer_than_dump.rb
|
160
|
+
- lib/test_data/warns_if_dump_is_newer_than_database.rb
|
155
161
|
- script/reset_example_app
|
156
162
|
- script/test
|
157
163
|
- test_data.gemspec
|