LogicGoInfotechSpaces commited on
Commit
2358803
·
1 Parent(s): 87f9058

Add MongoDB verification to test.py

Browse files
Files changed (1) hide show
  1. test.py +123 -0
test.py CHANGED
@@ -6,8 +6,11 @@ import os
6
  import sys
7
  import time
8
  from typing import Optional
 
9
 
10
  import requests
 
 
11
 
12
 
13
  def configure_logging(verbose: bool) -> None:
@@ -103,6 +106,111 @@ def download_result(base_url: str, result_id: str, output_path: str, auth_bearer
103
  logging.info("Saved colorized image to: %s", output_path)
104
 
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  def main() -> int:
107
  parser = argparse.ArgumentParser(description="End-to-end test for Colorize API")
108
  parser.add_argument("--base-url", type=str, help="API base URL, e.g. https://<space>.hf.space")
@@ -112,6 +220,9 @@ def main() -> int:
112
  parser.add_argument("--app-check", type=str, default=os.getenv("APP_CHECK_TOKEN", ""), help="Optional App Check token")
113
  parser.add_argument("--skip-wait", action="store_true", help="Skip waiting for model to load")
114
  parser.add_argument("--verbose", action="store_true", help="Verbose logging")
 
 
 
115
  args = parser.parse_args()
116
 
117
  configure_logging(args.verbose)
@@ -155,6 +266,18 @@ def main() -> int:
155
  return 1
156
 
157
  logging.info("Test workflow completed successfully.")
 
 
 
 
 
 
 
 
 
 
 
 
158
  return 0
159
 
160
 
 
6
  import sys
7
  import time
8
  from typing import Optional
9
+ from datetime import datetime, timedelta
10
 
11
  import requests
12
+ from pymongo import MongoClient
13
+ from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
14
 
15
 
16
  def configure_logging(verbose: bool) -> None:
 
106
  logging.info("Saved colorized image to: %s", output_path)
107
 
108
 
109
+ def verify_mongodb_storage(mongodb_uri: str, db_name: str = "colorization_db",
110
+ test_endpoint: str = None, wait_seconds: int = 5) -> bool:
111
+ """
112
+ Verify that API calls were stored in MongoDB.
113
+
114
+ Args:
115
+ mongodb_uri: MongoDB connection string
116
+ db_name: Database name
117
+ test_endpoint: Specific endpoint to check (optional)
118
+ wait_seconds: Wait time before checking (to allow async writes)
119
+
120
+ Returns:
121
+ True if data is found, False otherwise
122
+ """
123
+ logging.info("Waiting %d seconds for MongoDB writes to complete...", wait_seconds)
124
+ time.sleep(wait_seconds)
125
+
126
+ try:
127
+ client = MongoClient(mongodb_uri, serverSelectionTimeoutMS=5000)
128
+ # Test connection
129
+ client.admin.command('ping')
130
+ logging.info("✅ Connected to MongoDB successfully")
131
+
132
+ db = client[db_name]
133
+
134
+ # Check api_calls collection
135
+ api_calls_collection = db["api_calls"]
136
+ recent_calls = api_calls_collection.find({
137
+ "timestamp": {"$gte": datetime.utcnow() - timedelta(minutes=5)}
138
+ }).sort("timestamp", -1).limit(10)
139
+
140
+ api_calls_count = api_calls_collection.count_documents({
141
+ "timestamp": {"$gte": datetime.utcnow() - timedelta(minutes=5)}
142
+ })
143
+
144
+ logging.info("Found %d API calls in the last 5 minutes", api_calls_count)
145
+
146
+ if api_calls_count > 0:
147
+ logging.info("Recent API calls:")
148
+ for call in list(recent_calls)[:5]: # Show last 5
149
+ logging.info(" - %s %s at %s (status: %d)",
150
+ call.get("method", "N/A"),
151
+ call.get("endpoint", "N/A"),
152
+ call.get("timestamp", "N/A"),
153
+ call.get("status_code", 0))
154
+
155
+ # Check image_uploads collection
156
+ uploads_collection = db["image_uploads"]
157
+ recent_uploads = uploads_collection.find({
158
+ "uploaded_at": {"$gte": datetime.utcnow() - timedelta(minutes=5)}
159
+ }).sort("uploaded_at", -1).limit(5)
160
+
161
+ uploads_count = uploads_collection.count_documents({
162
+ "uploaded_at": {"$gte": datetime.utcnow() - timedelta(minutes=5)}
163
+ })
164
+
165
+ logging.info("Found %d image uploads in the last 5 minutes", uploads_count)
166
+
167
+ if uploads_count > 0:
168
+ logging.info("Recent uploads:")
169
+ for upload in list(recent_uploads)[:3]: # Show last 3
170
+ logging.info(" - Image ID: %s, Size: %d bytes, Uploaded at: %s",
171
+ upload.get("image_id", "N/A"),
172
+ upload.get("file_size", 0),
173
+ upload.get("uploaded_at", "N/A"))
174
+
175
+ # Check colorizations collection
176
+ colorizations_collection = db["colorizations"]
177
+ recent_colorizations = colorizations_collection.find({
178
+ "created_at": {"$gte": datetime.utcnow() - timedelta(minutes=5)}
179
+ }).sort("created_at", -1).limit(5)
180
+
181
+ colorizations_count = colorizations_collection.count_documents({
182
+ "created_at": {"$gte": datetime.utcnow() - timedelta(minutes=5)}
183
+ })
184
+
185
+ logging.info("Found %d colorizations in the last 5 minutes", colorizations_count)
186
+
187
+ if colorizations_count > 0:
188
+ logging.info("Recent colorizations:")
189
+ for colorization in list(recent_colorizations)[:3]: # Show last 3
190
+ logging.info(" - Result ID: %s, Model: %s, Time: %.2fs, Created at: %s",
191
+ colorization.get("result_id", "N/A"),
192
+ colorization.get("model_type", "N/A"),
193
+ colorization.get("processing_time", 0),
194
+ colorization.get("created_at", "N/A"))
195
+
196
+ client.close()
197
+
198
+ # Return True if we found any recent data
199
+ if api_calls_count > 0 or uploads_count > 0 or colorizations_count > 0:
200
+ logging.info("✅ MongoDB storage verification PASSED")
201
+ return True
202
+ else:
203
+ logging.warning("⚠️ No recent data found in MongoDB (this might be normal if no API calls were made)")
204
+ return False
205
+
206
+ except (ConnectionFailure, ServerSelectionTimeoutError) as e:
207
+ logging.error("❌ Failed to connect to MongoDB: %s", str(e))
208
+ return False
209
+ except Exception as e:
210
+ logging.error("❌ MongoDB verification error: %s", str(e))
211
+ return False
212
+
213
+
214
  def main() -> int:
215
  parser = argparse.ArgumentParser(description="End-to-end test for Colorize API")
216
  parser.add_argument("--base-url", type=str, help="API base URL, e.g. https://<space>.hf.space")
 
220
  parser.add_argument("--app-check", type=str, default=os.getenv("APP_CHECK_TOKEN", ""), help="Optional App Check token")
221
  parser.add_argument("--skip-wait", action="store_true", help="Skip waiting for model to load")
222
  parser.add_argument("--verbose", action="store_true", help="Verbose logging")
223
+ parser.add_argument("--verify-mongodb", action="store_true", help="Verify MongoDB storage after API calls")
224
+ parser.add_argument("--mongodb-uri", type=str, default=os.getenv("MONGODB_URI", ""), help="MongoDB connection string for verification")
225
+ parser.add_argument("--mongodb-db", type=str, default=os.getenv("MONGODB_DB_NAME", "colorization_db"), help="MongoDB database name")
226
  args = parser.parse_args()
227
 
228
  configure_logging(args.verbose)
 
266
  return 1
267
 
268
  logging.info("Test workflow completed successfully.")
269
+
270
+ # Verify MongoDB storage if requested
271
+ if args.verify_mongodb:
272
+ if not args.mongodb_uri:
273
+ logging.warning("⚠️ MongoDB URI not provided. Skipping MongoDB verification.")
274
+ logging.info("Set MONGODB_URI environment variable or use --mongodb-uri flag")
275
+ else:
276
+ logging.info("=" * 60)
277
+ logging.info("Verifying MongoDB storage...")
278
+ logging.info("=" * 60)
279
+ verify_mongodb_storage(args.mongodb_uri, args.mongodb_db)
280
+
281
  return 0
282
 
283