How to use _make_one method in tappy

Best Python code snippet using tappy_python

test_locked_file.py

Source:test_locked_file.py Github

copy

Full Screen

...18import mock19import unittest220from oauth2client.contrib import locked_file21class TestOpener(unittest2.TestCase):22 def _make_one(self):23 _filehandle, filename = tempfile.mkstemp()24 os.close(_filehandle)25 return locked_file._Opener(filename, 'r+', 'r'), filename26 def test_ctor(self):27 instance, filename = self._make_one()28 self.assertFalse(instance._locked)29 self.assertEqual(instance._filename, filename)30 self.assertEqual(instance._mode, 'r+')31 self.assertEqual(instance._fallback_mode, 'r')32 self.assertIsNone(instance._fh)33 self.assertIsNone(instance._lock_fd)34 def test_is_locked(self):35 instance, _ = self._make_one()36 self.assertFalse(instance.is_locked())37 instance._locked = True38 self.assertTrue(instance.is_locked())39 def test_file_handle(self):40 instance, _ = self._make_one()41 self.assertIsNone(instance.file_handle())42 fh = mock.Mock()43 instance._fh = fh44 self.assertEqual(instance.file_handle(), fh)45 def test_filename(self):46 instance, filename = self._make_one()47 self.assertEqual(instance.filename(), filename)48 def test_open_and_lock(self):49 instance, _ = self._make_one()50 instance.open_and_lock(1, 1)51 def test_unlock_and_close(self):52 instance, _ = self._make_one()53 instance.unlock_and_close()54class TestPosixOpener(TestOpener):55 def _make_one(self):56 _filehandle, filename = tempfile.mkstemp()57 os.close(_filehandle)58 return locked_file._PosixOpener(filename, 'r+', 'r'), filename59 def test_relock_fail(self):60 instance, _ = self._make_one()61 instance.open_and_lock(1, 1)62 self.assertTrue(instance.is_locked())63 self.assertIsNotNone(instance.file_handle())64 self.assertRaises(65 locked_file.AlreadyLockedException, instance.open_and_lock, 1, 1)66 @mock.patch('oauth2client.contrib.locked_file.open', create=True)67 def test_lock_access_error_fallback_mode(self, mock_open):68 # NOTE: This is a bad case. The behavior here should be that the69 # error gets re-raised, but the module lets the if statement fall70 # through.71 instance, _ = self._make_one()72 mock_open.side_effect = [IOError(errno.ENOENT, '')]73 instance.open_and_lock(1, 1)74 self.assertIsNone(instance.file_handle())75 self.assertTrue(instance.is_locked())76 @mock.patch('oauth2client.contrib.locked_file.open', create=True)77 def test_lock_non_access_error(self, mock_open):78 instance, _ = self._make_one()79 fh_mock = mock.Mock()80 mock_open.side_effect = [IOError(errno.EACCES, ''), fh_mock]81 instance.open_and_lock(1, 1)82 self.assertEqual(instance.file_handle(), fh_mock)83 self.assertFalse(instance.is_locked())84 @mock.patch('oauth2client.contrib.locked_file.open', create=True)85 def test_lock_unexpected_error(self, mock_open):86 instance, _ = self._make_one()87 with mock.patch('os.open') as mock_os_open:88 mock_os_open.side_effect = [OSError(errno.EPERM, '')]89 self.assertRaises(OSError, instance.open_and_lock, 1, 1)90 @mock.patch('oauth2client.contrib.locked_file.open', create=True)91 @mock.patch('oauth2client.contrib.locked_file.logger')92 @mock.patch('time.time')93 def test_lock_timeout_error(self, mock_time, mock_logger, mock_open):94 instance, _ = self._make_one()95 # Make it seem like 10 seconds have passed between calls.96 mock_time.side_effect = [0, 10]97 with mock.patch('os.open') as mock_os_open:98 # Raising EEXIST should cause it to try to retry locking.99 mock_os_open.side_effect = [OSError(errno.EEXIST, '')]100 instance.open_and_lock(1, 1)101 self.assertFalse(instance.is_locked())102 self.assertTrue(mock_logger.warn.called)103 @mock.patch('oauth2client.contrib.locked_file.open', create=True)104 @mock.patch('oauth2client.contrib.locked_file.logger')105 @mock.patch('time.time')106 def test_lock_timeout_error_no_fh(self, mock_time, mock_logger, mock_open):107 instance, _ = self._make_one()108 # Make it seem like 10 seconds have passed between calls.109 mock_time.side_effect = [0, 10]110 # This will cause the retry loop to enter without a file handle.111 fh_mock = mock.Mock()112 mock_open.side_effect = [IOError(errno.ENOENT, ''), fh_mock]113 with mock.patch('os.open') as mock_os_open:114 # Raising EEXIST should cause it to try to retry locking.115 mock_os_open.side_effect = [OSError(errno.EEXIST, '')]116 instance.open_and_lock(1, 1)117 self.assertFalse(instance.is_locked())118 self.assertTrue(mock_logger.warn.called)119 self.assertEqual(instance.file_handle(), fh_mock)120 @mock.patch('oauth2client.contrib.locked_file.open', create=True)121 @mock.patch('time.time')122 @mock.patch('time.sleep')123 def test_lock_retry_success(self, mock_sleep, mock_time, mock_open):124 instance, _ = self._make_one()125 # Make it seem like 1 second has passed between calls. Extra values126 # are needed by the logging module.127 mock_time.side_effect = [0, 1]128 with mock.patch('os.open') as mock_os_open:129 # Raising EEXIST should cause it to try to retry locking.130 mock_os_open.side_effect = [131 OSError(errno.EEXIST, ''), mock.Mock()]132 instance.open_and_lock(10, 1)133 print(mock_os_open.call_args_list)134 self.assertTrue(instance.is_locked())135 mock_sleep.assert_called_with(1)136 @mock.patch('oauth2client.contrib.locked_file.os')137 def test_unlock(self, os_mock):138 instance, _ = self._make_one()139 instance._locked = True140 lock_fd_mock = instance._lock_fd = mock.Mock()141 instance._fh = mock.Mock()142 instance.unlock_and_close()143 self.assertFalse(instance.is_locked())144 os_mock.close.assert_called_once_with(lock_fd_mock)145 self.assertTrue(os_mock.unlink.called)146 self.assertTrue(instance._fh.close.called)147class TestLockedFile(unittest2.TestCase):148 @mock.patch('oauth2client.contrib.locked_file._PosixOpener')149 def _make_one(self, opener_ctor_mock):150 opener_mock = mock.Mock()151 opener_ctor_mock.return_value = opener_mock152 return locked_file.LockedFile(153 'a_file', 'r+', 'r', use_native_locking=False), opener_mock154 @mock.patch('oauth2client.contrib.locked_file._PosixOpener')155 def test_ctor_minimal(self, opener_mock):156 locked_file.LockedFile(157 'a_file', 'r+', 'r', use_native_locking=False)158 opener_mock.assert_called_with('a_file', 'r+', 'r')159 @mock.patch.dict('sys.modules', {160 'oauth2client.contrib._win32_opener': mock.Mock()})161 def test_ctor_native_win32(self):162 _win32_opener_mock = sys.modules['oauth2client.contrib._win32_opener']163 locked_file.LockedFile(164 'a_file', 'r+', 'r', use_native_locking=True)165 _win32_opener_mock._Win32Opener.assert_called_with('a_file', 'r+', 'r')166 @mock.patch.dict('sys.modules', {167 'oauth2client.contrib._win32_opener': None,168 'oauth2client.contrib._fcntl_opener': mock.Mock()})169 def test_ctor_native_fcntl(self):170 _fnctl_opener_mock = sys.modules['oauth2client.contrib._fcntl_opener']171 locked_file.LockedFile(172 'a_file', 'r+', 'r', use_native_locking=True)173 _fnctl_opener_mock._FcntlOpener.assert_called_with('a_file', 'r+', 'r')174 @mock.patch('oauth2client.contrib.locked_file._PosixOpener')175 @mock.patch.dict('sys.modules', {176 'oauth2client.contrib._win32_opener': None,177 'oauth2client.contrib._fcntl_opener': None})178 def test_ctor_native_posix_fallback(self, opener_mock):179 locked_file.LockedFile(180 'a_file', 'r+', 'r', use_native_locking=True)181 opener_mock.assert_called_with('a_file', 'r+', 'r')182 def test_filename(self):183 instance, opener = self._make_one()184 opener._filename = 'some file'185 self.assertEqual(instance.filename(), 'some file')186 def test_file_handle(self):187 instance, opener = self._make_one()188 self.assertEqual(instance.file_handle(), opener.file_handle())189 self.assertTrue(opener.file_handle.called)190 def test_is_locked(self):191 instance, opener = self._make_one()192 self.assertEqual(instance.is_locked(), opener.is_locked())193 self.assertTrue(opener.is_locked.called)194 def test_open_and_lock(self):195 instance, opener = self._make_one()196 instance.open_and_lock()197 opener.open_and_lock.assert_called_with(0, 0.05)198 def test_unlock_and_close(self):199 instance, opener = self._make_one()200 instance.unlock_and_close()201 opener.unlock_and_close.assert_called_with()202if __name__ == '__main__': # pragma: NO COVER...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...9 def u(value):10 return unicode(value, 'utf-8') # NOQA: F82111class ValueTests(object):12 def test_cmp(self):13 value = self._make_one()14 self.assertNotEqual(value, 12)15 self.assertNotEqual(12, value)16 self.assertNotEqual(value, b'abc')17 self.assertNotEqual(b'abc', value)18 self.assertNotEqual(value, u(b'abc'))19 self.assertNotEqual(u(b'abc'), value)20 def test_lt(self):21 value = self._make_one()22 self.assertTrue(1 < value)23 self.assertFalse(value < 1)24 self.assertFalse(value < value)25 def test_le(self):26 value = self._make_one()27 self.assertTrue(1 <= value)28 self.assertFalse(value <= 1)29 self.assertTrue(value <= value)30 def test_eq(self):31 value = self._make_one()32 self.assertFalse(1 == value)33 self.assertFalse(value == 1)34 self.assertTrue(value == value)35 def test_ne(self):36 value = self._make_one()37 self.assertTrue(1 != value)38 self.assertTrue(value != 1)39 self.assertFalse(value != value)40 def test_gt(self):41 value = self._make_one()42 self.assertFalse(1 > value)43 self.assertTrue(value > 1)44 self.assertFalse(value > value)45 def test_ge(self):46 value = self._make_one()47 self.assertFalse(1 >= value)48 self.assertTrue(value >= 1)49 self.assertTrue(value >= value)50 def test_add(self):51 value = self._make_one()52 self.assertEqual(1 + value, value)53 self.assertEqual(value + 1, value)54 self.assertEqual(value, 1 + value)55 self.assertEqual(value, value + 1)56 def test_sub(self):57 value = self._make_one()58 self.assertEqual(1 - value, value)59 self.assertEqual(value - 1, value)60 self.assertEqual(value, 1 - value)61 self.assertEqual(value, value - 1)62 @unittest.skipUnless(PY35, 'Python 3.5+ syntax')63 def test_matmul(self):64 value = self._make_one()65 class Dummy(object):66 def __init__(self, value):67 self.value = value68 def __matmul__(self, other):69 return self.value * other70 def __rmatmul__(self, other):71 return self.value * other72 def matmul(a, b):73 # Don't use the @ operator to avoid SyntaxError's74 return a.__matmul__(b)75 dummy = Dummy(2)76 self.assertEqual(matmul(dummy, value), value)77 self.assertEqual(matmul(value, dummy), value)78 self.assertEqual(value, matmul(dummy, value))79 self.assertEqual(value, matmul(value, dummy))80 def test_mul(self):81 value = self._make_one()82 self.assertEqual(2 * value, value)83 self.assertEqual(value * 2, value)84 self.assertEqual(value, 2 * value)85 self.assertEqual(value, value * 2)86 def test_div(self):87 value = self._make_one()88 self.assertEqual(2 / value, value)89 self.assertEqual(value / 2, value)90 self.assertEqual(value, 2 / value)91 self.assertEqual(value, value / 2)92 def test_integer_div(self):93 value = self._make_one()94 self.assertEqual(2 // value, value)95 self.assertEqual(value // 2, value)96 self.assertEqual(value, 2 // value)97 self.assertEqual(value, value // 2)98 def test_mod(self):99 value = self._make_one()100 self.assertEqual(2 % value, value)101 self.assertEqual(value % 2, value)102 self.assertEqual(value, 2 % value)103 self.assertEqual(value, value % 2)104 def test_divmod(self):105 value = self._make_one()106 self.assertEqual(divmod(2, value), value)107 self.assertEqual(divmod(value, 2), value)108 self.assertEqual(value, divmod(2, value))109 self.assertEqual(value, divmod(value, 2))110 def test_pow(self):111 value = self._make_one()112 self.assertEqual(2 ** value, value)113 self.assertEqual(value ** 2, value)114 self.assertEqual(value, 2 ** value)115 self.assertEqual(value, value ** 2)116 def test_neg(self):117 value = self._make_one()118 self.assertEqual(-value, value)119 self.assertEqual(value, -value)120 def test_pos(self):121 value = self._make_one()122 self.assertEqual(+value, value)123 self.assertEqual(value, +value)124 def test_abs(self):125 value = self._make_one()126 self.assertEqual(abs(value), value)127 self.assertEqual(value, abs(value))128 def test_inv(self):129 value = self._make_one()130 self.assertEqual(~value, value)131 self.assertEqual(value, ~value)132 def test_nonzero(self):133 value = self._make_one()134 self.assertEqual(bool(value), False)135 self.assertEqual(False, bool(value))136 def test_lshift(self):137 value = self._make_one()138 self.assertEqual(2 << value, value)139 self.assertEqual(value << 2, value)140 self.assertEqual(value, 2 << value)141 self.assertEqual(value, value << 2)142 def test_rshift(self):143 value = self._make_one()144 self.assertEqual(2 >> value, value)145 self.assertEqual(value >> 2, value)146 self.assertEqual(value, 2 >> value)147 self.assertEqual(value, value >> 2)148 def test_and(self):149 value = self._make_one()150 self.assertEqual(2 & value, value)151 self.assertEqual(value & 2, value)152 self.assertEqual(value, 2 & value)153 self.assertEqual(value, value & 2)154 def test_or(self):155 value = self._make_one()156 self.assertEqual(2 | value, value)157 self.assertEqual(value | 2, value)158 self.assertEqual(value, 2 | value)159 self.assertEqual(value, value | 2)160 def test_xor(self):161 value = self._make_one()162 self.assertEqual(2 ^ value, value)163 self.assertEqual(value ^ 2, value)164 self.assertEqual(value, 2 ^ value)165 self.assertEqual(value, value ^ 2)166 @unittest.skipIf(PY3, 'Python 2 only.')167 def test_coerce(self):168 from Missing import notMissing169 value = self._make_one()170 self.assertEqual(coerce(value, 1), (value, notMissing)) # NOQA: F821171 self.assertEqual(coerce(1, value), (notMissing, value)) # NOQA: F821172 self.assertEqual(coerce(value, 1.0), (value, notMissing)) # NOQA: F821173 self.assertEqual(coerce(1.0, value), (notMissing, value)) # NOQA: F821174 self.assertEqual(coerce(value, 0x1), (value, notMissing)) # NOQA: F821175 self.assertEqual(coerce(0x1, value), (notMissing, value)) # NOQA: F821176 def test_number_conversion(self):177 value = self._make_one()178 self.assertRaises(TypeError, int, value)179 self.assertRaises(TypeError, long, value)180 self.assertRaises(TypeError, float, value)181 self.assertRaises(TypeError, oct, value)182 self.assertRaises(TypeError, hex, value)183 def test_getattr(self):184 value = self._make_one()185 valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'186 for v in valid:187 self.assertEqual(getattr(value, v), value)188 self.assertEqual(getattr(value, 'abc'), value)189 self.assertEqual(getattr(value, 'a_b'), value)190 self.assertRaises(AttributeError, getattr, value, 'a1')191 self.assertRaises(AttributeError, getattr, value, '_a')192 self.assertRaises(AttributeError, getattr, value, 'a_b1')193 def test_setattr(self):194 value = self._make_one()195 self.assertRaises(AttributeError, setattr, value, 'a', 2)196 self.assertRaises(AttributeError, setattr, value, 'a1', 2)197 def test_call(self):198 value = self._make_one()199 self.assertEqual(value, value())200 self.assertEqual(value(), value)201 def test_hash(self):202 value = self._make_one()203 self.assertRaises(TypeError, hash, value)204 def test_repr(self):205 value = self._make_one()206 self.assertEqual(repr(value), 'Missing.Value')207 def test_bytes(self):208 value = self._make_one()209 self.assertEqual(bytes(value), b'')210 def test_str(self):211 value = self._make_one()212 self.assertEqual(str(value), '')213 def test_reduce(self):214 value = self._make_one()215 self.assertEqual(value.__reduce__(), 'V')216 def test_class(self):217 value = self._make_one()218 self.assertTrue('Missing.Missing' in repr(value.__class__))219 def test_type(self):220 value = self._make_one()221 self.assertTrue('Missing.Missing' in repr(type(value)))222class TestValue(ValueTests, unittest.TestCase):223 def _get_target(self):224 from Missing import Value225 return Value226 def _make_one(self):227 return self._get_target()228class TestMV(ValueTests, unittest.TestCase):229 def _get_target(self):230 from Missing import MV231 return MV232 def _make_one(self):233 return self._get_target()234class TestV(ValueTests, unittest.TestCase):235 def _get_target(self):236 from Missing import V237 return V238 def _make_one(self):239 return self._get_target()240class TestMissing(ValueTests, unittest.TestCase):241 def _get_target(self):242 from Missing import Missing243 return Missing244 def _make_one(self):245 return self._get_target()()246 def test_reduce(self):247 klass = self._get_target()248 value = self._make_one()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tappy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful