Best Python code snippet using autotest_python
afe_store_unittest.py
Source:afe_store_unittest.py
1# Copyright 2017 The Chromium OS Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import mock5import unittest6import common7from autotest_lib.frontend.afe.json_rpc import proxy as rpc_proxy8from autotest_lib.server import frontend9from autotest_lib.server.hosts import afe_store10from autotest_lib.server.hosts import host_info11class AfeStoreTest(unittest.TestCase):12 """Test refresh/commit success cases for AfeStore."""13 def setUp(self):14 self.hostname = 'some-host'15 self.mock_afe = mock.create_autospec(frontend.AFE, instance=True)16 self.store = afe_store.AfeStore(self.hostname, afe=self.mock_afe)17 def _create_mock_host(self, labels, attributes):18 """Create a mock frontend.Host with the given labels and attributes.19 @param labels: The labels to set on the host.20 @param attributes: The attributes to set on the host.21 @returns: A mock object for frontend.Host.22 """23 mock_host = mock.create_autospec(frontend.Host, instance=True)24 mock_host.labels = labels25 mock_host.attributes = attributes26 return mock_host27 def test_refresh(self):28 """Test that refresh correctly translates host information."""29 self.mock_afe.get_hosts.return_value = [30 self._create_mock_host(['label1'], {'attrib1': 'val1'})]31 info = self.store._refresh_impl()32 self.assertListEqual(info.labels, ['label1'])33 self.assertDictEqual(info.attributes, {'attrib1': 'val1'})34 def test_refresh_no_host_raises(self):35 """Test that refresh complains if no host is found."""36 self.mock_afe.get_hosts.return_value = []37 with self.assertRaises(host_info.StoreError):38 self.store._refresh_impl()39 def test_refresh_multiple_hosts_picks_first(self):40 """Test that refresh returns the first host if multiple match."""41 self.mock_afe.get_hosts.return_value = [42 self._create_mock_host(['label1'], {'attrib1': 'val1'}),43 self._create_mock_host(['label2'], {'attrib2': 'val2'})]44 info = self.store._refresh_impl()45 self.assertListEqual(info.labels, ['label1'])46 self.assertDictEqual(info.attributes, {'attrib1': 'val1'})47 def test_commit_labels(self):48 """Tests that labels are updated correctly on commit."""49 self.mock_afe.get_hosts.return_value = [50 self._create_mock_host(['label1'], {})]51 info = host_info.HostInfo(['label2'], {})52 self.store._commit_impl(info)53 self.assertEqual(self.mock_afe.run.call_count, 2)54 expected_run_calls = [55 mock.call('host_remove_labels', id='some-host',56 labels=['label1']),57 mock.call('host_add_labels', id='some-host',58 labels=['label2']),59 ]60 self.mock_afe.run.assert_has_calls(expected_run_calls,61 any_order=True)62 def test_commit_labels_raises(self):63 """Test that exception while committing is translated properly."""64 self.mock_afe.get_hosts.return_value = [65 self._create_mock_host(['label1'], {})]66 self.mock_afe.run.side_effect = rpc_proxy.JSONRPCException('some error')67 info = host_info.HostInfo(['label2'], {})68 with self.assertRaises(host_info.StoreError):69 self.store._commit_impl(info)70 def test_commit_adds_attributes(self):71 """Tests that new attributes are added correctly on commit."""72 self.mock_afe.get_hosts.return_value = [73 self._create_mock_host([], {})]74 info = host_info.HostInfo([], {'attrib1': 'val1'})75 self.store._commit_impl(info)76 self.assertEqual(self.mock_afe.set_host_attribute.call_count, 1)77 self.mock_afe.set_host_attribute.assert_called_once_with(78 'attrib1', 'val1', hostname=self.hostname)79 def test_commit_updates_attributes(self):80 """Tests that existing attributes are updated correctly on commit."""81 self.mock_afe.get_hosts.return_value = [82 self._create_mock_host([], {'attrib1': 'val1'})]83 info = host_info.HostInfo([], {'attrib1': 'val1_updated'})84 self.store._commit_impl(info)85 self.assertEqual(self.mock_afe.set_host_attribute.call_count, 1)86 self.mock_afe.set_host_attribute.assert_called_once_with(87 'attrib1', 'val1_updated', hostname=self.hostname)88 def test_commit_deletes_attributes(self):89 """Tests that deleted attributes are updated correctly on commit."""90 self.mock_afe.get_hosts.return_value = [91 self._create_mock_host([], {'attrib1': 'val1'})]92 info = host_info.HostInfo([], {})93 self.store._commit_impl(info)94 self.assertEqual(self.mock_afe.set_host_attribute.call_count, 1)95 self.mock_afe.set_host_attribute.assert_called_once_with(96 'attrib1', None, hostname=self.hostname)97 def test_str(self):98 """Sanity tests the __str__ implementaiton"""99 self.assertEqual(str(self.store), 'AfeStore[some-host]')100class DictDiffTest(unittest.TestCase):101 """Tests the afe_store._dict_diff private method."""102 def _assert_dict_diff(self, got_tuple, expectation_tuple):103 """Verifies the result from _dict_diff104 @param got_tuple: The tuple returned by afe_store._dict_diff105 @param expectatin_tuple: tuple (left_only, right_only, differing)106 containing iterable of keys to verify against got_tuple.107 """108 for got, expect in zip(got_tuple, expectation_tuple):109 self.assertEqual(got, set(expect))110 def test_both_empty(self):111 """Tests the case when both dicts are empty."""112 self._assert_dict_diff(afe_store._dict_diff({}, {}),113 ((), (), ()))114 def test_right_dict_only(self):115 """Tests the case when left dict is empty."""116 self._assert_dict_diff(afe_store._dict_diff({}, {1: 1}),117 ((), (1,), ()))118 def test_left_dict_only(self):119 """Tests the case when right dict is empty."""120 self._assert_dict_diff(afe_store._dict_diff({1: 1}, {}),121 ((1,), (), ()))122 def test_left_dict_extra(self):123 """Tests the case when left dict has extra keys."""124 self._assert_dict_diff(afe_store._dict_diff({1: 1, 2: 2}, {1: 1}),125 ((2,), (), ()))126 def test_right_dict_extra(self):127 """Tests the case when right dict has extra keys."""128 self._assert_dict_diff(afe_store._dict_diff({1: 1}, {1: 1, 2: 2}),129 ((), (2,), ()))130 def test_identical_keys_with_different_values(self):131 """Tests the case when the set of keys is same, but values differ."""132 self._assert_dict_diff(afe_store._dict_diff({1: 1, 2: 3}, {1: 1, 2: 2}),133 ((), (), (2,)))134if __name__ == '__main__':...
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!!