-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_orm.py
90 lines (62 loc) · 3.94 KB
/
test_orm.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pytest
from orm import Database, Table
def test_database_creation(db: Database):
assert db.tables == [], "Database should start with no tables."
def test_table_creation(db: Database, UserProfile: type[Table]):
assert 'userprofile' in db.tables, "UserProfile table should be created in the database."
def test_column_definition(UserProfile: type[Table]):
assert hasattr(UserProfile, 'username'), "UserProfile should have a 'username' column."
assert hasattr(UserProfile, 'email'), "UserProfile should have an 'email' column."
assert UserProfile.username.sql_type == "TEXT", "UserProfile 'username' should be of type TEXT."
assert UserProfile.email.sql_type == "TEXT", "UserProfile 'email' should be of type TEXT."
def test_foreign_key_definition(StatusUpdate: type[Table], UserProfile: type[Table]):
assert hasattr(StatusUpdate, 'user_profile'), "StatusUpdate should have a 'user_profile' ForeignKey."
assert StatusUpdate.user_profile.table == UserProfile, "StatusUpdate 'user_profile' should reference UserProfile."
def test_insert_and_query(db: Database, UserProfile: type[Table]):
user = UserProfile(username="testuser", email="[email protected]")
db.save(user)
assert user.id is not None, "User should have an id after being saved to the database."
queried_user = db.get(UserProfile, user.id)
assert queried_user.username == "testuser", "Queried user should have the correct username."
assert queried_user.email == "[email protected]", "Queried user should have the correct email."
def test_update_and_query(db: Database, UserProfile: type[Table]):
user = UserProfile(username="updatable", email="[email protected]")
db.save(user)
user.email = "[email protected]"
db.save(user)
queried_user = db.get(UserProfile, user.id)
assert queried_user.email == "[email protected]", "User email should be updated in the database."
def test_all_query(db: Database, UserProfile: type[Table]):
user1 = UserProfile(username="user1", email="[email protected]")
user2 = UserProfile(username="user2", email="[email protected]")
db.save(user1)
db.save(user2)
users = db.all(UserProfile)
assert len(users) == 2, "There should be two users in the database."
assert set(user.username for user in users) == {"user1", "user2"}, "All users should be retrieved."
def test_foreign_key_query(db: Database, UserProfile: type[Table], StatusUpdate: type[Table]):
user = UserProfile(username="fk_user", email="[email protected]")
db.save(user)
status = StatusUpdate(content="Testing foreign keys", user_profile=user)
db.save(status)
queried_status = db.get(StatusUpdate, status.id)
assert queried_status.user_profile.id == user.id, "StatusUpdate should reference the correct UserProfile."
assert queried_status.user_profile.username == user.username, "The referenced UserProfile should have the correct username."
def test_update_record(db: Database, UserProfile: type[Table]):
user = UserProfile(username="update_test", email="[email protected]")
db.save(user)
assert user.id is not None, "User should have an id after being saved to the database."
user.email = "[email protected]"
db.update(user)
updated_user = db.get(UserProfile, user.id)
assert updated_user.email == "[email protected]", "User email should be updated in the database."
def test_delete_record(db: Database, UserProfile: type[Table]):
user = UserProfile(username="delete_test", email="[email protected]")
db.save(user)
assert user.id is not None, "User should have an id after being saved to the database."
db.delete(UserProfile, user.id)
try:
db.get(UserProfile, user.id)
assert False, "Deleted user should not be found in the database."
except Exception as e:
assert str(e) == f"{UserProfile.__name__} instance with id {user.id} does not exist", "Exception message should indicate that the user does not exist."