Added tests
This commit is contained in:
37
test_scrape.py
Normal file
37
test_scrape.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from unittest.mock import Mock, patch
|
||||
import pytest
|
||||
import scrape
|
||||
import logging
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def log():
|
||||
log = scrape.get_logger(logging.DEBUG)
|
||||
yield log
|
||||
|
||||
|
||||
@patch('scrape.does_file_exist')
|
||||
def test_already_run(mock_does_file_exist, log):
|
||||
mock_does_file_exist.return_value = True
|
||||
assert scrape.does_file_exist(scrape.config['message_sent_file'])
|
||||
with pytest.raises(StopIteration):
|
||||
scrape.main(scrape.config, log)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("json, expected",
|
||||
[({"Y1357": [{"edp": "40263442", "stockStatus": "OutOfStock"}]}, False),
|
||||
({"Y1357": [{"edp": "40263442", "stockStatus": "LowStock"}]}, True)])
|
||||
@patch('scrape.does_file_exist')
|
||||
@patch('scrape.requests.get')
|
||||
@patch('scrape.create_file')
|
||||
@patch('scrape.send_sms')
|
||||
def test_full_process(mock_send_sms, mock_create_file, mock_request_get, mock_does_file_exist, json, expected, log):
|
||||
mock_does_file_exist.return_value = False
|
||||
mock_response = Mock(**{'json.return_value': json})
|
||||
mock_request_get.return_value = mock_response
|
||||
|
||||
assert scrape.main(scrape.config, log) == expected
|
||||
if expected:
|
||||
mock_send_sms.assert_called_once_with(scrape.config['msg_to'],
|
||||
'The coat is in stock at ' + scrape.config['website_url'])
|
||||
mock_create_file.assert_called_once()
|
||||
Reference in New Issue
Block a user