Class: TestData::Manager
- Inherits:
-
Object
- Object
- TestData::Manager
- Defined in:
- lib/test_data/manager.rb
Instance Method Summary collapse
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #load ⇒ Object
- #load_custom_data(loader, **options) ⇒ Object
- #rollback_to_before_data_load ⇒ Object
- #truncate ⇒ Object
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
3 4 5 6 7 8 9 |
# File 'lib/test_data/manager.rb', line 3 def initialize @inserts_test_data = InsertsTestData.new @truncates_test_data = TruncatesTestData.new @config = TestData.config @statistics = TestData.statistics @save_points = [] end |
Instance Method Details
#load ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/test_data/manager.rb', line 11 def load ensure_after_load_save_point_is_active_if_data_is_loaded! return rollback_to_after_data_load if save_point_active?(:after_data_load) create_save_point(:before_data_load) @inserts_test_data.call @config.after_test_data_load_hook.call create_save_point(:after_data_load) end |
#load_custom_data(loader, **options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/test_data/manager.rb', line 50 def load_custom_data(loader, **) loader.validate!(**) snapshot_name = "user_#{loader.name}".to_sym ensure_after_load_save_point_is_active_if_data_is_loaded! ensure_after_truncate_save_point_is_active_if_data_is_truncated! ensure_custom_save_point_is_active_if_memo_exists!(snapshot_name) loader.load_requested(**) if save_point_active?(snapshot_name) && loader.loaded?(**) return rollback_to_custom_savepoint(snapshot_name) end if save_point_active?(:after_data_truncate) rollback_to_after_data_truncate else truncate end loader.load(**) (snapshot_name) create_save_point(snapshot_name) end |
#rollback_to_before_data_load ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/test_data/manager.rb', line 74 def rollback_to_before_data_load if save_point_active?(:before_data_load) rollback_save_point(:before_data_load) # No need to recreate the save point # (TestData.uses_test_data will if called) end end |
#truncate ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/test_data/manager.rb', line 22 def truncate ensure_after_load_save_point_is_active_if_data_is_loaded! ensure_after_truncate_save_point_is_active_if_data_is_truncated! return rollback_to_after_data_truncate if save_point_active?(:after_data_truncate) if save_point_active?(:after_data_load) # If a test that uses the test data runs before a test that starts by # calling truncate, tables in the database that would NOT be truncated # may have been changed. To avoid this category of test pollution, start # the truncation by rolling back to the known clean point rollback_to_after_data_load else # Seems silly loading data when the user asked us to truncate, but # it's important that the state of the transaction stack matches the # mental model we advertise, because any _other_ test in their suite # should expect that the existence of :after_data_truncate save point # implies that it's safe to rollback to the :after_data_load save # point; since tests run in random order, it's likely to happen TestData.log.debug("TestData.uses_clean_slate was called, but data was not loaded. Loading data before truncate to preserve the transaction save point ordering") load end @truncates_test_data.call @config.after_test_data_truncate_hook.call create_save_point(:after_data_truncate) end |