File size: 1,311 Bytes
58258b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """
Quick smoke-test: write a document to MongoDB and read it back.
Usage:
python -m sdg.test_mongo # localhost:27017
python -m sdg.test_mongo --mongo-uri mongodb://HOST:PORT
"""
import argparse
import sys
from pymongo import MongoClient
def main() -> None:
parser = argparse.ArgumentParser(description="Test MongoDB read/write")
parser.add_argument("--mongo-uri", default="mongodb://john5.stanford.edu:27017",
help="MongoDB connection URI")
args = parser.parse_args()
print(f"Connecting to {args.mongo_uri} ...")
client = MongoClient(args.mongo_uri, serverSelectionTimeoutMS=5000)
# Verify connectivity
client.admin.command("ping")
print("Ping OK")
db = client["sdg_test"]
col = db["smoke_test"]
# Write
doc = {"key": "hello", "value": "world"}
col.replace_one({"key": "hello"}, doc, upsert=True)
print(f"Wrote: {doc}")
# Read
result = col.find_one({"key": "hello"})
assert result is not None, "Read back None — write may have failed"
print(f"Read: key={result['key']} value={result['value']}")
# Cleanup
col.delete_one({"key": "hello"})
print("Cleaned up test document")
client.close()
print("All good!")
if __name__ == "__main__":
main()
|