def test_begin(self, mocker): grabber = Grabber('http://example.com') grabber._test_max_ticks = 5 grabber.every = 13 grabber.tick = mocker.Mock(grabber.tick, autospec=True) mock_sleep = mocker.patch('camgrab.camgrab.sleep', autospec=True) grabber.begin() assert grabber.tick.call_count == 5 assert mock_sleep.call_count == 5 mock_sleep.assert_called_with(13)
def test_save_simple_image(self, mocker): dummy_datetime = datetime(2017, 1, 2, 12, 13, 14, 987654) mocked_datetime = mocker.patch('camgrab.camgrab.datetime', autospec=True, return_value=dummy_datetime) mocked_datetime.now = mocker.Mock(return_value=dummy_datetime) mock_sleep = mocker.patch('camgrab.camgrab.sleep', autospec=True) mock_image_save = mocker.patch('camgrab.camgrab.Image.Image.save') mock_makedirs = mocker.patch('camgrab.camgrab.makedirs') dummy_url = 'http://some-url.com/image.jpg' dummy_image_path = os.path.join(os.path.dirname(__file__), 'assets', 'kitty.jpg') dummy_body = open(dummy_image_path, 'rb').read() httpretty.register_uri(httpretty.GET, dummy_url, body=dummy_body, content_type='image/jpeg') num_ticks = 5 every = 5 save_dir = 'some_dir' grabber = Grabber(dummy_url, every=every, save_dir=save_dir) grabber._test_max_ticks = num_ticks grabber.save_filename = '{Y}{m}{d}/{H}/{Y}{m}{d}-{H}{M}{S}-{f}.jpg' expected_dir = 'some_dir/20170102/12' expected_filename = 'some_dir/20170102/12/20170102-121314-987654.jpg' grabber.begin() assert mock_sleep.call_count == num_ticks mock_sleep.assert_called_with(every) assert mock_image_save.call_count == num_ticks mock_image_save.assert_called_with(expected_filename) assert mock_makedirs.call_count == num_ticks mock_makedirs.assert_called_with(expected_dir, exist_ok=True)