Best Python code snippet using localstack_python
test_chown.py
Source:test_chown.py
...16 return_value=self.path17 )18 self.normalize_path = patcher.start()19 self.addCleanup(patcher.stop)20 # Mock the flutils.pathutils.get_os_user() return value21 patcher = patch(22 'flutils.pathutils.get_os_user',23 return_value=SimpleNamespace(pw_uid=9753)24 )25 self.get_os_user = patcher.start()26 self.addCleanup(patcher.stop)27 # Mock the flutils.pathutils.get_os_group() return value28 patcher = patch(29 'flutils.pathutils.get_os_group',30 return_value=SimpleNamespace(gr_gid=1357)31 )32 self.get_os_group = patcher.start()33 self.addCleanup(patcher.stop)34 # Mock os.chown35 patcher = patch(36 'flutils.pathutils.os.chown',37 return_value=True38 )39 self.os_chown = patcher.start()40 self.addCleanup(patcher.stop)41 def test_chown_default(self):42 chown('~/tmp/test.txt')43 self.normalize_path.assert_called_with('~/tmp/test.txt')44 self.get_os_user.assert_called_with(None)45 self.get_os_group.assert_called_with(None)46 self.os_chown.assert_called_with(self.path.as_posix(), 9753, 1357)47 def test_chown_current_owner(self):48 chown('~/tmp/test.txt', user='-1', group='-1')49 self.normalize_path.assert_called_with('~/tmp/test.txt')50 self.get_os_user.assert_not_called()51 self.get_os_group.assert_not_called()52 self.os_chown.assert_called_with(self.path.as_posix(), -1, -1)53 def test_chown_user_group(self):54 chown('~/tmp/test.txt', user='test_user', group='test_group')55 self.normalize_path.assert_called_with('~/tmp/test.txt')56 self.get_os_user.assert_called_with('test_user')57 self.get_os_group.assert_called_with('test_group')58 self.os_chown.assert_called_with(self.path.as_posix(), 9753, 1357)59class TestChownGlob(unittest.TestCase):60 def setUp(self):61 # Mock out the following structure:62 #63 # /home/test_user64 # â 65 # âââ dir_tmp66 # âââ dir_sub67 # â  âââ file_four68 # âââ file_one69 # âââ fifo_two70 # âââ file_three71 self.file_four = PosixPathMock(72 '/home/test_user/dir_tmp/dir_sub/file_four',73 is_file=True74 )75 self.dir_sub = PosixPathMock(76 '/home/test_user/dir_tmp/dir_sub',77 is_dir=True78 )79 self.file_three = PosixPathMock(80 '/home/test_user/dir_tmp/file_three',81 is_file=True82 )83 self.fifo_two = PosixPathMock(84 '/home/test_user/dir_tmp/fifo_two',85 is_fifo=True86 )87 self.file_one = PosixPathMock(88 '/home/test_user/dir_tmp/file_one',89 is_file=True90 )91 self.dir_tmp = PosixPathMock(92 '/home/test_user/dir_tmp',93 is_dir=True94 )95 # The function input as a glob96 self.path = PosixPathMock(97 '/home/test_user/**',98 glob=[99 self.file_four,100 self.dir_sub,101 self.file_three,102 self.fifo_two,103 self.file_one,104 self.dir_tmp105 ]106 )107 # Mock the flutils.pathutils.normalize_path() return value108 patcher = patch(109 'flutils.pathutils.normalize_path',110 return_value=self.path111 )112 self.normalize_path = patcher.start()113 self.addCleanup(patcher.stop)114 # Mock the pathlib.Path() return value115 patcher = patch(116 'flutils.pathutils.Path',117 return_value=self.path118 )119 self.path_func = patcher.start()120 self.addCleanup(patcher.stop)121 # Mock the flutils.pathutils.get_os_user() return value122 patcher = patch(123 'flutils.pathutils.get_os_user',124 return_value=SimpleNamespace(pw_uid=9753)125 )126 self.get_os_user = patcher.start()127 self.addCleanup(patcher.stop)128 # Mock the flutils.pathutils.get_os_group() return value129 patcher = patch(130 'flutils.pathutils.get_os_group',131 return_value=SimpleNamespace(gr_gid=1357)132 )133 self.get_os_group = patcher.start()134 self.addCleanup(patcher.stop)135 # Mock os.chown136 patcher = patch(137 'flutils.pathutils.os.chown',138 return_value=True139 )140 self.os_chown = patcher.start()141 self.addCleanup(patcher.stop)142 def test_chown_glob(self):143 chown('~/**')144 self.normalize_path.assert_called_with('~/**')145 self.get_os_user.assert_called_with(None)146 self.get_os_group.assert_called_with(None)147 for path in self.path.glob_data:148 if path.kwargs.get('is_dir', False) is True:149 self.os_chown.assert_any_call(path.as_posix(), 9753, 1357)150 path.is_dir.assert_called()151 path.is_file.assert_not_called()152 elif path.kwargs.get('is_file', False) is True:153 self.os_chown.assert_any_call(path.as_posix(), 9753, 1357)154 path.is_dir.assert_called()155 path.is_file.assert_called()156 else:157 path.is_dir.assert_called()158 path.is_file.assert_called()159 def test_chown_include_parent(self):160 chown('~/tmp/*', include_parent=True)161 self.normalize_path.assert_called_with('~/tmp/*')162 self.get_os_user.assert_called_with(None)163 self.get_os_group.assert_called_with(None)164 for path in self.path.glob_data:165 if path.kwargs.get('is_dir', False) is True:166 self.os_chown.assert_any_call(path.as_posix(), 9753, 1357)167 path.is_dir.assert_called()168 path.is_file.assert_not_called()169 elif path.kwargs.get('is_file', False) is True:170 self.os_chown.assert_any_call(path.as_posix(), 9753, 1357)171 path.is_dir.assert_called()172 path.is_file.assert_called()173 else:174 path.is_dir.assert_called()175 path.is_file.assert_called()176 self.os_chown.assert_any_call(self.path.parent.as_posix(), 9753, 1357)177class TestChownEmptyGlob(unittest.TestCase):178 def setUp(self):179 # The function input as a glob180 self.path = PosixPathMock(181 '/home/test_user/**',182 )183 # Mock the flutils.pathutils.normalize_path() return value184 patcher = patch(185 'flutils.pathutils.normalize_path',186 return_value=self.path187 )188 self.normalize_path = patcher.start()189 self.addCleanup(patcher.stop)190 # Mock the pathlib.Path() return value191 patcher = patch(192 'flutils.pathutils.Path',193 return_value=self.path194 )195 self.path_func = patcher.start()196 self.addCleanup(patcher.stop)197 # Mock the flutils.pathutils.get_os_user() return value198 patcher = patch(199 'flutils.pathutils.get_os_user',200 return_value=SimpleNamespace(pw_uid=9753)201 )202 self.get_os_user = patcher.start()203 self.addCleanup(patcher.stop)204 # Mock the flutils.pathutils.get_os_group() return value205 patcher = patch(206 'flutils.pathutils.get_os_group',207 return_value=SimpleNamespace(gr_gid=1357)208 )209 self.get_os_group = patcher.start()210 self.addCleanup(patcher.stop)211 # Mock os.chown...
test_get_os_group.py
Source:test_get_os_group.py
1import unittest2from types import SimpleNamespace3from unittest.mock import patch4from flutils.pathutils import get_os_group5class TestGetOsGroup(unittest.TestCase):6 def setUp(self):7 # Mock get_os_user8 patcher = patch(9 'flutils.pathutils.get_os_user',10 return_value=SimpleNamespace(pw_gid=3287)11 )12 self.get_os_user = patcher.start()13 self.addCleanup(patcher.stop)14 # Mock grp.getgrgid15 patcher = patch(16 'flutils.pathutils.grp.getgrgid',17 return_value=SimpleNamespace(gr_name='foo')18 )19 self.getgrgid = patcher.start()20 self.addCleanup(patcher.stop)21 # Mock grp.getgrnam22 patcher = patch(23 'flutils.pathutils.grp.getgrnam',24 return_value=SimpleNamespace(gr_name='foo')25 )26 self.getgrnam = patcher.start()27 self.addCleanup(patcher.stop)28 def test_get_os_group_name_is_none(self):29 group_obj = get_os_group()30 self.assertEqual(group_obj.gr_name, 'foo')31 self.getgrnam.assert_not_called()32 def test_get_os_group_name_is_given(self):33 group_obj = get_os_group('test_group')34 self.assertEqual(group_obj.gr_name, 'foo')35 self.getgrnam.assert_called_with('test_group')36 self.get_os_user.assert_not_called()37 self.getgrgid.assert_not_called()38 def test_get_os_group_name_is_gid(self):39 group_obj = get_os_group(254)40 self.get_os_user.assert_not_called()41 self.getgrgid.assert_called_once_with(254)42 self.assertEqual(group_obj.gr_name, 'foo')43 self.getgrnam.assert_not_called()44class TestGetOsGroupException(unittest.TestCase):45 def setUp(self):46 # Mock get_os_user47 patcher = patch(48 'flutils.pathutils.get_os_user',49 return_value=SimpleNamespace(pw_gid=3287)50 )51 self.get_os_user = patcher.start()52 self.addCleanup(patcher.stop)53 # Mock grp.getgrgid54 patcher = patch(55 'flutils.pathutils.grp.getgrgid',56 side_effect=KeyError('Boom!')57 )58 self.getgrgid = patcher.start()59 self.addCleanup(patcher.stop)60 # Mock grp.getgrnam61 patcher = patch(62 'flutils.pathutils.grp.getgrnam',63 side_effect=KeyError('Boom!')64 )65 self.getgrnam = patcher.start()66 self.addCleanup(patcher.stop)67 def test_get_os_group_name_is_invalid(self):68 self.assertRaises(OSError, get_os_group, 'test_group')69 self.get_os_user.assert_not_called()70 self.getgrgid.assert_not_called()71 def test_get_os_group_gid_is_invalid(self):72 self.assertRaises(OSError, get_os_group, 254)73 self.getgrgid.assert_called_once_with(254)74 self.get_os_user.assert_not_called()...
test_env.py
Source:test_env.py
...17---------- -------- ---------------------------------------------------------18'''19import os20import pytest21def get_os_user():22 username = os.getenv('USER')23 if username is None:24 raise IOError('"USER" environment variable is not set.')25 return username26def test_user(monkeypatch):27 monkeypatch.setenv('USER', 'luizyao')28 assert get_os_user() == 'luizyao'29def test_raise_exception(monkeypatch):30 monkeypatch.delenv('USER', raising=False)...
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!!