feat(projekt-matching): mail parsing, project splitting, canonical URLs
This commit is contained in:
69
projekt-matching/tests/test_mailparse.py
Normal file
69
projekt-matching/tests/test_mailparse.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import email
|
||||
from email.policy import default as default_policy
|
||||
from unittest import mock
|
||||
|
||||
from projektmatch import mailparse
|
||||
|
||||
HTML = """
|
||||
<html><body>
|
||||
<h2><a href="https://www.freelancermap.de/nproj/3020338.html?utm_source=x&t=1">
|
||||
Python Entwickler für eine KI-Anwendung (m/w/d)</a></h2>
|
||||
<p>Beschreibung ...</p>
|
||||
<a href="https://www.freelancermap.de/nproj/3020338.html?utm_source=x&t=1">Zum Projekt</a>
|
||||
<h2><a href="https://www.freelancermap.de/nproj/3020995.html?agent=2">
|
||||
Test Engineer (m/w/d)</a></h2>
|
||||
<a href="https://www.freelancermap.de/other/page.html">Impressum</a>
|
||||
</body></html>
|
||||
"""
|
||||
|
||||
TEXT = """Neue Projekte fuer Sie:
|
||||
|
||||
Python Entwickler für eine KI-Anwendung (m/w/d)
|
||||
https://www.freelancermap.de/nproj/3020338.html?utm_source=x
|
||||
|
||||
Test Engineer (m/w/d)
|
||||
https://www.freelancermap.de/nproj/3020995.html?agent=2
|
||||
"""
|
||||
|
||||
|
||||
def test_is_own_mail():
|
||||
assert mailparse.is_own_mail("[Projekt-Match] Freelancermap — 86 % — X")
|
||||
assert mailparse.is_own_mail("[Projekt-Match-Fehler] Freelancermap — 2 Projekt(e)")
|
||||
assert not mailparse.is_own_mail("Example 1 for Langflow process")
|
||||
assert not mailparse.is_own_mail(None)
|
||||
|
||||
|
||||
def test_split_projects_html_dedupes_and_prefers_long_title():
|
||||
items = mailparse.split_projects(HTML, "")
|
||||
assert [i["title"] for i in items] == [
|
||||
"Python Entwickler für eine KI-Anwendung (m/w/d)", "Test Engineer (m/w/d)"]
|
||||
assert "3020338" in items[0]["url"]
|
||||
|
||||
|
||||
def test_split_projects_text_fallback():
|
||||
items = mailparse.split_projects("", TEXT)
|
||||
assert len(items) == 2
|
||||
assert items[0]["title"] == "Python Entwickler für eine KI-Anwendung (m/w/d)"
|
||||
|
||||
|
||||
def test_split_projects_none_for_normal_mail():
|
||||
assert mailparse.split_projects("<p>Hallo</p>", "Hallo") == []
|
||||
|
||||
|
||||
def test_bodies_multipart():
|
||||
msg = email.message.EmailMessage(policy=default_policy)
|
||||
msg["Subject"] = "x"
|
||||
msg.set_content("plain body")
|
||||
msg.add_alternative("<p>html body</p>", subtype="html")
|
||||
html, text = mailparse.bodies(msg)
|
||||
assert "html body" in html and "plain body" in text
|
||||
|
||||
|
||||
def test_canonical_url_strips_query():
|
||||
session = mock.Mock()
|
||||
session.get.return_value = mock.Mock(
|
||||
url="https://www.freelancermap.de/projekt/python-entwickler?ref=1",
|
||||
raise_for_status=lambda: None)
|
||||
out = mailparse.canonical_url("https://www.freelancermap.de/nproj/1.html?x=1",
|
||||
session=session)
|
||||
assert out == "https://www.freelancermap.de/projekt/python-entwickler"
|
||||
Reference in New Issue
Block a user