-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_database.py
More file actions
35 lines (27 loc) · 825 Bytes
/
test_database.py
File metadata and controls
35 lines (27 loc) · 825 Bytes
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
from database import SessionLocal
from sqlalchemy import text
from models import User, Note
#testing connection
def test_database_connection():
db = SessionLocal()
try:
result = db.execute(text("SELECT 1"))
value = result.scalar()
assert value == 1
finally:
db.close()
def test_create_user():
db = SessionLocal()
try:
#1. Create new user
new_user = User(name = "Test User", email = "test@example.com")
db.add(new_user)
db.commit()
# query the user back
user_in_db = db.query(User).filter_by(email = "test@example.com").first()
#assertions
assert user_in_db is not None
assert user_in_db.name == "Test User"
assert user_in_db.email == "test@example.com"
finally:
db.close()