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(mox3_stubout_example.check_if_exists(non_existing_path))
63        self.stubber.set(os.path, "exists", lambda x: True)
64        self.assertTrue(mox3_stubout_example.check_if_exists(non_existing_path))
65        self.stubber.unset_all()
66        self.assertFalse(mox3_stubout_example.check_if_exists(non_existing_path))
67
68    def test_stubout_class_with_set(self):
69        self.assertGreater(mox3_stubout_example.tomorrow().year, 2000)
70
71        self.stubber.set(datetime, "date", GroundhogDate)
72        self.assertEqual(mox3_stubout_example.tomorrow(), datetime.date(1993, 2, 3))
73
74        self.stubber.unset_all()
75        self.assertGreater(mox3_stubout_example.tomorrow().year, 2000)
76
77    def test_stubout_module_with_set(self):
78        self.assertEqual(10, mox3_stubout_example.fabs(-10))
79
80        self.stubber.set(mox3_stubout_example, "math", NoPanicMath)
81        self.assertEqual(42, mox3_stubout_example.fabs(-10))
82
83        self.stubber.unset_all()
84        self.assertEqual(10, mox3_stubout_example.fabs(-10))
85
86    def test_set_raise_if_unknown_attribute(self):
87        self.assertRaises(
88            AttributeError,
89            self.stubber.set,
90            os.path,
91            "exists_not",
92            lambda x: True,
93        )
94        self.assertRaises(
95            AttributeError,
96            self.stubber.set,
97            datetime,
98            "tomorrow",
99            GroundhogDate,
100        )
101        self.assertRaises(
102            AttributeError,
103            self.stubber.set,
104            mox3_stubout_example,
105            "math1",
106            NoPanicMath,
107        )
108
109    def test_stubout_method_with_smart_set(self):
110        non_existing_path = "non_existing_path"
111        self.stubber.smart_set(os.path, "exists", lambda x: True)
112        self.assertTrue(mox3_stubout_example.check_if_exists(non_existing_path))
113        self.stubber.smart_unset_all()
114        self.assertFalse(mox3_stubout_example.check_if_exists(non_existing_path))
115
116    def test_stubout_class_with_smart_set(self):
117        self.stubber.smart_set(datetime, "date", GroundhogDate)
118        self.assertEqual(mox3_stubout_example.tomorrow(), datetime.date(1993, 2, 3))
119
120        self.stubber.smart_unset_all()
121        self.assertGreater(mox3_stubout_example.tomorrow().year, 2000)
122
123    def test_stubout_module_with_smart_set(self):
124        self.assertEqual(10, mox3_stubout_example.fabs(-10))
125
126        self.stubber.smart_set(mox3_stubout_example, "math", NoPanicMath)
127        self.assertEqual(42, mox3_stubout_example.fabs(-10))
128
129        self.stubber.smart_unset_all()
130        self.assertEqual(10, mox3_stubout_example.fabs(-10))
131
132    def test_stubout_submodule_with_smart_set(self):
133        # this one does not work with Set
134        non_existing_path = "non_existing_path"
135        self.assertFalse(mox3_stubout_example.check_if_exists(non_existing_path))
136        self.stubber.smart_set(os, "path", ExistingPath)
137        self.assertTrue(mox3_stubout_example.check_if_exists(non_existing_path))
138        self.stubber.smart_unset_all()
139        self.assertFalse(mox3_stubout_example.check_if_exists(non_existing_path))
140
141    def test_smart_set_raise_if_unknown_attribute(self):
142        self.assertRaises(
143            AttributeError,
144            self.stubber.smart_set,
145            os.path,
146            "exists_not",
147            lambda x: True,
148        )
149        self.assertRaises(
150            AttributeError,
151            self.stubber.smart_set,
152            datetime,
153            "tomorrow",
154            GroundhogDate,
155        )
156        self.assertRaises(
157            AttributeError,
158            self.stubber.smart_set,
159            mox3_stubout_example,
160            "math1",
161            NoPanicMath,
162        )
163
164
165if __name__ == "__main__":
166    unittest.main()
167