99import pytest
1010from customize_schema import validate_metadata as validator43 # Import validator
1111
12+
1213class CaltechDataTester :
1314 def __init__ (self ):
1415 self .test_dir = "caltech_test_data"
1516 self .timestamp = datetime .now ().strftime ("%Y%m%d_%H%M%S" )
1617 if not os .path .exists (self .test_dir ):
1718 os .makedirs (self .test_dir )
18-
19+
1920 # Create test data directory with timestamp
2021 self .test_run_dir = os .path .join (self .test_dir , f"test_run_{ self .timestamp } " )
2122 os .makedirs (self .test_run_dir )
22-
23+
2324 # Initialize logging
2425 self .log_file = os .path .join (self .test_run_dir , "test_log.txt" )
25-
26+
2627 def log (self , message ):
2728 """Log message to both console and file"""
2829 print (message )
@@ -38,7 +39,7 @@ def create_test_files(self):
3839 f .write ("2023-01-01,25.5,60\n " )
3940 f .write ("2023-01-02,26.0,62\n " )
4041 f .write ("2023-01-03,24.8,65\n " )
41-
42+
4243 self .log (f"Created test CSV file: { csv_path } " )
4344 return csv_path
4445
@@ -64,9 +65,9 @@ def generate_test_responses(self):
6465 def extract_record_id (self , output_text ):
6566 """Extract record ID from CLI output"""
6667 try :
67- for line in output_text .split (' \n ' ):
68- if ' uploads/' in line :
69- return line .strip ().split ('/' )[- 1 ]
68+ for line in output_text .split (" \n " ):
69+ if " uploads/" in line :
70+ return line .strip ().split ("/" )[- 1 ]
7071 except Exception as e :
7172 self .log (f"Error extracting record ID: { e } " )
7273 return None
@@ -76,22 +77,22 @@ def download_and_validate_record(self, record_id):
7677 try :
7778 # Wait for record to be available
7879 time .sleep (5 )
79-
80+
8081 # Download metadata
81- url = f"https://data.caltech.edu /records/{ record_id } /export/datacite-json?preview=1 "
82+ url = f"https://data.caltechlibrary.dev /records/{ record_id } /export/datacite-json"
8283 response = requests .get (url )
8384 response .raise_for_status ()
84-
85+
8586 # Save metadata
8687 json_path = os .path .join (self .test_run_dir , f"{ record_id } .json" )
87- with open (json_path , 'w' ) as f :
88+ with open (json_path , "w" ) as f :
8889 json .dump (response .json (), f , indent = 2 )
89-
90+
9091 self .log (f"Downloaded metadata to: { json_path } " )
91-
92+
9293 # Validate metadata using the imported validator
9394 validation_errors = validator43 (response .json ())
94-
95+
9596 if validation_errors :
9697 self .log ("❌ Validation errors found:" )
9798 for error in validation_errors :
@@ -100,7 +101,7 @@ def download_and_validate_record(self, record_id):
100101 else :
101102 self .log ("✅ Validation passed successfully" )
102103 return True
103-
104+
104105 except Exception as e :
105106 self .log (f"Error in download and validation: { e } " )
106107 return False
@@ -109,28 +110,31 @@ def run_test_submission(self):
109110 """Run the complete test submission process"""
110111 try :
111112 self .log ("Starting test submission process..." )
112-
113+
113114 # Create test files
114115 test_csv = self .create_test_files ()
115-
116+
116117 # Generate responses
117118 responses = self .generate_test_responses ()
118-
119+
119120 # Setup output capture
120121 class OutputCapture :
121122 def __init__ (self ):
122123 self .output = []
124+
123125 def write (self , text ):
124126 self .output .append (text )
125127 sys .__stdout__ .write (text )
128+
126129 def flush (self ):
127130 pass
131+
128132 def get_output (self ):
129- return '' .join (self .output )
130-
133+ return "" .join (self .output )
134+
131135 output_capture = OutputCapture ()
132136 sys .stdout = output_capture
133-
137+
134138 # Mock input and run CLI
135139 def mock_input (prompt ):
136140 self .log (f"Prompt: { prompt } " )
@@ -139,31 +143,32 @@ def mock_input(prompt):
139143 self .log (f"Response: { response } " )
140144 return response
141145 return ""
142-
143- with patch (' builtins.input' , side_effect = mock_input ):
146+
147+ with patch (" builtins.input" , side_effect = mock_input ):
144148 try :
145149 import cli
150+
146151 cli .main ()
147152 except Exception as e :
148153 self .log (f"Error during CLI execution: { e } " )
149154 return False
150-
155+
151156 # Restore stdout
152157 sys .stdout = sys .__stdout__
153-
158+
154159 # Get output and extract record ID
155160 cli_output = output_capture .get_output ()
156161 record_id = self .extract_record_id (cli_output )
157-
162+
158163 if not record_id :
159164 self .log ("Failed to extract record ID" )
160165 return False
161-
166+
162167 self .log (f"Successfully created record with ID: { record_id } " )
163-
168+
164169 # Validate the record
165170 return self .download_and_validate_record (record_id )
166-
171+
167172 except Exception as e :
168173 self .log (f"Error in test submission: { e } " )
169174 return False
@@ -173,17 +178,19 @@ def mock_input(prompt):
173178 os .remove (test_csv )
174179 self .log ("Test files cleaned up" )
175180
181+
176182def main ():
177183 tester = CaltechDataTester ()
178-
184+
179185 success = tester .run_test_submission ()
180-
186+
181187 if success :
182188 tester .log ("\n 🎉 Test submission and validation completed successfully!" )
183189 else :
184190 tester .log ("\n ❌ Test submission or validation failed - check logs for details" )
185-
191+
186192 tester .log (f"\n Test logs available at: { tester .log_file } " )
187193
194+
188195if __name__ == "__main__" :
189196 main ()
0 commit comments