← back to Paul Conrad Archive

tests/test_web_images.py

48 lines

"""web_images parsers (exhibit / dealer / auction / blog), incl. negative cases (TK-12199)."""
import json

from conrad.crawlers.web_images import (parse_invaluable, parse_pcg_item, parse_pophistorydig, parse_syracuse,
                                        stated_year)


def test_syracuse_alt_titles():
    b = ('<img alt="Banner" src="images/Title-Web2.jpg"/><img alt="Violation cartoon" src="images/24.jpg"/>'
         '<img alt="Special Collections Exhibits banner" src="../_assets/x.png"/>')
    assert parse_syracuse(b) == [{"title": "Violation",
                                  "image": "https://library.syracuse.edu/extsites/cartoonists/images/24.jpg"}]


def test_dealer_item_fields_and_title_fallback():
    b = ('<title>Watergate Tapes - Cartoon Gallery</title><img src="/media/filer_public_thumbnails/x/conrad1.jpg__345x300.jpg"'
         ' alt="None" title="None"/> Description tapes Size 31cm x 33cm Medium Pen and ink Publication Los Angeles Times'
         ' Published 9 November 1973 Home')
    it = parse_pcg_item(b)
    assert it["title"] == "Watergate Tapes" and it["publication"] == "Los Angeles Times"
    assert it["published"] == "9 November 1973" and it["image"].endswith("conrad1.jpg__345x300.jpg")


def _lot(ref, title, ts=1600000000):
    return json.dumps({"lotTitle": title, "lotRef": ref, "photoPath": "h/1.jpg", "dateTimeUTCUnix": ts,
                       "houseName": "House", "lotNumber": "1"})


def test_invaluable_keeps_cartoons_only():
    b = "[" + ",".join([_lot("A", "(POLITICAL CARTOON) PAUL CONRAD"),
                        _lot("B", 'PAUL CONRAD (1924-2010) "Flag Raising At Guantanamo."'),
                        _lot("C", "Paul Conrad American 1924-2010 Lithograph Martin Luther King Signed Ltd"),
                        _lot("D", "Conrad Paul (traceable 1525-1547 Lauingen) - Christ on the Mount of Olives"),
                        _lot("E", "The Beatles, signed photograph")]) + "]"
    got = {d["ref"]: d["named"] for d in parse_invaluable(b)}
    assert got == {"A": None, "B": "Flag Raising At Guantanamo"}  # lithograph, 16th-c. Conrad, Beatles: rejected


def test_pophistorydig_skips_photos_and_books():
    b = ('<img alt="Paul Conrad working on a drawing at his desk, 1970s." src="/a.jpg">'
         '<img alt="Paul Conrad’s 1975 book, “The King and Us”" src="/b.jpg">'
         '<img alt="In this June 1972 Paul Conrad cartoon, Democrats are peeking" src="/c.jpg">')
    items = parse_pophistorydig(b)
    assert [i["image"] for i in items] == ["https://pophistorydig.com/c.jpg"]
    assert stated_year(items[0]["alt"])[0] == 1972
    assert stated_year("a 1970s cartoon") == (None, ("1970-01-01", "1979-12-31"))
    assert stated_year("undated") == (None, None)