def test_should_save_image(self): good_grabber = Grabber('http://example.com') good_grabber.save = True good_grabber.save_filename = 'some_filename' good_grabber.save_dir = 'some_dir' assert good_grabber.should_save_image() is True no_save_grabber = Grabber('http://example.com') no_save_filename_grabber = Grabber('http://example.com') no_save_dir_grabber = Grabber('http://example.com') no_save_grabber.save = False no_save_grabber.save_filename = 'some_filename' no_save_grabber.save_dir = 'some_dir' no_save_filename_grabber.save = True no_save_filename_grabber.save_filename = None no_save_filename_grabber.save_dir = 'some_dir' no_save_dir_grabber.save = True no_save_dir_grabber.save_filename = 'some_filename' no_save_dir_grabber.save_dir = None bad_grabbers = (no_save_grabber, no_save_dir_grabber, no_save_filename_grabber) for grabber in bad_grabbers: assert grabber.should_save_image() is False
def test_get_save_path(self, mocker): mocked_datetime = mocker.patch('camgrab.camgrab.datetime', autospec=True) fake_datetime = datetime(2017, 1, 2, 12, 13, 14, 987654) mocked_datetime.now = mocker.Mock(return_value=fake_datetime) grabber = Grabber('http://example.com') grabber.save_dir = 'a_dir' grabber.save_filename = '{Y}{m}{d}/blah/{H}{M}{S}{f}.jpg' expected = 'a_dir/{Y}{m}{d}/blah/{H}{M}{S}{f}.jpg' grabber.format_path = mocker.Mock(grabber.format_path, autospec=True, return_value=expected) full_save_path = grabber.get_save_path() assert full_save_path == expected
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)