File: test_cursor_namespace.py

package info (click to toggle)
pymongo 4.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,676 kB
  • sloc: python: 107,763; ansic: 4,597; javascript: 137; makefile: 38; sh: 18
file content (153 lines) | stat: -rw-r--r-- 4,675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2015 MongoDB, Inc.
#
# 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.

"""Test list_indexes with more than one batch."""
from __future__ import annotations

import unittest
from test import PyMongoTestCase

import pytest

try:
    from mockupdb import MockupDB, going

    _HAVE_MOCKUPDB = True
except ImportError:
    _HAVE_MOCKUPDB = False


from pymongo import MongoClient
from pymongo.common import MIN_SUPPORTED_WIRE_VERSION

pytestmark = pytest.mark.mockupdb


class TestCursorNamespace(PyMongoTestCase):
    server: MockupDB
    client: MongoClient

    @classmethod
    def setUpClass(cls):
        cls.server = MockupDB(auto_ismaster={"maxWireVersion": 8})
        cls.server.run()
        cls.client = cls.unmanaged_simple_client(cls.server.uri)

    @classmethod
    def tearDownClass(cls):
        cls.client.close()
        cls.server.stop()

    def _test_cursor_namespace(self, cursor_op, command):
        with going(cursor_op) as docs:
            request = self.server.receives(**{command: "collection", "namespace": "test"})
            # Respond with a different namespace.
            request.reply(
                {
                    "cursor": {
                        "firstBatch": [{"doc": 1}],
                        "id": 123,
                        "ns": "different_db.different.coll",
                    }
                }
            )
            # Client uses the namespace we returned.
            request = self.server.receives(
                getMore=123, namespace="different_db", collection="different.coll"
            )

            request.reply({"cursor": {"nextBatch": [{"doc": 2}], "id": 0}})

        self.assertEqual([{"doc": 1}, {"doc": 2}], docs())

    def test_aggregate_cursor(self):
        def op():
            return list(self.client.test.collection.aggregate([]))

        self._test_cursor_namespace(op, "aggregate")

    def test_find_cursor(self):
        def op():
            return list(self.client.test.collection.find())

        self._test_cursor_namespace(op, "find")

    def test_list_indexes(self):
        def op():
            return list(self.client.test.collection.list_indexes())

        self._test_cursor_namespace(op, "listIndexes")


class TestKillCursorsNamespace(PyMongoTestCase):
    server: MockupDB
    client: MongoClient

    @classmethod
    def setUpClass(cls):
        cls.server = MockupDB(auto_ismaster={"maxWireVersion": MIN_SUPPORTED_WIRE_VERSION})
        cls.server.run()
        cls.client = cls.unmanaged_simple_client(cls.server.uri)

    @classmethod
    def tearDownClass(cls):
        cls.client.close()
        cls.server.stop()

    def _test_killCursors_namespace(self, cursor_op, command):
        with going(cursor_op):
            request = self.server.receives(**{command: "collection", "namespace": "test"})
            # Respond with a different namespace.
            request.reply(
                {
                    "cursor": {
                        "firstBatch": [{"doc": 1}],
                        "id": 123,
                        "ns": "different_db.different.coll",
                    }
                }
            )
            # Client uses the namespace we returned for killCursors.
            request = self.server.receives(
                **{"killCursors": "different.coll", "cursors": [123], "$db": "different_db"}
            )
            request.reply(
                {
                    "ok": 1,
                    "cursorsKilled": [123],
                    "cursorsNotFound": [],
                    "cursorsAlive": [],
                    "cursorsUnknown": [],
                }
            )

    def test_aggregate_killCursor(self):
        def op():
            cursor = self.client.test.collection.aggregate([], batchSize=1)
            next(cursor)
            cursor.close()

        self._test_killCursors_namespace(op, "aggregate")

    def test_find_killCursor(self):
        def op():
            cursor = self.client.test.collection.find(batch_size=1)
            next(cursor)
            cursor.close()

        self._test_killCursors_namespace(op, "find")


if __name__ == "__main__":
    unittest.main()