Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and alphatownsman committed Jul 13, 2024
1 parent 18e0d78 commit e3d629e
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 69 deletions.
8 changes: 4 additions & 4 deletions catalog/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ def link_to_related_book(self, target: "Edition") -> bool:
)
work.save()
else:
work = Work.objects.create(title=self.title)
work = Work.objects.create(localized_title=self.localized_title)
work.editions.add(self, target)
work.localized_title = self.localized_title
work.save()
# work.localized_title = self.localized_title
# work.save()
return True

def unlink_from_all_works(self):
Expand Down Expand Up @@ -362,7 +362,7 @@ def merge_to(self, to_item: "Work | None"): # type: ignore[reportIncompatibleMe
for edition in self.editions.all():
to_item.editions.add(edition)
self.editions.clear()
to_item.other_title = uniq(to_item.other_title + [self.title]) # type: ignore
to_item.language = uniq(to_item.language + self.language) # type: ignore
to_item.localized_title = uniq(to_item.localized_title + self.localized_title)
to_item.save()

Expand Down
81 changes: 43 additions & 38 deletions catalog/book/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,26 @@ class WorkTestCase(TestCase):
databases = "__all__"

def setUp(self):
self.hyperion_hardcover = Edition.objects.create(title="Hyperion")
self.hyperion_hardcover = Edition.objects.create(
localized_title=[{"lang": "en", "text": "Hyperion"}]
)
self.hyperion_hardcover.pages = 481
self.hyperion_hardcover.isbn = "9780385249492"
self.hyperion_hardcover.save()
self.hyperion_print = Edition.objects.create(title="Hyperion")
self.hyperion_print = Edition.objects.create(
localized_title=[{"lang": "en", "text": "Hyperion"}]
)
self.hyperion_print.pages = 500
self.hyperion_print.isbn = "9780553283686"
self.hyperion_print.save()
self.hyperion_ebook = Edition(title="Hyperion")
self.hyperion_ebook.asin = "B0043M6780"
self.hyperion_ebook.save()
self.andymion_print = Edition.objects.create(title="Andymion", pages=42)
self.andymion_print = Edition.objects.create(
localized_title=[{"lang": "en", "text": "Andymion"}], pages=42
)
# serie = Serie(title="Hyperion Cantos")
self.hyperion = Work(title="Hyperion")
self.hyperion = Work(localized_title=[{"lang": "en", "text": "Hyperion"}])
self.hyperion.save()

def test_work(self):
Expand All @@ -86,11 +92,9 @@ def test_work(self):
def test_merge(self):
title1 = [{"lang": "zh", "text": "z"}]
title2 = [{"lang": "en", "text": "e"}]
w1 = Work.objects.create(title="title1", localized_title=title1)
w2 = Work.objects.create(title="title2", localized_title=title2)
w1 = Work.objects.create(localized_title=title1)
w2 = Work.objects.create(localized_title=title2)
w2.merge_to(w1)
self.assertEqual(w1.title, "title1")
self.assertEqual(w1.other_title, ["title2"])
self.assertEqual(len(w1.localized_title), 2)

def test_link(self):
Expand All @@ -99,7 +103,8 @@ def test_link(self):
self.assertTrue(self.hyperion_ebook.has_related_books())
self.assertTrue(self.hyperion_print.has_works())
self.assertEqual(
self.hyperion_print.works.first().title, self.hyperion_print.title
self.hyperion_print.works.first().display_title,
self.hyperion_print.display_title,
)
self.hyperion_print.unlink_from_all_works()
self.assertFalse(self.hyperion_print.has_related_books())
Expand Down Expand Up @@ -162,7 +167,7 @@ def test_scrape(self):
self.assertEqual(resource.id_value, "77566")
self.assertNotEqual(resource.cover, "/media/item/default.svg")
self.assertEqual(edition.isbn, "9780553283686")
self.assertEqual(edition.title, "Hyperion")
self.assertEqual(edition.display_title, "Hyperion")

edition.delete()
site = SiteManager.get_site_by_url(t_url)
Expand All @@ -186,14 +191,14 @@ def test_asin(self):
t_url = "https://www.goodreads.com/book/show/45064996-hyperion"
site = SiteManager.get_site_by_url(t_url)
site.get_resource_ready()
self.assertEqual(site.resource.item.title, "Hyperion")
self.assertEqual(site.resource.item.display_title, "Hyperion")
self.assertEqual(site.resource.item.asin, "B004G60EHS")

@use_local_response
def test_work(self):
url = "https://www.goodreads.com/work/editions/153313"
p = SiteManager.get_site_by_url(url).get_resource_ready()
self.assertEqual(p.item.title, "1984")
self.assertEqual(p.item.display_title, "1984")
url1 = "https://www.goodreads.com/book/show/3597767-rok-1984"
url2 = "https://www.goodreads.com/book/show/40961427-1984"
p1 = SiteManager.get_site_by_url(url1).get_resource_ready()
Expand Down Expand Up @@ -233,7 +238,7 @@ def test_scrape(self):
self.assertEqual(site.resource.id_type, IdType.GoogleBooks)
self.assertEqual(site.resource.id_value, "hV--zQEACAAJ")
self.assertEqual(site.resource.item.isbn, "9781847498571")
self.assertEqual(site.resource.item.title, "1984 Nineteen Eighty-Four")
self.assertEqual(site.resource.item.display_title, "1984 Nineteen Eighty-Four")


class BooksTWTestCase(TestCase):
Expand Down Expand Up @@ -271,7 +276,7 @@ def test_scrape(self):
self.assertEqual(site.resource.metadata.get("isbn"), "9786263152236")
self.assertEqual(site.resource.metadata.get("author"), ["Tim Mackintosh-Smith"])
self.assertEqual(site.resource.metadata.get("translator"), ["吳莉君"])
self.assertEqual(site.resource.metadata.get("language"), "繁體中文")
self.assertEqual(site.resource.metadata.get("language"), ["繁體中文"])
self.assertEqual(site.resource.metadata.get("pub_house"), "臉譜")
self.assertEqual(site.resource.metadata.get("pub_year"), 2023)
self.assertEqual(site.resource.metadata.get("pub_month"), 2)
Expand All @@ -282,7 +287,7 @@ def test_scrape(self):
self.assertEqual(site.resource.id_value, "0010947886")
self.assertEqual(site.resource.item.isbn, "9786263152236")
self.assertEqual(
site.resource.item.title,
site.resource.item.display_title,
"阿拉伯人三千年:從民族、部落、語言、文化、宗教到帝國,綜覽阿拉伯世界的崛起、衰落與再興",
)

Expand Down Expand Up @@ -320,7 +325,7 @@ def test_scrape(self):
self.assertEqual(site.resource.id_type, IdType.DoubanBook)
self.assertEqual(site.resource.id_value, "35902899")
self.assertEqual(site.resource.item.isbn, "9781847498571")
self.assertEqual(site.resource.item.title, "1984 Nineteen Eighty-Four")
self.assertEqual(site.resource.item.display_title, "1984 Nineteen Eighty-Four")

@use_local_response
def test_publisher(self):
Expand All @@ -342,13 +347,13 @@ def test_work(self):
p2 = SiteManager.get_site_by_url(url2).get_resource_ready()
w1 = p1.item.works.all().first()
w2 = p2.item.works.all().first()
self.assertEqual(w1.title, "黄金时代")
self.assertEqual(w2.title, "黄金时代")
self.assertEqual(w1.display_title, "黄金时代")
self.assertEqual(w2.display_title, "黄金时代")
self.assertEqual(w1, w2)
editions = w1.editions.all().order_by("title")
self.assertEqual(editions.count(), 2)
self.assertEqual(editions[0].title, "Wang in Love and Bondage")
self.assertEqual(editions[1].title, "黄金时代")
editions = sorted(list(w1.editions.all()), key=lambda e: e.display_title)
self.assertEqual(len(editions), 2)
self.assertEqual(editions[0].display_title, "Wang in Love and Bondage")
self.assertEqual(editions[1].display_title, "黄金时代")


class MultiBookSitesTestCase(TestCase):
Expand Down Expand Up @@ -388,16 +393,16 @@ def test_works(self):
self.assertEqual(p4.item.id, p1.item.id)
self.assertEqual(p4.item.works.all().count(), 2)
self.assertEqual(p1.item.works.all().count(), 2)
w2e = w2.editions.all().order_by("title")
self.assertEqual(w2e.count(), 2)
self.assertEqual(w2e[0].title, "Wang in Love and Bondage")
self.assertEqual(w2e[1].title, "黄金时代")
w3e = w3.editions.all().order_by("title")
self.assertEqual(w3e.count(), 2)
self.assertEqual(w3e[0].title, "Golden Age: A Novel")
self.assertEqual(w3e[1].title, "黄金时代")
w2e = sorted(list(w2.editions.all()), key=lambda e: e.display_title)
self.assertEqual(len(w2e), 2)
self.assertEqual(w2e[0].display_title, "Wang in Love and Bondage")
self.assertEqual(w2e[1].display_title, "黄金时代")
w3e = sorted(list(w3.editions.all()), key=lambda e: e.display_title)
self.assertEqual(len(w3e), 2)
self.assertEqual(w3e[0].display_title, "Golden Age: A Novel")
self.assertEqual(w3e[1].display_title, "黄金时代")
e = Edition.objects.get(primary_lookup_id_value=9781662601217)
self.assertEqual(e.title, "Golden Age: A Novel")
self.assertEqual(e.display_title, "Golden Age: A Novel")

@use_local_response
def test_works_merge(self):
Expand Down Expand Up @@ -428,16 +433,16 @@ def test_works_merge(self):
self.assertEqual(p4.item.id, p1.item.id)
self.assertEqual(p4.item.works.all().count(), 1)
self.assertEqual(p1.item.works.all().count(), 1)
w2e = w2.editions.all().order_by("title")
self.assertEqual(w2e.count(), 3)
self.assertEqual(w2e[0].title, "Golden Age: A Novel")
self.assertEqual(w2e[1].title, "Wang in Love and Bondage")
self.assertEqual(w2e[2].title, "黄金时代")
w2e = sorted(list(w2.editions.all()), key=lambda e: e.display_title)
self.assertEqual(len(w2e), 3)
self.assertEqual(w2e[0].display_title, "Golden Age: A Novel")
self.assertEqual(w2e[1].display_title, "Wang in Love and Bondage")
self.assertEqual(w2e[2].display_title, "黄金时代")
w3e = w3.editions.all().order_by("title")
self.assertEqual(w3e.count(), 0)
e = Edition.objects.get(primary_lookup_id_value=9781662601217)
self.assertEqual(e.title, "Golden Age: A Novel")
self.assertEqual(e.display_title, "Golden Age: A Novel")
w2e[1].delete()
self.assertEqual(w2.editions.all().count(), 2)
w2e.delete()
w2.editions.all().delete()
self.assertEqual(p1.item.works.all().count(), 0)
13 changes: 4 additions & 9 deletions catalog/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,10 @@ def get_localized_description(self) -> str | None:

@property
def display_title(self) -> str:
return (
self.get_localized_title()
or self.title
or (
self.orig_title # type:ignore
if hasattr(self, "orig_title")
else ""
)
) or (self.localized_title[0]["text"] if self.localized_title else "")
# return title in current locale if possible, otherwise any title
return (self.get_localized_title() or self.title) or (
self.localized_title[0]["text"] if self.localized_title else ""
)

@property
def display_description(self) -> str:
Expand Down
3 changes: 2 additions & 1 deletion catalog/game/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def test_scrape(self):
site.get_resource_ready()
self.assertEqual(site.ready, True)
self.assertIsInstance(site.resource.item, Game)
self.assertEqual(site.resource.item.display_title, "Portal 2")
titles = sorted([t["text"] for t in site.resource.item.localized_title])
self.assertEqual(titles, ["Portal 2", "传送门2"])
self.assertEqual(site.resource.item.douban_game, "10734307")
self.assertEqual(site.resource.item.genre, ["第一人称射击", "益智"])
self.assertEqual(site.resource.item.other_title, [])
Expand Down
6 changes: 2 additions & 4 deletions catalog/performance/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def test_scrape(self):
site = SiteManager.get_site_by_url(t_url)
resource = site.get_resource_ready()
item = site.get_item()
self.assertEqual(
item.display_title, "眠らない男・ナポレオン ―愛と栄光の涯(はて)に―"
)
self.assertEqual(item.display_title, "不眠之人·拿破仑")
self.assertEqual(len(item.localized_title), 2)
self.assertEqual(item.genre, ["音乐剧"])
self.assertEqual(item.troupe, ["宝塚歌剧团"])
Expand All @@ -43,7 +41,7 @@ def test_scrape(self):
site = SiteManager.get_site_by_url(t_url)
resource = site.get_resource_ready()
item = site.get_item()
self.assertEqual(item.display_title, "相聲說垮鬼子們")
self.assertEqual(item.display_title, "相声说垮鬼子们")
self.assertEqual(item.opening_date, "1997-05")
self.assertEqual(item.location, ["臺北新舞臺"])

Expand Down
11 changes: 4 additions & 7 deletions catalog/sites/bookstw.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def scrape(self):
"//div/ul/li[starts-with(text(),'語言:')]/text()"
)
language = (
language_elem[0].strip().split(":")[1].strip() if language_elem else None # type: ignore
[language_elem[0].strip().split(":")[1].strip()] if language_elem else [] # type: ignore
)

pub_house = content.xpath("string(//div/ul/li[contains(text(),'出版社:')])")
Expand Down Expand Up @@ -117,15 +117,12 @@ def scrape(self):
"string(//div[contains(@class,'cover_img')]//img[contains(@class,'cover')]/@src)"
)
img_url = re.sub(r"&[wh]=\d+", "", img_url) if img_url else None # type: ignore
localized_title = [{"lang": "zh-tw", "text": title}]
if orig_title:
localized_title.append(
{"lang": detect_language(orig_title), "text": orig_title}
)
data = {
"title": title,
"localized_title": localized_title,
"subtitle": subtitle,
"localized_title": [{"lang": "zh-tw", "text": title}],
"localized_subtitle": [{"lang": "zh-tw", "text": subtitle}],
"localized_description": [{"lang": "zh-tw", "text": brief}],
"orig_title": orig_title,
"author": authors,
"translator": translators,
Expand Down
21 changes: 18 additions & 3 deletions catalog/sites/douban_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from catalog.book.models import *
from catalog.book.utils import *
from catalog.common import *
from common.models.lang import detect_language

from .douban import *

Expand Down Expand Up @@ -51,7 +52,7 @@ def scrape(self):
language_elem = content.xpath(
"//div[@id='info']//span[text()='语言:']/following::text()"
)
language = language_elem[0].strip() if language_elem else None
language = [language_elem[0].strip()] if language_elem else []

pub_house_elem = content.xpath(
"//div[@id='info']//span[text()='出版社:']/following::text()"
Expand Down Expand Up @@ -187,9 +188,13 @@ def scrape(self):
)
imprint = imprint_elem[0].strip() if imprint_elem else None

lang = detect_language(title + " " + (brief or ""))
data = {
"title": title,
"subtitle": subtitle,
"localized_title": [{"lang": lang, "text": title}],
"localized_subtitle": [{"lang": lang, "text": subtitle}],
"localized_description": [{"lang": lang, "text": brief}],
"orig_title": orig_title,
"author": authors,
"translator": translators,
Expand Down Expand Up @@ -221,7 +226,12 @@ def scrape(self):
"id_value": r[1] if r else None,
"title": data["title"],
"url": works_element[0],
"content": {"metadata": {"title": data["title"]}},
"content": {
"metadata": {
"title": data["title"],
"localized_title": data["localized_title"],
}
},
}
]

Expand Down Expand Up @@ -255,5 +265,10 @@ def scrape(self):
if not title:
raise ParseError(self, "title")
book_urls = content.xpath('//a[@class="pl2"]/@href')
pd = ResourceContent(metadata={"title": title, "edition_urls": book_urls})
d = {
"title": title,
"localized_title": [{"lang": "zh-cn", "text": title}],
"edition_urls": book_urls,
}
pd = ResourceContent(metadata=d)
return pd
9 changes: 8 additions & 1 deletion catalog/sites/goodreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from catalog.book.models import Edition, Work
from catalog.book.utils import detect_isbn_asin
from catalog.common import *
from common.models.lang import detect_language

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -69,6 +70,11 @@ def scrape(self, response=None):
raise ParseError(self, "Book in __NEXT_DATA__ json")
data["title"] = b["title"]
data["brief"] = b["description"]
lang = detect_language(b["title"] + " " + (b["description"] or ""))
data["localized_title"] = [{"lang": lang, "text": b["title"]}]
data["localized_subtitle"] = [] # Goodreads does not support subtitle
data["localized_description"] = [{"lang": lang, "text": b["description"]}]

if data["brief"]:
data["brief"] = re.sub(
r"<[^>]*>", "", data["brief"].replace("<br />", "\n")
Expand Down Expand Up @@ -96,7 +102,7 @@ def scrape(self, response=None):
data["pub_year"] = dt.year
data["pub_month"] = dt.month
if b["details"].get("language"):
data["language"] = b["details"].get("language").get("name")
data["language"] = [b["details"].get("language").get("name")]
data["cover_image_url"] = b["imageUrl"]
w = next(filter(lambda x: x.get("details"), o["Work"]), None)
if w:
Expand Down Expand Up @@ -149,6 +155,7 @@ def scrape(self, response=None):
pd = ResourceContent(
metadata={
"title": title,
"localized_title": [{"lang": "en", "text": title}],
"author": author,
"first_published": first_published,
}
Expand Down
Loading

0 comments on commit e3d629e

Please sign in to comment.