File size: 3,954 Bytes
f2fb12f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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)