|
1 |
| -import unittest |
2 |
| -from unittest.mock import patch, MagicMock |
| 1 | +import pytest |
3 | 2 | from datetime import datetime
|
4 |
| -from scraper import Scraper, extract_dwr_token, create_cookiedict, create_search_id, create_session_script_id |
5 |
| -from models import StationRecord |
6 |
| -from errors import InvalidTrainRideFilter, InvalidDWRToken |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | +from src.scraper import Scraper, extract_dwr_token, extract_train_list, create_search_id, create_session_script_id, tokenify |
| 5 | +from models import StationRecord, TrainRideRecord |
| 6 | +from errors import InvalidDWRToken, InvalidTrainRideFilter |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def scraper(): |
| 10 | + origin = StationRecord(name="Madrid", code="MAD") |
| 11 | + destination = StationRecord(name="Barcelona", code="BCN") |
| 12 | + departure_date = datetime(2023, 12, 25) |
| 13 | + return Scraper(origin, destination, departure_date) |
| 14 | + |
| 15 | +def test_create_search_id(): |
| 16 | + search_id = create_search_id() |
| 17 | + assert len(search_id) == 5 |
| 18 | + assert search_id.startswith("_") |
| 19 | + |
| 20 | +def test_create_session_script_id(): |
| 21 | + dwr_token = "test_token" |
| 22 | + session_script_id = create_session_script_id(dwr_token) |
| 23 | + assert session_script_id.startswith(dwr_token) |
| 24 | + |
| 25 | +def test_tokenify(): |
| 26 | + number = 123456 |
| 27 | + token = tokenify(number) |
| 28 | + assert isinstance(token, str) |
| 29 | + |
| 30 | +def test_extract_dwr_token(): |
| 31 | + response_text = 'r.handleCallback("0","0","test_token")' |
| 32 | + token = extract_dwr_token(response_text) |
| 33 | + assert token == "test_token" |
| 34 | + |
| 35 | +def test_extract_dwr_token_invalid(): |
| 36 | + response_text = 'invalid response' |
| 37 | + with pytest.raises(InvalidDWRToken): |
| 38 | + extract_dwr_token(response_text) |
| 39 | + |
| 40 | +def test_extract_train_list(): |
| 41 | + response_text = 'r.handleCallback(0,0,{"listadoTrenes":[]});' |
| 42 | + train_list = extract_train_list(response_text) |
| 43 | + assert "listadoTrenes" in train_list |
| 44 | + |
| 45 | +def test_invalid_return_date(): |
| 46 | + origin = StationRecord(name="Madrid", code="MAD") |
| 47 | + destination = StationRecord(name="Barcelona", code="BCN") |
| 48 | + departure_date = datetime(2023, 12, 25) |
| 49 | + return_date = datetime(2023, 12, 24) |
| 50 | + with pytest.raises(InvalidTrainRideFilter): |
| 51 | + Scraper(origin, destination, departure_date, return_date) |
| 52 | + |
| 53 | +@patch('src.scraper.requests.Session.post') |
| 54 | +def test_do_search(mock_post, scraper): |
| 55 | + mock_post.return_value.ok = True |
| 56 | + scraper._do_search() |
| 57 | + assert mock_post.called |
| 58 | + |
| 59 | +@patch('src.scraper.requests.Session.post') |
| 60 | +def test_do_get_dwr_token(mock_post, scraper): |
| 61 | + mock_post.return_value.ok = True |
| 62 | + mock_post.return_value.text = 'r.handleCallback("0","0","test_token")' |
| 63 | + scraper._do_get_dwr_token() |
| 64 | + assert scraper.dwr_token == "test_token" |
7 | 65 |
|
8 |
| -class TestScraper(unittest.TestCase): |
9 |
| - def setUp(self): |
10 |
| - # Setup mock data for testing |
11 |
| - self.origin = StationRecord(name="Madrid", code="MAD") |
12 |
| - self.destination = StationRecord(name="Barcelona", code="BCN") |
13 |
| - self.departure_date = datetime(2025, 1, 31, 10, 0) |
14 |
| - self.return_date = datetime(2025, 2, 1, 10, 0) |
15 |
| - |
16 |
| - self.scraper = Scraper( |
17 |
| - origin=self.origin, |
18 |
| - destination=self.destination, |
19 |
| - departure_date=self.departure_date, |
20 |
| - return_date=self.return_date |
21 |
| - ) |
22 |
| - |
23 |
| - @patch('scraper.requests.Session.post') |
24 |
| - def test_get_trainrides(self, mock_post): |
25 |
| - # Mock the HTTP responses for the scraper methods |
26 |
| - mock_post.return_value.ok = True |
27 |
| - mock_post.return_value.text = '{"listadoTrenes": []}' # mock response for getting train list |
| 66 | +@patch('src.scraper.requests.Session.post') |
| 67 | +def test_do_update_session_objects(mock_post, scraper): |
| 68 | + mock_post.return_value.ok = True |
| 69 | + scraper.dwr_token = "test_token" |
| 70 | + scraper.script_session_id = "test_script_session_id" |
| 71 | + scraper._do_update_session_objects() |
| 72 | + assert mock_post.called |
28 | 73 |
|
29 |
| - # Mock necessary functions |
30 |
| - self.scraper._do_search = MagicMock() |
31 |
| - self.scraper._do_get_dwr_token = MagicMock() |
32 |
| - self.scraper._do_update_session_objects = MagicMock() |
33 |
| - self.scraper._do_get_train_list = MagicMock(return_value={"listadoTrenes": []}) |
| 74 | +@patch('src.scraper.requests.Session.post') |
| 75 | +def test_do_get_train_list(mock_post, scraper): |
| 76 | + mock_post.return_value.ok = True |
| 77 | + mock_post.return_value.text = 'r.handleCallback(0,0,{"listadoTrenes":[]});' |
| 78 | + train_list = scraper._do_get_train_list() |
| 79 | + assert "listadoTrenes" in train_list |
34 | 80 |
|
35 |
| - # Test if get_trainrides returns an empty list when no trains are found |
36 |
| - result = self.scraper.get_trainrides() |
37 |
| - self.assertEqual(result, []) |
38 |
| - |
39 |
| - def test_invalid_trainride_filter(self): |
40 |
| - # Test the case where return_date is before departure_date |
41 |
| - with self.assertRaises(InvalidTrainRideFilter): |
42 |
| - Scraper( |
43 |
| - origin=self.origin, |
44 |
| - destination=self.destination, |
45 |
| - departure_date=self.departure_date, |
46 |
| - return_date=datetime(2025, 1, 30, 10, 0) # Invalid return date |
47 |
| - ) |
48 |
| - |
49 |
| - @patch('scraper.extract_dwr_token') |
50 |
| - def test_extract_dwr_token(self, mock_extract_dwr_token): |
51 |
| - # Test extracting the DWR token with a valid response |
52 |
| - mock_response = 'throw #DWR-REPLY\nr.handleCallback("1","0","12345");' |
53 |
| - mock_extract_dwr_token.return_value = '12345' |
54 |
| - token = extract_dwr_token(mock_response) |
55 |
| - self.assertEqual(token, '12345') |
| 81 | +def test_change_datetime_hour(): |
| 82 | + date = datetime(2023, 12, 25) |
| 83 | + hour = "14:30" |
| 84 | + new_date = Scraper._change_datetime_hour(hour, date) |
| 85 | + assert new_date.hour == 14 |
| 86 | + assert new_date.minute == 30 |
56 | 87 |
|
57 |
| - def test_extract_dwr_token_invalid(self): |
58 |
| - # Test extracting the DWR token with an invalid response |
59 |
| - with self.assertRaises(InvalidDWRToken): |
60 |
| - extract_dwr_token('Invalid response') |
61 |
| - |
62 |
| - def test_create_cookiedict(self): |
63 |
| - # Test the creation of cookies for the search |
64 |
| - cookies = create_cookiedict(self.origin, self.destination) |
65 |
| - self.assertIn("Search", cookies["name"]) |
66 |
| - self.assertIn(self.origin.code, cookies["value"]) |
67 |
| - |
68 |
| - def test_create_search_id(self): |
69 |
| - # Test the creation of search_id |
70 |
| - search_id = create_search_id() |
71 |
| - self.assertTrue(search_id.startswith('_')) |
72 |
| - self.assertEqual(len(search_id), 5) |
73 |
| - |
74 |
| - def test_create_session_script_id(self): |
75 |
| - # Test the creation of session script ID |
76 |
| - with patch('scraper.tokenify', return_value='test_token'): |
77 |
| - script_id = create_session_script_id('dwr_token') |
78 |
| - self.assertTrue(script_id.startswith('dwr_token/')) |
79 |
| - self.assertIn('test_token', script_id) |
| 88 | +def test_is_train_available(): |
| 89 | + train = { |
| 90 | + "completo": False, |
| 91 | + "razonNoDisponible": "", |
| 92 | + "tarifaMinima": "10.00" |
| 93 | + } |
| 94 | + assert Scraper._is_train_available(train) |
80 | 95 |
|
81 |
| -if __name__ == "__main__": |
82 |
| - unittest.main() |
| 96 | +def test_is_train_not_available(): |
| 97 | + train = { |
| 98 | + "completo": True, |
| 99 | + "razonNoDisponible": "1", |
| 100 | + "tarifaMinima": None |
| 101 | + } |
| 102 | + assert not Scraper._is_train_available(train) |
0 commit comments