|
| 1 | +# Copyright 2022-present MongoDB, Inc. |
| 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 | +"""Test the default exports of the top level packages.""" |
| 16 | +import inspect |
| 17 | +import unittest |
| 18 | + |
| 19 | +import bson |
| 20 | +import gridfs |
| 21 | +import pymongo |
| 22 | + |
| 23 | +BSON_IGNORE = [] |
| 24 | +GRIDFS_IGNORE = [ |
| 25 | + "ASCENDING", |
| 26 | + "DESCENDING", |
| 27 | + "ClientSession", |
| 28 | + "Collection", |
| 29 | + "ObjectId", |
| 30 | + "validate_string", |
| 31 | + "Database", |
| 32 | + "ConfigurationError", |
| 33 | + "WriteConcern", |
| 34 | +] |
| 35 | +PYMONGO_IGNORE = [] |
| 36 | +GLOBAL_INGORE = ["TYPE_CHECKING"] |
| 37 | + |
| 38 | + |
| 39 | +class TestDefaultExports(unittest.TestCase): |
| 40 | + def check_module(self, mod, ignores): |
| 41 | + names = dir(mod) |
| 42 | + names.remove("__all__") |
| 43 | + for name in mod.__all__: |
| 44 | + if name not in names and name not in ignores: |
| 45 | + self.fail(f"{name} was included in {mod}.__all__ but is not a valid symbol") |
| 46 | + |
| 47 | + for name in names: |
| 48 | + if name not in mod.__all__ and name not in ignores: |
| 49 | + if name in GLOBAL_INGORE: |
| 50 | + continue |
| 51 | + value = getattr(mod, name) |
| 52 | + if inspect.ismodule(value): |
| 53 | + continue |
| 54 | + if getattr(value, "__module__", None) == "typing": |
| 55 | + continue |
| 56 | + if not name.startswith("_"): |
| 57 | + self.fail(f"{name} was not included in {mod}.__all__") |
| 58 | + |
| 59 | + def test_pymongo(self): |
| 60 | + self.check_module(pymongo, PYMONGO_IGNORE) |
| 61 | + |
| 62 | + def test_gridfs(self): |
| 63 | + self.check_module(gridfs, GRIDFS_IGNORE) |
| 64 | + |
| 65 | + def test_bson(self): |
| 66 | + self.check_module(bson, BSON_IGNORE) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + unittest.main() |
0 commit comments