Skip to content

Commit

Permalink
Add tests to test all the logging capabilities
Browse files Browse the repository at this point in the history
Currently , there are no tests to test textattack attack logging into
files such as csv, text and json.

Commit adds tests to test logging in the 3 above mentioned formats.
  • Loading branch information
VijayKalmath committed Jun 21, 2022
1 parent 646b50d commit af17300
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -47,3 +47,4 @@ checkpoints/

.vscode
*.csv
!tests/sample_outputs/csv_attack_log.csv
3 changes: 3 additions & 0 deletions tests/sample_outputs/csv_attack_log.csv
@@ -0,0 +1,3 @@
"original_text","perturbed_text","original_score","perturbed_score","original_output","perturbed_output","ground_truth_output","num_queries","result_type"
"lovingly photographed in the manner of a golden book sprung to [[life]] , stuart little 2 [[manages]] [[sweetness]] largely without stickiness .","lovingly photographed in the manner of a golden book sprung to [[ife]] , stuart little 2 [[manager]] [[seetness]] largely without stickiness .",0.09334743022918701,0.6904040575027466,1,0,1,28,"Successful"
"[[consistently]] [[clever]] and [[suspenseful]] .","[[conisstently]] [[celver]] and [[Huspenseful]] .",0.009427368640899658,0.8219608664512634,1,0,1,16,"Successful"
13 changes: 13 additions & 0 deletions tests/sample_outputs/json_attack_summary.json
@@ -0,0 +1,13 @@
{
"Attack Results": {
"Number of successful attacks:": 2,
"Number of failed attacks:": 0,
"Number of skipped attacks:": 0,
"Original accuracy:": 100.0,
"Accuracy under attack:": 0.0,
"Attack success rate:": 100.0,
"Average perturbed word %:": 45.0,
"Average num. words per input:": 12.0,
"Avg num queries:": 22.0
}
}
21 changes: 21 additions & 0 deletions tests/sample_outputs/txt_attack_log.txt
@@ -0,0 +1,21 @@
--------------------------------------------- Result 1 ---------------------------------------------
[[Positive (91%)]] --> [[Negative (69%)]]

lovingly photographed in the manner of a golden book sprung to [[life]] , stuart little 2 [[manages]] [[sweetness]] largely without stickiness .

lovingly photographed in the manner of a golden book sprung to [[ife]] , stuart little 2 [[manager]] [[seetness]] largely without stickiness .
--------------------------------------------- Result 2 ---------------------------------------------
[[Positive (99%)]] --> [[Negative (82%)]]

[[consistently]] [[clever]] and [[suspenseful]] .

[[conisstently]] [[celver]] and [[Huspenseful]] .
Number of successful attacks: 2
Number of failed attacks: 0
Number of skipped attacks: 0
Original accuracy: 100.0%
Accuracy under attack: 0.0%
Attack success rate: 100.0%
Average perturbed word %: 45.0%
Average num. words per input: 12.0
Avg num queries: 22.0
84 changes: 84 additions & 0 deletions tests/test_command_line/test_loggers.py
@@ -0,0 +1,84 @@
import json
import os

from helpers import run_command_and_get_result
import pytest

DEBUG = False

"""
Attack command-line tests in the format (name, args, sample_output_file)
"""

"""
list_test_params data structure requires
1) test name
2) logger filetype - json/text/csv. # Future Work : Tests for Wandb and Visdom
3) logger file name
4) sample log file
"""

list_test_params = [
(
"json_summary_logger",
"json",
"textattack attack --recipe deepwordbug --model lstm-mr --num-examples 2 --log-summary-to-json attack_summary.json",
"attack_summary.json",
"tests/sample_outputs/json_attack_summary.json",
),
(
"csv_logger",
"csv",
"textattack attack --recipe deepwordbug --model lstm-mr --num-examples 2 --log-to-csv attack_log.csv",
"attack_log.csv",
"tests/sample_outputs/csv_attack_log.csv",
),
(
"txt_logger",
"txt",
"textattack attack --recipe deepwordbug --model lstm-mr --num-examples 2 --log-to-txt attack_log.txt",
"attack_log.txt",
"tests/sample_outputs/txt_attack_log.txt",
),
]


@pytest.mark.parametrize(
"name, filetype, command, test_log_file, sample_log_file", list_test_params
)
def test_logger(name, filetype, command, test_log_file, sample_log_file):
# Run command and validate outputs.
result = run_command_and_get_result(command)

assert result.stdout is not None
assert result.stderr is not None
assert result.returncode == 0
assert os.path.exists(test_log_file), f"{test_log_file} did not get generated"

if filetype == "json":
with open(sample_log_file) as f:
desired_dictionary = json.load(f)

with open(test_log_file) as f:
test_dictionary = json.load(f)

assert (
desired_dictionary == test_dictionary
), f"{filetype} file {test_log_file} differs from {sample_log_file}"

elif filetype == "csv" or filetype == "txt":
assert (
os.system(f"diff {test_log_file} {sample_log_file}") == 0
), f"{filetype} file {test_log_file} differs from {sample_log_file}"

# cleanup
os.remove(test_log_file)


# result = run_command_and_get_result(command)
# stdout = result.stdout.decode().strip()
# print("stdout =>", stdout)
# stderr = result.stderr.decode().strip()
# print("stderr =>", stderr)

# assert stdout == desired_text

0 comments on commit af17300

Please sign in to comment.