60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
import asyncio
|
|
from app.database import db, ObjectId
|
|
from app.config import settings
|
|
|
|
async def test():
|
|
# Force in-memory database for testing
|
|
settings.database_path = ":memory:"
|
|
|
|
print("Connecting to DB...")
|
|
await db.connect()
|
|
|
|
print("Inserting mock data...")
|
|
user_id = ObjectId()
|
|
await db.users.insert_one({
|
|
"_id": user_id,
|
|
"firstname": "Dennis",
|
|
"lastname": "S",
|
|
"email": "dennis@test.local",
|
|
"isAdmin": True,
|
|
"balance": 10.0,
|
|
"password_hash": "hash"
|
|
})
|
|
|
|
drink_id = ObjectId()
|
|
await db.drinks.insert_one({
|
|
"_id": drink_id,
|
|
"name": "Club Mate",
|
|
"price": 1.5,
|
|
"stock": 10
|
|
})
|
|
|
|
print("Testing find_one...")
|
|
admin = await db.users.find_one({"isAdmin": True})
|
|
print(f"Found admin: {admin}")
|
|
assert admin is not None
|
|
assert admin["_id"] == user_id
|
|
|
|
print("Testing find and async iterator...")
|
|
cursor = db.users.find()
|
|
users = []
|
|
async for u in cursor:
|
|
users.append(u)
|
|
print(f"List of users: {users}")
|
|
assert len(users) == 1
|
|
|
|
print("Testing cursor chain sort & limit...")
|
|
cursor2 = db.drinks.find().sort("name", 1).limit(5)
|
|
drinks = []
|
|
async for d in cursor2:
|
|
drinks.append(d)
|
|
print(f"List of drinks: {drinks}")
|
|
assert len(drinks) == 1
|
|
assert drinks[0]["name"] == "Club Mate"
|
|
|
|
await db.close()
|
|
print("All tests passed successfully!")
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test())
|