Best Python code snippet using lisa_python
test_fallback.py
Source:test_fallback.py
1from node.behaviors import Adopt2from node.behaviors import Attributes3from node.behaviors import DefaultInit4from node.behaviors import Fallback5from node.behaviors import NodeChildValidate6from node.behaviors import Nodespaces7from node.behaviors import Nodify8from node.behaviors import OdictStorage9from node.tests import NodeTestCase10from plumber import plumbing11###############################################################################12# Mock objects13###############################################################################14@plumbing(15 Nodespaces,16 Fallback,17 Adopt,18 DefaultInit,19 Nodify,20 OdictStorage)21class FallbackNodeAttributes(object):22 """Attributes Node for testing23 """24@plumbing(25 NodeChildValidate,26 Nodespaces,27 Adopt,28 Attributes,29 DefaultInit,30 Nodify,31 OdictStorage)32class FallbackNode(object):33 """Normal Node for testing34 """35 attributes_factory = FallbackNodeAttributes36###############################################################################37# Tests38###############################################################################39class TestFallback(NodeTestCase):40 def setUp(self):41 # Setup test data42 super(TestFallback, self).setUp()43 # Define a root node44 fb_node = self.fb_node = FallbackNode(name='root')45 # It has a fallback subtree defined46 fb_node.fallback_key = 'x'47 # The fallback subtree defines a fallback sub tree for itself.48 # Note that attrs internally is also a tree!49 fb_node['x'] = FallbackNode()50 fb_node['x'].fallback_key = '1'51 # Define node without fallback, but with data52 fb_node['x']['1'] = FallbackNode()53 # An expected fallback value54 fb_node['x']['1'].attrs['a'] = 155 # An unexpected fallback value. To make them better visible, they are56 # negative in this test57 fb_node['x']['1'].attrs['d'] = -358 # Same on a second node for a different use case, where it find the59 # value on this level60 fb_node['x']['2'] = FallbackNode()61 fb_node['x']['2'].attrs['b'] = 262 fb_node['x']['2'].attrs['d'] = -263 # Define a second subtree64 fb_node['y'] = FallbackNode()65 # Here we have also a subtree which acts as fallback66 fb_node['y'].fallback_key = '1'67 # Again some data-only nodes in the subtree, still a fallback use case68 fb_node['y']['1'] = FallbackNode()69 fb_node['y']['1'].attrs['c'] = 370 fb_node['y']['1'].attrs['d'] = -171 # Define the node where our tests will look for the value72 fb_node['y']['2'] = FallbackNode()73 fb_node['y']['2'].attrs['d'] = 474 def test_test_data(self):75 # Visualize the tree76 self.assertEqual(self.fb_node.treerepr(), (77 '<class \'node.tests.test_fallback.FallbackNode\'>: root\n'78 ' <class \'node.tests.test_fallback.FallbackNode\'>: x\n'79 ' <class \'node.tests.test_fallback.FallbackNode\'>: 1\n'80 ' <class \'node.tests.test_fallback.FallbackNode\'>: 2\n'81 ' <class \'node.tests.test_fallback.FallbackNode\'>: y\n'82 ' <class \'node.tests.test_fallback.FallbackNode\'>: 1\n'83 ' <class \'node.tests.test_fallback.FallbackNode\'>: 2\n'84 ))85 def test_Fallback(self):86 # We always ask for attributes in the path 'root, y, 2, attrs'.87 # 'attrs' is in fact a nodespace '__attributes__', but internally its88 # handled like contained. See nodespaces for more info on it, this is89 # not fallback specific.90 # Case 1 - Directly ask for the key 'd'91 self.assertEqual(self.fb_node['y']['2'].attrs['d'], 4)92 # Case 2 - Ask for a key 'c' which does not exist in path. Now after93 # not finding it there it goes up one level to 'root, y'. Here it looks94 # if there is a fallback defined. There is one, its the subtree95 # 'root, y, 1'. Now it looks there relative in 'attrs' for 'c' and has96 # a hit. Value returned.97 self.assertEqual(self.fb_node['y']['2'].attrs['c'], 3)98 # Case 3 - Ask for a key 'b' which does not exist in the path. Now99 # after not finding it there it goes up one level to 'root, y'. Here it100 # looks if there is a fallback defined. There is one, its the subtree101 # 'root, y, 1'. It looks there relative in attrs for 'b' and it does102 # not exist. After not finding it there it goes up one level to103 # 'root, y'. It has a fallback, but that one was already visited.104 # Now it goes up another level on 'root' and looks if there is a105 # fallback defined. There is one, its the subtree 'root, x'. Now it106 # looks there relative for path '2, attrs, b' and has a hit. Value107 # returned.108 self.assertEqual(self.fb_node['y']['2'].attrs['b'], 2)109 # Case 4 - Ask for a key 'a' which does not exist in the path. Now110 # after not finding it there it goes up one level to 'root, y'. Here111 # it looks if there is a fallback defined. There is one, its the112 # subtree 'root, y, 1'. It looks there relative in attrs for a and it113 # does not exist. After not finding it there it goes up one level to114 # 'root, y'. It has a fallback, but that one was already visited.115 # Now it goes up another level on 'root' and looks if there is a116 # fallback defined. There is one, its the subtree 'root, x'. Now it117 # looks there relative for path '2, attrs, a' and it does not exist.118 # After not finding it there it goes up one level to 'root, x'. Here119 # it looks if there is a fallback defined. There is one, its the120 # subtree 'root, x, 1'. Now it looks there relative for path121 # 'attrs, a' and hit! Return value.122 self.assertEqual(self.fb_node['y']['2'].attrs['a'], 1)123 # Case 5 - When there is no fallback defined. We ask for a key 'z'124 # which does not exist in the path. Now after not finding it there it125 # goes up one level to 'root, y'. Here it looks if there is a fallback126 # defined. There is one, its the subtree 'root, y, 1'. It looks there127 # relative in attrs for z and it does not exist. After not finding it128 # there it goes up one level to 'root, y'. It has a fallback, but that129 # one was already visited. Now it goes up another level on 'root' and130 # looks if there is a fallback defined. There is one, its the subtree131 # 'root, x'. Now it looks there relative for path '2, attrs, z' and it132 # does not exist. After not finding it there it goes up one level to133 # 'root, x'. Here it looks if there is a fallback defined. There is134 # one, its the subtree 'root, x, 1'. Now it looks there relative for135 # path 'attrs, z' and it does not exist. After not finding it there it136 # goes up one level to 'root'. It has a fallback, but that one was137 # already visited. Next parent is None. Exit. No value found. Raise138 # KeyError139 def case_5_raises():140 self.fb_node['y']['2'].attrs['z']141 err = self.expectError(KeyError, case_5_raises)...
test_get_discovery_directory.py
Source:test_get_discovery_directory.py
1# Copyright (C) 2020 The Android Open Source Project2#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 at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14import pytest15import os16import platform17from aemu.discovery.emulator_discovery import get_discovery_directory18test_expected = [19 ("Linux", {"XDG_RUNTIME_DIR": "foo"}, "foo"),20 (21 "Darwin",22 {"HOME": "foo"},23 os.path.join("foo", "Library", "Caches", "TemporaryItems"),24 ),25 ("Windows", {"LOCALAPPDATA": "foo"}, os.path.join("foo", "Temp")),26]27@pytest.mark.parametrize("platform,env,expected", test_expected)28def test_get_discovery_directory(platform, env, expected, mocker):29 env["ANDROID_SDK_HOME"] = "bar"30 mocker.patch.dict(os.environ, env)31 mocker.patch.object(os.path, "exists", return_value=True)32 mocker.patch("platform.system", return_value=platform)33 disc = get_discovery_directory()34 assert disc == os.path.join(expected, "avd", "running")35test_fallback = [36 ("Linux", {"XDG_RUNTIME_DIR": "foo"}),37 ("Darwin", {"HOME": "foo"}),38 ("Windows", {"LOCALAPPDATA": "foo"}),39]40@pytest.mark.parametrize("platform,env", test_fallback)41def test_get_discovery_directory_fallback(platform, env, mocker):42 env["ANDROID_EMULATOR_HOME"] = "bar"43 mocker.patch.dict(os.environ, env)44 mocker.patch.object(os, "getuid", return_value="bar")45 mocker.patch("platform.system", return_value=platform)46 disc = get_discovery_directory()47 assert disc == os.path.join("bar", "avd", "running")48@pytest.mark.parametrize("platform,env", test_fallback)49def test_get_discovery_directory_fallback(platform, env, mocker):50 env["ANDROID_SDK_HOME"] = "bar"51 mocker.patch.dict(os.environ, env)52 mocker.patch.object(os, "getuid", return_value="bar")53 mocker.patch("platform.system", return_value=platform)54 disc = get_discovery_directory()...
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!!