MCPcopy
hub / github.com/QData/TextAttack / test_logger

Function test_logger

tests/test_command_line/test_loggers.py:48–109  ·  view source on GitHub ↗
(name, filetype, command, test_log_file, sample_log_file)

Source from the content-addressed store, hash-verified

46 "name, filetype, command, test_log_file, sample_log_file", list_test_params
47)
48def test_logger(name, filetype, command, test_log_file, sample_log_file):
49 # Run command and validate outputs.
50 result = run_command_and_get_result(command)
51
52 assert result.stdout is not None
53 assert result.stderr is not None
54 assert result.returncode == 0
55 assert os.path.exists(test_log_file), f"{test_log_file} did not get generated"
56
57 if filetype == "json":
58 with open(sample_log_file) as f:
59 desired_dictionary = json.load(f)
60
61 with open(test_log_file) as f:
62 test_dictionary = json.load(f)
63
64 assert (
65 desired_dictionary == test_dictionary
66 ), f"{filetype} file {test_log_file} differs from {sample_log_file}"
67
68 elif filetype == "txt":
69 with open(sample_log_file) as f:
70 desired_output = f.read().strip()
71 with open(test_log_file) as f:
72 test_output = f.read().strip()
73 desired_re = (
74 re.escape(desired_output)
75 .replace("/\\.\\/", ".")
76 .replace("/\\.\\*/", ".*")
77 .replace("\\/\\.\\*\\/", ".*")
78 )
79 assert re.match(
80 desired_re, test_output, flags=re.S
81 ), f"{filetype} file {test_log_file} differs from {sample_log_file}"
82
83 elif filetype == "csv":
84 import pandas as pd
85
86 # Convert them into dataframes and compare.
87 test_df = pd.read_csv(test_log_file)
88 sample_df = pd.read_csv(sample_log_file)
89 try:
90 test_df = test_df[sorted(list(test_df.columns.values))]
91 sample_df = sample_df[sorted(list(test_df.columns.values))]
92
93 for c in test_df.columns:
94 if test_df[c].dtype == int:
95 test_df[c] = test_df[c].astype(float)
96
97 if sample_df[c].dtype == int:
98 sample_df[c] = sample_df[c].astype(float)
99 except KeyError:
100 assert (
101 False
102 ), f"{filetype} file {test_log_file} differs from {sample_log_file}"
103
104 assert sample_df.equals(
105 test_df

Callers

nothing calls this directly

Calls 3

loadMethod · 0.80
stripMethod · 0.80

Tested by

no test coverage detected