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'
Exemple #2
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'
    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'
    def test_update_entity(self):
        test_pm_sales_value = dict(ProductID=1,
                                   ManufacturerID=2,
                                   SalesAmount=23.0)

        with responses.RequestsMock() as rsps:
            rsps.add(rsps.GET,
                     ProductManufacturerSales.__odata_url__(),
                     content_type='application/json',
                     json=dict(value=[test_pm_sales_value]))

            query = Service.query(ProductManufacturerSales)
            query = query.filter(ProductManufacturerSales.product_id == 1)
            query = query.filter(ProductManufacturerSales.manufacturer_id == 2)
            pm_sales = query.first()  # type: ProductManufacturerSales

            sales_id = pm_sales.__odata__.id
            self.assertIn('ProductID=1', sales_id)
            self.assertIn('ManufacturerID=2', sales_id)

            rsps.add(rsps.PATCH,
                     pm_sales.__odata__.instance_url,
                     content_type='application/json')
            rsps.add(rsps.GET,
                     pm_sales.__odata__.instance_url,
                     content_type='application/json',
                     json=dict(value=[test_pm_sales_value]))

            pm_sales.sales_amount = Decimal('50.0')
            Service.save(pm_sales)
 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
Exemple #6
0
 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
Exemple #7
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
    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
    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'))
Exemple #10
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)
Exemple #11
0
    def test_read_expanded_navigation_property(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID':
                51,
                'ProductName':
                'Foo',
                'Category':
                'Bar',
                'Price':
                12.3,
                'Parts': [{
                    'PartID': 512,
                    'PartName': 'Bits and bobs',
                    'Size': 5.333,
                }]
            }

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

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

        query = Service.query(ProductWithNavigation)
        query.expand(ProductWithNavigation.parts)
        product = query.first()

        # Get parts ###########################################################
        parts_url = product.__odata__.instance_url + '/Parts'

        def request_callback_parts(request):
            assert False, 'Expanded NavigationProperty should not cause GET'

        responses.add_callback(
            responses.GET,
            parts_url,
            callback=request_callback_parts,
            content_type='application/json',
        )
        #######################################################################

        for part in product.parts:
            assert isinstance(part, ProductPart)
Exemple #12
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)
    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()
    def test_insert_entity(self):
        def request_callback(request):
            content = json.loads(request.body)
            self.assertIn('ProductID', content)
            self.assertIn('ManufacturerID', content)
            self.assertIsNotNone(content.get('ProductID'),
                                 msg='ProductID received as None')
            self.assertIsNotNone(content.get('ManufacturerID'),
                                 msg='ManufacturerID received as None')

            headers = {}
            return requests.codes.ok, headers, json.dumps(content)

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

            pm_sales = ProductManufacturerSales()
            pm_sales.product_id = 1
            pm_sales.manufacturer_id = 2
            pm_sales.sales_amount = Decimal('3.0')
            Service.save(pm_sales)
    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)
Exemple #16
0
    def test_read_single_navigation_property(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 51,
                'ProductName': 'Foo',
                'Category': 'Bar',
                'Price': 12.3,
            }

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

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

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

        # Get parts ###########################################################
        parts_url = product.__odata__.instance_url + '/Manufacturer'

        def request_callback_manufacturer(request):
            payload = {
                'ManufacturerID': 33,
                'Name': 'Best Parts Ltd.',
                'DateEstablished': '2007-06-05T12:00:00Z',
            }

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

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.GET,
                parts_url,
                callback=request_callback_manufacturer,
                content_type='application/json',
            )
            mf = product.manufacturer

        assert isinstance(mf, Manufacturer)
Exemple #17
0
    def test_read_collection_navigation_property(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 51,
                'ProductName': 'Foo',
                'Category': 'Bar',
                'Price': 12.3,
            }

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

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.GET,
                ProductWithNavigation.__odata_url__(),
                callback=request_callback,
                content_type='application/json',
            )
            product = Service.query(ProductWithNavigation).first()

        # Get parts ###########################################################
        parts_url = product.__odata__.instance_url + '/Parts'

        def request_callback_parts(request):
            payload = {
                'PartID': 512,
                'PartName': 'Bits and bobs',
                'Size': 5.333,
            }

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

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.GET,
                parts_url,
                callback=request_callback_parts,
                content_type='application/json',
            )

            for part in product.parts:
                assert isinstance(part, ProductPart)
    def test_read_expanded_navigation_property(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 51,
                'ProductName': 'Foo',
                'Category': 'Bar',
                'Price': 12.3,
                'Parts': [
                    {
                        'PartID': 512,
                        'PartName': 'Bits and bobs',
                        'Size': 5.333,
                    }
                ]
            }

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

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

        query = Service.query(ProductWithNavigation)
        query.expand(ProductWithNavigation.parts)
        product = query.first()

        # Get parts ###########################################################
        parts_url = product.__odata__.instance_url + '/Parts'

        def request_callback_parts(request):
            assert False, 'Expanded NavigationProperty should not cause GET'

        responses.add_callback(
            responses.GET, parts_url,
            callback=request_callback_parts,
            content_type='application/json',
        )
        #######################################################################

        for part in product.parts:
            assert isinstance(part, ProductPart)
Exemple #19
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)
    def test_read_single_navigation_property(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 51,
                'ProductName': 'Foo',
                'Category': 'Bar',
                'Price': 12.3,
            }

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

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

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

        # Get parts ###########################################################
        parts_url = product.__odata__.instance_url + '/Manufacturer'

        def request_callback_manufacturer(request):
            payload = {
                'ManufacturerID': 33,
                'Name': 'Best Parts Ltd.',
                'DateEstablished': '2007-06-05T12:00:00Z',
            }

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

        responses.add_callback(
            responses.GET, parts_url,
            callback=request_callback_manufacturer,
            content_type='application/json',
        )
        #######################################################################

        mf = product.manufacturer
        assert isinstance(mf, Manufacturer)
Exemple #21
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)()
    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()
Exemple #23
0
    def test_set_navigation_property(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 51,
                'ProductName': 'Foo',
                'Category': 'Bar',
                'Price': 12.3,
            }

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

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

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

        # Get part ############################################################
        def request_callback_parts(request):
            payload = {
                'PartID': 512,
                'PartName': 'Bits and bobs',
                'Size': 5.333,
            }

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

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.GET,
                ProductPart.__odata_url__(),
                callback=request_callback_parts,
                content_type='application/json',
            )
            part = Service.query(ProductPart).first()

        product.parts = [part]

        # Patch call ##########################################################
        def request_callback_set_parts(request):
            payload = json.loads(request.body)

            key = '*****@*****.**'
            assert key in payload
            assert payload.get(key) == ['ProductParts(512)']

            headers = {}
            return requests.codes.no_content, headers, ''

        with responses.RequestsMock() as rsps:
            rsps.add_callback(
                rsps.PATCH,
                product.__odata__.instance_url,
                callback=request_callback_set_parts,
                content_type='application/json',
            )

            # Reload data #####################################################
            rsps.add_callback(
                rsps.GET,
                product.__odata__.instance_url,
                callback=request_callback,
                content_type='application/json',
            )

            Service.save(product)
    def test_set_navigation_property(self):
        # Initial data ########################################################
        def request_callback(request):
            payload = {
                'ProductID': 51,
                'ProductName': 'Foo',
                'Category': 'Bar',
                'Price': 12.3,
            }

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

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

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

        # Get part ############################################################
        def request_callback_parts(request):
            payload = {
                'PartID': 512,
                'PartName': 'Bits and bobs',
                'Size': 5.333,
            }

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

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

        part = Service.query(ProductPart).first()
        product.parts = [part]

        # Patch call ##########################################################
        def request_callback_set_parts(request):
            payload = json.loads(request.body)

            key = '*****@*****.**'
            assert key in payload
            assert payload.get(key) == ['ProductParts(512)']

            headers = {}
            return requests.codes.no_content, headers, ''

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

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

        Service.save(product)
Exemple #25
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
    def test_context_call_unbound_action(self):
        with responses.RequestsMock() as rsps:
            rsps.add(rsps.POST, Service.url + 'ODataTest.DemoUnboundAction')

            context = Service.create_context()
            context.call(DemoUnboundAction)
    def test_create_deep_inserts(self):
        # Initial part data ###################################################
        def request_callback_part(request):

            payload = {
                'PartID': 35,
                'PartName': 'Testing',
                'Size': 55,
            }

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

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

        queried_part = Service.query(ProductPart).first()

        # 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'

            assert '*****@*****.**' in payload, 'Parts bind not in payload'
            assert payload['*****@*****.**'] == ['ProductParts(35)']

            assert 'Parts' in payload, 'Parts not in payload'
            parts = payload['Parts']
            assert isinstance(parts, list), 'Parts is not a list'
            part = parts[0]
            assert len(part) == 3, 'Extra keys in deep inserted part: {0}'.format(part)
            assert '@odata.type' in part
            assert part['PartName'] == 'Foo'
            assert part['Size'] == 12.5

            payload['ProductID'] = 1

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

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

        part = ProductPart()
        part.name = 'Foo'
        part.size = 12.5

        new_product = ProductWithNavigation()
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5
        new_product.parts = [part, queried_part]

        Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
Exemple #28
0
    def test_create_deep_inserts(self):
        # Initial part data ###################################################
        def request_callback_part(request):

            payload = {
                'PartID': 35,
                'PartName': 'Testing',
                'Size': 55,
            }

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

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

        queried_part = Service.query(ProductPart).first()

        # 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'

            assert '*****@*****.**' in payload, 'Parts bind not in payload'
            assert payload['*****@*****.**'] == ['ProductParts(35)']

            assert 'Parts' in payload, 'Parts not in payload'
            parts = payload['Parts']
            assert isinstance(parts, list), 'Parts is not a list'
            part = parts[0]
            assert len(
                part) == 3, 'Extra keys in deep inserted part: {0}'.format(
                    part)
            assert '@odata.type' in part
            assert part['PartName'] == 'Foo'
            assert part['Size'] == 12.5

            payload['ProductID'] = 1

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

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

        part = ProductPart()
        part.name = 'Foo'
        part.size = 12.5

        new_product = ProductWithNavigation()
        new_product.name = u'New Test Product'
        new_product.category = u'Category #1'
        new_product.price = 34.5
        new_product.parts = [part, queried_part]

        Service.save(new_product)

        assert new_product.id is not None, 'Product.id is not set'
    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