Best Python code snippet using lisa_python
test_variable.py
Source:test_variable.py  
...66            {},67        )68    def test_in_runbook_format_file(self) -> None:69        runbook_data: Dict[str, Any] = {"variable": [{"file": "variable_normal.yml"}]}70        data = self._test_runbook_file_entry(71            runbook_data,72            {73                "secret_guid": "12345678-****-****-****-********90ab",74                "secret_int": "1****0",75                "secret_head_tail": "a****h",76            },77            {},78        )79        self.assertEqual("12345678-abcd-efab-cdef-1234567890ab", data["list"][0])80        self.assertEqual(1234567890, data["list"][1]["dictInList"])81        self.assertEqual("abcdefgh", data["headtail"])82        self.assertEqual("normal_value", data["nested"]["normal_value"])83        self.assertEqual("entry_value", data["normal_entry"])84    def test_in_variable_path_with_variable(self) -> None:85        runbook_data: Dict[str, Any] = {86            "variable": [87                {"file": "variable_$(var_in_var1).yml"},88                {"name": "var_in_var1", "value": "$(var_in_var2)"},89                {"name": "var_in_var2", "value": "normal"},90            ]91        }92        data = self._test_runbook_file_entry(93            runbook_data,94            {95                "secret_guid": "12345678-****-****-****-********90ab",96                "secret_int": "1****0",97                "secret_head_tail": "a****h",98            },99            {},100        )101        self.assertEqual("12345678-abcd-efab-cdef-1234567890ab", data["list"][0])102        self.assertEqual(1234567890, data["list"][1]["dictInList"])103        self.assertEqual("abcdefgh", data["headtail"])104        self.assertEqual("normal_value", data["nested"]["normal_value"])105        self.assertEqual("entry_value", data["normal_entry"])106    def test_in_runbook_path_with_variable(self) -> None:107        runbook_data: Dict[str, Any] = {108            "variable": [{"file": "variable_$(var_in_cmd).yml"}]109        }110        data = self._test_runbook_file_entry(111            runbook_data,112            {113                "secret_guid": "12345678-****-****-****-********90ab",114                "secret_int": "1****0",115                "secret_head_tail": "a****h",116            },117            {118                "var_in_cmd": variable.VariableEntry(119                    name="var_in_cmd", data="normal", is_used=False120                )121            },122        )123        self.assertEqual("12345678-abcd-efab-cdef-1234567890ab", data["list"][0])124        self.assertEqual(1234567890, data["list"][1]["dictInList"])125        self.assertEqual("abcdefgh", data["headtail"])126        self.assertEqual("normal_value", data["nested"]["normal_value"])127        self.assertEqual("entry_value", data["normal_entry"])128    def test_in_runbook_format_variable(self) -> None:129        runbook_data: Dict[str, Any] = {130            "variable": [131                {"name": "normal_value", "value": "normal_value"},132                {"name": "normal_entry", "value": "entry_value"},133                {134                    "name": "secret_guid",135                    "value": "12345678-abcd-efab-cdef-1234567890ab",136                    "is_secret": True,137                    "mask": "guid",138                },139                {140                    "name": "secret_int",141                    "value": 1234567890,142                    "is_secret": True,143                    "mask": "headtail",144                },145                {146                    "name": "secret_head_tail",147                    "value": "abcdefgh",148                    "is_secret": True,149                    "mask": "headtail",150                },151            ]152        }153        data = self._test_runbook_file_entry(154            runbook_data,155            {156                "secret_guid": "12345678-****-****-****-********90ab",157                "secret_int": "1****0",158                "secret_head_tail": "a****h",159            },160            {},161        )162        self.assertEqual("12345678-abcd-efab-cdef-1234567890ab", data["list"][0])163        self.assertEqual(1234567890, data["list"][1]["dictInList"])164        self.assertEqual("abcdefgh", data["headtail"])165        self.assertEqual("normal_value", data["nested"]["normal_value"])166        self.assertEqual("entry_value", data["normal_entry"])167    def test_in_runbook_ordered(self) -> None:168        runbook_data: Dict[str, Any] = {169            "variable": [170                {"file": "variable_normal.yml"},171                {"name": "normal_value", "value": "normal_value1"},172                {"name": "normal_entry", "value": "entry_value1"},173                {174                    "name": "secret_guid",175                    "value": "12345678-abcd-efab-cdef-1234567890ac",176                    "is_secret": True,177                    "mask": "guid",178                },179                {180                    "name": "secret_int",181                    "value": 1234567891,182                    "is_secret": True,183                    "mask": "headtail",184                },185                {186                    "name": "secret_head_tail",187                    "value": "abcdefgi",188                    "is_secret": True,189                    "mask": "headtail",190                },191            ]192        }193        data = self._test_runbook_file_entry(194            runbook_data,195            {196                "secret_guid": "12345678-****-****-****-********90ac",197                "secret_int": "1****1",198                "secret_head_tail": "a****i",199            },200            {},201        )202        self.assertEqual("12345678-abcd-efab-cdef-1234567890ac", data["list"][0])203        self.assertEqual(1234567891, data["list"][1]["dictInList"])204        self.assertEqual("abcdefgi", data["headtail"])205        self.assertEqual("normal_value1", data["nested"]["normal_value"])206        self.assertEqual("entry_value1", data["normal_entry"])207    def test_variable_not_found(self) -> None:208        variables = self._get_default_variables()209        with self.assertRaises(LisaException) as cm:210            variable.replace_variables({"item": "$(notexists)"}, variables)211        self.assertIsInstance(cm.exception, LisaException)212        self.assertIn("cannot find variable", str(cm.exception))213    def test_variable_not_used(self) -> None:214        variables = self._get_default_variables()215        variables["unused"] = variable.VariableEntry(name="unused", data="value")216        self.assertFalse(variables["unused"].is_used)217        self.assertFalse(variables["normal_value"].is_used)218        self._replace_and_validate(variables, {"normal_entry": "original"})219        self.assertFalse(variables["unused"].is_used)220        self.assertTrue(variables["normal_value"].is_used)221    def test_invalid_file_extension(self) -> None:222        variables = self._get_default_variables()223        with self.assertRaises(LisaException) as cm:224            variables.update(variable._load_from_file("file.xml"))225        self.assertIsInstance(cm.exception, LisaException)226        self.assertIn("variable support only yaml and yml", str(cm.exception))227    def _test_runbook_file_entry(228        self,229        data: Any,230        secret_variables: Dict[str, str],231        current_variables: Dict[str, variable.VariableEntry],232    ) -> Any:233        constants.RUNBOOK_PATH = Path(__file__).parent234        variables = self._get_default_variables()235        variables.update(variable._load_from_runbook(data, current_variables))236        data = self._replace_and_validate(variables, secret_variables)237        return data238    def _test_files(239        self, file_name: str, all_secret: bool, secret_variables: Dict[str, str]240    ) -> Any:241        constants.RUNBOOK_PATH = Path(__file__).parent...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!!
