from fastcore.test import test_eq
from sqlmodel import create_engine, Session, SQLModel
from seo_rat.models import Website
from seo_rat.article import ArticleArticle
Database model and CRUD operations for tracking article files and their SEO metadata.
insert_article
def insert_article(
session:Session, # Active database session
article:Article, # Article instance to insert
)->Article:
Insert a new article into the database.
get_article_by_id
def get_article_by_id(
session:Session, # Active database session
article_id:int, # Article primary key
)->__main__.Article | None:
Get an article by its ID.
get_article_by_path
def get_article_by_path(
session:Session, # Active database session
file_path:str, # File path to match
)->__main__.Article | None:
Get an article by its file path.
get_articles_by_website
def get_articles_by_website(
session:Session, # Active database session
website_id:int, # Parent website ID
)->list:
Get all articles belonging to a website.
delete_article
def delete_article(
session:Session, # Active database session
article_id:int, # Article to delete
)->None:
Delete an article by ID. Raises ValueError if not found.
update_article
def update_article(
session:Session, # Active database session
article_id:int, # Article to update
focus_keyword:str | None=None, # New focus keyword
secondary_keywords:list[str] | None=None, # New secondary keywords
target_goal:str | None=None, # New target goal
url:str | None=None, # New public URL
)->Article:
Update article fields and set last_optimized to now. Raises ValueError if not found.
# create a test session
session = get_session()
# insert
article = insert_article(session, Article(
website_id=1,
file_path="/tmp/test.md",
focus_keyword="seo",
url="https://example.com/test"
))
print("inserted:", article)
# get by id
print("by id:", get_article_by_id(session, article.id))
# get by path
print("by path:", get_article_by_path(session, "/tmp/test.md"))
# get by website
# print("by website:", get_articles_by_website(session, 1))
# update
updated = update_article(session, article.id, focus_keyword="seo tools", target_goal="rank #1")
print("updated:", updated)
# delete
delete_article(session, article.id)
print("deleted, verify gone:", get_article_by_id(session, article.id))inserted: focus_keyword='seo' id=213 target_goal=None created_at=datetime.datetime(2026, 4, 4, 10, 39, 16, 204615) website_id=1 file_path='/tmp/test.md' url='https://example.com/test' secondary_keywords=None last_optimized=None
by id: focus_keyword='seo' id=213 target_goal=None created_at=datetime.datetime(2026, 4, 4, 10, 39, 16, 204615) website_id=1 file_path='/tmp/test.md' url='https://example.com/test' secondary_keywords=None last_optimized=None
by path: focus_keyword='seo' id=213 target_goal=None created_at=datetime.datetime(2026, 4, 4, 10, 39, 16, 204615) website_id=1 file_path='/tmp/test.md' url='https://example.com/test' secondary_keywords=None last_optimized=None
updated: focus_keyword='seo tools' id=213 target_goal='rank #1' created_at=datetime.datetime(2026, 4, 4, 10, 39, 16, 204615) website_id=1 file_path='/tmp/test.md' url='https://example.com/test' secondary_keywords=None last_optimized=datetime.datetime(2026, 4, 4, 10, 39, 16, 219366)
deleted, verify gone: None