Spaces:
Sleeping
Sleeping
File size: 5,697 Bytes
f2fb12f c562ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | """Tests for data loading functionality."""
import pytest
import pandas as pd
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from phising_detection.data import load_phishing_urls
class TestLoadPhishingUrls:
"""Tests for load_phishing_urls function."""
def test_load_phishing_urls_basic(self, temp_phishing_file, sample_phishing_urls):
"""Test basic loading of phishing URLs."""
df = load_phishing_urls(temp_phishing_file)
# Check DataFrame shape
assert len(df) == len(sample_phishing_urls)
assert df.shape[1] == 3 # url_id, url, is_phishing
# Check column names
assert list(df.columns) == ['url_id', 'url', 'is_phishing']
# Check data types
assert df['url_id'].dtype == 'int64'
assert df['url'].dtype == 'object'
assert df['is_phishing'].dtype == 'int64'
def test_load_phishing_urls_content(self, temp_phishing_file, sample_phishing_urls):
"""Test that URLs are loaded correctly."""
df = load_phishing_urls(temp_phishing_file)
# Check URLs match
assert df['url'].tolist() == sample_phishing_urls
# Check url_ids are sequential
assert df['url_id'].tolist() == list(range(len(sample_phishing_urls)))
# Check all are labeled as phishing
assert all(df['is_phishing'] == 1)
def test_load_phishing_urls_with_label_false(self, temp_phishing_file):
"""Test loading URLs without phishing label."""
df = load_phishing_urls(temp_phishing_file, is_phishing=False)
# Check that is_phishing is 0
assert all(df['is_phishing'] == 0)
def test_load_phishing_urls_file_not_found(self):
"""Test error handling for non-existent file."""
with pytest.raises(FileNotFoundError) as exc_info:
load_phishing_urls("non_existent_file.txt")
assert "File not found" in str(exc_info.value)
def test_load_phishing_urls_empty_file(self, temp_empty_file):
"""Test loading from empty file."""
df = load_phishing_urls(temp_empty_file)
# Should return empty DataFrame with correct columns
assert len(df) == 0
assert list(df.columns) == ['url_id', 'url', 'is_phishing']
def test_load_phishing_urls_with_blank_lines(
self, temp_file_with_blank_lines, sample_phishing_urls
):
"""Test that blank lines are filtered out."""
df = load_phishing_urls(temp_file_with_blank_lines)
# Should only have 4 URLs (blank lines removed)
assert len(df) == 4
# Check that only non-empty URLs are present
assert sample_phishing_urls[0] in df['url'].values
assert sample_phishing_urls[1] in df['url'].values
assert sample_phishing_urls[2] in df['url'].values
assert sample_phishing_urls[3] in df['url'].values
def test_load_phishing_urls_pathlib_path(self, temp_phishing_file):
"""Test that function accepts pathlib.Path objects."""
path = Path(temp_phishing_file)
df = load_phishing_urls(path)
assert isinstance(df, pd.DataFrame)
assert len(df) > 0
def test_load_phishing_urls_string_path(self, temp_phishing_file):
"""Test that function accepts string paths."""
df = load_phishing_urls(str(temp_phishing_file))
assert isinstance(df, pd.DataFrame)
assert len(df) > 0
def test_load_phishing_urls_returns_dataframe(self, temp_phishing_file):
"""Test that function returns a pandas DataFrame."""
result = load_phishing_urls(temp_phishing_file)
assert isinstance(result, pd.DataFrame)
def test_load_phishing_urls_url_id_uniqueness(self, temp_phishing_file):
"""Test that url_id values are unique."""
df = load_phishing_urls(temp_phishing_file)
assert df['url_id'].is_unique
assert len(df['url_id'].unique()) == len(df)
class TestLoadLegitimateUrls:
"""Tests for loading legitimate URLs using load_phishing_urls with is_phishing=False."""
def test_load_legitimate_urls_basic(self, temp_legitimate_file, sample_legitimate_urls):
"""Test basic loading of legitimate URLs."""
df = load_phishing_urls(temp_legitimate_file, is_phishing=False)
# Check DataFrame shape
assert len(df) == len(sample_legitimate_urls)
assert df.shape[1] == 3 # url_id, url, is_phishing
# Check all are labeled as non-phishing
assert all(df['is_phishing'] == 0)
def test_load_legitimate_urls_content(self, temp_legitimate_file, sample_legitimate_urls):
"""Test that legitimate URLs are loaded correctly."""
df = load_phishing_urls(temp_legitimate_file, is_phishing=False)
# Check URLs match
assert df['url'].tolist() == sample_legitimate_urls
# Check url_ids are sequential
assert df['url_id'].tolist() == list(range(len(sample_legitimate_urls)))
def test_combined_phishing_and_legitimate(
self, temp_phishing_file, temp_legitimate_file
):
"""Test loading and combining phishing and legitimate URLs."""
phishing_df = load_phishing_urls(temp_phishing_file, is_phishing=True)
legitimate_df = load_phishing_urls(temp_legitimate_file, is_phishing=False)
# Combine datasets
combined_df = pd.concat([phishing_df, legitimate_df], ignore_index=True)
# Check that we have both types
assert (combined_df['is_phishing'] == 1).sum() == len(phishing_df)
assert (combined_df['is_phishing'] == 0).sum() == len(legitimate_df)
assert len(combined_df) == len(phishing_df) + len(legitimate_df)
|