Best Python code snippet using localstack_python
test_objects.py
Source:test_objects.py  
1from unittest.mock import MagicMock2import pytest3from localstack.utils.objects import SubtypesInstanceManager, singleton_factory4def test_subtypes_instance_manager():5    class BaseClass(SubtypesInstanceManager):6        def foo(self):7            pass8    class C1(BaseClass):9        @staticmethod10        def impl_name() -> str:11            return "c1"12        def foo(self):13            return "bar"14    instance1 = BaseClass.get("c1")15    assert instance116    assert BaseClass.get("c1") == instance117    assert instance1.foo() == "bar"18    with pytest.raises(Exception):19        assert BaseClass.get("c2")20    class C2(BaseClass):21        @staticmethod22        def impl_name() -> str:23            return "c2"24        def foo(self):25            return "baz"26    instance2 = BaseClass.get("c2")27    assert BaseClass.get("c2") == instance228    assert instance2.foo() == "baz"29class TestSingletonFactory:30    def test_call_and_clear(self):31        mock = MagicMock()32        mock.return_value = "foobar"33        @singleton_factory34        def my_singleton():35            return mock()36        assert my_singleton() == mock.return_value37        mock.assert_called_once()38        assert my_singleton() == mock.return_value39        mock.assert_called_once()40        my_singleton.clear()41        assert my_singleton() == mock.return_value42        mock.assert_has_calls([(), ()])43        assert my_singleton() == mock.return_value44        mock.assert_has_calls([(), ()])45    def test_exception_does_not_set_a_value(self):46        mock = MagicMock()47        @singleton_factory48        def my_singleton():49            mock()50            raise ValueError("oh noes")51        with pytest.raises(ValueError):52            my_singleton()53        mock.assert_has_calls([()])54        with pytest.raises(ValueError):55            my_singleton()56        mock.assert_has_calls([(), ()])57    def test_set_none_value_does_not_set_singleton(self):58        mock = MagicMock()59        mock.return_value = None60        @singleton_factory61        def my_singleton():62            return mock()63        assert my_singleton() is None64        mock.assert_has_calls([()])65        assert my_singleton() is None66        mock.assert_has_calls([(), ()])67    def test_set_falsy_value_sets_singleton(self):68        mock = MagicMock()69        mock.return_value = False70        @singleton_factory71        def my_singleton():72            return mock()73        assert my_singleton() is False74        mock.assert_called_once()75        assert my_singleton() is False...singleton_decorator.py
Source:singleton_decorator.py  
...18    ...class Demo:19    ...     pass20    """21    _set_singleton_instance(original_class)22    def singleton_factory(cls):23        return singletons[original_class]24    original_class._instance_factory = classmethod(singleton_factory)25    return original_class26def _set_singleton_instance(original_class):27    lock.acquire()28    if original_class not in singletons:29        singletons[original_class] = original_class()...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!!
