-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
article can have many events also add seed for categories (with scuffed association example)
- Loading branch information
Showing
2 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from datetime import datetime | ||
from sqlalchemy import select | ||
from src.events.models import Article, ArticleSource, Category, Event | ||
from sqlalchemy.orm import Session | ||
from src.common.database import engine | ||
|
||
|
||
def add_categories(): | ||
CATEGORIES = [ | ||
"Arts & Humanities", | ||
"Science & Tech", | ||
"Politics", | ||
"Media", | ||
"Environment", | ||
"Education", | ||
"Sports", | ||
"Gender & Equality", | ||
"Religion", | ||
"Society & Culture", | ||
"Economics", | ||
] | ||
categories = [Category(name=category_name) for category_name in CATEGORIES] | ||
with Session(engine) as session: | ||
# If categories are already added, return | ||
if session.scalars(select(Category)).first() is not None: | ||
return | ||
session.add_all(categories) | ||
session.commit() | ||
|
||
|
||
add_categories() | ||
|
||
|
||
def test_associations(): | ||
with Session(engine) as session: | ||
article = Article( | ||
title="test article", | ||
summary="test summary", | ||
url="https://whatever.com", | ||
source=ArticleSource.CNA, | ||
body="test body", | ||
date="2024-02-05", | ||
) | ||
event = Event( | ||
title="test event 1", | ||
description="x", | ||
analysis="x", | ||
duplicate=False, | ||
date=datetime.now(), | ||
is_singapore=False, | ||
) | ||
article.events.append(event) | ||
session.add(article) | ||
session.commit() | ||
|
||
session.refresh(article) | ||
session.refresh(event) | ||
print(article) | ||
print(event) | ||
event_id = event.id | ||
|
||
with Session(engine) as session: | ||
event_again = session.scalar(select(Event).where(Event.id == event_id)) | ||
categories = session.scalars( | ||
select(Category).where(Category.name.in_(["Environment", "Media"])) | ||
) | ||
event_again.categories.extend(categories) | ||
session.add(event_again) | ||
session.commit() | ||
|
||
with Session(engine) as session: | ||
event_again = session.scalar(select(Event).where(Event.id == event_id)) | ||
print(event_again) | ||
print(event_again.original_article) | ||
print(event_again.categories) | ||
event_again.categories.clear() | ||
original_article = event_again.original_article | ||
session.add(event_again) | ||
session.commit() | ||
session.delete(event_again) | ||
session.delete(original_article) | ||
|
||
|
||
# test_associations() |