def it_can_construct_a_special_uri_for_delete_operations(self):
        class Doc(Document):
            id = fields.NumberField()

            class Meta:
                backend_type = 'http'

            def uri(self):
                return 'get'

            def delete_uri(self):
                return 'delete'

        manager = BackendManager('http')
        doc = Doc({'id': 1})

        expected = {'id': 1}

        response = Mock(name='mock_http_response')
        response.status_code = 200

        # set the return value of the GET request and the DELETE request
        self.mock_request.delete.return_value = response
        response.content = json.dumps(expected)
        self.mock_request.get.return_value = response

        # now make the fetch operation
        manager.delete(doc)

        # we should have made one GET and one PUT request
        eq_([('delete', {'url': 'delete'})],
                self.mock_request.method_calls)
    def it_can_delete_resources_from_a_remote_endpoint(self):
        mock_resp = Mock(name="mock_response")
        expected = {'id': 1}
        mock_resp.content = json.dumps(expected)
        mock_resp.status_code = 204

        self.mock_request.delete.return_value = mock_resp

        manager = BackendManager('http')

        class Doc(Document):
            id = fields.NumberField()

            class Meta:
                backend_type = 'http'

            def uri(self):
                return 'http://location'

        doc = Doc({'id': 1})

        manager.delete(doc)

        # make sure we are working with correct expectations
        eq_(HttpBackendManager, type(manager))
        eq_([
            ('delete', {'url': doc.uri()})
            ],
                self.mock_request.method_calls)
 def it_can_fetch_save_and_delete_to_the_specific_backend_manager(self):
     with patch("docar.backends.DjangoBackendManager") as mock:
         mock_manager = Mock()
         mock_manager = mock.return_value
         manager = BackendManager("django")
         # first assert that the manager is really mocked
         ok_(isinstance(manager, Mock))
         manager.fetch()
         manager.save()
         manager.delete()
         eq_(True, mock_manager.fetch.called)
         eq_(True, mock_manager.save.called)
         eq_(True, mock_manager.delete.called)
    def it_can_take_credentials_as_arguments(self, mock_auth):
        auth_token = HTTPBasicAuth('crito', 'secret')
        mock_auth.return_value = auth_token

        mock_resp = Mock(name="mock_response")
        expected = {'id': 1}
        mock_resp.status_code = 200
        mock_resp.content = json.dumps(expected)

        self.mock_request.get.return_value = mock_resp
        self.mock_request.delete.return_value = mock_resp
        self.mock_request.put.return_value = mock_resp

        manager = BackendManager('http')

        class Doc(Document):
            id = fields.NumberField()

            class Meta:
                backend_type = 'http'

            def uri(self):
                return 'http://location'

        doc = Doc({'id': 1})

        # the http manager returns the response as python dict
        content = manager.fetch(doc, username='******', password='******')
        manager.delete(doc, username='******', password='******')
        manager.save(doc, username='******', password='******')

        # make sure we are working with correct expectations
        eq_(HttpBackendManager, type(manager))
        eq_(mock_resp, manager.response)
        ok_(isinstance(content, dict))
        eq_([
            ('get', {'url': doc.uri(), 'auth': auth_token}),
            ('delete', {'url': doc.uri(), 'auth': auth_token}),
            ('put', {'url': doc.uri(), 'data': '{"id": 1}',
                'auth': auth_token})],
                self.mock_request.method_calls)
    def it_can_delete_the_underlying_model_instance(self):
        DjangoModel = Mock(name="DjangoModel")
        mock_model = Mock()
        mock_model.id = 1
        DjangoModel.objects.get.return_value = mock_model

        manager = BackendManager('django')
        # The manager needs to know which model it connects to
        # This is normally done when the Document is created.
        manager._model = DjangoModel

        # make sure we are working with correct expectations
        eq_(DjangoBackendManager, type(manager))

        class Doc(Document):
            id = fields.NumberField()

            class Meta:
                backend_type = 'django'
                model = DjangoModel

        doc = Doc({'id': 1})

        manager.delete(doc)

        eq_([('objects.get', {'id': 1})], DjangoModel.method_calls)
        eq_([('delete',)], mock_model.method_calls)

        # If the model does not exist, nothing happens
        DjangoModel.reset_mock()
        mock_model.reset_mock()

        DjangoModel.DoesNotExist = Exception
        DjangoModel.objects.get.side_effect = DjangoModel.DoesNotExist

        manager.delete(doc)

        eq_([('objects.get', {'id': 1})], DjangoModel.method_calls)
        # no method of the model should have been called
        eq_([], mock_model.method_calls)