Best Python code snippet using molecule_python
_metadata_test.py
Source:_metadata_test.py
1# Copyright 2020 gRPC authors.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 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.14"""Tests for the metadata abstraction that's used in the asynchronous driver."""15import logging16import unittest17from grpc.experimental.aio import Metadata18class TestTypeMetadata(unittest.TestCase):19 """Tests for the metadata type"""20 _DEFAULT_DATA = (("key1", "value1"), ("key2", "value2"))21 _MULTI_ENTRY_DATA = (("key1", "value1"), ("key1", "other value 1"),22 ("key2", "value2"))23 def test_init_metadata(self):24 test_cases = {25 "emtpy": (),26 "with-single-data": self._DEFAULT_DATA,27 "with-multi-data": self._MULTI_ENTRY_DATA,28 }29 for case, args in test_cases.items():30 with self.subTest(case=case):31 metadata = Metadata(*args)32 self.assertEqual(len(metadata), len(args))33 def test_get_item(self):34 metadata = Metadata(("key", "value1"), ("key", "value2"),35 ("key2", "other value"))36 self.assertEqual(metadata["key"], "value1")37 self.assertEqual(metadata["key2"], "other value")38 self.assertEqual(metadata.get("key"), "value1")39 self.assertEqual(metadata.get("key2"), "other value")40 with self.assertRaises(KeyError):41 metadata["key not found"]42 self.assertIsNone(metadata.get("key not found"))43 def test_add_value(self):44 metadata = Metadata()45 metadata.add("key", "value")46 metadata.add("key", "second value")47 metadata.add("key2", "value2")48 self.assertEqual(metadata["key"], "value")49 self.assertEqual(metadata["key2"], "value2")50 def test_get_all_items(self):51 metadata = Metadata(*self._MULTI_ENTRY_DATA)52 self.assertEqual(metadata.get_all("key1"), ["value1", "other value 1"])53 self.assertEqual(metadata.get_all("key2"), ["value2"])54 self.assertEqual(metadata.get_all("non existing key"), [])55 def test_container(self):56 metadata = Metadata(*self._MULTI_ENTRY_DATA)57 self.assertIn("key1", metadata)58 def test_equals(self):59 metadata = Metadata()60 for key, value in self._DEFAULT_DATA:61 metadata.add(key, value)62 metadata2 = Metadata(*self._DEFAULT_DATA)63 self.assertEqual(metadata, metadata2)64 self.assertNotEqual(metadata, "foo")65 def test_repr(self):66 metadata = Metadata(*self._DEFAULT_DATA)67 expected = "Metadata({0!r})".format(self._DEFAULT_DATA)68 self.assertEqual(repr(metadata), expected)69 def test_set(self):70 metadata = Metadata(*self._MULTI_ENTRY_DATA)71 override_value = "override value"72 for _ in range(3):73 metadata["key1"] = override_value74 self.assertEqual(metadata["key1"], override_value)75 self.assertEqual(metadata.get_all("key1"),76 [override_value, "other value 1"])77 empty_metadata = Metadata()78 for _ in range(3):79 empty_metadata["key"] = override_value80 self.assertEqual(empty_metadata["key"], override_value)81 self.assertEqual(empty_metadata.get_all("key"), [override_value])82 def test_set_all(self):83 metadata = Metadata(*self._DEFAULT_DATA)84 metadata.set_all("key", ["value1", b"new value 2"])85 self.assertEqual(metadata["key"], "value1")86 self.assertEqual(metadata.get_all("key"), ["value1", b"new value 2"])87 def test_delete_values(self):88 metadata = Metadata(*self._MULTI_ENTRY_DATA)89 del metadata["key1"]90 self.assertEqual(metadata.get("key1"), "other value 1")91 metadata.delete_all("key1")92 self.assertNotIn("key1", metadata)93 metadata.delete_all("key2")94 self.assertEqual(len(metadata), 0)95 with self.assertRaises(KeyError):96 del metadata["other key"]97 def test_metadata_from_tuple(self):98 scenarios = (99 (None, Metadata()),100 (Metadata(), Metadata()),101 (self._DEFAULT_DATA, Metadata(*self._DEFAULT_DATA)),102 (self._MULTI_ENTRY_DATA, Metadata(*self._MULTI_ENTRY_DATA)),103 (Metadata(*self._DEFAULT_DATA), Metadata(*self._DEFAULT_DATA)),104 )105 for source, expected in scenarios:106 with self.subTest(raw_metadata=source, expected=expected):107 self.assertEqual(expected, Metadata.from_tuple(source))108if __name__ == '__main__':109 logging.basicConfig()...
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!!