def test_new_entity(self): uuid = '3d46cd74-a3af-4afd-af94-512b5cee1ef0' product = Product() product.id = uuid product.name = u'Defender' product.category = u'Cars' product.price = 40000.00 state = EntityState(product) data = dict(state.data_for_insert()) assert data['ProductID'] == uuid assert data['ProductName'] == 'Defender' assert data['Category'] == 'Cars' assert data['Price'] == 40000.00 assert state.dirty == [] product.name = 'Toyota Carola' product.price = 32500.00 data = dict(state.data_for_update()) assert data['ProductName'] == 'Toyota Carola' assert data['Category'] == 'Cars' assert data['Price'] == 32500.00
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_call_bound_action_on_instance(self): test_product = Product() test_product.name = 'TestProduct' test_product.id = 1234 test_product.price = decimal.Decimal('20.0') # shortcut for saving the entity test_product.__odata__.persisted = True with responses.RequestsMock() as rsps: rsps.add( rsps.POST, test_product.__odata__.instance_url + '/ODataTest.DemoAction', ) result = test_product.DemoAction() self.assertIsNone(result)
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)