示例#1
0
    def post_subscription(self, subscription: Subscription) -> Subscription:
        subscription_data = subscription.to_json()

        return self.perform_request('POST',
                                    self._url_subscriptions,
                                    json=subscription_data,
                                    response_class=Subscription)
def test__get_subscription_by_name__subscription_is_found_and_returned():
    subscription = Subscription(queue='queue')

    sm_client = SubscriptionManagerClient(mock.Mock())

    sm_service = SubscriptionManagerService(sm_client)

    sm_client.get_subscriptions = mock.Mock(return_value=[subscription])

    db_subscription = sm_service._get_subscription_by_queue(subscription.queue)

    assert db_subscription == subscription
def test_unsubscribe__sm_api_error__raises_SubscriptionManagerServiceError():
    subscription = Subscription(id=1, queue=uuid.uuid4().hex)

    sm_client = SubscriptionManagerClient(mock.Mock())

    sm_service = SubscriptionManagerService(sm_client)

    sm_service._get_subscription_by_queue = mock.Mock(return_value=subscription)

    sm_client.delete_subscription_by_id = mock.Mock(side_effect=APIError('server error', status_code=500))

    with pytest.raises(SubscriptionManagerServiceError) as e:
        sm_service.unsubscribe(subscription.queue)
    assert f"Error while deleting subscription '{subscription.id}': [500] - server error" == str(e.value)
def test_resume__no_errors():
    subscription = Subscription(id=1, queue=uuid.uuid4().hex, active=False)

    sm_client = SubscriptionManagerClient(mock.Mock())

    sm_service = SubscriptionManagerService(sm_client)

    sm_service._get_subscription_by_queue = mock.Mock(return_value=subscription)

    sm_client.put_subscription = mock.Mock()

    sm_service.resume(subscription.queue)

    called_subscription_id, called_subscription = sm_client.put_subscription.call_args[0]
    assert called_subscription_id == subscription.id
    assert called_subscription.active is True
def test_unsubscribe__no_errors():
    subscription = Subscription(id=1, queue=uuid.uuid4().hex)

    sm_client = SubscriptionManagerClient(mock.Mock())

    sm_service = SubscriptionManagerService(sm_client)

    sm_service._get_subscription_by_queue = mock.Mock(return_value=subscription)

    sm_client.delete_subscription_by_id = mock.Mock()

    sm_service.unsubscribe(subscription.queue)

    called_subscription_id = sm_client.delete_subscription_by_id.call_args[0][0]

    assert subscription.id == called_subscription_id
def make_subscription(queue='queue'):
    subscription_dict = {
        'queue': queue,
        'topic': {
            'name': 'topic',
            'id': 1
        },
        'active': True,
        'qos': 'EXACTLY_ONCE',
        'durable': True,
        'id': 1
    }

    subscription = Subscription.from_json(subscription_dict)

    return subscription_dict, subscription
def test_resume__sm_api_error__raises_SubscriptionManagerServiceError():
    subscription = Subscription(id=1, queue=uuid.uuid4().hex, active=False)

    sm_client = SubscriptionManagerClient(mock.Mock())

    sm_service = SubscriptionManagerService(sm_client)

    sm_service._get_subscription_by_queue = mock.Mock(return_value=subscription)

    sm_client.put_subscription = mock.Mock(side_effect=APIError('server error', status_code=500))

    with pytest.raises(SubscriptionManagerServiceError) as e:
        sm_service.resume(subscription.queue)
    assert f"Error while updating subscription '{subscription.id}': [500] - server error" == str(e.value)
    called_subscription_id, called_subscription = sm_client.put_subscription.call_args[0]
    assert called_subscription_id == subscription.id
    assert called_subscription.active is True
def test_subscribe__no_errors__returns_subscription_queue():
    topic = Topic(name='topics', id=1)

    sm_client = SubscriptionManagerClient(mock.Mock())

    sm_client.get_topics = mock.Mock(return_value=[topic])

    sm_service = SubscriptionManagerService(sm_client)

    subscription = Subscription(queue=uuid.uuid4().hex, topic_id=topic.id)

    sm_client.post_subscription = mock.Mock(return_value=subscription)

    queue = sm_service.subscribe('topics')

    assert subscription.queue == queue

    called_subscription = sm_client.post_subscription.call_args[0][0]

    assert topic.id == called_subscription.topic_id
    def subscribe(self, topic_name: str) -> str:
        """
        Subscribes the client to the given topics.

        :return: A unique queue corresponding to this subscription
        """
        db_topics = self.client.get_topics()

        try:
            topic = [topic for topic in db_topics
                     if topic.name == topic_name][0]
        except IndexError:
            raise SubscriptionManagerServiceError(
                f"{topic_name} is not registered in Subscription Manager")

        subscription = Subscription(topic_id=topic.id)

        try:
            db_subscription = self.client.post_subscription(subscription)
        except APIError as e:
            raise SubscriptionManagerServiceError(
                f"Error while subscribing to {topic_name}: {str(e)}")

        return db_subscription.queue
def test_subscription__from_json(subscription_dict, expected_subscription):
    subscription = Subscription.from_json(subscription_dict)

    assert expected_subscription == subscription
def test_subscription__invalid_qos__raises_valueerror(qos):
    subscription = Subscription(queue='queue')

    with pytest.raises(ValueError) as e:
        subscription.qos = qos
    assert f'qos should be one of {QOS.all()}' == str(e.value)
    (
        {
            'queue': 'queue name',
            'topic': {
                'name': 'topic',
                'id': 1
            },
            'active': True,
            'qos': 'EXACTLY_ONCE',
            'durable': True,
            'id': 1
        },
        Subscription(
            queue='queue name',
            topic=Topic(name='topic', id=1),
            active=True,
            qos=QOS.EXACTLY_ONCE.value,
            durable=True,
            id=1
        )
    )
])
def test_subscription__from_json(subscription_dict, expected_subscription):
    subscription = Subscription.from_json(subscription_dict)

    assert expected_subscription == subscription


@pytest.mark.parametrize('subscription, expected_dict', [
    (
        Subscription(
            queue='queue name',