コード例 #1
0
    def test_on_put_error_result(self):
        """
        Test if we will receive correct response
        """

        resource = CollectionResource(objects_class=FakeObjectClass)
        env = create_environ(path='/')
        req = Request(env)
        req.context = {
            'doc': {
                'id': 1,
            }
        }

        def clean(data):
            if 'name' not in data:
                return {}, {'name': ['Test Error Message']}

        resource.clean = clean

        resp = Response()
        resource.on_put(req=req, resp=resp)
        self.assertEqual(resp.body,
                         {'errors': {
                             'name': ['Test Error Message']
                         }})
コード例 #2
0
 def test_on_put_success_result(self):
     """
     Test if we will receive correct response
     """
     resource = CollectionResource(objects_class=FakeObjectClass)
     env = create_environ(path='/')
     req = Request(env)
     req.context = {'doc': {'id': 1, 'name': 'Opentopic'}}
     resp = Response()
     resource.on_put(req=req, resp=resp)
     self.assertEqual(resp.body, {'id': 1, 'name': 'Opentopic'})
コード例 #3
0
ファイル: test_update.py プロジェクト: Opentopic/falcon-api
    def test_update_withtout_pk(self):
        """
        test how update function will handle when we are not going to pass pk value
        """
        resource = SingleResource(objects_class=None)
        env = create_environ(path='/')
        req = Request(env)
        req.context = {'doc': {}}
        resp = Response()

        with self.assertRaises(Exception):
            resource.on_patch(req, resp)
コード例 #4
0
ファイル: test_update.py プロジェクト: Opentopic/falcon-api
    def test_update_get_object(self):
        """
        Test `get_object` func
        """
        resource = SingleResource(objects_class=None)
        env = create_environ(path='/')
        req = Request(env)
        req.context = {'doc': {'pk': 1}}
        resp = Response()

        resource.objects_class = FakeObjectList()
        obj = resource.get_object(req=req, resp=resp, path_params={})
        self.assertEqual(obj.pk, 1)
コード例 #5
0
 def test_on_head(self):
     """
     need to check if status of the response is set for 200 and
     """
     env = create_environ(path='/')
     req = Request(env)
     req.context = {'doc': {}}
     req.params[mongoengine.CollectionResource.PARAM_TOTAL_COUNT] = '1'
     resp = Response()
     resource = mongoengine.CollectionResource(
         objects_class=Mock(return_value=[1, 2, 3]), max_limit=2)
     resource.get_object_list = Mock(return_value=[1, 2])
     resource.get_total_objects = Mock(return_value={'total_count': 3})
     resource.on_head(req=req, resp=resp)
     self.assertIsNot(resp.body, [1, 2, 3])
     self.assertEqual(resp.get_header('x-api-total'), '3')
     self.assertEqual(resp.get_header('x-api-returned'), '2')
コード例 #6
0
 def test_on_options(self):
     """
     need to check if status of the response is set for 200 and
     """
     env = create_environ(path='/')
     req = Request(env)
     req.context = {'doc': {}}
     resp = Response()
     resource = mongoengine.CollectionResource(objects_class=FakeObjectList,
                                               max_limit=2)
     resource.on_options(req=req, resp=resp)
     self.assertEqual(resp.get_header('Allow'),
                      'GET, HEAD, OPTIONS, POST, PUT')
     self.assertEqual(
         resp.body, {
             'name': 'FakeObjectList',
             'description':
             'Extend list to match interface of List resource',
         })
コード例 #7
0
    def test_clean_check_error_raising(self):
        """
        Check if clean function returns errors dict when `clean_param_name` raise `ParamException`
        """
        resource = CollectionResource(objects_class=None)
        env = create_environ(path='/')
        req = Request(env)
        req.context = {'doc': {'id': 1, 'name': 'Opentopic'}}
        Response()

        def clean_name_test(self):
            raise ParamException('Test Error Message')

        resource.clean_name = clean_name_test

        data, errors = resource.clean(req.context['doc'])
        self.assertEqual(data, {
            'id': 1,
        })
        self.assertEqual(errors, {'name': ['Test Error Message']})
コード例 #8
0
ファイル: test_update.py プロジェクト: Opentopic/falcon-api
    def test_update_params(self):
        """
        Test if param is updated and returned
        """
        resource = SingleResource(objects_class=None)
        env = create_environ(path='/')
        req = Request(env)
        req.context = {
            'doc': {
                'pk': 1,
                'name': 'TestNewName'
            },
        }
        resp = Response()

        resource.objects_class = FakeObjectList()
        resource.serialize = fake_serialize
        resource.on_patch(req, resp)
        self.assertEqual({
            'pk': 1,
            'name': 'TestNewName',
            '_saved': True
        }, resp.body)
コード例 #9
0
ファイル: test_update.py プロジェクト: Opentopic/falcon-api
    def test_update_when_no_expected_params_is_set(self):
        """
        Test if update func will not update param if it's not defined in expected params
        """
        resource = SingleResource(objects_class=None)
        env = create_environ(path='/')
        req = Request(env)
        req.context = {'doc': {'pk': 1, 'name': 'TestNewName'}}

        def clean(data):
            return {}, {}

        resource.clean = clean

        resp = Response()

        resource.objects_class = FakeObjectList()
        resource.serialize = fake_serialize
        resource.on_patch(req, resp)
        self.assertEqual({
            'pk': 1,
            'name': 'OldName',
            '_saved': True
        }, resp.body)