def _test_sqcmds(testvar, context_config): output, error = conftest.setup_sqcmds(testvar, context_config) jout = [] if output: try: jout = json.loads(output.decode('utf-8').strip()) except json.JSONDecodeError: jout = output if 'output' in testvar: try: expected_jout = json.loads(testvar['output'].strip()) except json.JSONDecodeError: expected_jout = testvar['output'] assert (type(expected_jout) == type(jout)) if len(expected_jout) > 0: assert len(jout) > 0 elif not error and 'xfail' in testvar: # this was marked to fail, but it succeeded so we must return return elif error and 'xfail' in testvar and 'error' in testvar['xfail']: if jout.decode("utf-8") == testvar['xfail']['error']: assert False else: assert True elif 'error' in testvar and 'error' in testvar['error']: assert error, \ f"expected error, but got: output: {output}, error: {error}, xfail: {xfail}" else: raise Exception(f"either xfail or output requried {error}")
def _test_sqcmds(testvar, context_config): output, error = setup_sqcmds(testvar, context_config) if output: try: jout = json.loads(output.decode('utf-8').strip()) except json.JSONDecodeError: jout = output if 'output' in testvar: expected_df = pd.read_json(testvar['output'].strip()) try: got_df = pd.read_json(output.decode('utf8').strip()) except AttributeError: if output: got_df = pd.read_json(output) else: got_df = pd.DataFrame() # expected_df.sort_values(by=expected_df.columns[:1].tolist()) \ # .reset_index(drop=True) # got_df = got_df.sort_values(by=got_df.columns[:1].tolist()) \ # .reset_index(drop=True) # assert(expected_df.shape == got_df.shape) rslt_df = pd.merge(got_df.reset_index(drop=True), expected_df.reset_index(drop=True), left_index=True, right_index=True, indicator=True) assert (got_df.shape == expected_df.shape) if not got_df.empty: assert (not rslt_df.empty and rslt_df.query('_merge != "both"').empty) elif not error and 'xfail' in testvar: # this was marked to fail, but it succeeded so we must return return elif error and 'xfail' in testvar and 'error' in testvar['xfail']: if jout.decode("utf-8") == testvar['xfail']['error']: assert False else: assert True elif error and 'error' in testvar and 'error' in testvar['error']: try: got_df = pd.DataFrame(json.loads(error.decode('utf-8').strip())) except json.JSONDecodeError: got_df = pd.DataFrame(error.decode('utf-8').strip()) expected_df = pd.DataFrame(json.loads(testvar['error']['error'])) rslt_df = pd.merge(got_df.reset_index(drop=True), expected_df.reset_index(drop=True), left_index=True, right_index=True, how='outer', indicator=True) assert (got_df.shape == expected_df.shape) if not got_df.empty: assert (not rslt_df.empty and rslt_df.query('_merge != "both"').empty) else: raise Exception(f"either xfail or output requried {error}")
def _test_sqcmds(testvar, context_config): output, error = setup_sqcmds(testvar, context_config) if output: try: jout = json.loads(output.decode('utf-8').strip()) except json.JSONDecodeError: jout = output if 'ignore-columns' in testvar: ignore_cols = testvar['ignore-columns'].split() else: ignore_cols = [] if 'output' in testvar: if testvar.get('format', '') == "text": assert output.decode('utf8') == testvar['output'] return # pandas uses ujson and needs to escape "/" in any string its trying # to decode. This is true in the case of NXOS' LLDP description which # contains a URL causing read_json to abort with weird error messages. expected_df = pd.read_json(testvar['output'].strip().replace( '/', r'\/')) try: got_df = pd.read_json(output.decode('utf8').strip()) except AttributeError: if output: got_df = pd.read_json(output) else: got_df = pd.DataFrame() # expected_df.sort_values(by=expected_df.columns[:1].tolist()) \ # .reset_index(drop=True) # got_df = got_df.sort_values(by=got_df.columns[:1].tolist()) \ # .reset_index(drop=True) # assert(expected_df.shape == got_df.shape) assert_df_equal(expected_df, got_df, ignore_cols) elif not error and 'xfail' in testvar: # this was marked to fail, but it succeeded so we must return return elif error and 'xfail' in testvar and 'error' in testvar['xfail']: if jout.decode("utf-8") == testvar['xfail']['error']: assert False else: assert True elif error and 'error' in testvar and 'error' in testvar['error']: try: got_df = pd.DataFrame(json.loads(error.decode('utf-8').strip())) except json.JSONDecodeError: got_df = pd.DataFrame({'error': [error.decode('utf-8').strip()]}) expected_df = pd.DataFrame(json.loads(testvar['error']['error'])) assert_df_equal(expected_df, got_df, ignore_cols) else: raise Exception(f"either xfail or output requried {error}")
def _test_sqcmds(testvar, context_config): output, error = setup_sqcmds(testvar, context_config) if output: try: jout = json.loads(output.decode('utf-8').strip()) except json.JSONDecodeError: jout = output if 'ignore-columns' in testvar: ignore_cols = testvar['ignore-columns'].split() else: ignore_cols = [] if 'output' in testvar: expected_df = pd.read_json(testvar['output'].strip()) try: got_df = pd.read_json(output.decode('utf8').strip()) except AttributeError: if output: got_df = pd.read_json(output) else: got_df = pd.DataFrame() # expected_df.sort_values(by=expected_df.columns[:1].tolist()) \ # .reset_index(drop=True) # got_df = got_df.sort_values(by=got_df.columns[:1].tolist()) \ # .reset_index(drop=True) # assert(expected_df.shape == got_df.shape) assert_df_equal(expected_df, got_df, ignore_cols) elif not error and 'xfail' in testvar: # this was marked to fail, but it succeeded so we must return return elif error and 'xfail' in testvar and 'error' in testvar['xfail']: if jout.decode("utf-8") == testvar['xfail']['error']: assert False else: assert True elif error and 'error' in testvar and 'error' in testvar['error']: try: got_df = pd.DataFrame(json.loads(error.decode('utf-8').strip())) except json.JSONDecodeError: got_df = pd.DataFrame(error.decode('utf-8').strip()) expected_df = pd.DataFrame(json.loads(testvar['error']['error'])) assert_df_equal(expected_df, got_df, ignore_cols) else: raise Exception(f"either xfail or output requried {error}")
def _test_sqcmds(testvar, context_config): output, error = setup_sqcmds(testvar, context_config) jout = [] # out_df = pd.DataFrame() if output: try: jout = json.loads(output.decode('utf-8').strip()) # out_df = pd.read_json(output.decode('utf-8').strip()) except json.JSONDecodeError: jout = output if 'output' in testvar: # expected_df = pd.DataFrame() try: expected_jout = json.loads(testvar['output'].strip()) # expected_df = pd.read_json(testvar['output'].strip()) except json.JSONDecodeError: expected_jout = testvar['output'] # # get rid of time columns to compare against # # if not out_df.empty and not expected_df.empty: # for col in ['timestamp', 'lastChangeTime', 'first_time']: # if col in out_df.columns: # out_df = out_df.drop(columns=[col]) # if col in expected_df: # expected_df = expected_df.drop(columns=[col]) # pd.testing.assert_frame_equal(out_df, expected_df) # jout = json.loads(out_df.to_json()) # expected_jout = json.loads(expected_df.to_json()) assert (type(expected_jout) == type(jout)) if isinstance(jout, dict): assert (Counter(expected_jout) == Counter(jout)) return try: expected_setlist = set(tuple(sorted(d.items())) for d in jout) got_setlist = set(tuple(sorted(d.items())) for d in expected_jout) assert (expected_setlist == got_setlist) except TypeError: # This is for commands that return lists in their outputs. # This isn't robust, because it assumes that we get the output # back in sorted order except that the keys within each entry # are not sorted. If the outer sort is changed, this won't work expected = [sorted(d.items()) for d in expected_jout] got = [sorted(d.items()) for d in jout] assert (expected == got) elif not error and 'xfail' in testvar: # this was marked to fail, but it succeeded so we must return return elif error and 'xfail' in testvar and 'error' in testvar['xfail']: if jout.decode("utf-8") == testvar['xfail']['error']: assert False else: assert True elif error and 'error' in testvar and 'error' in testvar['error']: try: jerror = json.loads(error.decode('utf-8').strip()) except json.JSONDecodeError: jerror = error.decode('utf-8').strip() jterror = json.loads(testvar['error']['error']) assert jerror == jterror else: raise Exception(f"either xfail or output requried {error}")