Skip to content

Commit

Permalink
final improvments on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yibeichan committed Apr 21, 2024
1 parent e1e847d commit 51d30b7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 26 deletions.
3 changes: 2 additions & 1 deletion reproschema/redcap2reproschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def clean_header(header):
cleaned_header[cleaned_key] = v
return cleaned_header


def normalize_condition(condition_str):
# Regular expressions for various pattern replacements
re_parentheses = re.compile(r"\(([0-9]*)\)")
Expand All @@ -42,6 +43,7 @@ def normalize_condition(condition_str):

return condition_str.strip()


def process_visibility(data):
condition = data.get("Branching Logic (Show field only if...)")
if condition:
Expand Down Expand Up @@ -94,7 +96,6 @@ def parse_field_type_and_value(field, input_type_map):
return input_type, value_type



def process_choices(field_type, choices_str):
if field_type not in ["radio", "dropdown"]: # Handle only radio and dropdown types
return None
Expand Down
1 change: 0 additions & 1 deletion reproschema/reproschema2redcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def get_csv_data(dir_path):
if item_json:
row_data = process_item(item_json, activity_path.stem)
csv_data.append(row_data)
print(f"Processed item {item_path}")

# Break after finding the first _schema file
break
Expand Down
7 changes: 1 addition & 6 deletions reproschema/tests/test_redcap2reproschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
os.path.dirname(__file__), "test_redcap2rs_data", YAML_FILE_NAME
)

def test_redcap2reproschema_success(tmpdir):
def test_redcap2reproschema(tmpdir):
runner = CliRunner()

# Define the paths to the CSV and YAML files in the temporary directory
temp_csv_file = tmpdir.join(CSV_FILE_NAME)
temp_yaml_file = tmpdir.join(YAML_FILE_NAME)

# Copy the test files to the temporary directory
shutil.copy(CSV_TEST_FILE, str(temp_csv_file)) # Convert to string
shutil.copy(YAML_TEST_FILE, str(temp_yaml_file)) # Convert to string

Expand All @@ -32,12 +30,9 @@ def test_redcap2reproschema_success(tmpdir):
protocol = yaml.safe_load(file)
protocol_name = protocol.get("protocol_name", "").replace(" ", "_")

# Run the command with the path arguments pointing to the temp directory files
result = runner.invoke(
main, ["redcap2reproschema", str(temp_csv_file), str(temp_yaml_file)] # Convert to string
)

# Assertions
assert result.exit_code == 0, f"The command failed to execute successfully: {result.output}"
assert os.path.isdir(protocol_name), f"Expected output directory '{protocol_name}' does not exist"
print("Command output:", result.output)
22 changes: 4 additions & 18 deletions reproschema/tests/test_reproschema2redcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,35 @@
from shutil import copytree, rmtree
from pathlib import Path
import csv
import tempfile

def test_reproschema2redcap_success():
def test_reproschema2redcap(tmpdir):
runner = CliRunner()

with runner.isolated_filesystem():
# Create a temporary directory for output
temp_dir = tempfile.mkdtemp(dir='.')
print(f"Temporary directory: {temp_dir}")
# Copy necessary test data into the isolated filesystem
original_data_dir = os.path.join(
os.path.dirname(__file__), "test_rs2redcap_data", "test_redcap2rs"
)
copytree(original_data_dir, "input_data")

input_path = Path("input_data") # Using Path object
output_csv_path = os.path.join(temp_dir, "output.csv")
input_path = Path("input_data")
output_csv_path = os.path.join(tmpdir, "output.csv")

# Invoke the reproschema2redcap command
result = runner.invoke(
main, ["reproschema2redcap", str(input_path), output_csv_path]
)

# Print the output for debugging
print(result.output)

# Assert the expected outcomes
assert result.exit_code == 0

# Check if the output CSV file has been created
assert os.path.exists(output_csv_path)

# Read and print the contents of the CSV file
with open(output_csv_path, "r", encoding="utf-8") as csv_file:
reader = csv.reader(csv_file)
csv_contents = list(reader)
print("CSV File Contents:")
for row in csv_contents:
print(row)

# Optionally, assert conditions about the CSV contents
# For example, assert that the file has more than just headers
assert len(csv_contents) > 1 # More than one row indicates content beyond headers

# Clean up temporary directory after use (optional)
# rmtree(temp_dir)
# rmtree(tmpdir)

0 comments on commit 51d30b7

Please sign in to comment.