示例#1
0
    def test_join_path(self):
        """
        Test that the resource path can be joined corectly with the base path.
        """
        mock_resource = MagicMock()
        mock_resource.PATH = "some-path"

        manager = ResourceManager(mock_resource, MagicMock())
        assert manager.join_path("test") == "some-path/test"
示例#2
0
 def test_create_unsupported(self):
     """
     Test a POST (create) request with a resource that does not support that type or request.
     Asserts that MethodNotSupportedException is raised.
     """
     mock_resource = MagicMock()
     mock_resource.SUPPORTED_METHODS = ()
     manager = ResourceManager(mock_resource, MagicMock())
     with pytest.raises(MethodNotSupportedException):
         manager.create()
示例#3
0
 def test_get_unsupported(self):
     """
     Test a GET request with a resource that does not support it. Asserts that
     MethodNotSupportedException is raised.
     """
     mock_resource = MagicMock()
     mock_resource.SUPPORTED_METHODS = ()
     manager = ResourceManager(mock_resource, MagicMock())
     with pytest.raises(MethodNotSupportedException):
         manager.get(1)
示例#4
0
 def test_create_id_already_exists(self):
     """
     Tests that once an object is created, create method can not be called again. Asserts that
     ObjectAlreadyCreatedException is raised.
     """
     mock_resource = MagicMock()
     mock_resource.SUPPORTED_METHODS = ("POST",)
     mock_resource.id = MagicMock()
     manager = ResourceManager(mock_resource, MagicMock())
     with pytest.raises(ObjectAlreadyCreatedException):
         manager.create()
示例#5
0
    def test_handle_refresh_data(self):
        """
        Tests the ResourceManager.refresh_data method. Ensures that Field.set is called once with
        the correct data value.
        """
        mock_resource = MagicMock()
        mock_client = MagicMock()

        fields = [MagicMock() for i in range(5)]

        mock_resource.fields = {f: MagicMock() for f in fields}
        mock_data = {f.name: MagicMock() for f in fields}

        manager = ResourceManager(mock_resource, mock_client)

        manager.refresh_data(mock_data)

        for field in mock_resource.fields:
            field.set.assert_called_once_with(mock_data[field.name])
示例#6
0
    def test_get(self, monkeypatch):
        """
        Test a successful GET request. Tests that manager.handle_response is being called with
        the correct Response object.
        """
        mock_resource = MagicMock()
        mock_client = MagicMock()
        mock_response = MagicMock()
        mock_client.get = MagicMock(return_value=mock_response)

        mock_resource.SUPPORTED_METHODS = ("GET",)

        manager = ResourceManager(mock_resource, mock_client)
        monkeypatch.setattr(manager, "handle_response", MagicMock())

        manager.get(1)

        # TODO test that this is called with correct path
        mock_client.get.assert_called_once()
        manager.handle_response.assert_called_once_with(mock_response)
示例#7
0
    def test_init(self):
        """
        Test that a resource manager instance can be initialized correctly with a resource and
        client instance. Assets that both manager.resource and manager.client are set.
        """
        resource = MagicMock()
        client = MagicMock()
        manager = ResourceManager(resource, client)

        assert manager.resource == resource
        assert manager.client == client
示例#8
0
    def test_create(self, monkeypatch):
        """
        Tests a successful POST (create) method. Asserts that handle_response is called with the
        correct Response object.
        """
        mock_resource = MagicMock()
        mock_client = MagicMock()
        mock_response = MagicMock()
        mock_client.post = MagicMock(return_value=mock_response)

        mock_resource.SUPPORTED_METHODS = ("POST",)
        mock_resource.id = None

        manager = ResourceManager(mock_resource, mock_client)
        monkeypatch.setattr(manager, "handle_response", MagicMock())

        manager.create()

        # TODO test that this is called with correct path and params
        mock_client.post.assert_called_once()
        manager.handle_response.assert_called_once_with(mock_response)
示例#9
0
    def test_handle_response(self, monkeypatch):
        """
        Tests that a successful response initiates a call to handle_success_response, and that an
        error response initiates a call to handle_error_response.
        """
        mock_resource = MagicMock()
        mock_client = MagicMock()
        mock_response = MagicMock()
        mock_handle_success_response = MagicMock()
        mock_handle_error_response = MagicMock()

        manager = ResourceManager(mock_resource, mock_client)

        monkeypatch.setattr(manager, "handle_success_response", mock_handle_success_response)

        monkeypatch.setattr(manager, "handle_error_response", mock_handle_error_response)

        manager.handle_response(mock_response)
        assert manager.http_response_data == mock_response.json()
        assert manager.http_response_status_code == mock_response.status_code
        mock_handle_error_response.assert_called_once_with(mock_response)

        mock_response.status_code = 200
        manager.handle_response(mock_response)
        mock_handle_success_response.assert_called_once_with(mock_response)