119 lines
4.0 KiB
Python
119 lines
4.0 KiB
Python
"""
|
|
Unit tests for ArticleLink repository
|
|
"""
|
|
|
|
import pytest
|
|
from src.database.repositories import ArticleLinkRepository
|
|
from src.database.models import ArticleLink, GeneratedContent, Project
|
|
|
|
|
|
class TestArticleLinkRepository:
|
|
"""Tests for ArticleLinkRepository"""
|
|
|
|
def test_create_internal_link(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
link = repo.create(
|
|
from_content_id=1,
|
|
to_content_id=2,
|
|
to_url=None,
|
|
link_type="tiered"
|
|
)
|
|
|
|
assert link.id is not None
|
|
assert link.from_content_id == 1
|
|
assert link.to_content_id == 2
|
|
assert link.to_url is None
|
|
assert link.link_type == "tiered"
|
|
|
|
def test_create_external_link(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
link = repo.create(
|
|
from_content_id=1,
|
|
to_content_id=None,
|
|
to_url="https://www.moneysite.com",
|
|
link_type="tiered"
|
|
)
|
|
|
|
assert link.id is not None
|
|
assert link.from_content_id == 1
|
|
assert link.to_content_id is None
|
|
assert link.to_url == "https://www.moneysite.com"
|
|
assert link.link_type == "tiered"
|
|
|
|
def test_create_without_target_raises_error(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
with pytest.raises(ValueError, match="Either to_content_id or to_url must be provided"):
|
|
repo.create(
|
|
from_content_id=1,
|
|
to_content_id=None,
|
|
to_url=None,
|
|
link_type="tiered"
|
|
)
|
|
|
|
def test_get_by_source_article(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
link1 = repo.create(from_content_id=1, to_content_id=2, link_type="tiered")
|
|
link2 = repo.create(from_content_id=1, to_content_id=3, link_type="wheel_next")
|
|
link3 = repo.create(from_content_id=2, to_content_id=4, link_type="tiered")
|
|
|
|
links = repo.get_by_source_article(1)
|
|
|
|
assert len(links) == 2
|
|
link_ids = [link.id for link in links]
|
|
assert link1.id in link_ids
|
|
assert link2.id in link_ids
|
|
assert link3.id not in link_ids
|
|
|
|
def test_get_by_target_article(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
link1 = repo.create(from_content_id=1, to_content_id=2, link_type="tiered")
|
|
link2 = repo.create(from_content_id=3, to_content_id=2, link_type="tiered")
|
|
link3 = repo.create(from_content_id=1, to_content_id=4, link_type="tiered")
|
|
|
|
links = repo.get_by_target_article(2)
|
|
|
|
assert len(links) == 2
|
|
link_ids = [link.id for link in links]
|
|
assert link1.id in link_ids
|
|
assert link2.id in link_ids
|
|
assert link3.id not in link_ids
|
|
|
|
def test_get_by_link_type(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
link1 = repo.create(from_content_id=1, to_content_id=2, link_type="tiered")
|
|
link2 = repo.create(from_content_id=2, to_content_id=3, link_type="wheel_next")
|
|
link3 = repo.create(from_content_id=3, to_content_id=4, link_type="tiered")
|
|
|
|
links = repo.get_by_link_type("tiered")
|
|
|
|
assert len(links) == 2
|
|
link_ids = [link.id for link in links]
|
|
assert link1.id in link_ids
|
|
assert link3.id in link_ids
|
|
assert link2.id not in link_ids
|
|
|
|
def test_delete(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
link = repo.create(from_content_id=1, to_content_id=2, link_type="tiered")
|
|
link_id = link.id
|
|
|
|
result = repo.delete(link_id)
|
|
assert result is True
|
|
|
|
links = repo.get_by_source_article(1)
|
|
assert len(links) == 0
|
|
|
|
def test_delete_nonexistent_returns_false(self, db_session):
|
|
repo = ArticleLinkRepository(db_session)
|
|
|
|
result = repo.delete(999)
|
|
assert result is False
|
|
|