def test_marker_does_not_exist(self):
        """Ensure the marker is saved if it does not already exist."""
        async_id = "asyncid"

        store_async_marker(async_id, 0)

        self.assertIsNotNone(FuriousAsyncMarker.get_by_id(async_id))
    def test_marker_does_not_exist(self):
        """Ensure the marker is saved if it does not already exist."""
        async_id = "asyncid"

        store_async_marker(async_id, 0)

        self.assertIsNotNone(FuriousAsyncMarker.get_by_id(async_id))
    def test_marker_does_exist(self):
        """Ensure the marker is not saved if it already exists."""
        async_id = "asyncid"
        result = '{"foo": "bar"}'
        FuriousAsyncMarker(id=async_id, result=result, status=1).put()

        store_async_marker(async_id, 1)

        marker = FuriousAsyncMarker.get_by_id(async_id)

        self.assertEqual(marker.result, result)
        self.assertEqual(marker.status, 1)
    def test_marker_does_exist(self):
        """Ensure the marker is not saved if it already exists."""
        async_id = "asyncid"
        result = '{"foo": "bar"}'
        FuriousAsyncMarker(id=async_id, result=result, status=1).put()

        store_async_marker(async_id, 1)

        marker = FuriousAsyncMarker.get_by_id(async_id)

        self.assertEqual(marker.result, result)
        self.assertEqual(marker.status, 1)
    def test_completion_store(self):
        """Ensure the marker is stored on completion even without a result."""

        async = Async('foo')
        async._executed = True

        result = context_completion_checker(async)

        self.assertTrue(result)

        marker = FuriousAsyncMarker.get_by_id(async.id)

        self.assertIsNotNone(marker)
        self.assertEqual(marker.key.id(), async.id)
        self.assertEqual(marker.status, -1)
class StoreAsyncMarkerTestCase(NdbTestBase):
    def test_marker_does_not_exist(self):
        """Ensure the marker is saved if it does not already exist."""
        async_id = "asyncid"

        store_async_marker(async_id, 0)

        self.assertIsNotNone(FuriousAsyncMarker.get_by_id(async_id))

    def test_marker_does_exist(self):
        """Ensure the marker is not saved if it already exists."""
        async_id = "asyncid"
        result = '{"foo": "bar"}'
        FuriousAsyncMarker(id=async_id, result=result, status=1).put()

        store_async_marker(async_id, 1)

        marker = FuriousAsyncMarker.get_by_id(async_id)

        self.assertEqual(marker.result, result)
        self.assertEqual(marker.status, 1)

    def test_store_async_exception(self):
        """Ensure an async exception is encoded correctly."""
        async_id = "asyncid"
        async_result = AsyncResult()
        try:
            raise Exception()
        except Exception, e:
            async_result.payload = encode_exception(e)
            async_result.status = async_result.ERROR

        store_async_result(async_id, async_result)

        marker = FuriousAsyncMarker.get_by_id(async_id)

        self.assertEqual(marker.result, json.dumps(async_result.to_dict()))
        self.assertEqual(marker.status, async_result.ERROR)