test_data 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84a9fc3fe7b3dda0cf1ebdc775c9191757d21de11e033189c6450b91f63332e7
4
- data.tar.gz: 0a47179f0607d9c57f63706e35de176b14d93989c6e5406094524a0fb3b09704
3
+ metadata.gz: 4178759cb111f2f83cad983712c1b82220aa09ecdbd7d710b7517974c63fee8c
4
+ data.tar.gz: dc83bc5526b88239ba2d55661a8ab61865a7a8d19f8c195d2caa41e00125b7e5
5
5
  SHA512:
6
- metadata.gz: 4c10de3f9e234ae53e47553d099612dc7474cb3e98022b6a953d60beb8da3f502b00f506f29729dad0a9eb37c8e7f05a662263963300d7c531f48fef660c9173
7
- data.tar.gz: d718e34070ca15bd9d0e27c75789e170e42da183fac8efc973404d9cea909ce984453f9393277b37ad36ff42fa4866c66de14d2a82a149ae536f0555c2f5f52d
6
+ metadata.gz: 7aba197cb61bff3286913de3678a6df0f4595774bb7283228de6e26ae64fb03586e8233362bf9cea3ef041cc1e53a539a323e3c5d59fb6d16470f6db904402e5
7
+ data.tar.gz: bc49d93f8dc68d09612ba36220dd7f380a2f8cc4657f968dc9a8220d96f07532c912f119fed9e2d25ba0840b9480f82a12954ea30a54bd1c1fe357bea8920154
data/CHANGELOG.md CHANGED
@@ -1,4 +1,22 @@
1
- # unreleased
1
+ # 0.3.1
2
+
3
+ - Loosen railties dependencies after verifying basic Rails 7 support
4
+
5
+ # 0.3.0
6
+
7
+ - Add a `test_data:reinitialize` task that will delete the `test_data` database
8
+ if necessary before invoking `test_data:initialize`
9
+ - Warn if re-initializing and the local database appears to have been dumped
10
+ or loaded from a dump that is newer than the dumps on disk
11
+ - Add a warning on app load if the dumps on disk appear
12
+ newer than the local `test_data` database
13
+
14
+ # 0.2.2
15
+
16
+ - Improve performance of Rails fixtures being repeatedly loaded by changing the
17
+ caching strategy
18
+
19
+ # 0.2.1
2
20
 
3
21
  - Adds several lifecycle hooks:
4
22
  - config.after_test_data_load
data/Gemfile.lock CHANGED
@@ -1,96 +1,96 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- test_data (0.2.1)
5
- railties (~> 6.0)
4
+ test_data (0.3.1)
5
+ railties (>= 6.0, < 8.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)
13
- rack (~> 2.0, >= 2.0.9)
10
+ actionpack (7.0.2.3)
11
+ actionview (= 7.0.2.3)
12
+ activesupport (= 7.0.2.3)
13
+ rack (~> 2.0, >= 2.2.0)
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 (7.0.2.3)
18
+ activesupport (= 7.0.2.3)
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 (7.0.2.3)
24
24
  concurrent-ruby (~> 1.0, >= 1.0.2)
25
25
  i18n (>= 1.6, < 2)
26
26
  minitest (>= 5.1)
27
27
  tzinfo (~> 2.0)
28
- zeitwerk (~> 2.3)
29
28
  ast (2.4.2)
30
29
  builder (3.2.4)
31
30
  coderay (1.1.3)
32
- concurrent-ruby (1.1.9)
31
+ concurrent-ruby (1.1.10)
33
32
  crass (1.0.6)
34
33
  erubi (1.10.0)
35
- i18n (1.8.10)
34
+ i18n (1.10.0)
36
35
  concurrent-ruby (~> 1.0)
37
- loofah (2.10.0)
36
+ loofah (2.15.0)
38
37
  crass (~> 1.0.2)
39
38
  nokogiri (>= 1.5.9)
40
39
  method_source (1.0.0)
41
- mini_portile2 (2.5.3)
42
- minitest (5.14.4)
43
- nokogiri (1.11.7)
44
- mini_portile2 (~> 2.5.0)
40
+ mini_portile2 (2.8.0)
41
+ minitest (5.15.0)
42
+ nokogiri (1.13.3)
43
+ mini_portile2 (~> 2.8.0)
45
44
  racc (~> 1.4)
46
- parallel (1.20.1)
47
- parser (3.0.1.0)
45
+ parallel (1.22.1)
46
+ parser (3.1.1.0)
48
47
  ast (~> 2.4.1)
49
48
  pry (0.14.1)
50
49
  coderay (~> 1.1)
51
50
  method_source (~> 1.0)
52
- racc (1.5.2)
51
+ racc (1.6.0)
53
52
  rack (2.2.3)
54
53
  rack-test (1.1.0)
55
54
  rack (>= 1.0, < 3)
56
55
  rails-dom-testing (2.0.3)
57
56
  activesupport (>= 4.2.0)
58
57
  nokogiri (>= 1.6)
59
- rails-html-sanitizer (1.3.0)
58
+ rails-html-sanitizer (1.4.2)
60
59
  loofah (~> 2.3)
61
- railties (6.1.4)
62
- actionpack (= 6.1.4)
63
- activesupport (= 6.1.4)
60
+ railties (7.0.2.3)
61
+ actionpack (= 7.0.2.3)
62
+ activesupport (= 7.0.2.3)
64
63
  method_source
65
- rake (>= 0.13)
64
+ rake (>= 12.2)
66
65
  thor (~> 1.0)
67
- rainbow (3.0.0)
68
- rake (13.0.3)
69
- regexp_parser (2.1.1)
66
+ zeitwerk (~> 2.5)
67
+ rainbow (3.1.1)
68
+ rake (13.0.6)
69
+ regexp_parser (2.2.1)
70
70
  rexml (3.2.5)
71
- rubocop (1.12.1)
71
+ rubocop (1.26.0)
72
72
  parallel (~> 1.10)
73
- parser (>= 3.0.0.0)
73
+ parser (>= 3.1.0.0)
74
74
  rainbow (>= 2.2.2, < 4.0)
75
75
  regexp_parser (>= 1.8, < 3.0)
76
76
  rexml
77
- rubocop-ast (>= 1.2.0, < 2.0)
77
+ rubocop-ast (>= 1.16.0, < 2.0)
78
78
  ruby-progressbar (~> 1.7)
79
79
  unicode-display_width (>= 1.4.0, < 3.0)
80
- rubocop-ast (1.4.1)
81
- parser (>= 2.7.1.5)
82
- rubocop-performance (1.10.1)
83
- rubocop (>= 0.90.0, < 2.0)
80
+ rubocop-ast (1.16.0)
81
+ parser (>= 3.1.1.0)
82
+ rubocop-performance (1.13.3)
83
+ rubocop (>= 1.7.0, < 2.0)
84
84
  rubocop-ast (>= 0.4.0)
85
85
  ruby-progressbar (1.11.0)
86
- standard (1.0.5)
87
- rubocop (= 1.12.1)
88
- rubocop-performance (= 1.10.1)
89
- thor (1.1.0)
86
+ standard (1.9.0)
87
+ rubocop (= 1.26.0)
88
+ rubocop-performance (= 1.13.3)
89
+ thor (1.2.1)
90
90
  tzinfo (2.0.4)
91
91
  concurrent-ruby (~> 1.0)
92
- unicode-display_width (2.0.0)
93
- zeitwerk (2.4.2)
92
+ unicode-display_width (2.1.0)
93
+ zeitwerk (2.5.4)
94
94
 
95
95
  PLATFORMS
96
96
  ruby
@@ -103,4 +103,4 @@ DEPENDENCIES
103
103
  test_data!
104
104
 
105
105
  BUNDLED WITH
106
- 2.2.15
106
+ 2.3.10
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2021 Test Double, LLC
1
+ Copyright (c) 2021 Test Double, Inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
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)
@@ -375,17 +376,30 @@ RSpec.describe "Kitchen sink", type: :request do
375
376
  end
376
377
  ```
377
378
 
379
+ But wait, there's more! If your test suite switches between multiple modes from
380
+ test-to-test, it's important to be aware of the marginal cost _between_ each of
381
+ those tests. For example, two tests in a row that call `TestData.uses_test_data`
382
+ only need a simple rollback as test setup, but a `TestData.uses_test_data`
383
+ followed by a `TestData.uses_clean_slate` requires a rollback, a truncation, and
384
+ another savepoint. These small costs add up, so consider [speeding up your
385
+ build](#im-worried-my-tests-arent-as-fast-as-they-should-be) by grouping your
386
+ tests into sub-suites based on their source of test data.
387
+
378
388
  #### If your situation is more complicated
379
389
 
380
390
  If you're adding `test_data` to an existing application, it's likely that you
381
391
  won't be able to easily adopt a one-size-fits-all approach to test setup across
382
392
  your entire suite. Some points of reference, if that's the situation you're in:
383
393
 
384
- * If your test suite is already using fixtures or factories and the above hooks
385
- just broke everything, check out our [interoperability
394
+ * If your test suite is **already using fixtures or factories** and the above
395
+ hooks just broke everything, check out our [interoperability
386
396
  guide](#factory--fixture-interoperability-guide) for help.
387
- * If you don't want `test_data` managing transactions and cleanup for you and
388
- just want to load the SQL dump, you can call
397
+ * If you need to make any changes to the data after it's loaded, truncated, or
398
+ after Rails fixtures are loaded, you can configure [lifecycle
399
+ hooks](#lifecycle-hooks) that will help you achieve a **very fast test suite**
400
+ by including those changes inside the transaction savepoints
401
+ * If you **don't want `test_data` managing transactions** and cleanup for you
402
+ and just want to load the SQL dump, you can call
389
403
  [TestData.insert_test_data_dump](#testdatainsert_test_data_dump)
390
404
  * For more information on how all this works, see the [API
391
405
  reference](#api-reference).
@@ -417,23 +431,21 @@ to update your test data. Here's how to do it:
417
431
  1. Be sure there's nothing in your local `test_data` database that you added
418
432
  intentionally and forgot to dump, because it's about to be erased
419
433
 
420
- 2. Run `rake test_data:drop_database`
421
-
422
- 3. Run `rake test_data:load` to recreate the `test_data` database and load
423
- the latest SQL dumps into it
434
+ 2. Run `rake test_data:reinitialize` drop and recreate the `test_data`
435
+ database and load the latest SQL dumps into it
424
436
 
425
- 4. Run any pending migrations with `RAILS_ENV=test_data bin/rake db:migrate`
437
+ 3. Run any pending migrations with `RAILS_ENV=test_data bin/rake db:migrate`
426
438
 
427
- 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
428
440
  (`RAILS_ENV=test_data bin/rails s`), just like in [Step
429
441
  2](#step-2-create-some-test-data)
430
442
 
431
- 6. Export your newly-updated `test_data` database with `rake test_data:dump`
443
+ 5. Export your newly-updated `test_data` database with `rake test_data:dump`
432
444
 
433
- 7. Ensure your tests are passing and then commit the resulting SQL files
445
+ 6. Ensure your tests are passing and then commit the resulting SQL files
434
446
 
435
447
  * If the local `test_data` database is already up-to-date with the current SQL
436
- dumps, follow steps **4 through 7** above
448
+ dumps, follow steps **3 through 6** above
437
449
 
438
450
  It's important to keep in mind that your test data SQL dumps are a shared,
439
451
  generated resource among your team (just like a `structure.sql` or `schema.rb`
@@ -489,15 +501,15 @@ suite after following the initial setup guide and see if the suite just passes.
489
501
 
490
502
  If you find that your test suite is failing after adding
491
503
  `TestData.uses_test_data` to your setup, don't panic! Test failures are most
492
- likely caused by the combination of your `test_data` database with the data
493
- persisted by your factories.
504
+ likely caused by the combination of your `test_data` SQL dump with the records
505
+ inserted by your factories.
494
506
 
495
507
  One approach would be to attempt to resolve each such failure one-by-one—usually
496
508
  by updating the offending factories or editing your `test_data` database to
497
509
  ensure they steer clear of one another. Care should be taken to preserve the
498
- conceptual encapsulation of each test, however, as naively squashing errors can
499
- introduce inadvertent coupling between your factories and your `test_data`
500
- database such that neither can be used independently of the other.
510
+ conceptual encapsulation of each test, however, as naively squashing errors
511
+ risks introducing inadvertent coupling between your factories and your
512
+ `test_data` data such that neither can be used independently of the other.
501
513
 
502
514
  Another approach that the `test_data` gem provides is an additional mode with
503
515
  `TestData.uses_clean_slate`, which—when called at the top of a factory-dependent
@@ -651,6 +663,18 @@ your seed file. Specifically:
651
663
  * Otherwise, it invokes the task `db:schema:load` and `db:seed` (similar to
652
664
  Rails' built-in `db:setup` task)
653
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
+
654
678
  ### test_data:dump
655
679
 
656
680
  This task is designed to be run after you've created or updated your test data
@@ -762,8 +786,8 @@ There are two additional things to keep in mind if using this method:
762
786
 
763
787
  1. Using this feature requires that you've first invoked
764
788
  [TestData.prevent_rails_fixtures_from_loading_automatically!](#testdataprevent_rails_fixtures_from_loading_automatically)
765
- to override Rails' default behavior before any of your tests have loaded or
766
- 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
767
791
 
768
792
  2. Because the method depends on Rails' fixture caching mechanism, it must be
769
793
  passed an instance of the running test class (e.g.
@@ -1203,8 +1227,104 @@ run. That said—and especially if you're adding `test_data` to an existing test
1203
1227
  suite—care should be taken to audit everything the suite does between tests in
1204
1228
  order to optimize its overall runtime.
1205
1229
 
1206
- The first and most likely source of unnecessary slowness is redundant test
1207
- cleanup—the speed gained from sandwiching every expensive operation between
1230
+ #### Randomized test order leading to data churn
1231
+
1232
+ Generally speaking, randomizing the order in which tests run is an unmitigated
1233
+ win: randomizing helps you catch any unintended dependency between two tests
1234
+ early, when it's still cheap & easy to fix. However, if your tests use different
1235
+ sources of test data (e.g. some call `TestData.uses_test_data` and some call
1236
+ `TestData.uses_clean_slate`), it's very likely that randomizing your tests will
1237
+ result in a significantly slower overall test suite. Instead, if you group tests
1238
+ that use the same type of test data together (e.g. by separating them into
1239
+ separate suites), you might find profound speed gains.
1240
+
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
+
1245
+ ```
1246
+ $ bin/rails test test/example_test.rb
1247
+ Run options: --seed 63999
1248
+
1249
+ # Running:
1250
+
1251
+ test_data -- loading test_data SQL dump
1252
+ . fixtures -- truncating tables, loading Rails fixtures
1253
+ . fixtures -- rolling back to Rails fixtures
1254
+ . test_data -- rolling back to clean test_data
1255
+ . fixtures -- truncating tables, loading Rails fixtures
1256
+ . test_data -- rolling back to clean test_data
1257
+ . fixtures -- truncating tables, loading Rails fixtures
1258
+ . test_data -- rolling back to clean test_data
1259
+ . fixtures -- truncating tables, loading Rails fixtures
1260
+ . test_data -- rolling back to clean test_data
1261
+ .
1262
+
1263
+ Finished in 2.449957s, 4.0817 runs/s, 4.0817 assertions/s.
1264
+ 10 runs, 10 assertions, 0 failures, 0 errors, 0 skips
1265
+ ```
1266
+
1267
+ So, what can you do to speed this up? The most effective strategy to avoiding
1268
+ this churn is to group the execution of each tests that use each source of test
1269
+ data into sub-suites that are run serially, on e after the other.
1270
+
1271
+ * If you're using Rails' defualt Minitest, we wrote a gem called
1272
+ [minitest-suite](https://github.com/testdouble/minitest-suite) to accomplish
1273
+ exactly this. Just declare something like `suite :test_data` or `suite
1274
+ :fixtures` at the top of each test class
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
1280
+ run a separate CLI invocation for each to avoid the tests from being
1281
+ interleaved
1282
+
1283
+ Here's what the same example would do at run-time after adding
1284
+ [minitest-suite](https://github.com/testdouble/minitest-suite):
1285
+
1286
+ ```
1287
+ $ bin/rails test test/example_test.rb
1288
+ Run options: --seed 50105
1289
+
1290
+ # Running:
1291
+
1292
+ test_data -- loading test_data SQL dump
1293
+ . test_data -- rolling back to clean test_data
1294
+ . test_data -- rolling back to clean test_data
1295
+ . test_data -- rolling back to clean test_data
1296
+ . test_data -- rolling back to clean test_data
1297
+ . fixtures -- truncating tables, loading Rails fixtures
1298
+ . fixtures -- rolling back to clean fixtures
1299
+ . fixtures -- rolling back to clean fixtures
1300
+ . fixtures -- rolling back to clean fixtures
1301
+ . fixtures -- rolling back to clean fixtures
1302
+ .
1303
+
1304
+ Finished in 2.377050s, 4.2069 runs/s, 4.2069 assertions/s.
1305
+ 10 runs, 10 assertions, 0 failures, 0 errors, 0 skips
1306
+ ```
1307
+
1308
+ By grouping the execution in this way, the most expensive operations will
1309
+ usually only be run once: at the beginning of the first test in each suite.
1310
+
1311
+ #### Expensive data manipulation
1312
+
1313
+ If you're doing anything repeatedly that's data-intensive in your test setup
1314
+ after calling one of the `TestData.uses_*` methods, that operation is being
1315
+ repeated once per test, which could be very slow. Instead, you might consider
1316
+ moving that behavior into a [lifecycle hook](#lifecycle-hooks).
1317
+
1318
+ Any code passed to a lifecycle hook will only be executed when data is
1319
+ _actually_ loaded or truncated and its effect will be included in the
1320
+ transaction savepoint that the `test_data` gem rolls back between tests.
1321
+ Seriously, appropriately moving data adjustments into these hooks can cut your
1322
+ test suite's runtime by an order of magnitude.
1323
+
1324
+ #### Redundant test setup tasks
1325
+
1326
+ One of the most likely sources of unnecessary slowness is redundant test
1327
+ cleanup. The speed gained from sandwiching every expensive operation between
1208
1328
  transaction savepoints can be profound… but can also easily be erased by a
1209
1329
  single before-each hook calling
1210
1330
  [database_cleaner](https://github.com/DatabaseCleaner/database_cleaner) to
@@ -1213,32 +1333,6 @@ time to take stock of everything that's called between tests during setup &
1213
1333
  teardown to ensure multiple tools aren't attempting to clean up the state of the
1214
1334
  database and potentially interfering with one another.
1215
1335
 
1216
- A second opportunity for optimization is to group tests that use the same type
1217
- of test data together, either into separate suites or by preventing them from
1218
- being run in random order across said types. For example, suppose you have 10
1219
- tests that call `TestData.uses_test_data` and 10 that call
1220
- `TestData.uses_rails_fixtures`. If a test that calls `TestData.uses_test_data`
1221
- is followed by another that calls `uses_test_data`, the only operation needed by
1222
- the second call will be a rollback to the savepoint taken after the test data
1223
- was loaded. If, however, a `uses_test_data` test is followed by a
1224
- `uses_rails_fixtures` test, then the test data will be truncated and the
1225
- fixtures loaded and new savepoints created (which would then be undone again if
1226
- the _next_ test happened to call `uses_test_data`).
1227
-
1228
- As a result of the above, the marginal runtime cost for each `TestData.uses_*`
1229
- method depends on which kinds of test precedes and follows it. That means your
1230
- tests will run faster overall if the tests that call `TestData.uses_test_data`
1231
- are run as a group separately from your tests that rely on
1232
- `TestData.uses_clean_slate` or `TestData.uses_rails_fixtures`. Separating your
1233
- tests into logical groups pretty trivial if you're using RSpec, as the
1234
- [tag](https://relishapp.com/rspec/rspec-core/v/3-10/docs/command-line/tag-option)
1235
- feature was built with this sort of need in mind. If you're using Minitest, you
1236
- might consider organizing the tests in different directories and running
1237
- multiple commands to execute them (e.g. `bin/rails test test/test_data_tests`
1238
- and `bin/rails test/factory_tests`). Every CI configuration is different,
1239
- however, and you may find yourself needing to get creative in configuring things
1240
- to achieve the fastest build time.
1241
-
1242
1336
  ## Code of Conduct
1243
1337
 
1244
1338
  This project follows Test Double's [code of