def test_exe_OK(self): with mock.patch('D365BCAPI.D365BCv1API.requests') as mocked_req: mocked_req.post.return_value.status_code = 204 mocked_req.post.return_value.reason = 'No content' ex = Connect('http://test.lt/test(123456789abcd)/NAV.Action', auth={'a': 'a'}, headers={'some headers'}) respo = ex.exe() mocked_req.post.assert_called_with('http://test.lt/test(123456789abcd)/NAV.Action', auth={'a': 'a'}, headers={'some headers'}, json=None) self.assertEqual(respo, [204, 'No content'], 'execution: no error returns [204, No content] ') # without error
def test_exe_FAIL(self): with mock.patch('D365BCAPI.D365BCv1API.requests') as mocked_req: mocked_req.post.return_value.status_code = 400 mocked_req.post.return_value.reason = 'Any error reason' # error message ex = Connect('http://test.lt/test(123456789abcd)/NAV.Action', auth={'a': 'a'}, headers={'some headers'}) respo = ex.exe() mocked_req.post.assert_called_with('http://test.lt/test(123456789abcd)/NAV.Action', auth={'a': 'a'}, headers={'some headers'}, json=None) self.assertEqual(respo, [], 'insert: on error return must be blank') # return blank if error self.assertEqual(ex.except_error, [400, 'Any error reason'], 'Execute: on error variable except_error ' 'must to include error code and reason')
# count lines sol.url = f"http://bs17:7048/BC/api/v1.0/salesOrders({so_id})/salesOrderLines" response_list = sol.read() # read all lines just for fun print(f"SO has {len(response_list)} lines after deleted one" ) # number of lines in the document # execute action - order ship and invoice # http://bs17:7048/BC/api/v1.0/salesOrders({so_id})/Microsoft.NAV.shipAndInvoice if len( so_id ) > 0: # if doc id not blank (we found it earlier)then we can ship and invoice it so.url = f"http://bs17:7048/BC/api/v1.0/salesOrders({so_id})/Microsoft.NAV.shipAndInvoice" # create URL else: raise Exception('Critical error - Can not find document') response_list = so.exe() # execute sales order action "ship and invoice" if (len(response_list) > 0) and so.except_error is None: print("Sales order is shipped and invoiced; response is ", response_list) else: raise Exception(so.except_error) # find just created invoice by "orderNumber"= so_number url_salesInvoices = "http://bs17:7048/BC/api/v1.0/salesInvoices" si = Connect(url_salesInvoices, (user, psw), {"Accept-Language": "en-us"}) so_number = '1001' si.filter_text = f"orderNumber eq '{so_number}'" response_list = si.read() if (len(response_list) > 0) and si.except_error is None: si_number = response_list[0].get('number')