""" Comprehensive tests for text utilities. """ import pytest from sentiment_engine.utils.text import ( clean_html, extract_tickers, extract_cashtags, detect_language, normalize_text, split_into_sentences, compute_token_proximity ) class TestCleanHtml: """Tests for clean_html""" def test_removes_html_tags(self): """Should remove all HTML tags""" html = "
Hello world
First paragraph
Second paragraph
" cleaned = clean_html(html) assert "First paragraph" in cleaned assert "Second paragraph" in cleaned def test_handles_entities(self): """Should handle HTML entities""" html = "Bitcoin & Ethereum < $100k" cleaned = clean_html(html) assert "&" in cleaned or "and" in cleaned def test_empty_input(self): """Should handle empty input""" assert clean_html("") == "" assert clean_html(None) == "" def test_no_html(self): """Should return plain text unchanged""" text = "Plain text without HTML" cleaned = clean_html(text) assert cleaned == text def test_self_closing_tags(self): """Should handle self-closing tags""" html = "Bitcoin surges
" normalized = normalize_text(text) assert "<" not in normalized assert "Bitcoin surges" in normalized def test_removes_urls(self): """Should remove URLs""" text = "Check https://example.com for more" normalized = normalize_text(text) assert "https://example.com" not in normalized def test_normalizes_whitespace(self): """Should normalize whitespace""" text = "Bitcoin surges to the moon" normalized = normalize_text(text) assert " " not in normalized def test_strips_whitespace(self): """Should strip leading/trailing whitespace""" text = " Bitcoin surges " normalized = normalize_text(text) assert normalized == "Bitcoin surges" def test_empty_input(self): """Should handle empty input""" assert normalize_text("") == "" assert normalize_text(None) == "" class TestSplitIntoSentences: """Tests for split_into_sentences""" def test_basic_split(self): """Should split on punctuation""" text = "Bitcoin surges. Ethereum rises! Bitcoin crashes?" sentences = split_into_sentences(text) assert len(sentences) == 3 def test_handles_multiple_punctuation(self): """Should handle multiple punctuation""" text = "Bitcoin surges!! Really??" sentences = split_into_sentences(text) assert len(sentences) >= 2 def test_strips_whitespace(self): """Should strip whitespace from sentences""" text = " Bitcoin surges. Ethereum rises. " sentences = split_into_sentences(text) assert all(not s.startswith(" ") and not s.endswith(" ") for s in sentences) def test_empty_input(self): """Should handle empty input""" assert split_into_sentences("") == [] class TestComputeTokenProximity: """Tests for compute_token_proximity""" def test_keyword_next_to_asset(self): """Should return high proximity when keyword next to asset""" sentence = "Bitcoin surges to new high" proximity = compute_token_proximity(sentence, ["surges"], "Bitcoin") assert proximity == 1.0 def test_keyword_close_to_asset(self): """Should return high proximity when keyword close to asset""" sentence = "Bitcoin rapidly surges to new high" proximity = compute_token_proximity(sentence, ["surges"], "Bitcoin") assert proximity == 1.0 def test_keyword_within_distance(self): """Should return high proximity when keyword within 3 tokens""" sentence = "Bitcoin rapidly surges to new high" proximity = compute_token_proximity(sentence, ["surges"], "Bitcoin") assert proximity == 1.0 def test_keyword_not_found(self): """Should return 0 when keyword not found""" sentence = "Bitcoin surges" proximity = compute_token_proximity(sentence, ["crashes"], "Bitcoin") assert proximity == 0.0 def test_asset_not_found(self): """Should return 0 when asset not found""" sentence = "Ethereum surges" proximity = compute_token_proximity(sentence, ["surges"], "Bitcoin") assert proximity == 0.0 def test_uppercase_asset(self): """Should match uppercase asset""" sentence = "BITCOIN SURGES" proximity = compute_token_proximity(sentence, ["surges"], "BITCOIN") assert proximity == 1.0 def test_partial_asset_match(self): """Should handle partial asset matches""" sentence = "BTC surges" proximity = compute_token_proximity(sentence, ["surges"], "BTC") assert proximity == 1.0 if __name__ == "__main__": pytest.main([__file__, "-v"])