示例#1
0
 def setUp(self):
     self.service = Mock()
     self.converter = Mock()
     self.resource = SingleAssociationResource(self.service, self.converter)
示例#2
0
 def setUp(self):
     self.service = Mock()
     self.converter = Mock()
     self.resource = SingleAssociationResource(self.service, self.converter)
示例#3
0
class TestSingleAssociationResource(unittest.TestCase):
    def setUp(self):
        self.service = Mock()
        self.converter = Mock()
        self.resource = SingleAssociationResource(self.service, self.converter)

    def test_when_getting_an_association_then_parent_validated(
            self, request, url_for):
        self.resource.get_association(sentinel.parent_id)

        self.service.validate_parent.assert_called_once_with(
            sentinel.parent_id)

    def test_when_getting_an_association_then_service_called(
            self, request, url_for):
        self.resource.get_association(sentinel.parent_id)

        self.service.get_by_parent.assert_called_once_with(sentinel.parent_id)

    def test_when_getting_an_association_then_model_converted(
            self, request, url_for):
        expected_list = self.service.get_by_parent.return_value
        expected_response = self.converter.encode.return_value

        result = self.resource.get_association(sentinel.parent_id)

        self.converter.encode.assert_called_once_with(expected_list)
        assert_that(
            result,
            equal_to((expected_response, 200, {
                'Content-Type': 'application/json'
            })))

    def test_when_associating_resource_then_request_decoded(self, request, __):
        self.resource.associate(sentinel.parent_id)

        self.converter.decode.assert_called_once_with(request)

    def test_when_associating_resource_then_parent_is_validated(self, _, __):
        self.resource.associate(sentinel.parent_id)

        self.service.validate_parent.assert_called_once_with(
            sentinel.parent_id)

    def test_when_associating_resource_then_association_created(self, _, __):
        expected_association = self.converter.decode.return_value

        self.resource.associate(sentinel.parent_id)

        self.service.associate.assert_called_once_with(expected_association)

    def test_when_associating_then_association_encoded(self, request, url_for):
        expected_association = self.service.associate.return_value
        expected_response = self.converter.encode.return_value

        result = self.resource.associate(sentinel.parent_id)

        self.converter.encode.assert_called_once_with(expected_association)
        assert_that(
            result,
            equal_to((expected_response, 201, {
                'Content-Type': 'application/json',
                'Location': url_for.return_value
            })))

    def test_when_dissociating_then_parent_is_validated(
            self, request, url_for):
        self.resource.dissociate(sentinel.parent_id)

        self.service.validate_parent.assert_called_once_with(
            sentinel.parent_id)

    def test_when_dissociating_then_association_fetched_through_service(
            self, request, url_for):
        self.resource.dissociate(sentinel.parent_id)

        self.service.get_by_parent.assert_called_once_with(sentinel.parent_id)

    def test_when_dissociating_then_service_called(self, request, url_for):
        expected_association = self.service.get_by_parent.return_value

        result = self.resource.dissociate(sentinel.parent_id)

        self.service.dissociate.assert_called_once_with(expected_association)
        assert_that(result, equal_to(('', 204)))
示例#4
0
class TestSingleAssociationResource(unittest.TestCase):

    def setUp(self):
        self.service = Mock()
        self.converter = Mock()
        self.resource = SingleAssociationResource(self.service, self.converter)

    def test_when_getting_an_association_then_parent_validated(self, request, url_for):
        self.resource.get_association(sentinel.parent_id)

        self.service.validate_parent.assert_called_once_with(sentinel.parent_id)

    def test_when_getting_an_association_then_service_called(self, request, url_for):
        self.resource.get_association(sentinel.parent_id)

        self.service.get_by_parent.assert_called_once_with(sentinel.parent_id)

    def test_when_getting_an_association_then_model_converted(self, request, url_for):
        expected_list = self.service.get_by_parent.return_value
        expected_response = self.converter.encode.return_value

        result = self.resource.get_association(sentinel.parent_id)

        self.converter.encode.assert_called_once_with(expected_list)
        assert_that(result, equal_to((expected_response,
                                     200,
                                     {'Content-Type': 'application/json'})))

    def test_when_associating_resource_then_request_decoded(self, request, _):
        self.resource.associate(sentinel.parent_id)

        self.converter.decode.assert_called_once_with(request)

    def test_when_associating_resource_then_parent_is_validated(self, request, _):
        self.resource.associate(sentinel.parent_id)

        self.service.validate_parent.assert_called_once_with(sentinel.parent_id)

    def test_when_associating_resource_then_association_created(self, request, _):
        expected_association = self.converter.decode.return_value

        self.resource.associate(sentinel.parent_id)

        self.service.associate.assert_called_once_with(expected_association)

    def test_when_associating_then_association_encoded(self, request, url_for):
        expected_association = self.service.associate.return_value
        expected_response = self.converter.encode.return_value

        result = self.resource.associate(sentinel.parent_id)

        self.converter.encode.assert_called_once_with(expected_association)
        assert_that(result, equal_to((expected_response,
                                      201,
                                      {'Content-Type': 'application/json',
                                       'Location': url_for.return_value})))

    def test_when_dissociating_then_parent_is_validated(self, request, url_for):
        self.resource.dissociate(sentinel.parent_id)

        self.service.validate_parent.assert_called_once_with(sentinel.parent_id)

    def test_when_dissociating_then_association_fetched_through_service(self, request, url_for):
        self.resource.dissociate(sentinel.parent_id)

        self.service.get_by_parent.assert_called_once_with(sentinel.parent_id)

    def test_when_dissociating_then_service_called(self, request, url_for):
        expected_association = self.service.get_by_parent.return_value

        result = self.resource.dissociate(sentinel.parent_id)

        self.service.dissociate.assert_called_once_with(expected_association)
        assert_that(result, equal_to(('', 204)))