Test relationship operations
(self)
| 158 | session.close() |
| 159 | |
| 160 | def test_relationships(self): |
| 161 | """Test relationship operations""" |
| 162 | session = self.Session() |
| 163 | try: |
| 164 | # Create user with posts |
| 165 | user = User(username="blogger", email="blogger@example.com") |
| 166 | post1 = Post(title="First Post", content="Hello World", author=user) |
| 167 | post2 = Post(title="Second Post", content="More content", author=user) |
| 168 | |
| 169 | session.add(user) |
| 170 | session.commit() |
| 171 | |
| 172 | # Test relationship loading |
| 173 | loaded_user = session.query(User).filter_by(username="blogger").first() |
| 174 | assert len(loaded_user.posts) == 2 |
| 175 | |
| 176 | # Test backref |
| 177 | loaded_post = session.query(Post).filter_by(title="First Post").first() |
| 178 | assert loaded_post.author.username == "blogger" |
| 179 | |
| 180 | # Test cascade delete |
| 181 | session.delete(loaded_user) |
| 182 | session.commit() |
| 183 | |
| 184 | orphaned_posts = session.query(Post).filter_by(author_id=loaded_user.id).all() |
| 185 | assert len(orphaned_posts) == 0 |
| 186 | |
| 187 | finally: |
| 188 | session.close() |
| 189 | |
| 190 | def test_complex_queries(self): |
| 191 | """Test complex query patterns""" |