How to use _validate_variables method in lisa

Best Python code snippet using lisa_python

test__variables_parsing.py

Source:test__variables_parsing.py Github

copy

Full Screen

...162 )163class TestValidateVariablesParsing:164 def test__empty_dict__should_be_left_as_is(self, parser: ConfigParser) -> None:165 dummy_variables: Dict[str, List[str]] = {}166 result = parser._validate_variables(variables=dummy_variables)167 assert result == {}168 def test__scalar_value__error_should_be_raised(self, parser: ConfigParser) -> None:169 dummy_variables = "I am a string"170 with pytest.raises(ParserError) as exception_info:171 parser._validate_variables(variables=dummy_variables)172 expected = r"The 'variables' section should have the following syntax: 'VARIABLE_NAME' = \['var_1', 'var_2', \.\.\] !"173 assert exception_info.match(expected)174 def test__non_compatible_variable__error_should_be_raised(175 self, parser: ConfigParser176 ) -> None:177 dummy_variables = {178 "VARIABLE": {"this is not a list": 42},179 }180 with pytest.raises(ParserError) as exception_info:181 parser._validate_variables(variables=dummy_variables)182 expected = "The 'variables' section should contain a single string or a list of strings for a variable name!"183 assert exception_info.match(expected)184 def test__non_list_variable_value__should_be_converted_to_a_list(185 self, parser: ConfigParser186 ) -> None:187 dummy_variables = {188 "VARIABLE": "I am not a list",189 }190 result = parser._validate_variables(variables=dummy_variables)191 assert result == {192 "VARIABLE": ["I am not a list"],193 }194 def test__list_variable_values__should_be_left_as_is(195 self, parser: ConfigParser196 ) -> None:197 dummy_variables = {198 "VARIABLE": ["I", "am", "a", "list"],199 }200 result = parser._validate_variables(variables=dummy_variables)201 assert result == {202 "VARIABLE": ["I", "am", "a", "list"],203 }204 def test__non_string_items__error_should_be_raised(205 self, parser: ConfigParser206 ) -> None:207 dummy_variables = {208 "VARIABLE": ["I am a string", 42],209 }210 with pytest.raises(ParserError) as exception_info:211 parser._validate_variables(variables=dummy_variables)212 expected = "The 'variables' section should contain a single string or a list of strings for a variable name!"...

Full Screen

Full Screen

test_transformer.py

Source:test_transformer.py Github

copy

Full Screen

...58 # no value is overridden, all values are kept59 transformers = self._generate_transformers_runbook(2)60 runbook_builder = self._generate_runbook_builder(transformers)61 result = transformer._run_transformers(runbook_builder)62 self._validate_variables(63 {64 "v0": "original",65 "va": "original",66 "t0_v0": "0_0 processed",67 "t1_v0": "1_0 processed",68 "t1_v1": "1_1 processed",69 },70 result,71 )72 def test_transformer_overridden_values(self) -> None:73 # value is overridden74 transformers = self._generate_transformers_runbook(2)75 transformers[0].rename = {"t0_v0": "v0"}76 transformers[1].rename = {"t1_v0": "v0", "t1_v1": "v1"}77 runbook_builder = self._generate_runbook_builder(transformers)78 result = transformer._run_transformers(runbook_builder)79 self._validate_variables(80 {81 "va": "original",82 "v0": "1_0 processed",83 "v1": "1_1 processed",84 },85 result,86 )87 def test_transformer_rename_not_exist(self) -> None:88 # not exist name raise exception89 transformers = self._generate_transformers_runbook(1)90 transformers[0].rename = {"v0": "v0_1"}91 runbook_builder = self._generate_runbook_builder(transformers)92 with self.assertRaises(LisaException) as cm:93 transformer._run_transformers(runbook_builder)94 self.assertEqual("unmatched rename variable: {'v0': 'v0_1'}", str(cm.exception))95 def test_transformer_customized_prefix(self) -> None:96 # modified prefix97 transformers = self._generate_transformers_runbook(1)98 transformers[0].prefix = "v0_1"99 runbook_builder = self._generate_runbook_builder(transformers)100 result = transformer._run_transformers(runbook_builder)101 self._validate_variables(102 {103 "v0": "original",104 "va": "original",105 "v0_1_v0": "0_0 processed",106 },107 result,108 )109 def test_transformer_no_name(self) -> None:110 # no name, the type will be used. in name111 transformers_data: List[Any] = [{"type": MOCK, "items": {"v0": "v0_1"}}]112 transformers = schema.load_by_type_many(schema.Transformer, transformers_data)113 runbook_builder = self._generate_runbook_builder(transformers)114 result = transformer._run_transformers(runbook_builder)115 self._validate_variables(116 {117 "v0": "original",118 "va": "original",119 "mock_v0": "v0_1 processed",120 },121 result,122 )123 def test_transformer_skip_disabled(self) -> None:124 # the second transformer should be skipped, so the value is original.125 transformers = self._generate_transformers_runbook(2)126 transformers[0].rename = {"t0_v0": "v0"}127 transformers[0].enabled = False128 runbook_builder = self._generate_runbook_builder(transformers)129 result = transformer._run_transformers(runbook_builder)130 self._validate_variables(131 {132 "v0": "original",133 "va": "original",134 "t1_v0": "1_0 processed",135 "t1_v1": "1_1 processed",136 },137 result,138 )139 def _validate_variables(140 self, expected: Dict[str, str], actual: Dict[str, VariableEntry]141 ) -> None:142 actual_pairs = {name: value.data for name, value in actual.items()}143 self.assertDictEqual(expected, actual_pairs)144 def _generate_runbook_builder(145 self, transformers: List[schema.Transformer]146 ) -> RunbookBuilder:147 transformers_data: List[Any] = [148 x.to_dict() for x in transformers # type:ignore149 ]150 runbook_builder = RunbookBuilder(Path("mock_runbook.yml"))151 runbook_builder._raw_data = {152 constants.TRANSFORMER: transformers_data,153 }...

Full Screen

Full Screen

psi.py

Source:psi.py Github

copy

Full Screen

...3"""4from typing import Union5import numpy as np6import pandas as pd7def _validate_variables(8 df : pd.DataFrame = None,9 variables : Union[str,list] = None):10 """Validate that `df` is a `pandas.DataFrame` and11 `variables` is a subset of columns"""12 if not isinstance(df, pd.DataFrame):13 raise TypeError("df must be instance of pandas.DataFrame")14 cols = df.columns.tolist()15 cols_to_str = ", ".join(cols)16 if variables is None:17 return cols18 elif isinstance(variables, str):19 if not variables in cols:20 raise ValueError(f"{variables} not in `df.columns`: {cols_to_str}")21 else:22 return [variables]23 elif isinstance(variables, list):24 missing_vars = [i for i in variables if i not in cols]25 if len(missing_vars) > 0:26 missing_vars = ", ".join(missing_vars)27 raise ValueError(f"{missing_vars} not in `df.columns`: {cols_to_str}")28 else:29 return variables30 else:31 t = str(type(variables))32 raise TypeError(f"`variables` must be str or list, but found {t}")33def _psi(e : np.ndarray, o : np.ndarray, k = 1e-6):34 assert e.shape == o.shape35 assert len(e.shape) == 136 if k > 0:37 e = np.maximum(e,k)38 o = np.maximum(o,k)39 psi = np.dot((o - e) , np.log(o / e))40 return psi41def _get_frequencies(df : pd.DataFrame = None, cols : list = None):42 dist = {}43 for c in cols:44 d = df.loc[:,c].value_counts(normalize = True, dropna = False).to_dict()45 dist[c] = d46 return dist47def _dicts_to_df(d1, d2, fillna = 0):48 d1 = pd.DataFrame.from_dict(d1, orient='index').sort_index()[0]49 d2 = pd.DataFrame.from_dict(d2, orient='index').sort_index()[0]50 d = pd.concat([d1,d2],axis=1).fillna(fillna)51 return d52class PSI:53 """Class for calculating Population Stability Index"""54 def __init__(self):55 self._cols = None56 self._dist = None57 def fit(self, df : pd.DataFrame = None, variables : Union[str,list] = None):58 """Fit method59 Parameters60 ----------61 df : pandas.DataFrame62 variables : Union[str,list]63 """64 df = df.copy()65 cols = _validate_variables(df, variables)66 self._cols = cols67 self._dist = _get_frequencies(df, cols)68 def transform(self, df : pd.DataFrame):69 df = df.copy()70 _cols = _validate_variables(df, self._cols)71 dist = _get_frequencies(df, self._cols)72 res = {}73 for col in self._cols:74 d = _dicts_to_df(self._dist[col],dist[col])75 psi = _psi(d.iloc[:,0].values, d.iloc[:,1].values)76 res[col] = psi...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lisa automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful