|
| 1 | +# © Copyright IBM Corporation 2025. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +# Testsuite for the examples in the README file. |
| 17 | + |
| 18 | +import unittest |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +import os |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +parent_dir = Path(__file__).resolve().parent.parent |
| 25 | + |
| 26 | + |
| 27 | +class TestReadmeExamples(unittest.TestCase): |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def setUpClass(cls): |
| 31 | + """Set up test environment before all tests""" |
| 32 | + # Get WireMock URL from environment |
| 33 | + wiremock_url = os.environ.get('WIREMOCK_URL') |
| 34 | + |
| 35 | + # Reset WireMock scenarios |
| 36 | + import requests |
| 37 | + requests.post(f"{wiremock_url}/__admin/scenarios/reset") |
| 38 | + |
| 39 | + # Set authentication environment variables |
| 40 | + os.environ['CLOUDANT_URL'] = wiremock_url |
| 41 | + os.environ['CLOUDANT_AUTH_TYPE'] = 'noauth' |
| 42 | + |
| 43 | + def run_example_and_check_output(self, script_path, expected_output_path): |
| 44 | + env = os.environ.copy() |
| 45 | + |
| 46 | + # Ensure the subprocess uses the same site-packages |
| 47 | + python_path = sys.path.copy() |
| 48 | + if 'PYTHONPATH' in env: |
| 49 | + python_path.append(env['PYTHONPATH']) |
| 50 | + env['PYTHONPATH'] = os.pathsep.join(python_path) |
| 51 | + |
| 52 | + result = subprocess.run( |
| 53 | + [sys.executable, str(script_path)], |
| 54 | + capture_output=True, |
| 55 | + text=True, |
| 56 | + timeout=5, |
| 57 | + env=env, |
| 58 | + ) |
| 59 | + if result.returncode != 0: |
| 60 | + self.fail(f"Script failed with return code {result.returncode}\n" |
| 61 | + f"STDOUT:\n{result.stdout}\n" |
| 62 | + f"STDERR:\n{result.stderr}") |
| 63 | + |
| 64 | + with open(expected_output_path, 'r') as f: |
| 65 | + expected_output = f.read().strip() |
| 66 | + |
| 67 | + actual_output = result.stdout.strip() |
| 68 | + |
| 69 | + self.assertEqual(actual_output, expected_output, |
| 70 | + f"Output mismatch.\nExpected:\n{expected_output}\n\nGot:\n{actual_output}") |
| 71 | + |
| 72 | + def test_1_create_db_and_doc_example(self): |
| 73 | + """Creates db and doc for the first time""" |
| 74 | + script = parent_dir / "src" / "create_db_and_doc.py" |
| 75 | + output = parent_dir / "output" / "create_db_and_doc.txt" |
| 76 | + self.run_example_and_check_output(script, output) |
| 77 | + |
| 78 | + def test_2_get_info_from_existing_database_example(self): |
| 79 | + """Gets document from orders database""" |
| 80 | + script = parent_dir / "src" / "get_info_from_existing_database.py" |
| 81 | + output = parent_dir / "output" / "get_info_from_existing_database.txt" |
| 82 | + self.run_example_and_check_output(script, output) |
| 83 | + |
| 84 | + def test_3_update_doc_example_first_time(self): |
| 85 | + """Updates doc for the first time""" |
| 86 | + script = parent_dir / "src" / "update_doc.py" |
| 87 | + output = parent_dir / "output" / "update_doc.txt" |
| 88 | + self.run_example_and_check_output(script, output) |
| 89 | + |
| 90 | + def test_4_update_doc_example_second_time(self): |
| 91 | + """Updates doc for the second time""" |
| 92 | + script = parent_dir / "src" / "update_doc.py" |
| 93 | + output = parent_dir / "output" / "update_doc2.txt" |
| 94 | + self.run_example_and_check_output(script, output) |
| 95 | + |
| 96 | + def test_5_delete_doc_example_existing(self): |
| 97 | + """Deletes existing doc""" |
| 98 | + script = parent_dir / "src" / "delete_doc.py" |
| 99 | + output = parent_dir / "output" / "delete_doc.txt" |
| 100 | + self.run_example_and_check_output(script, output) |
| 101 | + |
| 102 | + def test_6_delete_doc_example_non_existing(self): |
| 103 | + """Deletes non-existing doc""" |
| 104 | + script = parent_dir / "src" / "delete_doc.py" |
| 105 | + output = parent_dir / "output" / "delete_doc2.txt" |
| 106 | + self.run_example_and_check_output(script, output) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + unittest.main() |
0 commit comments