summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/suite/test_cursor_bound02.py
blob: ce308dbe473f37984e5100af86a21a662ae0b10a (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
#
# Public Domain 2014-present MongoDB, Inc.
# Public Domain 2008-2014 WiredTiger, Inc.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import wiredtiger, wttest
from wtscenario import make_scenarios
from wtbound import bound_base

# test_cursor_bound02.py
#    Test that setting bounds of different key formats works in the cursor bound API. Make
# sure that WiredTiger complains when the upper and lower bounds overlap and that clearing the 
# bounds through the bound API and reset calls work appriopately.
class test_cursor_bound02(bound_base):
    file_name = 'test_cursor_bound02'

    types = [
        ('file', dict(uri='file:', use_colgroup=False)),
        ('table', dict(uri='table:', use_colgroup=False)),
        ('colgroup', dict(uri='table:', use_colgroup=True))
    ]

    key_formats = [
        ('string', dict(key_format='S')),
        ('var', dict(key_format='r')),
        ('int', dict(key_format='i')),
        ('bytes', dict(key_format='u')),
        ('composite_string', dict(key_format='SSS')),
        ('composite_int_string', dict(key_format='iS')),
        ('composite_complex', dict(key_format='iSru')),
    ]

    value_formats = [
        ('string', dict(value_format='S')),
        ('complex-string', dict(value_format='SS')),
    ]

    inclusive = [
        ('inclusive', dict(inclusive=True)),
        ('no-inclusive', dict(inclusive=False))
    ]
    scenarios = make_scenarios(types, key_formats, value_formats, inclusive)

    def test_bound_api(self):
        uri = self.uri + self.file_name
        create_params = 'value_format={},key_format={}'.format(self.value_format, self.key_format)
        if self.use_colgroup:
            create_params += self.gen_colgroup_create_param()
        self.session.create(uri, create_params)

        # Add in column groups.
        if self.use_colgroup:
            for i in range(0, len(self.value_format)):
                create_params = 'columns=(v{0}),'.format(i)
                suburi = 'colgroup:{0}:g{1}'.format(self.file_name, i)
                self.session.create(suburi, create_params)

        cursor = self.session.open_cursor(uri)

        # Test bound API: Basic usage.
        self.assertEqual(self.set_bounds(cursor, 40, "lower"), 0) 
        cursor.set_key(self.gen_key(90))
        self.assertEqual(self.set_bounds(cursor, 90, "upper"), 0)

        # Test bound API: Upper bound < lower bound.
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: self.set_bounds(cursor, 30, "upper"), '/Invalid argument/')

        # Test bound API: Lower bound > upper bound.
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: self.set_bounds(cursor, 95, "lower"), '/Invalid argument/')

        # Test bound API: Test setting lower bound to 20, which would succeed setting upper
        # bound to 30
        self.assertEqual(self.set_bounds(cursor, 20, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 30, "upper"), 0)
    
        # Test bound API: Test setting upper bound to 99, which would succeed setting lower
        # bound to 90
        self.assertEqual(self.set_bounds(cursor, 99, "upper"), 0)
        self.assertEqual(self.set_bounds(cursor, 90, "lower"), 0)

        # Test bound API: No key set.
        cursor.reset()
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=lower"), '/Invalid argument/')
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=upper"), '/Invalid argument/')

        # Test bound API: Test that the key persists after lower bound call.
        cursor.set_value(self.gen_val("30"))
        self.assertEqual(self.set_bounds(cursor, 30, "lower"), 0)
        cursor.insert()

        # Test bound API: Test that the key persists after upper bound call.
        cursor.set_value(self.gen_val("90"))
        self.assertEqual(self.set_bounds(cursor, 90, "upper"), 0)
        cursor.insert()

        # Test bound API: that if lower bound is equal to the upper bound, that both bounds needs to
        # have inclusive configured. 
        cursor.bound("action=clear")
        self.assertEqual(self.set_bounds(cursor, 50, "lower", True), 0)
        self.assertEqual(self.set_bounds(cursor, 50, "upper", True), 0)
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=lower,inclusive=false"), '/Invalid argument/')
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=upper,inclusive=false"), '/Invalid argument/')
        
        # Test bound API: Test that only setting one of the bound inclusive config to true, should
        # fail too.
        cursor.bound("action=clear")
        self.assertEqual(self.set_bounds(cursor, 50, "lower", False), 0)
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=upper,inclusive=false"), '/Invalid argument/')
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=upper,inclusive=true"), '/Invalid argument/')

        cursor.bound("action=clear")
        self.assertEqual(self.set_bounds(cursor, 50, "upper", False), 0)
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=lower,inclusive=false"), '/Invalid argument/')
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: cursor.bound("bound=lower,inclusive=true"), '/Invalid argument/')


    def test_bound_api_reset(self):
        uri = self.uri + self.file_name
        create_params = 'value_format={},key_format={}'.format(self.value_format, self.key_format)
        if self.use_colgroup:
            create_params += self.gen_colgroup_create_param()
        self.session.create(uri, create_params)
        # Add in column groups.
        if self.use_colgroup:
            for i in range(0, len(self.value_format)):
                create_params = 'columns=(v{0}),'.format(i)
                suburi = 'colgroup:{0}:g{1}'.format(self.file_name, i)
                self.session.create(suburi, create_params)
        cursor = self.session.open_cursor(uri)

        self.assertEqual(self.set_bounds(cursor, 30, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 90, "upper"), 0)

        # Test bound API: Test that cursor reset works on the lower bound.
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: self.set_bounds(cursor, 10, "upper"), '/Invalid argument/')
        cursor.reset()
        self.assertEqual(self.set_bounds(cursor, 10, "upper"), 0)

        # Test bound API: Test that cursor reset works on the upper bound.
        self.assertRaisesWithMessage(wiredtiger.WiredTigerError, lambda: self.set_bounds(cursor, 99, "lower"), '/Invalid argument/')
        cursor.reset()
        self.assertEqual(self.set_bounds(cursor, 99, "lower"), 0)
    
        # Test bound API: Test that cursor reset works the clearing bounds both ways. 
        self.assertEqual(self.set_bounds(cursor, 50, "lower"), 0)
        cursor.reset()
        self.assertEqual(self.set_bounds(cursor, 20, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 99, "upper"), 0)

        self.assertEqual(self.set_bounds(cursor, 55, "upper"), 0)
        cursor.reset()
        self.assertEqual(self.set_bounds(cursor, 90, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 99, "upper"), 0)

        # Test bound API: Make sure that a clear and reset works sequentially.
        cursor.reset()
        self.assertEqual(cursor.bound("action=clear"), 0)

        self.assertEqual(self.set_bounds(cursor, 30, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 90, "upper"), 0)
        self.assertEqual(cursor.bound("action=clear"), 0)
        cursor.reset()

        # Test bound API: Test that reset works after a reset.
        self.assertEqual(self.set_bounds(cursor, 30, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 90, "upper"), 0)
        cursor.reset()
        cursor.reset()

    def test_bound_api_clear(self):
        uri = self.uri + self.file_name
        create_params = 'value_format={},key_format={}'.format(self.value_format, self.key_format)
        if self.use_colgroup:
            create_params += self.gen_colgroup_create_param()
        self.session.create(uri, create_params)
        # Add in column groups.
        if self.use_colgroup:
            for i in range(0, len(self.value_format)):
                create_params = 'columns=(v{0}),'.format(i)
                suburi = 'colgroup:{0}:g{1}'.format(self.file_name, i)
                self.session.create(suburi, create_params)
        cursor = self.session.open_cursor(uri)

        self.assertEqual(self.set_bounds(cursor, 30, "lower"), 0)
        cursor.set_key(self.gen_key(90))
        self.assertEqual(self.set_bounds(cursor, 90, "upper"), 0)

        # Test bound API: Test that clearing both of the bounds works. 
        cursor.reset()
        self.assertEqual(self.set_bounds(cursor, 50, "upper"), 0)
        self.assertEqual(cursor.bound("action=clear"), 0)
        self.assertEqual(self.set_bounds(cursor, 90, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 99, "upper"), 0)

        cursor.reset()
        self.assertEqual(self.set_bounds(cursor, 50, "lower"), 0)
        self.assertEqual(cursor.bound("action=clear"), 0)
        self.assertEqual(self.set_bounds(cursor, 20, "lower"), 0)
        self.assertEqual(self.set_bounds(cursor, 99, "upper"), 0)

        # Test bound API: Test that clear works after a clear.
        self.assertEqual(cursor.bound("action=clear"), 0)
        self.assertEqual(cursor.bound("action=clear"), 0)

if __name__ == '__main__':
    wttest.run()