crate 1.0.0.dev1__py3-none-any.whl → 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
crate/client/test_util.py DELETED
@@ -1,69 +0,0 @@
1
- # -*- coding: utf-8; -*-
2
- #
3
- # Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
4
- # license agreements. See the NOTICE file distributed with this work for
5
- # additional information regarding copyright ownership. Crate licenses
6
- # this file to you under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License. You may
8
- # obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
- # License for the specific language governing permissions and limitations
16
- # under the License.
17
- #
18
- # However, if you have executed another commercial license agreement
19
- # with Crate these terms will supersede the license and you may use the
20
- # software solely pursuant to the terms of the relevant commercial agreement.
21
- import unittest
22
-
23
-
24
- class ClientMocked(object):
25
-
26
- active_servers = ["http://localhost:4200"]
27
-
28
- def __init__(self):
29
- self.response = {}
30
- self._server_infos = ("http://localhost:4200", "my server", "2.0.0")
31
-
32
- def sql(self, stmt=None, parameters=None, bulk_parameters=None):
33
- return self.response
34
-
35
- def server_infos(self, server):
36
- return self._server_infos
37
-
38
- def set_next_response(self, response):
39
- self.response = response
40
-
41
- def set_next_server_infos(self, server, server_name, version):
42
- self._server_infos = (server, server_name, version)
43
-
44
- def close(self):
45
- pass
46
-
47
-
48
- class ParametrizedTestCase(unittest.TestCase):
49
- """
50
- TestCase classes that want to be parametrized should
51
- inherit from this class.
52
-
53
- https://eli.thegreenplace.net/2011/08/02/python-unit-testing-parametrized-test-cases
54
- """
55
- def __init__(self, methodName="runTest", param=None):
56
- super(ParametrizedTestCase, self).__init__(methodName)
57
- self.param = param
58
-
59
- @staticmethod
60
- def parametrize(testcase_klass, param=None):
61
- """ Create a suite containing all tests taken from the given
62
- subclass, passing them the parameter 'param'.
63
- """
64
- testloader = unittest.TestLoader()
65
- testnames = testloader.getTestCaseNames(testcase_klass)
66
- suite = unittest.TestSuite()
67
- for name in testnames:
68
- suite.addTest(testcase_klass(name, param=param))
69
- return suite
crate/client/tests.py DELETED
@@ -1,340 +0,0 @@
1
- # -*- coding: utf-8; -*-
2
- #
3
- # Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
4
- # license agreements. See the NOTICE file distributed with this work for
5
- # additional information regarding copyright ownership. Crate licenses
6
- # this file to you under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License. You may
8
- # obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
- # License for the specific language governing permissions and limitations
16
- # under the License.
17
- #
18
- # However, if you have executed another commercial license agreement
19
- # with Crate these terms will supersede the license and you may use the
20
- # software solely pursuant to the terms of the relevant commercial agreement.
21
-
22
- from __future__ import absolute_import
23
-
24
- import json
25
- import os
26
- import socket
27
- import unittest
28
- import doctest
29
- from pprint import pprint
30
- from http.server import HTTPServer, BaseHTTPRequestHandler
31
- import ssl
32
- import time
33
- import threading
34
- import logging
35
-
36
- import stopit
37
-
38
- from crate.testing.layer import CrateLayer
39
- from crate.testing.settings import \
40
- crate_host, crate_path, crate_port, \
41
- crate_transport_port, docs_path, localhost
42
- from crate.client import connect
43
-
44
- from .test_cursor import CursorTest
45
- from .test_connection import ConnectionTest
46
- from .test_http import (
47
- HttpClientTest,
48
- ThreadSafeHttpClientTest,
49
- KeepAliveClientTest,
50
- ParamsTest,
51
- RetryOnTimeoutServerTest,
52
- RequestsCaBundleTest,
53
- TestUsernameSentAsHeader,
54
- TestCrateJsonEncoder,
55
- TestDefaultSchemaHeader,
56
- )
57
-
58
- makeSuite = unittest.TestLoader().loadTestsFromTestCase
59
-
60
- log = logging.getLogger('crate.testing.layer')
61
- ch = logging.StreamHandler()
62
- ch.setLevel(logging.ERROR)
63
- log.addHandler(ch)
64
-
65
-
66
- def cprint(s):
67
- if isinstance(s, bytes):
68
- s = s.decode('utf-8')
69
- print(s)
70
-
71
-
72
- settings = {
73
- 'udc.enabled': 'false',
74
- 'lang.js.enabled': 'true',
75
- 'auth.host_based.enabled': 'true',
76
- 'auth.host_based.config.0.user': 'crate',
77
- 'auth.host_based.config.0.method': 'trust',
78
- 'auth.host_based.config.98.user': 'trusted_me',
79
- 'auth.host_based.config.98.method': 'trust',
80
- 'auth.host_based.config.99.user': 'me',
81
- 'auth.host_based.config.99.method': 'password',
82
- }
83
- crate_layer = None
84
-
85
-
86
- def ensure_cratedb_layer():
87
- """
88
- In order to skip individual tests by manually disabling them within
89
- `def test_suite()`, it is crucial make the test layer not run on each
90
- and every occasion. So, things like this will be possible::
91
-
92
- ./bin/test -vvvv --ignore_dir=testing
93
-
94
- TODO: Through a subsequent patch, the possibility to individually
95
- unselect specific tests might be added to `def test_suite()`
96
- on behalf of environment variables.
97
- A blueprint for this kind of logic can be found at
98
- https://github.com/crate/crate/commit/414cd833.
99
- """
100
- global crate_layer
101
-
102
- if crate_layer is None:
103
- crate_layer = CrateLayer('crate',
104
- crate_home=crate_path(),
105
- port=crate_port,
106
- host=localhost,
107
- transport_port=crate_transport_port,
108
- settings=settings)
109
- return crate_layer
110
-
111
-
112
- def setUpCrateLayerBaseline(test):
113
- test.globs['crate_host'] = crate_host
114
- test.globs['pprint'] = pprint
115
- test.globs['print'] = cprint
116
-
117
- with connect(crate_host) as conn:
118
- cursor = conn.cursor()
119
-
120
- with open(docs_path('testing/testdata/mappings/locations.sql')) as s:
121
- stmt = s.read()
122
- cursor.execute(stmt)
123
- stmt = ("select count(*) from information_schema.tables "
124
- "where table_name = 'locations'")
125
- cursor.execute(stmt)
126
- assert cursor.fetchall()[0][0] == 1
127
-
128
- data_path = docs_path('testing/testdata/data/test_a.json')
129
- # load testing data into crate
130
- cursor.execute("copy locations from ?", (data_path,))
131
- # refresh location table so imported data is visible immediately
132
- cursor.execute("refresh table locations")
133
- # create blob table
134
- cursor.execute("create blob table myfiles clustered into 1 shards " +
135
- "with (number_of_replicas=0)")
136
-
137
- # create users
138
- cursor.execute("CREATE USER me WITH (password = 'my_secret_pw')")
139
- cursor.execute("CREATE USER trusted_me")
140
-
141
- cursor.close()
142
-
143
-
144
- def tearDownDropEntitiesBaseline(test):
145
- """
146
- Drop all tables, views, and users created by `setUpWithCrateLayer*`.
147
- """
148
- ddl_statements = [
149
- "DROP TABLE locations",
150
- "DROP BLOB TABLE myfiles",
151
- "DROP USER me",
152
- "DROP USER trusted_me",
153
- ]
154
- _execute_statements(ddl_statements)
155
-
156
-
157
- class HttpsTestServerLayer:
158
- PORT = 65534
159
- HOST = "localhost"
160
- CERT_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__),
161
- "pki/server_valid.pem"))
162
- CACERT_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__),
163
- "pki/cacert_valid.pem"))
164
-
165
- __name__ = "httpsserver"
166
- __bases__ = tuple()
167
-
168
- class HttpsServer(HTTPServer):
169
- def get_request(self):
170
-
171
- # Prepare SSL context.
172
- context = ssl._create_unverified_context(
173
- protocol=ssl.PROTOCOL_TLS_SERVER,
174
- cert_reqs=ssl.CERT_OPTIONAL,
175
- check_hostname=False,
176
- purpose=ssl.Purpose.CLIENT_AUTH,
177
- certfile=HttpsTestServerLayer.CERT_FILE,
178
- keyfile=HttpsTestServerLayer.CERT_FILE,
179
- cafile=HttpsTestServerLayer.CACERT_FILE)
180
-
181
- # Set minimum protocol version, TLSv1 and TLSv1.1 are unsafe.
182
- context.minimum_version = ssl.TLSVersion.TLSv1_2
183
-
184
- # Wrap TLS encryption around socket.
185
- socket, client_address = HTTPServer.get_request(self)
186
- socket = context.wrap_socket(socket, server_side=True)
187
-
188
- return socket, client_address
189
-
190
- class HttpsHandler(BaseHTTPRequestHandler):
191
-
192
- payload = json.dumps({"name": "test", "status": 200, })
193
-
194
- def do_GET(self):
195
- self.send_response(200)
196
- payload = self.payload.encode('UTF-8')
197
- self.send_header("Content-Length", len(payload))
198
- self.send_header("Content-Type", "application/json; charset=UTF-8")
199
- self.end_headers()
200
- self.wfile.write(payload)
201
-
202
- def setUp(self):
203
- self.server = self.HttpsServer(
204
- (self.HOST, self.PORT),
205
- self.HttpsHandler
206
- )
207
- thread = threading.Thread(target=self.serve_forever)
208
- thread.daemon = True # quit interpreter when only thread exists
209
- thread.start()
210
- self.waitForServer()
211
-
212
- def serve_forever(self):
213
- print("listening on", self.HOST, self.PORT)
214
- self.server.serve_forever()
215
- print("server stopped.")
216
-
217
- def tearDown(self):
218
- self.server.shutdown()
219
- self.server.server_close()
220
-
221
- def isUp(self):
222
- """
223
- Test if a host is up.
224
- """
225
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
226
- ex = s.connect_ex((self.HOST, self.PORT))
227
- s.close()
228
- return ex == 0
229
-
230
- def waitForServer(self, timeout=5):
231
- """
232
- Wait for the host to be available.
233
- """
234
- with stopit.ThreadingTimeout(timeout) as to_ctx_mgr:
235
- while True:
236
- if self.isUp():
237
- break
238
- time.sleep(0.001)
239
-
240
- if not to_ctx_mgr:
241
- raise TimeoutError("Could not properly start embedded webserver "
242
- "within {} seconds".format(timeout))
243
-
244
-
245
- def setUpWithHttps(test):
246
- test.globs['crate_host'] = "https://{0}:{1}".format(
247
- HttpsTestServerLayer.HOST, HttpsTestServerLayer.PORT
248
- )
249
- test.globs['pprint'] = pprint
250
- test.globs['print'] = cprint
251
-
252
- test.globs['cacert_valid'] = os.path.abspath(
253
- os.path.join(os.path.dirname(__file__), "pki/cacert_valid.pem")
254
- )
255
- test.globs['cacert_invalid'] = os.path.abspath(
256
- os.path.join(os.path.dirname(__file__), "pki/cacert_invalid.pem")
257
- )
258
- test.globs['clientcert_valid'] = os.path.abspath(
259
- os.path.join(os.path.dirname(__file__), "pki/client_valid.pem")
260
- )
261
- test.globs['clientcert_invalid'] = os.path.abspath(
262
- os.path.join(os.path.dirname(__file__), "pki/client_invalid.pem")
263
- )
264
-
265
-
266
- def _execute_statements(statements, on_error="ignore"):
267
- with connect(crate_host) as conn:
268
- cursor = conn.cursor()
269
- for stmt in statements:
270
- _execute_statement(cursor, stmt, on_error=on_error)
271
- cursor.close()
272
-
273
-
274
- def _execute_statement(cursor, stmt, on_error="ignore"):
275
- try:
276
- cursor.execute(stmt)
277
- except Exception: # pragma: no cover
278
- # FIXME: Why does this croak on statements like ``DROP TABLE cities``?
279
- # Note: When needing to debug the test environment, you may want to
280
- # enable this logger statement.
281
- # log.exception("Executing SQL statement failed")
282
- if on_error == "ignore":
283
- pass
284
- elif on_error == "raise":
285
- raise
286
-
287
-
288
- def test_suite():
289
- suite = unittest.TestSuite()
290
- flags = (doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS)
291
-
292
- # Unit tests.
293
- suite.addTest(makeSuite(CursorTest))
294
- suite.addTest(makeSuite(HttpClientTest))
295
- suite.addTest(makeSuite(KeepAliveClientTest))
296
- suite.addTest(makeSuite(ThreadSafeHttpClientTest))
297
- suite.addTest(makeSuite(ParamsTest))
298
- suite.addTest(makeSuite(ConnectionTest))
299
- suite.addTest(makeSuite(RetryOnTimeoutServerTest))
300
- suite.addTest(makeSuite(RequestsCaBundleTest))
301
- suite.addTest(makeSuite(TestUsernameSentAsHeader))
302
- suite.addTest(makeSuite(TestCrateJsonEncoder))
303
- suite.addTest(makeSuite(TestDefaultSchemaHeader))
304
- suite.addTest(doctest.DocTestSuite('crate.client.connection'))
305
- suite.addTest(doctest.DocTestSuite('crate.client.http'))
306
-
307
- s = doctest.DocFileSuite(
308
- 'docs/by-example/connection.rst',
309
- 'docs/by-example/cursor.rst',
310
- module_relative=False,
311
- optionflags=flags,
312
- encoding='utf-8'
313
- )
314
- suite.addTest(s)
315
-
316
- s = doctest.DocFileSuite(
317
- 'docs/by-example/https.rst',
318
- module_relative=False,
319
- setUp=setUpWithHttps,
320
- optionflags=flags,
321
- encoding='utf-8'
322
- )
323
- s.layer = HttpsTestServerLayer()
324
- suite.addTest(s)
325
-
326
- # Integration tests.
327
- s = doctest.DocFileSuite(
328
- 'docs/by-example/http.rst',
329
- 'docs/by-example/client.rst',
330
- 'docs/by-example/blob.rst',
331
- module_relative=False,
332
- setUp=setUpCrateLayerBaseline,
333
- tearDown=tearDownDropEntitiesBaseline,
334
- optionflags=flags,
335
- encoding='utf-8'
336
- )
337
- s.layer = ensure_cratedb_layer()
338
- suite.addTest(s)
339
-
340
- return suite
crate/testing/settings.py DELETED
@@ -1,51 +0,0 @@
1
- # vi: set encoding=utf-8
2
- # -*- coding: utf-8; -*-
3
- #
4
- # Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
5
- # license agreements. See the NOTICE file distributed with this work for
6
- # additional information regarding copyright ownership. Crate licenses
7
- # this file to you under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License. You may
9
- # obtain a copy of the License at
10
- #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
- #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
- # License for the specific language governing permissions and limitations
17
- # under the License.
18
- #
19
- # However, if you have executed another commercial license agreement
20
- # with Crate these terms will supersede the license and you may use the
21
- # software solely pursuant to the terms of the relevant commercial agreement.
22
- from __future__ import absolute_import
23
-
24
- import os
25
-
26
-
27
- def docs_path(*parts):
28
- return os.path.abspath(
29
- os.path.join(
30
- os.path.dirname(os.path.dirname(__file__)), *parts
31
- )
32
- )
33
-
34
-
35
- def project_root(*parts):
36
- return os.path.abspath(
37
- os.path.join(docs_path("..", ".."), *parts)
38
- )
39
-
40
-
41
- def crate_path(*parts):
42
- return os.path.abspath(
43
- project_root("parts", "crate", *parts)
44
- )
45
-
46
-
47
- crate_port = 44209
48
- crate_transport_port = 44309
49
- localhost = '127.0.0.1'
50
- crate_host = "{host}:{port}".format(host=localhost, port=crate_port)
51
- crate_uri = "http://%s" % crate_host
@@ -1,90 +0,0 @@
1
- # -*- coding: utf-8; -*-
2
- #
3
- # Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
4
- # license agreements. See the NOTICE file distributed with this work for
5
- # additional information regarding copyright ownership. Crate licenses
6
- # this file to you under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License. You may
8
- # obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
- # License for the specific language governing permissions and limitations
16
- # under the License.
17
- #
18
- # However, if you have executed another commercial license agreement
19
- # with Crate these terms will supersede the license and you may use the
20
- # software solely pursuant to the terms of the relevant commercial agreement.
21
-
22
- import os
23
- import sys
24
- import unittest
25
- from datetime import datetime, date
26
- from unittest import TestCase, mock
27
-
28
- import time_machine
29
-
30
-
31
- class DateTodayTest(TestCase):
32
- """
33
- `date.today()` returns the current local date, as advertised in the
34
- documentation [1]. Thus, it depends on the system time zone.
35
-
36
- The following test cases demonstrate that the test suite previously
37
- failed around midnight, where the UTC vs. non-UTC days overlapped,
38
- and when running on machines with non-UTC time zone.
39
-
40
- On the other hand, `datetime.utcnow().date()` works equally well in all
41
- situations, so we want to use that within the SQLAlchemy test cases.
42
-
43
- Funny enough, the problem is not observable on Linux?
44
-
45
- [1] https://docs.python.org/3/library/datetime.html#datetime.date.today
46
- """
47
-
48
- @mock.patch.dict(os.environ, {"TZ": "UTC"})
49
- @time_machine.travel("2022-07-22T00:42:00+0200")
50
- def test_date_today_depends_on_system_timezone_success_on_utc(self):
51
- today_local = date.today()
52
- today_utc = datetime.utcnow().date()
53
- self.assertEqual(today_local, today_utc)
54
- self.assertEqual(today_local, date(2022, 7, 21))
55
- self.assertEqual(today_utc, date(2022, 7, 21))
56
-
57
- @unittest.skipIf(sys.platform == "linux", "Problem not observable on Linux")
58
- @mock.patch.dict(os.environ, {"TZ": "Europe/Prague"})
59
- @time_machine.travel("2022-07-22T00:42:00+0200")
60
- def test_date_today_depends_on_system_timezone_failure_on_non_utc(self):
61
- today_local = date.today()
62
- today_utc = datetime.utcnow().date()
63
- self.assertNotEqual(today_local, today_utc)
64
- self.assertEqual(today_local, date(2022, 7, 22))
65
- self.assertEqual(today_utc, date(2022, 7, 21))
66
-
67
- @mock.patch.dict(os.environ, {"TZ": "UTC"})
68
- @time_machine.travel("2022-07-22T00:42:00+0200")
69
- def test_date_today_utc(self):
70
- today_local = date.today()
71
- self.assertEqual(today_local, date(2022, 7, 21))
72
-
73
- @unittest.skipIf(sys.platform == "linux", "Problem not observable on Linux")
74
- @mock.patch.dict(os.environ, {"TZ": "Europe/Prague"})
75
- @time_machine.travel("2022-07-22T00:42:00+0200")
76
- def test_date_today_non_utc(self):
77
- today_local = date.today()
78
- self.assertEqual(today_local, date(2022, 7, 22))
79
-
80
- @mock.patch.dict(os.environ, {"TZ": "UTC"})
81
- @time_machine.travel("2022-07-22T00:42:00+0200")
82
- def test_utcnow_date_utc(self):
83
- today_utc = datetime.utcnow().date()
84
- self.assertEqual(today_utc, date(2022, 7, 21))
85
-
86
- @mock.patch.dict(os.environ, {"TZ": "Europe/Prague"})
87
- @time_machine.travel("2022-07-22T00:42:00+0200")
88
- def test_utcnow_date_non_utc(self):
89
- today_utc = datetime.utcnow().date()
90
- self.assertEqual(today_utc, date(2022, 7, 21))