1# Licensed under the Apache License, Version 2.0 (the "License"); 2# you may not use this file except in compliance with the License. 3# You may obtain a copy of the License at 4# 5# http://www.apache.org/licenses/LICENSE-2.0 6# 7# Unless required by applicable law or agreed to in writing, software 8# distributed under the License is distributed on an "AS IS" BASIS, 9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10# See the License for the specific language governing permissions and 11# limitations under the License. 12 13"""Unit tests for mox3_stubout.""" 14 15import datetime 16import math 17import os 18import unittest 19from os import path 20 21from pyfakefs import mox3_stubout 22from pyfakefs.tests import mox3_stubout_example 23 24 25class NoPanicMath: 26 real_math = math 27 28 @staticmethod 29 def fabs(_x): 30 return 42 31 32 def __getattr__(self, name): 33 """Forwards any unfaked calls to the standard module.""" 34 return getattr(self.real_math, name) 35 36 37class ExistingPath: 38 real_path = path 39 40 @staticmethod 41 def exists(_path): 42 return True 43 44 def __getattr__(self, name): 45 """Forwards any unfaked calls to the standard module.""" 46 return getattr(self.real_path, name) 47 48 49class GroundhogDate(datetime.date): 50 @classmethod 51 def today(cls): 52 return datetime.date(1993, 2, 2) 53 54 55class StubOutForTestingTest(unittest.TestCase): 56 def setUp(self): 57 super(StubOutForTestingTest, self).setUp() 58 self.stubber = mox3_stubout.StubOutForTesting() 59 60 def test_stubout_method_with_set(self): 61 non_existing_path = 'non_existing_path' 62 self.assertFalse( 63 mox3_stubout_example.check_if_exists(non_existing_path)) 64 self.stubber.set(os.path, 'exists', lambda x: True) 65 self.assertTrue( 66 mox3_stubout_example.check_if_exists(non_existing_path)) 67 self.stubber.unset_all() 68 self.assertFalse( 69 mox3_stubout_example.check_if_exists(non_existing_path)) 70 71 def test_stubout_class_with_set(self): 72 self.assertGreater(mox3_stubout_example.tomorrow().year, 2000) 73 74 self.stubber.set(datetime, 'date', GroundhogDate) 75 self.assertEqual(mox3_stubout_example.tomorrow(), 76 datetime.date(1993, 2, 3)) 77 78 self.stubber.unset_all() 79 self.assertGreater(mox3_stubout_example.tomorrow().year, 2000) 80 81 def test_stubout_module_with_set(self): 82 self.assertEqual(10, mox3_stubout_example.fabs(-10)) 83 84 self.stubber.set(mox3_stubout_example, 'math', NoPanicMath) 85 self.assertEqual(42, mox3_stubout_example.fabs(-10)) 86 87 self.stubber.unset_all() 88 self.assertEqual(10, mox3_stubout_example.fabs(-10)) 89 90 def test_set_raise_if_unknown_attribute(self): 91 self.assertRaises(AttributeError, self.stubber.set, 92 os.path, 'exists_not', lambda x: True) 93 self.assertRaises(AttributeError, self.stubber.set, 94 datetime, 'tomorrow', GroundhogDate) 95 self.assertRaises(AttributeError, self.stubber.set, 96 mox3_stubout_example, 'math1', NoPanicMath) 97 98 def test_stubout_method_with_smart_set(self): 99 non_existing_path = 'non_existing_path' 100 self.stubber.smart_set(os.path, 'exists', lambda x: True) 101 self.assertTrue( 102 mox3_stubout_example.check_if_exists(non_existing_path)) 103 self.stubber.smart_unset_all() 104 self.assertFalse( 105 mox3_stubout_example.check_if_exists(non_existing_path)) 106 107 def test_stubout_class_with_smart_set(self): 108 self.stubber.smart_set(datetime, 'date', GroundhogDate) 109 self.assertEqual(mox3_stubout_example.tomorrow(), 110 datetime.date(1993, 2, 3)) 111 112 self.stubber.smart_unset_all() 113 self.assertGreater(mox3_stubout_example.tomorrow().year, 2000) 114 115 def test_stubout_module_with_smart_set(self): 116 self.assertEqual(10, mox3_stubout_example.fabs(-10)) 117 118 self.stubber.smart_set(mox3_stubout_example, 'math', NoPanicMath) 119 self.assertEqual(42, mox3_stubout_example.fabs(-10)) 120 121 self.stubber.smart_unset_all() 122 self.assertEqual(10, mox3_stubout_example.fabs(-10)) 123 124 def test_stubout_submodule_with_smart_set(self): 125 # this one does not work with Set 126 non_existing_path = 'non_existing_path' 127 self.assertFalse( 128 mox3_stubout_example.check_if_exists(non_existing_path)) 129 self.stubber.smart_set(os, 'path', ExistingPath) 130 self.assertTrue( 131 mox3_stubout_example.check_if_exists(non_existing_path)) 132 self.stubber.smart_unset_all() 133 self.assertFalse( 134 mox3_stubout_example.check_if_exists(non_existing_path)) 135 136 def test_smart_set_raise_if_unknown_attribute(self): 137 self.assertRaises(AttributeError, self.stubber.smart_set, 138 os.path, 'exists_not', lambda x: True) 139 self.assertRaises(AttributeError, self.stubber.smart_set, 140 datetime, 'tomorrow', GroundhogDate) 141 self.assertRaises(AttributeError, self.stubber.smart_set, 142 mox3_stubout_example, 'math1', NoPanicMath) 143 144 145if __name__ == '__main__': 146 unittest.main() 147