def test_only_deletes_appropriate_transaction(self):
        raw_task = self.mock_task()
        task = worker.task(wait_for_transaction(raw_task), bind=True)
        transaction_id = TaskTransaction.new_id()
        other_transaction_id = TaskTransaction.new_id()
        task(transaction_id)

        session = Client().session()
        transaction_ids = session.query(TaskTransaction.transaction_id).all()
        eq_(transaction_ids, [(other_transaction_id,)])
Esempio n. 2
0
    def test_toomanyinvalidation_errors_cause_retry(self, MockClient):
        error = CloudFrontServerError(400, 'Bad Request')
        error.error_code = 'TooManyInvalidationsInProgress'

        cloudfront = Mock()
        cloudfront.create_invalidation_request.side_effect = error

        config = Client().config()
        client = Mock()
        client.config.return_value = config
        client.get_cloudfront.return_value = cloudfront
        MockClient.return_value = client

        session = Client().session()
        transaction_id = TaskTransaction.new_id()
        image = Image(filename='abad1dea')
        image.created_at = '2001-01-01 00:00:00'
        session.add(image)
        session.flush()

        invalidate = NeverCalledDirectlyInvalidate()
        invalidate.retry = Mock()
        invalidate(transaction_id, image.image_id)

        invalidate.retry.assert_called_with(exc=error)
    def test_runs_if_transaction_found(self):
        raw_task = self.mock_task()
        task = worker.task(wait_for_transaction(raw_task), bind=True)
        transaction_id = TaskTransaction.new_id()
        task(transaction_id)

        raw_task.assert_called_once_with(task)
    def test_transaction_is_deleted_after_running(self):
        raw_task = self.mock_task()
        task = worker.task(wait_for_transaction(raw_task), bind=True)
        transaction_id = TaskTransaction.new_id()
        task(transaction_id)

        transactions = Client().session().query(TaskTransaction).all()
        eq_(transactions, [])
Esempio n. 5
0
def delay(queue, task, *args, **kwargs):
    transaction_id = TaskTransaction.new_id()
    args = [str(transaction_id)] + list(args)
    task_info = task.apply_async(
        args=args,
        kwargs=kwargs,
        retry=True,
        retry_policy=RETRY_POLICY
    )
    queue.append(task_info)
    return task_info.id
Esempio n. 6
0
    def test_uploads_to_s3(self, ImageTruck):
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        process_image(transaction_id, contents.image_contents_id)

        ImageTruck.assert_called_with(image_data, 'image/png', None)
        truck.upload.assert_called_with()
Esempio n. 7
0
    def test_reorients_images(self, ReorientImage, ImageTruck):
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck
        ReorientImage.reorient_image.return_value = image_data

        process_image(transaction_id, contents.image_contents_id)

        ReorientImage.reorient_image.assert_called_with(image_data)
Esempio n. 8
0
    def test_deletes_processed_contents(self, ImageTruck):
        session = Client().session()
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        process_image(transaction_id, contents.image_contents_id)

        session = Client().session()
        contents = session.query(ImageContents).all()

        eq_(contents, [])
Esempio n. 9
0
    def test_makes_resizes(self, ResizeImage, ImageTruck):
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        process_image(transaction_id, contents.image_contents_id)

        call_args = ResizeImage.make_resizes.call_args
        eq_(call_args[0][0], image)
        eq_(call_args[0][1], truck)
        # the 3rd arg is the after_upload callback, which we can't assert well
        eq_(len(call_args[0]), 3)
Esempio n. 10
0
    def test_invalidate_an_image__with_a_resize_suffix(self):
        cloudfront = Mock()
        Client().get_cloudfront = Mock()
        Client().get_cloudfront.return_value = cloudfront

        transaction_id = TaskTransaction.new_id()
        image = Image(filename='f131d')
        image.created_at = '2001-05-09 13:00:00'
        session = Client().session()
        session.add(image)
        session.flush()

        Invalidate().run(transaction_id, image.image_id, suffix="teensy")

        cloudfront.create_invalidation_request.assert_called_with(
            'FETCHISNTGOINGTOHAPPEN', ['f131d_teensy'])
Esempio n. 11
0
    def test_invalidate_an_image(self):
        cloudfront = Mock()
        Client().get_cloudfront = Mock()
        Client().get_cloudfront.return_value = cloudfront

        transaction_id = TaskTransaction.new_id()
        image = Image(filename='c1a115')
        image.created_at = '2001-05-09 13:00:00'
        session = Client().session()
        session.add(image)
        session.flush()

        Invalidate().run(transaction_id, image.image_id)

        cloudfront.create_invalidation_request.assert_called_with(
            'JEEZAMANDA', ['c1a115'])
Esempio n. 12
0
    def test_invalidation_of_new_images_is_a_noop(self, mock_time, MockClient):
        now = time.strptime('2011-05-09 15:01:01', '%Y-%m-%d %H:%M:%S')
        mock_time.strftime = time.strftime
        mock_time.gmtime.return_value = now

        get_cloudfront = Mock()
        get_cloudfront.side_effect = AssertionError('should not have called me!')
        Client().get_cloudfront = get_cloudfront

        transaction_id = TaskTransaction.new_id()
        image = Image(filename='c1a115')
        # created 1 hour before "now"
        image.created_at = '2011-05-09 13:00:00'
        session = Client().session()
        session.add(image)
        session.flush()

        Invalidate().run(transaction_id, image.image_id)
Esempio n. 13
0
    def test_unknown_cloudfront_errors_reraise(self, MockClient):
        error = CloudFrontServerError(400, 'Bad Request')
        error.error_code = 'CloudFrontHatesYouToday'

        cloudfront = Mock()
        cloudfront.create_invalidation_request.side_effect = error

        config = Client().config()
        client = Mock()
        client.config.return_value = config
        client.get_cloudfront.return_value = cloudfront
        MockClient.return_value = client

        transaction_id = TaskTransaction.new_id()
        session = Client().session()
        image = Image(filename='abad1dea')
        image.created_at = '2001-01-01 00:00:00'
        session.add(image)
        session.flush()

        Invalidate()(transaction_id, image.image_id)
Esempio n. 14
0
    def test_resize_happens_before_main_upload(
            self, ResizeImage, ImageTruck):
        class StopDoingThingsNow(StandardError): pass

        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        truck.upload.side_effect = StopDoingThingsNow
        ImageTruck.return_value = truck

        try:
            process_image(transaction_id, contents.image_contents_id)
        except StopDoingThingsNow:
            pass

        call_args = ResizeImage.make_resizes.call_args
        eq_(call_args[0][0], image)
        eq_(call_args[0][1], truck)
        # the 3rd arg is the after_upload callback, which we can't assert well
        eq_(len(call_args[0]), 3)
Esempio n. 15
0
    def test_calculates_metadata(self, ImageTruck, ReorientImage, ResizeImage):
        session = Client().session()
        with open(EXIF_JPG, 'r') as fh:
            image_data = fh.read()

        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        transaction_id = TaskTransaction.new_id()
        session.flush()
        process_image(transaction_id, contents.image_contents_id)

        image = session.query(Image).\
            filter(Image.image_id == image.image_id)\
            .one()

        eq_(image.camera, 'SAMSUNG NX210')
        eq_(image.photographed_at, '2013-05-03 09:17:02')
        eq_(image.aperture, '1/4.5')
        eq_(image.shutter_speed, '1/800')
        eq_(image.focal_length, 30.0)
        eq_(image.iso, 200)