68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""brand_links DB CRUD 테스트."""
|
|
import os
|
|
import pytest
|
|
from app import db
|
|
from app.config import DB_PATH
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def setup_db(tmp_path):
|
|
"""테스트용 임시 DB 사용."""
|
|
test_db = str(tmp_path / "test.db")
|
|
import app.config as config
|
|
config.DB_PATH = test_db
|
|
db.DB_PATH = test_db
|
|
db.init_db()
|
|
yield
|
|
|
|
|
|
def test_add_brand_link():
|
|
link = db.add_brand_link({
|
|
"keyword_id": 1,
|
|
"url": "https://link.coupang.com/abc",
|
|
"product_name": "테스트 상품",
|
|
"description": "상품 설명",
|
|
"placement_hint": "본문 중간",
|
|
})
|
|
assert link["id"] is not None
|
|
assert link["url"] == "https://link.coupang.com/abc"
|
|
assert link["product_name"] == "테스트 상품"
|
|
assert link["keyword_id"] == 1
|
|
assert link["post_id"] is None
|
|
|
|
|
|
def test_get_brand_links_by_keyword_id():
|
|
db.add_brand_link({"keyword_id": 1, "url": "https://a.com", "product_name": "A"})
|
|
db.add_brand_link({"keyword_id": 1, "url": "https://b.com", "product_name": "B"})
|
|
db.add_brand_link({"keyword_id": 2, "url": "https://c.com", "product_name": "C"})
|
|
links = db.get_brand_links(keyword_id=1)
|
|
assert len(links) == 2
|
|
|
|
|
|
def test_get_brand_links_by_post_id():
|
|
db.add_brand_link({"post_id": 10, "url": "https://a.com", "product_name": "A"})
|
|
links = db.get_brand_links(post_id=10)
|
|
assert len(links) == 1
|
|
assert links[0]["post_id"] == 10
|
|
|
|
|
|
def test_update_brand_link():
|
|
link = db.add_brand_link({"url": "https://a.com", "product_name": "원래 이름"})
|
|
updated = db.update_brand_link(link["id"], {"product_name": "새 이름", "post_id": 5})
|
|
assert updated["product_name"] == "새 이름"
|
|
assert updated["post_id"] == 5
|
|
|
|
|
|
def test_delete_brand_link():
|
|
link = db.add_brand_link({"url": "https://a.com", "product_name": "삭제할 링크"})
|
|
assert db.delete_brand_link(link["id"]) is True
|
|
assert db.delete_brand_link(link["id"]) is False
|
|
|
|
|
|
def test_link_keyword_to_post():
|
|
db.add_brand_link({"keyword_id": 1, "url": "https://a.com", "product_name": "A"})
|
|
db.add_brand_link({"keyword_id": 1, "url": "https://b.com", "product_name": "B"})
|
|
db.link_brand_links_to_post(keyword_id=1, post_id=10)
|
|
links = db.get_brand_links(post_id=10)
|
|
assert len(links) == 2
|