Best Python code snippet using avocado_python
test_datadir.py
Source:test_datadir.py
...3import unittest.mock4from avocado.core import settings5from .. import temp_dir_prefix6class Base(unittest.TestCase):7 def _get_temp_dirs_mapping_and_config(self):8 """9 Creates a temporary bogus base data dir10 And returns a dictionary containing the temporary data dir paths and11 a the path to a configuration file contain those same settings12 """13 prefix = temp_dir_prefix(__name__, self, 'setUp')14 base_dir = tempfile.TemporaryDirectory(prefix=prefix)15 test_dir = os.path.join(base_dir.name, 'tests')16 os.mkdir(test_dir)17 mapping = {'base_dir': base_dir.name,18 'test_dir': test_dir,19 'data_dir': os.path.join(base_dir.name, 'data'),20 'logs_dir': os.path.join(base_dir.name, 'logs')}21 temp_settings = ('[datadir.paths]\n'22 'base_dir = %(base_dir)s\n'23 'test_dir = %(test_dir)s\n'24 'data_dir = %(data_dir)s\n'25 'logs_dir = %(logs_dir)s\n') % mapping26 config_file = tempfile.NamedTemporaryFile('w', delete=False)27 config_file.write(temp_settings)28 config_file.close()29 return (base_dir, mapping, config_file.name)30 def setUp(self):31 (self.base_dir,32 self.mapping,33 self.config_file_path) = self._get_temp_dirs_mapping_and_config()34 def tearDown(self):35 os.unlink(self.config_file_path)36 self.base_dir.cleanup()37class DataDirTest(Base):38 def test_datadir_from_config(self):39 """40 When avocado.conf is present, honor the values coming from it.41 """42 stg = settings.Settings()43 with unittest.mock.patch('avocado.core.stgs', stg):44 import avocado.core45 avocado.core.register_core_options()46 stg.process_config_path(self.config_file_path)47 stg.merge_with_configs()48 with unittest.mock.patch('avocado.core.data_dir.settings', stg):49 from avocado.core import data_dir50 for key in self.mapping.keys():51 data_dir_func = getattr(data_dir, 'get_%s' % key)52 namespace = 'datadir.paths.{}'.format(key)53 self.assertEqual(data_dir_func(), stg.as_dict().get(namespace))54 def test_unique_log_dir(self):55 """56 Tests that multiple queries for a logdir at the same time provides57 unique results.58 """59 from avocado.core import data_dir60 with unittest.mock.patch('avocado.core.data_dir.time.strftime',61 return_value="date_would_go_here"):62 logdir = os.path.join(self.mapping['base_dir'], "foor", "bar", "baz")63 path_prefix = os.path.join(logdir, "job-date_would_go_here-")64 uid = "1234567890"*465 for i in range(7, 40):66 path = data_dir.create_job_logs_dir(logdir, uid)67 self.assertEqual(path, path_prefix + uid[:i])68 self.assertTrue(os.path.exists(path))69 path = data_dir.create_job_logs_dir(logdir, uid)70 self.assertEqual(path, path_prefix + uid + ".0")71 self.assertTrue(os.path.exists(path))72 path = data_dir.create_job_logs_dir(logdir, uid)73 self.assertEqual(path, path_prefix + uid + ".1")74 self.assertTrue(os.path.exists(path))75 def test_get_job_results_dir(self):76 from avocado.core import data_dir77 from avocado.core import job_id78 # First let's mock a jobs results directory79 #80 logs_dir = self.mapping.get('logs_dir')81 self.assertNotEqual(None, logs_dir)82 unique_id = job_id.create_unique_job_id()83 # Expected job results dir84 expected_jrd = data_dir.create_job_logs_dir(logs_dir, unique_id)85 # Now let's test some cases86 #87 self.assertEqual(None,88 data_dir.get_job_results_dir(expected_jrd, logs_dir),89 ("If passing a directory reference, it expects the id"90 "file"))91 # Create the id file.92 id_file_path = os.path.join(expected_jrd, 'id')93 with open(id_file_path, 'w') as id_file:94 id_file.write("%s\n" % unique_id)95 id_file.flush()96 os.fsync(id_file)97 self.assertEqual(expected_jrd,98 data_dir.get_job_results_dir(expected_jrd, logs_dir),99 "It should get from the path to the directory")100 results_dirname = os.path.basename(expected_jrd)101 self.assertEqual(None,102 data_dir.get_job_results_dir(results_dirname,103 logs_dir),104 "It should not get from a valid path to the directory")105 pwd = os.getcwd()106 os.chdir(logs_dir)107 self.assertEqual(expected_jrd,108 data_dir.get_job_results_dir(results_dirname,109 logs_dir),110 "It should get from relative path to the directory")111 os.chdir(pwd)112 self.assertEqual(expected_jrd,113 data_dir.get_job_results_dir(id_file_path, logs_dir),114 "It should get from the path to the id file")115 self.assertEqual(expected_jrd,116 data_dir.get_job_results_dir(unique_id, logs_dir),117 "It should get from the id")118 another_id = job_id.create_unique_job_id()119 self.assertNotEqual(unique_id, another_id)120 self.assertEqual(None,121 data_dir.get_job_results_dir(another_id, logs_dir),122 "It should not get from unexisting job")123 self.assertEqual(expected_jrd,124 data_dir.get_job_results_dir(unique_id[:7], logs_dir),125 "It should get from partial id equals to 7 digits")126 self.assertEqual(expected_jrd,127 data_dir.get_job_results_dir(unique_id[:4], logs_dir),128 "It should get from partial id less than 7 digits")129 almost_id = unique_id[:7] + ('a' * (len(unique_id) - 7))130 self.assertNotEqual(unique_id, almost_id)131 self.assertEqual(None,132 data_dir.get_job_results_dir(almost_id, logs_dir),133 ("It should not get if the id is equal on only"134 "the first 7 characters"))135 os.symlink(expected_jrd, os.path.join(logs_dir, 'latest'))136 self.assertEqual(expected_jrd,137 data_dir.get_job_results_dir('latest', logs_dir),138 "It should get from the 'latest' id")139 stg = settings.Settings()140 with unittest.mock.patch('avocado.core.stgs', stg):141 import avocado.core142 avocado.core.register_core_options()143 stg.process_config_path(self.config_file_path)144 stg.merge_with_configs()145 with unittest.mock.patch('avocado.core.data_dir.settings',146 stg):147 self.assertEqual(expected_jrd,148 data_dir.get_job_results_dir(unique_id),149 "It should use the default base logs directory")150class AltDataDirTest(Base):151 def test_settings_dir_alternate_dynamic(self):152 """153 Tests that changes to the data_dir settings are applied dynamically154 To guarantee that, first the data_dir module is loaded. Then a new,155 alternate set of data directories are created and set in the156 "canonical" settings location, that is, avocado.core.settings.settings.157 No data_dir module reload should be necessary to get the new locations158 from data_dir APIs.159 """160 # Initial settings with initial data_dir locations161 stg = settings.Settings()162 with unittest.mock.patch('avocado.core.stgs', stg):163 import avocado.core164 avocado.core.register_core_options()165 stg.process_config_path(self.config_file_path)166 stg.merge_with_configs()167 with unittest.mock.patch('avocado.core.data_dir.settings', stg):168 from avocado.core import data_dir169 for key in self.mapping.keys():170 data_dir_func = getattr(data_dir, 'get_%s' % key)171 self.assertEqual(data_dir_func(), self.mapping[key])172 (self.alt_base_dir, # pylint: disable=W0201173 alt_mapping,174 # pylint: disable=W0201175 self.alt_config_file_path) = self._get_temp_dirs_mapping_and_config()176 # Alternate settings with different data_dir location177 alt_stg = settings.Settings()178 with unittest.mock.patch('avocado.core.stgs', alt_stg):179 import avocado.core180 avocado.core.register_core_options()181 alt_stg.process_config_path(self.alt_config_file_path)182 alt_stg.merge_with_configs()183 with unittest.mock.patch('avocado.core.data_dir.settings', alt_stg):184 for key in alt_mapping.keys():185 data_dir_func = getattr(data_dir, 'get_%s' % key)186 self.assertEqual(data_dir_func(), alt_mapping[key])187 def tearDown(self):188 super(AltDataDirTest, self).tearDown()189 os.unlink(self.alt_config_file_path)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!