コード例 #1
0
ファイル: test_objects.py プロジェクト: lodex/python-odata
    def test_create(self):
        # Post call ###########################################################
        def request_callback(request):
            payload = json.loads(request.body)

            assert 'OData-Version' in request.headers, 'OData-Version header not in request'

            assert 'ProductID' not in payload, 'Payload contains primary key'
            assert '@odata.type' in payload, 'Payload did not contain @odata.type'

            payload['ProductID'] = 1

            resp_body = payload
            headers = {}
            return requests.codes.created, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.POST,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        new_product = Product()
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5

        Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
コード例 #2
0
    def test_read(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        assert product.id == expected_id
        assert product.name == expected_name
        assert product.category == expected_category
        assert product.price == expected_price
コード例 #3
0
    def test_create_with_primary_key(self):
        # Post call ###########################################################
        def request_callback(request):
            payload = json.loads(request.body)
            self.assertEqual(payload.get('ProductID'),
                             55,
                             msg='Did not receive ProductID')
            resp_body = payload
            headers = {}
            return requests.codes.created, headers, json.dumps(resp_body)

        new_product = Product()
        new_product.id = 55
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.POST,
                Product.__odata_url__(),
                callback=request_callback,
                content_type='application/json',
            )

            Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
コード例 #4
0
ファイル: test_objects.py プロジェクト: lodex/python-odata
    def test_read(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        assert product.id == expected_id
        assert product.name == expected_name
        assert product.category == expected_category
        assert product.price == expected_price
コード例 #5
0
    def test_create(self):
        # Post call ###########################################################
        def request_callback(request):
            payload = json.loads(request.body)

            assert 'OData-Version' in request.headers, 'OData-Version header not in request'

            assert 'ProductID' not in payload, 'Payload contains primary key'
            assert '@odata.type' in payload, 'Payload did not contain @odata.type'

            payload['ProductID'] = 1

            resp_body = payload
            headers = {}
            return requests.codes.created, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.POST, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        new_product = Product()
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5

        Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
コード例 #6
0
 def test_call_bound_action_empty_result(self):
     with responses.RequestsMock() as rsps:
         rsps.add(
             rsps.POST,
             Product.__odata_url__() + '/ODataTest.DemoCollectionAction',
         )
         result = Product.DemoCollectionAction()
     self.assertIsNone(result)
コード例 #7
0
 def test_call_function_empty_result(self):
     with responses.RequestsMock() as rsps:
         rsps.add(rsps.GET,
                  Product.__odata_url__() + '/ODataTest.DemoFunction()',
                  content_type='application/json',
                  status=requests.codes.no_content)
         result = Product.DemoFunction()
     self.assertIsNone(result)
コード例 #8
0
    def test_context_call_bound_action(self):
        with responses.RequestsMock() as rsps:
            rsps.add(rsps.POST, Product.__odata_url__() + '/ODataTest.DemoActionParameters')

            context = Service.create_context()
            context.call(Product.DemoActionWithParameters,
                         Name='TestName',
                         Price=decimal.Decimal('25.0'))
コード例 #9
0
 def test_call_bound_action_with_result(self):
     with responses.RequestsMock() as rsps:
         rsps.add(
             rsps.POST,
             Product.__odata_url__() + '/ODataTest.DemoCollectionAction',
             content_type='application/json',
             json=dict(value='test'),
         )
         result = Product.DemoCollectionAction()
     self.assertEqual(result, 'test')
コード例 #10
0
    def test_context_query_without_auth(self):
        def request_callback(request):
            self.assertIsNone(request.headers.get('Authorization'))
            headers = {}
            body = dict(value=[])
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.GET, Product.__odata_url__(),
                              callback=request_callback,
                              content_type='application/json')

            context = Service.create_context()
            context.query(Product).first()
コード例 #11
0
    def test_read_value(self):
        test_product_values = dict(
            ProductID=1,
            ProductName='Test Product',
            Category='',
            ColorSelection='Red',
            Price=0.0,
        )
        with responses.RequestsMock() as rsps:
            rsps.add(rsps.GET, Product.__odata_url__(),
                     content_type='application/json',
                     json=dict(value=[test_product_values]))

            product = Service.query(Product).get(1)

        self.assertIsInstance(product.color_selection, ColorSelection)
        self.assertEqual(product.color_selection, ColorSelection.Red)
コード例 #12
0
    def test_call_function_with_result_query(self):
        def request_callback(request):
            self.assertTrue(
                'filter=ProductName+eq+%27testtest%27' in request.url)

            headers = {}
            body = dict(value='ok')
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.GET,
                              Product.__odata_url__() +
                              '/ODataTest.DemoFunction()',
                              request_callback,
                              content_type='application/json')

            query = Service.query(Product)
            query = query.filter(Product.name == 'testtest')
            Product.DemoFunction.with_query(query)()
コード例 #13
0
    def test_insert_value(self):

        def request_callback(request):
            content = json.loads(request.body)
            content['ProductID'] = 1
            self.assertIn('ColorSelection', content)
            self.assertEqual(content.get('ColorSelection'), 'Black')
            headers = {}
            return requests.codes.ok, headers, json.dumps(content)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.POST,
                              Product.__odata_url__(),
                              callback=request_callback,
                              content_type='application/json')

            new_product = Product()
            new_product.name = 'Test Product'
            new_product.color_selection = ColorSelection.Black
            Service.save(new_product)
コード例 #14
0
    def test_parse_error_json(self):
        expected_code = '0451'
        expected_message = 'Testing error message handling'
        expected_innererror_message = 'Detailed messages here'

        # Initial data ########################################################
        def request_callback(request):
            resp_body = {
                'error': {
                    'code': expected_code,
                    'message': expected_message,
                    'innererror': {
                        'message': expected_innererror_message
                    }
                }
            }
            headers = {
                'Content-Type': 'application/json;odata.metadata=minimal'
            }
            return requests.codes.bad_request, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )

        #######################################################################

        def action():
            try:
                Service.query(Product).first()
            except ODataError as e:
                errmsg = str(e)
                assert expected_code in errmsg, 'Code not in text'
                assert expected_message in errmsg, 'Upper level message not in text'
                assert expected_innererror_message in errmsg, 'Detailed message not in text'
                raise

        self.assertRaises(ODataError, action)
コード例 #15
0
    def test_call_action_with_result(self):
        def request_callback(request):
            payload = json.loads(request.body)
            expected = dict(Name='TestName', Price=decimal.Decimal('10.0'))

            self.assertDictEqual(payload, expected)

            headers = {}
            body = dict(value='ok')
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.POST,
                Product.__odata_url__() + '/ODataTest.DemoActionParameters',
                callback=request_callback,
                content_type='application/json',
            )
            result = Product.DemoActionWithParameters(
                Name='TestName', Price=decimal.Decimal('10.0'))
        self.assertEqual(result, 'ok')
コード例 #16
0
ファイル: test_objects.py プロジェクト: lodex/python-odata
    def test_delete(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 2048,
                'ProductName': 'This product will be deleted',
                'Category': 'Something',
                'Price': 1234.5,
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()

        # Delete call #########################################################
        def request_callback_delete(request):
            headers = {}
            return requests.codes.ok, headers, ''

        responses.add_callback(
            responses.DELETE,
            product.__odata__.instance_url,
            callback=request_callback_delete,
            content_type='application/json',
        )
        #######################################################################

        Service.delete(product)
コード例 #17
0
    def test_parse_error_json(self):
        expected_code = '0451'
        expected_message = 'Testing error message handling'
        expected_innererror_message = 'Detailed messages here'

        # Initial data ########################################################
        def request_callback(request):
            resp_body = {'error': {
                'code': expected_code,
                'message': expected_message,
                'innererror': {
                    'message': expected_innererror_message
                }
            }}
            headers = {
                'Content-Type': 'application/json;odata.metadata=minimal'
            }
            return requests.codes.bad_request, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        def action():
            try:
                Service.query(Product).first()
            except ODataError as e:
                errmsg = str(e)
                assert expected_code in errmsg, 'Code not in text'
                assert expected_message in errmsg, 'Upper level message not in text'
                assert expected_innererror_message in errmsg, 'Detailed message not in text'
                raise

        self.assertRaises(ODataError, action)
コード例 #18
0
    def test_delete(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 2048,
                'ProductName': 'This product will be deleted',
                'Category': 'Something',
                'Price': 1234.5,
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()

        # Delete call #########################################################
        def request_callback_delete(request):
            headers = {}
            return requests.codes.ok, headers, ''

        responses.add_callback(
            responses.DELETE, product.__odata__.instance_url,
            callback=request_callback_delete,
            content_type='application/json',
        )
        #######################################################################

        Service.delete(product)
コード例 #19
0
    def test_context_query_with_basic_auth(self):
        test_username = '******'
        test_password = '******'
        test_auth = (test_username, test_password)

        def request_callback(request):
            auth_text = request.headers.get('Authorization')
            _, auth_b64 = auth_text.split(' ', 1)
            decoded = base64.urlsafe_b64decode(auth_b64.encode()).decode()
            username, password = decoded.split(':', 1)

            self.assertEqual(test_username, username)
            self.assertEqual(test_password, password)

            headers = {}
            body = dict(value=[])
            return requests.codes.ok, headers, json.dumps(body)

        with responses.RequestsMock() as rsps:
            rsps.add_callback(rsps.GET, Product.__odata_url__(), request_callback,
                              content_type='application/json')

            context = Service.create_context(auth=test_auth)
            context.query(Product).first()
コード例 #20
0
    def test_update(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        new_name = 'Changed value'

        # Patch call ##########################################################
        def request_callback_patch(request):
            payload = json.loads(request.body)
            assert 'ProductName' in payload
            assert payload['ProductName'] == new_name
            headers = {}
            return requests.codes.no_content, headers, ''

        responses.add_callback(
            responses.PATCH, product.__odata__.instance_url,
            callback=request_callback_patch,
            content_type='application/json',
        )
        #######################################################################

        # Reload call #########################################################
        def request_callback_reload(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': new_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET, product.__odata__.instance_url,
            callback=request_callback_reload,
            content_type='application/json',
        )
        #######################################################################

        product.name = new_name
        Service.save(product)

        assert product.name == new_name
コード例 #21
0
ファイル: test_objects.py プロジェクト: lodex/python-odata
    def test_update(self):
        expected_id = 1024
        expected_name = 'Existing entity'
        expected_category = 'Existing category'
        expected_price = Decimal('85.2')

        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': expected_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            Product.__odata_url__(),
            callback=request_callback,
            content_type='application/json',
        )
        #######################################################################

        product = Service.query(Product).first()
        new_name = 'Changed value'

        # Patch call ##########################################################
        def request_callback_patch(request):
            payload = json.loads(request.body)
            assert 'ProductName' in payload
            assert payload['ProductName'] == new_name
            headers = {}
            return requests.codes.no_content, headers, ''

        responses.add_callback(
            responses.PATCH,
            product.__odata__.instance_url,
            callback=request_callback_patch,
            content_type='application/json',
        )

        #######################################################################

        # Reload call #########################################################
        def request_callback_reload(request):
            payload = {
                'ProductID': expected_id,
                'ProductName': new_name,
                'Category': expected_category,
                'Price': float(expected_price),
            }

            resp_body = {'value': [payload]}
            headers = {}
            return requests.codes.ok, headers, json.dumps(resp_body)

        responses.add_callback(
            responses.GET,
            product.__odata__.instance_url,
            callback=request_callback_reload,
            content_type='application/json',
        )
        #######################################################################

        product.name = new_name
        Service.save(product)

        assert product.name == new_name