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
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
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_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 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)
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)
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)
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)
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_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_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_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_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_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_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)
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_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_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'