diff --git a/test/examples/output/create_db_and_doc.txt b/test/examples/output/create_db_and_doc.txt index 52cd7a00..8dfe98cf 100644 --- a/test/examples/output/create_db_and_doc.txt +++ b/test/examples/output/create_db_and_doc.txt @@ -1,8 +1,7 @@ "orders" database created. -You have created the document: +You have created the document. Response body: { - "_id": "example", - "_rev": "1-1b403633540686aa32d013fda9041a5d", - "name": "Bob Smith", - "joined": "2019-01-24T10:42:99.000Z" + "ok": true, + "id": "example", + "rev": "1-1b403633540686aa32d013fda9041a5d" } \ No newline at end of file diff --git a/test/examples/output/delete_doc2.txt b/test/examples/output/delete_doc2.txt new file mode 100644 index 00000000..35ea06bd --- /dev/null +++ b/test/examples/output/delete_doc2.txt @@ -0,0 +1 @@ +Cannot delete document because either "orders" database or "example" document was not found. \ No newline at end of file diff --git a/test/examples/output/get_info_from_existing_database.txt b/test/examples/output/get_info_from_existing_database.txt index f9074276..ba544c46 100644 --- a/test/examples/output/get_info_from_existing_database.txt +++ b/test/examples/output/get_info_from_existing_database.txt @@ -1,9 +1,9 @@ -Server Version: 2.1.1 +Server Version: 3.2.1 Document count in "orders" database is 1. Document retrieved from database: { "_id": "example", "_rev": "1-1b403633540686aa32d013fda9041a5d", "name": "Bob Smith", - "joined": "2019-01-24T10:42:99.000Z" + "joined": "2019-01-24T10:42:59.000Z" } \ No newline at end of file diff --git a/test/examples/output/update_doc.txt b/test/examples/output/update_doc.txt index 4ec63a73..5ed0026e 100644 --- a/test/examples/output/update_doc.txt +++ b/test/examples/output/update_doc.txt @@ -1,3 +1,4 @@ +You have updated the document: { "_id": "example", "_rev": "2-4e2178e85cffb32d38ba4e451f6ca376", diff --git a/test/examples/output/update_doc2.txt b/test/examples/output/update_doc2.txt new file mode 100644 index 00000000..3bf62984 --- /dev/null +++ b/test/examples/output/update_doc2.txt @@ -0,0 +1,7 @@ +You have updated the document: +{ + "_id": "example", + "_rev": "3-4f1787d7a0520f825bd36822d7be627a", + "name": "Bob Smith", + "address": "19 Front Street, Darlington, DL5 1TY" +} \ No newline at end of file diff --git a/test/examples/src/create_db_and_doc.py b/test/examples/src/create_db_and_doc.py index aceebf8e..92472605 100644 --- a/test/examples/src/create_db_and_doc.py +++ b/test/examples/src/create_db_and_doc.py @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json + from ibm_cloud_sdk_core import ApiException from ibmcloudant.cloudant_v1 import CloudantV1, Document @@ -38,10 +40,10 @@ # 3. Create a document ================================================ # Create a document object with "example" id example_doc_id = "example" -# Setting `id` for the document is optional when "post_document" -# function is used for CREATE. When `id` is not provided the server +# Setting `_id` for the document is optional when "post_document" +# function is used for CREATE. When `_id` is not provided the server # will generate one for your document. -example_document: Document = Document(id=example_doc_id) +example_document: Document = Document(_id=example_doc_id) # Add "name" and "joined" fields to the document example_document.name = "Bob Smith" @@ -65,7 +67,5 @@ """ # ===================================================================== -# Keeping track of the revision number of the document object -# is necessary for further UPDATE/DELETE operations: -example_document.rev = create_document_response["rev"] -print(f'You have created the document:\n{example_document}') +response_body = json.dumps(create_document_response, indent=2) +print(f'You have created the document. Response body:\n{response_body}') diff --git a/test/examples/src/delete_doc.py b/test/examples/src/delete_doc.py index 993233a3..5cc9461c 100644 --- a/test/examples/src/delete_doc.py +++ b/test/examples/src/delete_doc.py @@ -43,5 +43,5 @@ except ApiException as ae: if ae.status_code == 404: print('Cannot delete document because either ' + - f'"{example_db_name}" database or "{example_doc_id}"' + + f'"{example_db_name}" database or "{example_doc_id}" ' + 'document was not found.') diff --git a/test/examples/src/test_readme.py b/test/examples/src/test_readme.py new file mode 100644 index 00000000..b8f5da38 --- /dev/null +++ b/test/examples/src/test_readme.py @@ -0,0 +1,110 @@ +# © Copyright IBM Corporation 2025. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# Testsuite for the examples in the README file. + +import unittest +import subprocess +import sys +import os +import requests +from pathlib import Path + +parent_dir = Path(__file__).resolve().parent.parent + + +class TestReadmeExamples(unittest.TestCase): + + @classmethod + def setUpClass(cls): + """Set up test environment before all tests""" + # Get WireMock URL from environment + wiremock_url = os.environ.get('WIREMOCK_URL') + + # Reset WireMock scenarios + requests.post(f"{wiremock_url}/__admin/scenarios/reset") + + # Set authentication environment variables + os.environ['CLOUDANT_URL'] = wiremock_url + os.environ['CLOUDANT_AUTH_TYPE'] = 'noauth' + + def run_example_and_check_output(self, script_path, expected_output_path): + env = os.environ.copy() + + # Ensure the subprocess uses the same site-packages + python_path = sys.path.copy() + if 'PYTHONPATH' in env: + python_path.append(env['PYTHONPATH']) + env['PYTHONPATH'] = os.pathsep.join(python_path) + + result = subprocess.run( + [sys.executable, str(script_path)], + capture_output=True, + text=True, + timeout=5, + env=env, + ) + if result.returncode != 0: + self.fail(f"Script failed with return code {result.returncode}\n" + f"STDOUT:\n{result.stdout}\n" + f"STDERR:\n{result.stderr}") + + with open(expected_output_path, 'r') as f: + expected_output = f.read().strip() + + actual_output = result.stdout.strip() + + self.assertEqual(actual_output, expected_output, + f"Output mismatch.\nExpected:\n{expected_output}\n\nGot:\n{actual_output}") + + def test_1_create_db_and_doc_example(self): + """Creates db and doc for the first time""" + script = parent_dir / "src" / "create_db_and_doc.py" + output = parent_dir / "output" / "create_db_and_doc.txt" + self.run_example_and_check_output(script, output) + + def test_2_get_info_from_existing_database_example(self): + """Gets document from orders database""" + script = parent_dir / "src" / "get_info_from_existing_database.py" + output = parent_dir / "output" / "get_info_from_existing_database.txt" + self.run_example_and_check_output(script, output) + + def test_3_update_doc_example_first_time(self): + """Updates doc for the first time""" + script = parent_dir / "src" / "update_doc.py" + output = parent_dir / "output" / "update_doc.txt" + self.run_example_and_check_output(script, output) + + def test_4_update_doc_example_second_time(self): + """Updates doc for the second time""" + script = parent_dir / "src" / "update_doc.py" + output = parent_dir / "output" / "update_doc2.txt" + self.run_example_and_check_output(script, output) + + def test_5_delete_doc_example_existing(self): + """Deletes existing doc""" + script = parent_dir / "src" / "delete_doc.py" + output = parent_dir / "output" / "delete_doc.txt" + self.run_example_and_check_output(script, output) + + def test_6_delete_doc_example_non_existing(self): + """Deletes non-existing doc""" + script = parent_dir / "src" / "delete_doc.py" + output = parent_dir / "output" / "delete_doc2.txt" + self.run_example_and_check_output(script, output) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/examples/src/update_doc.py b/test/examples/src/update_doc.py index e66ae530..82170758 100644 --- a/test/examples/src/update_doc.py +++ b/test/examples/src/update_doc.py @@ -89,6 +89,6 @@ except ApiException as ae: if ae.status_code == 404: - print('Cannot delete document because either ' + + print('Cannot update document because either ' + f'"{example_db_name}" database or "{example_doc_id}" ' + 'document was not found.')