|
| 1 | +# Copyright 2025 Xanadu Quantum Technologies Inc. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for the jax_extras.patches module""" |
| 15 | + |
| 16 | +from catalyst.jax_extras.patches import mock_attributes |
| 17 | + |
| 18 | + |
| 19 | +# pylint: disable=missing-class-docstring,missing-function-docstring |
| 20 | +class TestMockAttributes: |
| 21 | + """Test the mock_attributes function and MockAttributeWrapper class.""" |
| 22 | + |
| 23 | + def test_mock_attributes_returns_mocked_value(self): |
| 24 | + """Test that accessing a mocked attribute returns the mocked value.""" |
| 25 | + |
| 26 | + class DummyClass: |
| 27 | + def __init__(self): |
| 28 | + self.original_attr = "original" |
| 29 | + |
| 30 | + obj = DummyClass() |
| 31 | + mocked = mock_attributes(obj, {"mocked_attr": "mocked_value"}) |
| 32 | + |
| 33 | + # Access the mocked attribute - this should come from the attrs dict |
| 34 | + assert mocked.mocked_attr == "mocked_value" |
| 35 | + |
| 36 | + def test_mock_attributes_returns_original_value(self): |
| 37 | + """Test that accessing an unmocked attribute returns the original value.""" |
| 38 | + |
| 39 | + class DummyClass: |
| 40 | + def __init__(self): |
| 41 | + self.original_attr = "original" |
| 42 | + |
| 43 | + obj = DummyClass() |
| 44 | + mocked = mock_attributes(obj, {"mocked_attr": "mocked_value"}) |
| 45 | + |
| 46 | + # Access the original attribute - this should come from the original object |
| 47 | + # This tests the else branch in __getattr__ |
| 48 | + assert mocked.original_attr == "original" |
| 49 | + |
| 50 | + def test_mock_attributes_with_methods(self): |
| 51 | + """Test that calling original methods works through the wrapper.""" |
| 52 | + |
| 53 | + class DummyClass: |
| 54 | + def __init__(self): |
| 55 | + self.value = 42 |
| 56 | + |
| 57 | + def get_value(self): |
| 58 | + return self.value |
| 59 | + |
| 60 | + obj = DummyClass() |
| 61 | + |
| 62 | + def mocked_method(): |
| 63 | + return "mocked" |
| 64 | + |
| 65 | + mocked = mock_attributes(obj, {"mocked_method": mocked_method}) |
| 66 | + |
| 67 | + # Access the mocked method |
| 68 | + assert mocked.mocked_method() == "mocked" |
| 69 | + |
| 70 | + # Access the original method - tests the getattr fallback |
| 71 | + assert mocked.get_value() == 42 |
| 72 | + |
| 73 | + def test_mock_attributes_with_callable(self): |
| 74 | + """Test mocking with callable attributes like lambda functions.""" |
| 75 | + |
| 76 | + class DummyClass: |
| 77 | + def __init__(self): |
| 78 | + self.original_func = lambda x: x * 2 |
| 79 | + |
| 80 | + obj = DummyClass() |
| 81 | + mocked = mock_attributes(obj, {"new_func": lambda x: x * 3}) |
| 82 | + |
| 83 | + # Access the mocked callable |
| 84 | + assert mocked.new_func(5) == 15 |
| 85 | + |
| 86 | + # Access the original callable - tests the getattr fallback |
| 87 | + assert mocked.original_func(5) == 10 |
| 88 | + |
| 89 | + def test_mock_attributes_override_existing(self): |
| 90 | + """Test that mocking can override existing attributes.""" |
| 91 | + |
| 92 | + class DummyClass: |
| 93 | + def __init__(self): |
| 94 | + self.attr = "original" |
| 95 | + |
| 96 | + obj = DummyClass() |
| 97 | + mocked = mock_attributes(obj, {"attr": "overridden"}) |
| 98 | + |
| 99 | + # The mocked value should take precedence |
| 100 | + assert mocked.attr == "overridden" |
| 101 | + |
| 102 | + def test_mock_attributes_stores_original(self): |
| 103 | + """Test that the original object is accessible through the wrapper.""" |
| 104 | + |
| 105 | + class DummyClass: |
| 106 | + def __init__(self): |
| 107 | + self.value = 100 |
| 108 | + |
| 109 | + obj = DummyClass() |
| 110 | + mocked = mock_attributes(obj, {}) |
| 111 | + |
| 112 | + # The wrapper should store the original object |
| 113 | + assert mocked.original is obj |
| 114 | + assert mocked.original.value == 100 |
0 commit comments