def test_from_file(self): """Test the `Config.from_file` class method. Regression test for #3790: make sure configuration is written to disk after it is loaded and migrated. """ # If the config file does not exist, a completely new file is created with a migrated config filepath_nonexisting = os.path.join(self.config_filebase, 'config_nonexisting.json') config = Config.from_file(filepath_nonexisting) # Make sure that the migrated config is written to disk, by loading it from disk and comparing to the content # of the in memory config object. self.compare_config_in_memory_and_on_disk(config, filepath_nonexisting) # Now repeat the test for an existing file. The previous filepath now *does* exist and is migrated filepath_existing = filepath_nonexisting config = Config.from_file(filepath_existing) self.compare_config_in_memory_and_on_disk(config, filepath_existing) # Finally, we test that an existing configuration file with an outdated schema is migrated and written to disk with tempfile.NamedTemporaryFile() as handle: # Write content of configuration with old schema to disk filepath = os.path.join(os.path.dirname(__file__), 'migrations', 'test_samples', 'input', '0.json') with open(filepath, 'rb') as source: handle.write(source.read()) handle.flush() config = Config.from_file(handle.name) self.compare_config_in_memory_and_on_disk(config, handle.name)
def test_from_file_no_migrate(self): """Test that ``Config.from_file`` does not overwrite if the content was not migrated.""" from time import sleep # Construct the ``Config`` instance and write it to disk Config(self.config_filepath, self.config_dictionary).store() timestamp = os.path.getmtime(self.config_filepath) Config.from_file(self.config_filepath) # Sleep a second, because for some operating systems the time resolution is of the order of a second sleep(1) assert os.path.getmtime(self.config_filepath) == timestamp