import datetime import logging import unittest import unittest.mock as mock from datetime import datetime from flask import json, url_for import app from openapi_server.test import BaseTestCase logging.basicConfig(level=logging.DEBUG) class TestPatchnotesController(BaseTestCase): """PatchnotesController integration test stubs""" def setUp(self): self.app = app.app self.app.config['SERVER_NAME'] = '127.0.0.1:8080' print('url map:' + str(self.app.url_map)) @unittest.mock.patch( 'openapi_server.controllers.patchnotes_controller.patchnotes_date_get') def test_get_patchnotes(self, mock_get_patchnotes): """Test case for get_patchnotes """ headers = { 'Accept': 'application/json', } logging.error('self client:' + str(self.client)) logging.error('mock get patchnotes' + str(mock_get_patchnotes)) response = self.client.open( '/patchnotes/2025-01-02', method='GET', headers=headers ) response_data = response.data.decode('utf-8').strip().strip('\"') logging.error(response_data + "response_data") response_data_type = type(response_data) logging.error("response_data_type:" + str(response_data_type)) logging.error("response data" + response_data) response_data = datetime.strptime(response_data, '%Y-%m-%d').date() logging.error(str(response_data) + "response_data after format") try: formatted_date = response_data.strftime('%Y-%m-%d') logging.error(formatted_date) logging.info("valid date") except ValueError: logging.error("Not valid date") self.assert200(response, 'Response body is : ' + response.data.decode('utf-8')) @unittest.mock.patch('openapi_server.controllers.patchnotes_controller.patchnotes_date_get') def test_get_patchnotes_404(self, mock_get_patchnotes): # DONE """Test case for get_patchnotes """ headers = { 'Accept': 'application/json', } with self.app.test_request_context(): full_url = url_for('get_patchnotes', date='2023-01-01', _external=True) logging.error("Full URL: " + full_url) url = '/api/patchnotes/{date}'.format(date='2023-01-01') logging.error(f"request URL: {url}") logging.error('self client:' + str(self.client)) logging.error('mock get patchnotes' + str(mock_get_patchnotes)) logging.error('self name stuff:' + str(self.app.config['SERVER_NAME'])) response = self.client.open( url, method='GET', headers=headers ) logging.error(f"Response Status Code: {response.status_code}") logging.error(f"Response Data: {response.data.decode('utf-8')}") self.assert404(response, 'Response body is : ' + response.data.decode('utf-8')) @unittest.mock.patch('openapi_server.controllers.patchnotes_controller.patchnotes_patch_iddelete') def test_patch_note_delete(self, balala): """Test case for patch_note_delete """ headers = { } patch_note_mock = { "changes": "test changes", "date": "2025-01-02", "patchID": "29e80bcc-5981-4a52-99e1-373442dea9b9", "title": "test patch" } self.client.open( '/patchnotes', method='POST', headers=headers, data=json.dumps(patch_note_mock), content_type='application/json' ) response = self.client.open( '/patchnotes/{patch_id}'.format(patch_id=patch_note_mock['patchID']), method='DELETE', headers=headers ) self.assert200(response, 'Response body is : ' + response.data.decode('utf-8')) @unittest.mock.patch('openapi_server.controllers.patchnotes_controller.patchnotes_patch_iddelete') def test_patch_note_delete_not_found(self, balala): """Test case for patch_note_delete """ headers = { } patch_note_mock = { "changes": "test changes", "date": "2025-01-02", "patchID": "29e80bcc-5981-4a52-99e1-373442dea9b9", "title": "test patch" } response = self.client.open( '/patchnotes/{patch_id}'.format(patch_id=''), method='DELETE', headers=headers ) self.assert404(response, 'Response body is : ' + response.data.decode('utf-8')) @unittest.mock.patch('openapi_server.controllers.patchnotes_controller.patchnotes_post') def test_post_patchnotes(self, balala): """Test case for post_patchnotes """ post_patchnotes_mock = { "changes": "test changes", "date": "2025-01-02", "patchID": "29e80bcc-5981-4a52-99e1-373442dea9b9", "title": "test patch" } headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', } response = self.client.open( '/patchnotes', method='POST', headers=headers, data=json.dumps(post_patchnotes_mock), content_type='application/json' ) self.assert200(response, 'Response body is : ' + response.data.decode('utf-8')) if __name__ == '__main__': unittest.main()