|
| 1 | +# Copyright 2018-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 | +from __future__ import annotations |
| 15 | + |
| 16 | +import os |
| 17 | +import sys |
| 18 | + |
| 19 | +sys.path[0:0] = [""] |
| 20 | + |
| 21 | +from test.asynchronous import AsyncUnitTest, SkipTest, async_client_context, unittest |
| 22 | + |
| 23 | +_IS_SYNC = False |
| 24 | + |
| 25 | + |
| 26 | +class TestAsyncClientContext(AsyncUnitTest): |
| 27 | + @classmethod |
| 28 | + async def _setup_class(cls): |
| 29 | + pass |
| 30 | + |
| 31 | + @classmethod |
| 32 | + async def _tearDown_class(cls): |
| 33 | + pass |
| 34 | + |
| 35 | + def test_must_connect(self): |
| 36 | + if "PYMONGO_MUST_CONNECT" not in os.environ: |
| 37 | + raise SkipTest("PYMONGO_MUST_CONNECT is not set") |
| 38 | + |
| 39 | + self.assertTrue( |
| 40 | + async_client_context.connected, |
| 41 | + "client context must be connected when " |
| 42 | + "PYMONGO_MUST_CONNECT is set. Failed attempts:\n{}".format( |
| 43 | + async_client_context.connection_attempt_info() |
| 44 | + ), |
| 45 | + ) |
| 46 | + |
| 47 | + def test_serverless(self): |
| 48 | + if "TEST_SERVERLESS" not in os.environ: |
| 49 | + raise SkipTest("TEST_SERVERLESS is not set") |
| 50 | + |
| 51 | + self.assertTrue( |
| 52 | + async_client_context.connected and async_client_context.serverless, |
| 53 | + "client context must be connected to serverless when " |
| 54 | + f"TEST_SERVERLESS is set. Failed attempts:\n{async_client_context.connection_attempt_info()}", |
| 55 | + ) |
| 56 | + |
| 57 | + def test_enableTestCommands_is_disabled(self): |
| 58 | + if "PYMONGO_DISABLE_TEST_COMMANDS" not in os.environ: |
| 59 | + raise SkipTest("PYMONGO_DISABLE_TEST_COMMANDS is not set") |
| 60 | + |
| 61 | + self.assertFalse( |
| 62 | + async_client_context.test_commands_enabled, |
| 63 | + "enableTestCommands must be disabled when PYMONGO_DISABLE_TEST_COMMANDS is set.", |
| 64 | + ) |
| 65 | + |
| 66 | + def test_setdefaultencoding_worked(self): |
| 67 | + if "SETDEFAULTENCODING" not in os.environ: |
| 68 | + raise SkipTest("SETDEFAULTENCODING is not set") |
| 69 | + |
| 70 | + self.assertEqual(sys.getdefaultencoding(), os.environ["SETDEFAULTENCODING"]) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + unittest.main() |
0 commit comments