Best Python code snippet using pandera_python
test_files.py
Source:test_files.py
1# -*- coding: utf-8 -*-2"""3Unit Tests for functions located in salt/utils/files.py4"""5# Import python libs6from __future__ import absolute_import, print_function, unicode_literals7import copy8import os9# Import Salt libs10import salt.utils.files11from salt.ext import six12# Import Salt Testing libs13from tests.support.helpers import with_tempdir14from tests.support.mock import patch15from tests.support.unit import TestCase, skipIf16class FilesTestCase(TestCase):17 """18 Test case for files util.19 """20 def test_safe_rm(self):21 with patch("os.remove") as os_remove_mock:22 salt.utils.files.safe_rm("dummy_tgt")23 self.assertTrue(os_remove_mock.called)24 @skipIf(25 os.path.exists("/tmp/no_way_this_is_a_file_nope.sh"),26 "Test file exists! Skipping safe_rm_exceptions test!",27 )28 def test_safe_rm_exceptions(self):29 error = False30 try:31 salt.utils.files.safe_rm("/tmp/no_way_this_is_a_file_nope.sh")32 except (IOError, OSError):33 error = True34 self.assertFalse(35 error, "salt.utils.files.safe_rm raised exception when it should not have"36 )37 @with_tempdir()38 def test_safe_walk_symlink_recursion(self, tmp):39 if os.stat(tmp).st_ino == 0:40 self.skipTest("inodes not supported in {0}".format(tmp))41 os.mkdir(os.path.join(tmp, "fax"))42 os.makedirs(os.path.join(tmp, "foo", "bar"))43 os.symlink(os.path.join("..", ".."), os.path.join(tmp, "foo", "bar", "baz"))44 os.symlink("foo", os.path.join(tmp, "root"))45 expected = [46 (os.path.join(tmp, "root"), ["bar"], []),47 (os.path.join(tmp, "root", "bar"), ["baz"], []),48 (os.path.join(tmp, "root", "bar", "baz"), ["fax", "foo", "root"], []),49 (os.path.join(tmp, "root", "bar", "baz", "fax"), [], []),50 ]51 paths = []52 for root, dirs, names in salt.utils.files.safe_walk(os.path.join(tmp, "root")):53 paths.append((root, sorted(dirs), names))54 if paths != expected:55 raise AssertionError(56 "\n".join(57 ["got:"]58 + [repr(p) for p in paths]59 + ["", "expected:"]60 + [repr(p) for p in expected]61 )62 )63 @skipIf(not six.PY3, "This test only applies to Python 3")64 def test_fopen_with_disallowed_fds(self):65 """66 This is safe to have as a unit test since we aren't going to actually67 try to read or write. We want to ensure that we are raising a68 TypeError. Python 3's open() builtin will treat the booleans as file69 descriptor numbers and try to open stdin/stdout. We also want to test70 fd 2 which is stderr.71 """72 for invalid_fn in (False, True, 0, 1, 2):73 try:74 with salt.utils.files.fopen(invalid_fn):75 pass76 except TypeError:77 # This is expected. We aren't using an assertRaises here78 # because we want to ensure that if we did somehow open the79 # filehandle, that it doesn't remain open.80 pass81 else:82 # We probably won't even get this far if we actually opened83 # stdin/stdout as a file descriptor. It is likely to cause the84 # integration suite to die since, news flash, closing85 # stdin/stdout/stderr is usually not a wise thing to do in the86 # middle of a program's execution.87 self.fail(88 "fopen() should have been prevented from opening a file "89 "using {0} as the filename".format(invalid_fn)90 )91 def _create_temp_structure(self, temp_directory, structure):92 for folder, files in six.iteritems(structure):93 current_directory = os.path.join(temp_directory, folder)94 os.makedirs(current_directory)95 for name, content in six.iteritems(files):96 path = os.path.join(temp_directory, folder, name)97 with salt.utils.files.fopen(path, "w+") as fh:98 fh.write(content)99 def _validate_folder_structure_and_contents(100 self, target_directory, desired_structure101 ):102 for folder, files in six.iteritems(desired_structure):103 for name, content in six.iteritems(files):104 path = os.path.join(target_directory, folder, name)105 with salt.utils.files.fopen(path) as fh:106 assert fh.read().strip() == content107 @with_tempdir()108 @with_tempdir()109 def test_recursive_copy(self, src, dest):110 src_structure = {111 "foo": {"foofile.txt": "fooSTRUCTURE"},112 "bar": {"barfile.txt": "barSTRUCTURE"},113 }114 dest_structure = {115 "foo": {"foo.txt": "fooTARGET_STRUCTURE"},116 "baz": {"baz.txt": "bazTARGET_STRUCTURE"},117 }118 # Create the file structures in both src and dest dirs119 self._create_temp_structure(src, src_structure)120 self._create_temp_structure(dest, dest_structure)121 # Perform the recursive copy122 salt.utils.files.recursive_copy(src, dest)123 # Confirm results match expected results124 desired_structure = copy.copy(dest_structure)125 desired_structure.update(src_structure)...
next.py
Source:next.py
...23 return dico24 def set_fn(dico, key, val):25 dico.update({key:val})26 return dico27 def invalid_fn(*args):28 print("> invalid command")29 return dico30 actions = {31 "help":print_help,32 "get":get_fn,33 "set":set_fn,34 "invalid":invalid_fn35 }36 37 action = actions[cmd]38 dico = action(dico, *args)39 return dico40def loop(dico):41 line = input(">>> ")...
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!!