|
| 1 | +"""Shared pytest configuration and fixtures.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +import tempfile |
| 5 | +from unittest.mock import Mock |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def temp_csv_file(): |
| 13 | + """Create a temporary CSV file.""" |
| 14 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as tmp: |
| 15 | + tmp_path = tmp.name |
| 16 | + yield Path(tmp_path) |
| 17 | + Path(tmp_path).unlink(missing_ok=True) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def temp_excel_file(): |
| 22 | + """Create a temporary Excel file.""" |
| 23 | + with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp: |
| 24 | + tmp_path = tmp.name |
| 25 | + yield Path(tmp_path) |
| 26 | + Path(tmp_path).unlink(missing_ok=True) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def temp_dir(): |
| 31 | + """Create a temporary directory.""" |
| 32 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 33 | + yield Path(tmpdir) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def mock_redcap_response(): |
| 38 | + """Mock successful REDCap API response.""" |
| 39 | + response = Mock() |
| 40 | + response.status_code = 200 |
| 41 | + response.text = "1" |
| 42 | + return response |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def mock_ripple_response(): |
| 47 | + """Mock successful Ripple API response.""" |
| 48 | + response = Mock() |
| 49 | + response.status_code = 200 |
| 50 | + response.text = "Success" |
| 51 | + return response |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def sample_ripple_data(): |
| 56 | + """Sample Ripple data for testing.""" |
| 57 | + return pd.DataFrame( |
| 58 | + { |
| 59 | + "globalId": ["ST001", "AA001"], |
| 60 | + "firstName": ["Alec", "Abby"], |
| 61 | + "lastName": ["Holland", "Arcane"], |
| 62 | + "cv.consent_form": ["Send to RedCap", "Send to RedCap"], |
| 63 | + "customId": [12345, 67890], |
| 64 | + "contact.1.infos.1.contactType": ["email", "email"], |
| 65 | + "contact.1.infos.1.information": ["alec@swamp.com", "abby@parliament.org"], |
| 66 | + "importType": ["HBN - Main", "HBN - Waitlist"], |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture |
| 72 | +def swamp_thing_participant(): |
| 73 | + """Fixture providing Alec Holland's data.""" |
| 74 | + return pd.DataFrame( |
| 75 | + { |
| 76 | + "globalId": ["ST001"], |
| 77 | + "customId": [12345], |
| 78 | + "firstName": ["Alec"], |
| 79 | + "lastName": ["Holland"], |
| 80 | + "cv.consent_form": ["Send to RedCap"], |
| 81 | + "contact.1.infos.1.contactType": ["email"], |
| 82 | + "contact.1.infos.1.information": ["alec.holland@swampthing.com"], |
| 83 | + "importType": ["HBN - Main"], |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def parliament_of_trees_participants(): |
| 90 | + """Fixture providing multiple Parliament of Trees members.""" |
| 91 | + return pd.DataFrame( |
| 92 | + { |
| 93 | + "globalId": ["ST001", "AA001", "TE001"], |
| 94 | + "customId": [12345, 67890, 11111], |
| 95 | + "firstName": ["Alec", "Abby", "Tefé"], |
| 96 | + "lastName": ["Holland", "Arcane", "Holland"], |
| 97 | + "cv.consent_form": ["Send to RedCap", "Send to RedCap", "Send to RedCap"], |
| 98 | + "contact.1.infos.1.contactType": ["email", "email", "email"], |
| 99 | + "contact.1.infos.1.information": [ |
| 100 | + "alec@swamp.com", |
| 101 | + "abby@parliament.org", |
| 102 | + "tefe@green.org", |
| 103 | + ], |
| 104 | + "importType": ["HBN - Main", "HBN - Main", "HBN - Waitlist"], |
| 105 | + } |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.fixture |
| 110 | +def anton_arcane_corrupted_data(): |
| 111 | + """Fixture providing Anton Arcane's corrupted participant data.""" |
| 112 | + return pd.DataFrame( |
| 113 | + { |
| 114 | + "globalId": ["ANT001"], |
| 115 | + "customId": [66666], |
| 116 | + "firstName": ["Anton"], |
| 117 | + "lastName": ["Arcane"], |
| 118 | + "cv.consent_form": ["Do Not Send"], |
| 119 | + "contact.1.infos.1.contactType": ["phone"], |
| 120 | + "contact.1.infos.1.information": ["666-666-6666"], |
| 121 | + "importType": ["HBN - Rejected"], |
| 122 | + } |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +@pytest.fixture |
| 127 | +def mock_ripple_variables(): |
| 128 | + """Mock ripple_variables configuration.""" |
| 129 | + mock_vars = Mock() |
| 130 | + mock_vars.study_ids = { |
| 131 | + "HBN - Main": "main_study_id", |
| 132 | + "HBN - Waitlist": "waitlist_study_id", |
| 133 | + } |
| 134 | + mock_vars.column_dict.return_value = {} |
| 135 | + mock_vars.headers = {"import": {"Content-Type": "application/octet-stream"}} |
| 136 | + return mock_vars |
| 137 | + |
| 138 | + |
| 139 | +@pytest.fixture |
| 140 | +def mock_redcap_variables(): |
| 141 | + """Mock redcap_variables configuration.""" |
| 142 | + mock_vars = Mock() |
| 143 | + mock_vars.Tokens.pid757 = "dev_token" |
| 144 | + mock_vars.Tokens.pid247 = "prod_token" |
| 145 | + return mock_vars |
| 146 | + |
| 147 | + |
| 148 | +@pytest.fixture |
| 149 | +def mock_endpoints(): |
| 150 | + """Mock Endpoints configuration.""" |
| 151 | + mock = Mock() |
| 152 | + mock.Ripple.import_data.return_value = "https://ripple.swamp.org/import" |
| 153 | + mock.REDCap.base_url = "https://redcap.swamp.org/api/" |
| 154 | + return mock |
| 155 | + |
| 156 | + |
| 157 | +@pytest.fixture |
| 158 | +def setup_ripple_mocks(mock_ripple_variables): |
| 159 | + """Set up common ripple variable mocks.""" |
| 160 | + return mock_ripple_variables |
| 161 | + |
| 162 | + |
| 163 | +@pytest.fixture |
| 164 | +def setup_redcap_mocks(mock_redcap_variables, temp_csv_file): |
| 165 | + """Set up common redcap variable mocks with temp file.""" |
| 166 | + mock_redcap_variables.redcap_import_file = temp_csv_file |
| 167 | + return mock_redcap_variables |
| 168 | + |
| 169 | + |
| 170 | +@pytest.fixture |
| 171 | +def excel_file_with_data(temp_excel_file): |
| 172 | + """Create Excel file with test data.""" |
| 173 | + test_df = pd.DataFrame( |
| 174 | + { |
| 175 | + "globalId": ["ST001"], |
| 176 | + "cv.consent_form": ["consent_form_created_in_redcap"], |
| 177 | + } |
| 178 | + ) |
| 179 | + test_df.to_excel(temp_excel_file, index=False) |
| 180 | + return temp_excel_file |
0 commit comments