Skip to content

Commit

Permalink
Change importing of RethinkDB class (#148)
Browse files Browse the repository at this point in the history
* Fix install-db script
* Replace rebirthdb leftovers with rethinkdb
* Fix unit tests
* Use pytest 5.2.2 on python3+
* Update requirements
* Adjust build matrix
* Do pip freeze after dependency installation
  • Loading branch information
gabor-boros authored Oct 28, 2019
1 parent 00ae565 commit 1d2a81d
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 193 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ python:
- "3.5"
- "3.6"
- "3.7"
- "3.7-dev"
- "3.8-dev"
- "3.8"

allow_failure:
- python: "3.8-dev"
- python: "3.8"

install:
- pip install -r requirements.txt
- pip freeze

before_script:
- make prepare
Expand All @@ -31,7 +31,7 @@ deploy:
provider: script
script: make upload-pypi
on:
python: 3.7
python: 3.8
tags: true

notifications:
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ test-unit:
pytest -v -m unit

test-integration:
@rebirthdb&
@rethinkdb&
pytest -v -m integration
@killall rebirthdb
@killall rethinkdb

test-ci:
@rebirthdb&
@rethinkdb&
pytest -v --cov rethinkdb --cov-report xml
@killall rebirthdb
@killall rethinkdb

test-remote:
python ${REMOTE_TEST_SETUP_NAME} pytest -m integration
Expand Down
4 changes: 3 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ python_files = test_*.py
markers =
unit: Run unit tests
integration: Run integration tests
asyncio: Run asyncio relates tests
trio: Run trio related tests
tornado: Run tornado related tests
asyncio: Run asyncio related tests
10 changes: 7 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ async-generator==1.10; python_version>="3.6"
codacy-coverage==1.3.11
mock==3.0.5
pytest-cov==2.8.1
pytest-tornasync; python_version >= '3.5'
pytest-tornasync==0.6.0; python_version >= '3.5'
pytest-trio==0.5.2; python_version>="3.6"
pytest==4.5.0
pytest==4.6.6; python_version<"3.5"
pytest==5.2.2; python_version>="3.5"
six==1.12.0
tornado>=5.0
tornado==5.1.1; python_version<"3.6"
tornado==6.0.3; python_version>="3.6"
trio==0.12.1; python_version>="3.6"
outcome==1.0.1; python_version>="3.5"
attrs==19.3.0; python_version>="3.5"
8 changes: 3 additions & 5 deletions scripts/install-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ set -u

export DISTRIB_CODENAME=$(lsb_release -sc)

# echo "This currently will not work for rethinkdb. It is in the process of being fixed."
# exit 1
echo "deb https://dl.bintray.com/rebirthdb/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rebirthdb.list
wget -qO- https://dl.bintray.com/rebirthdb/keys/pubkey.gpg | sudo apt-key add -
echo "deb https://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -

sudo apt-get update --option Acquire::Retries=100 --option Acquire::http::Timeout="300"
sudo apt-get --allow-unauthenticated install rebirthdb --option Acquire::Retries=100 --option Acquire::http::Timeout="300"
sudo apt-get install -y --option Acquire::Retries=100 --option Acquire::http::Timeout="300" rethinkdb
24 changes: 11 additions & 13 deletions scripts/prepare_remote_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ def _execute_command(self, command):
std_in, _, std_err = self.ssh_client.exec_command(command)
std_in.close()

#for line in std_out.readlines():
# print(line.replace('\n', ''))

has_err = False
for line in std_err.readlines():
has_err = True
Expand Down Expand Up @@ -134,22 +131,23 @@ def __enter__(self):
return self.__enter__()
return self

def install_rebirthdb(self):
self._print_info('getting rebirthdb')
self._execute_command('source /etc/lsb-release && echo "deb https://dl.bintray.com/{username}/apt $DISTRIB_CODENAME main" | tee /etc/apt/sources.list.d/rebirthdb.list'.format(username=BINTRAY_USERNAME))
self._execute_command('wget -qO- https://dl.bintray.com/{username}/keys/pubkey.gpg | apt-key add -'.format(username=BINTRAY_USERNAME))
def install_rethinkdb(self):
self._print_info('getting rethinkdb')

self._execute_command('source /etc/lsb-release && echo "deb https://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list')
self._execute_command('wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -')

self._print_info('installing rethinkdb')
self._execute_command('apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --allow-unauthenticated -y rethinkdb')
self._execute_command('echo "bind=all" > /etc/rethinkdb/instances.d/default.conf')

def start_rebirthdb(self):
self._print_info('restarting rebirthdb')
self._execute_command('/etc/init.d/rebirthdb restart')
def start_rethinkdb(self):
self._print_info('restarting rethinkdb')
self._execute_command('/etc/init.d/rethinkdb restart')

def run_script(self, script, script_arguments):
self._print_info('executing script')
os.environ["REBIRTHDB_HOST"] = self.droplet.ip_address
os.environ["RETHINKDB_HOST"] = self.droplet.ip_address
check_call([script, ' '.join(script_arguments)])

def __exit__(self, *args):
Expand All @@ -176,8 +174,8 @@ def main():
setup.create_droplet()

with setup:
setup.install_rebirthdb()
setup.start_rebirthdb()
setup.install_rethinkdb()
setup.start_rethinkdb()
setup.run_script(script, script_arguments)


Expand Down
18 changes: 7 additions & 11 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@


class IntegrationTestCaseBase(object):
conn = None
def setup_method(self):
self.r = r
self.rethinkdb_host = os.getenv('RETHINKDB_HOST')

def connect(self):
self.conn = r.connect(
self.conn = self.r.connect(
host=self.rethinkdb_host
)

def setup_method(self):
self.rethinkdb_host=os.getenv('RETHINKDB_HOST')

self.connect()

if INTEGRATION_TEST_DB not in r.db_list().run(self.conn):
r.db_create(INTEGRATION_TEST_DB).run(self.conn)
if INTEGRATION_TEST_DB not in self.r.db_list().run(self.conn):
self.r.db_create(INTEGRATION_TEST_DB).run(self.conn)

self.conn.use(INTEGRATION_TEST_DB)

def teardown_method(self):
r.db_drop(INTEGRATION_TEST_DB).run(self.conn)
self.r.db_drop(INTEGRATION_TEST_DB).run(self.conn)
self.conn.close()
Empty file added tests/integration/__init__.py
Empty file.
76 changes: 36 additions & 40 deletions tests/integration/test_asyncio.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
import os
import sys
from collections import namedtuple
import pytest
from rethinkdb import r
from rethinkdb.errors import ReqlRuntimeError

Helper = namedtuple("Helper", "r connection")

INTEGRATION_TEST_DB = 'integration_test'
from asyncio import coroutine
from tests.helpers import INTEGRATION_TEST_DB, IntegrationTestCaseBase


@pytest.mark.asyncio
@pytest.mark.integration
@pytest.mark.skipif(sys.version_info < (3, 6),
reason="requires python3.6 or higher")
async def test_flow():
"""
Test the flow for 3.6 and up, async generators are
not supported in 3.5.
"""

r.set_loop_type("asyncio")

connection = await r.connect(os.getenv("REBIRTHDB_HOST"))

try:
await r.db_create(INTEGRATION_TEST_DB).run(connection)
except ReqlRuntimeError:
pass

connection.use(INTEGRATION_TEST_DB)

await r.table_create("marvel").run(connection)

marvel_heroes = r.table('marvel')
await marvel_heroes.insert({
'id': 1,
'name': 'Iron Man',
'first_appearance': 'Tales of Suspense #39'
}).run(connection)

cursor = await marvel_heroes.run(connection)
async for hero in cursor:
assert hero['name'] == 'Iron Man'

await connection.close()
@pytest.mark.skipif(
sys.version_info == (3, 4) or sys.version_info == (3, 5),
reason="requires python3.4 or python3.5"
)
class TestAsyncio(IntegrationTestCaseBase):
def setup_method(self):
super(TestAsyncio, self).setup_method()
self.table_name = 'test_asyncio'
self.r.set_loop_type('asyncio')

def teardown_method(self):
super(TestAsyncio, self).teardown_method()
self.r.set_loop_type(None)

@coroutine
def test_flow_coroutine_paradigm(self):
connection = yield from self.conn

yield from self.r.table_create(self.table_name).run(connection)

table = self.r.table(self.table_name)
yield from table.insert({
'id': 1,
'name': 'Iron Man',
'first_appearance': 'Tales of Suspense #39'
}).run(connection)

cursor = yield from table.run(connection)

while (yield from cursor.fetch_next()):
hero = yield from cursor.__anext__()
assert hero['name'] == 'Iron Man'

yield from connection.close()
44 changes: 0 additions & 44 deletions tests/integration/test_asyncio_coroutine.py

This file was deleted.

9 changes: 6 additions & 3 deletions tests/integration/test_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ def test_password_connect(self):
'replaced': 0,
'skipped': 0,
'unchanged': 0}
curr = self.r.db("rethinkdb").grant(new_user, {"read": True}).run(conn)
curr = self.r.grant(new_user, {"read": True}).run(conn)
assert curr == {
'granted': 1,
'permissions_changes': [
{
'new_val': {'read': True},
'old_val': None}]}
with self.r.connect(user=new_user, password=BAD_PASSWORD, host=self.rethinkdb_host) as conn:
curr = self.r.db("rethinkdb").table("users").get("admin").run(conn)
assert curr == {'id': 'admin', 'password': False}
with pytest.raises(self.r.ReqlPermissionError):
# Only administrators may access system tables
curr = self.r.db("rethinkdb").table("users").get("admin").run(conn)

with pytest.raises(self.r.ReqlPermissionError):
# No permission for write. Only for read.
self.r.db("rethinkdb").table("users").insert(
{"id": "bob", "password": ""}
).run(conn)
Expand Down
37 changes: 19 additions & 18 deletions tests/integration/test_tornado.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import os
import sys
from collections import namedtuple
import pytest
from rethinkdb import r
from rethinkdb.errors import ReqlRuntimeError

Helper = namedtuple("Helper", "r connection")

INTEGRATION_TEST_DB = 'integration_test'
from tests.helpers import IntegrationTestCaseBase


@pytest.mark.tornado
@pytest.mark.integration
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
async def test_tornado_connect(io_loop):
"""
Test the flow for 3.6 and up, async generators are
not supported in 3.5.
"""
class TestTornado(IntegrationTestCaseBase):
def setup_method(self):
super(TestTornado, self).setup_method()
self.table_name = 'test_tornado'
self.r.set_loop_type('tornado')
self.r.table_create(self.table_name).run(self.conn)

def teardown_method(self):
super(TestTornado, self).teardown_method()
self.r.set_loop_type(None)

r.set_loop_type("tornado")
async def test_tornado_list_tables(self):
"""
Test the flow for 3.6 and up, async generators are
not supported in 3.5.
"""

connection = await r.connect(os.getenv("REBIRTHDB_HOST"))
dbs = await r.db_list().run(connection)
assert isinstance(dbs, list)
await connection.close()
tables = self.r.table_list().run(self.conn)
assert isinstance(tables, list)
Loading

0 comments on commit 1d2a81d

Please sign in to comment.