hades / tests /data /test_clean.py
Kolgrim's picture
HADES pipeline code, tokenizer, tests, docs
5f02c05 verified
Raw History Blame Contribute Delete
1.85 kB
from hades.data.clean import dedupe, keep, normalize_text
from hades.data.sources.base import Record
ENGLISH = ("Secure Shell is a cryptographic network protocol for operating network services securely over an "
"unsecured network. Typical applications include remote command-line login and remote command execution. "
"Administrators should disable password authentication where keys are available.")
def rec(text, source="s", id="1", group="security"):
return Record(source=source, id=id, license="L", group=group, text=text)
def test_lone_surrogate_replaced():
assert normalize_text("a\ud800b") == "a�b"
def test_control_chars_replaced_but_whitespace_kept():
assert normalize_text("a\x00b\x1bc\td\re\nf") == "a�b�c\td\re\nf"
def test_nfc():
assert normalize_text("é") == "é"
def test_inline_whitespace_preserved():
assert normalize_text("x y") == "x y"
def test_html_stripped():
assert normalize_text("<p>hi</p>", html=True) == "hi"
assert normalize_text("<p>a &amp; b</p><script>x()</script>", html=True) == "a & b"
def test_blank_lines_collapsed():
assert normalize_text("a\n\n\n\n\n\nb") == "a\n\n\nb"
def test_keep_rules():
assert keep(rec(ENGLISH))
assert not keep(rec("too short"))
assert not keep(rec("Привет мир, это тест на русском языке. " * 10))
assert not keep(rec("\n".join(["same line here"] * 50)))
assert keep(rec("\n".join(["Oct 28 sshd[1]: Failed password for root"] * 50), group="logs"))
def test_dedupe_across_sources():
out = list(dedupe([rec(ENGLISH, "a", "1"), rec(ENGLISH, "b", "2"), rec(ENGLISH + "!", "b", "3")]))
assert [r.id for r in out] == ["1", "3"]
def test_record_json_roundtrip():
r = rec("x\ny é", "cve", "CVE-1")
assert Record.from_json(r.to_json()) == r