2016-10-31 15:39:12 -07:00
|
|
|
'''
|
2017-03-02 10:16:41 -08:00
|
|
|
tests_rethinker.py - unit tests for doublethink connection manager
|
2016-10-31 15:39:12 -07:00
|
|
|
|
2017-02-17 17:23:53 -08:00
|
|
|
Copyright (C) 2015-2017 Internet Archive
|
2016-10-31 15:39:12 -07:00
|
|
|
|
|
|
|
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.
|
|
|
|
'''
|
|
|
|
|
2017-02-28 16:23:59 -08:00
|
|
|
import doublethink
|
2015-09-21 22:19:09 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import types
|
2015-09-21 23:30:43 +00:00
|
|
|
import gc
|
2015-09-22 21:34:12 +00:00
|
|
|
import pytest
|
2017-03-01 11:20:27 -08:00
|
|
|
import rethinkdb as r
|
2015-11-17 20:06:51 +00:00
|
|
|
import datetime
|
2015-09-21 22:19:09 +00:00
|
|
|
|
|
|
|
logging.basicConfig(stream=sys.stderr, level=logging.INFO,
|
|
|
|
format="%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s")
|
|
|
|
|
2017-02-28 16:23:59 -08:00
|
|
|
class RethinkerForTesting(doublethink.Rethinker):
|
2015-09-21 23:30:43 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(RethinkerForTesting, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def _random_server_connection(self):
|
|
|
|
self.last_conn = super(RethinkerForTesting, self)._random_server_connection()
|
2015-09-22 01:06:25 +00:00
|
|
|
# logging.info("self.last_conn=%s", self.last_conn)
|
2015-09-21 23:30:43 +00:00
|
|
|
return self.last_conn
|
|
|
|
|
2015-09-29 22:07:50 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2017-03-01 11:20:27 -08:00
|
|
|
def rr():
|
|
|
|
rr = RethinkerForTesting()
|
2017-02-23 16:07:14 -08:00
|
|
|
try:
|
2017-03-01 11:20:27 -08:00
|
|
|
rr.db_drop("doublethink_test_db").run()
|
|
|
|
except r.errors.ReqlOpFailedError:
|
2017-02-23 16:07:14 -08:00
|
|
|
pass
|
2017-03-01 11:20:27 -08:00
|
|
|
result = rr.db_create("doublethink_test_db").run()
|
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-21 22:19:09 +00:00
|
|
|
assert result["dbs_created"] == 1
|
2017-02-28 16:23:59 -08:00
|
|
|
return RethinkerForTesting(db="doublethink_test_db")
|
2015-09-21 22:19:09 +00:00
|
|
|
|
2015-09-29 22:07:50 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2017-03-01 11:20:27 -08:00
|
|
|
def my_table(rr):
|
|
|
|
assert rr.table_list().run() == []
|
|
|
|
result = rr.table_create("my_table").run()
|
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-21 22:19:09 +00:00
|
|
|
assert result["tables_created"] == 1
|
|
|
|
|
2017-03-01 11:20:27 -08:00
|
|
|
def test_rethinker(rr, my_table):
|
|
|
|
assert rr.table("my_table").index_create("foo").run() == {"created": 1}
|
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-21 22:19:09 +00:00
|
|
|
|
2017-03-01 11:20:27 -08:00
|
|
|
result = rr.table("my_table").insert(({"foo":i,"bar":"repeat"*i} for i in range(2000))).run()
|
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-21 22:19:09 +00:00
|
|
|
assert len(result["generated_keys"]) == 2000
|
|
|
|
assert result["inserted"] == 2000
|
|
|
|
|
2017-03-01 11:20:27 -08:00
|
|
|
result = rr.table("my_table").run()
|
|
|
|
assert rr.last_conn.is_open() # should still be open this time
|
2015-09-21 23:30:43 +00:00
|
|
|
assert isinstance(result, types.GeneratorType)
|
|
|
|
n = 0
|
|
|
|
for x in result:
|
|
|
|
n += 1
|
|
|
|
pass
|
|
|
|
# connection should be closed after finished iterating over results
|
2017-03-01 11:20:27 -08:00
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-21 23:30:43 +00:00
|
|
|
assert n == 2000
|
|
|
|
|
2017-03-01 11:20:27 -08:00
|
|
|
result = rr.table("my_table").run()
|
|
|
|
assert rr.last_conn.is_open() # should still be open this time
|
2015-09-21 23:30:43 +00:00
|
|
|
assert isinstance(result, types.GeneratorType)
|
|
|
|
next(result)
|
|
|
|
result = None
|
|
|
|
gc.collect()
|
|
|
|
# connection should be closed after result is garbage-collected
|
2017-03-01 11:20:27 -08:00
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-21 23:30:43 +00:00
|
|
|
|
2017-03-01 11:20:27 -08:00
|
|
|
result = rr.table("my_table").run()
|
|
|
|
assert rr.last_conn.is_open() # should still be open this time
|
2015-09-21 22:19:09 +00:00
|
|
|
assert isinstance(result, types.GeneratorType)
|
2015-09-21 23:30:43 +00:00
|
|
|
result = None
|
|
|
|
gc.collect()
|
2015-09-22 01:06:25 +00:00
|
|
|
# connection should be closed after result is garbage-collected
|
2017-03-01 11:20:27 -08:00
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-21 22:19:09 +00:00
|
|
|
|
2017-03-01 11:20:27 -08:00
|
|
|
def test_too_many_errors(rr):
|
|
|
|
with pytest.raises(r.errors.ReqlOpFailedError):
|
|
|
|
rr.table_create("too_many_replicas", replicas=99).run()
|
|
|
|
with pytest.raises(r.errors.ReqlOpFailedError):
|
|
|
|
rr.table_create("too_many_shards", shards=99).run()
|
2015-09-22 21:34:12 +00:00
|
|
|
|
2017-03-01 11:20:27 -08:00
|
|
|
def test_slice(rr, my_table):
|
2015-09-29 22:07:50 +00:00
|
|
|
"""Tests RethinkerWrapper.__getitem__()"""
|
2017-03-01 11:20:27 -08:00
|
|
|
result = rr.table("my_table")[5:10].run()
|
|
|
|
assert rr.last_conn.is_open() # should still be open this time
|
2015-09-29 22:07:50 +00:00
|
|
|
assert isinstance(result, types.GeneratorType)
|
|
|
|
n = 0
|
|
|
|
for x in result:
|
|
|
|
n += 1
|
|
|
|
pass
|
|
|
|
# connection should be closed after finished iterating over results
|
2017-03-01 11:20:27 -08:00
|
|
|
assert not rr.last_conn.is_open()
|
2015-09-29 22:07:50 +00:00
|
|
|
assert n == 5
|
2015-10-30 19:50:33 +00:00
|
|
|
|
2015-11-17 20:38:10 +00:00
|
|
|
def test_utcnow():
|
2015-11-17 20:06:51 +00:00
|
|
|
now_notz = datetime.datetime.utcnow() # has no timezone :(
|
|
|
|
assert not now_notz.tzinfo
|
|
|
|
|
2017-02-28 16:23:59 -08:00
|
|
|
now_tz = doublethink.utcnow() # solution to that problem
|
2015-11-17 20:06:51 +00:00
|
|
|
assert now_tz.tzinfo
|
|
|
|
|
2016-04-19 19:24:33 +00:00
|
|
|
## .timestamp() was added in python 3.3
|
|
|
|
if hasattr(now_tz, 'timestamp'):
|
|
|
|
assert now_tz.timestamp() - now_notz.timestamp() < 0.1
|
2015-11-17 20:38:10 +00:00
|
|
|
|
|
|
|
## XXX TypeError: can't subtract offset-naive and offset-aware datetimes
|
|
|
|
# assert abs((now_tz - now_notz).total_seconds()) < 0.1
|
|
|
|
|
|
|
|
## XXX what else can we test without jumping through hoops?
|
2015-11-17 20:06:51 +00:00
|
|
|
|