diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..ad841ec --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "ms-python.python", + "ms-python.black-formatter" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 348b66e..21dd184 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,9 @@ { - "python.pythonPath": ".venv\\Scripts\\python.exe", "python.linting.pylintEnabled": false, "python.linting.flake8Enabled": true, "python.linting.enabled": true, - "editor.formatOnSave": true + "editor.formatOnSave": true, + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" + } } diff --git a/README.md b/README.md index 199e6db..4d8380a 100644 --- a/README.md +++ b/README.md @@ -25,23 +25,25 @@ pipenv install --dev rethinkdb-mock ### Basic ```python - from pprint import pprint - from rethinkdb_mock import MockThink - import rethinkdb as r - - db = MockThink({ - 'dbs': { - 'tara': { - 'tables': { - 'people': [ - {'id': 'john-id', 'name': 'John'}, - {'id': 'sam-id', 'name': 'Sam'} - ] - } +from pprint import pprint +from rethinkdb import RethinkDB +from rethinkdb_mock import MockThink + +r = RethinkDB() +db = MockThink({ + 'dbs': { + 'tara': { + 'tables': { + 'people': [ + {'id': 'john-id', 'name': 'John'}, + {'id': 'sam-id', 'name': 'Sam'} + ] } } - }) + } +}) +def test_mytest(): with db.connect() as conn: result = r.db('tara').table('people').map( lambda doc: doc.merge({'also_name': doc['name']}) @@ -81,78 +83,97 @@ pipenv install --dev rethinkdb-mock > Like `r.connect(db='database')` ```python - from pprint import pprint - from rethinkdb_mock import MockThink - import rethinkdb as r - - db = MockThink({ - 'dbs': { - 'tara': { - 'tables': { - 'people': [ - {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'}, - {'id': 'sam-id', 'first_name': 'Sam', 'last_name': 'Dull'}, - {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'} - ] +from pprint import pprint +from rethinkdb import RethinkDB +from rethinkdb_mock import MockThink + +r = RethinkDB() + +def test_mytest_with_default_db(): + db = MockThink( + { + "dbs": { + "tara": { + "tables": { + "people": [ + { + "id": "john-id", + "first_name": "John", + "last_name": "Generic", + }, + { + "id": "sam-id", "first_name": + "Sam", "last_name": "Dull" + }, + { + "id": "adam-id", + "first_name": "Adam", + "last_name": "Average", + }, + ] + } } - } + }, + "default": "tara", } - 'default': 'tara' - }) + ) with db.connect() as conn: - - r.db('tara').table('people').index_create( - 'full_name', - lambda doc: doc['last_name'] + doc['first_name'] + r.table("people").index_create( + "full_name", lambda doc: doc["last_name"] + doc["first_name"] ).run(conn) - r.table('people').index_wait().run(conn) + r.table("people").index_wait().run(conn) - result = r..table('people').get_all( - 'GenericJohn', 'AverageAdam', index='full_name' - ).run(conn) + result = ( + r.table("people") + .get_all("GenericJohn", "AverageAdam", index="full_name") + .run(conn) + ) pprint(list(result)) # {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'}, # {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'} + ``` ### Full support for secondary indexes ```python - from pprint import pprint - from rethinkdb_mock import MockThink - import rethinkdb as r - - db = MockThink({ - 'dbs': { - 'tara': { - 'tables': { - 'people': [ - {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'}, - {'id': 'sam-id', 'first_name': 'Sam', 'last_name': 'Dull'}, - {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'} - ] - } +from pprint import pprint +from rethinkdb import RethinkDB +from rethinkdb_mock import MockThink + +r = RethinkDB() + +db = MockThink({ + 'dbs': { + 'tara': { + 'tables': { + 'people': [ + {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'}, + {'id': 'sam-id', 'first_name': 'Sam', 'last_name': 'Dull'}, + {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'} + ] } } - }) + } +}) - with db.connect() as conn: +with db.connect() as conn: - r.db('tara').table('people').index_create( - 'full_name', - lambda doc: doc['last_name'] + doc['first_name'] - ).run(conn) + r.db('tara').table('people').index_create( + 'full_name', + lambda doc: doc['last_name'] + doc['first_name'] + ).run(conn) - r.db('tara').table('people').index_wait().run(conn) + r.db('tara').table('people').index_wait().run(conn) - result = r.db('tara').table('people').get_all( - 'GenericJohn', 'AverageAdam', index='full_name' - ).run(conn) - pprint(list(result)) - # {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'}, - # {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'} + result = r.db('tara').table('people').get_all( + 'GenericJohn', 'AverageAdam', index='full_name' + ).run(conn) + pprint(list(result)) + # {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'}, + # {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'} ``` diff --git a/docker/test_live.Dockerfile b/docker/test_live.Dockerfile index 5e8d4c8..faced20 100644 --- a/docker/test_live.Dockerfile +++ b/docker/test_live.Dockerfile @@ -1,6 +1,6 @@ -FROM rethinkdb:2.4.1-buster-slim as rdb +FROM rethinkdb:2.4.2-bullseye-slim as rdb -FROM python:3.9-slim-buster +FROM python:3.11-slim-bullseye RUN apt update && apt install -y \ libcurl4-openssl-dev \ diff --git a/rethinkdb_mock/db.py b/rethinkdb_mock/db.py index b187172..f416af6 100644 --- a/rethinkdb_mock/db.py +++ b/rethinkdb_mock/db.py @@ -8,6 +8,8 @@ from . import rtime from . import util from .rql_rewrite import rewrite_query +from .ast_base import BinExp +from .rql_rewrite import RQL_TYPE_TRANSLATIONS from .scope import Scope @@ -181,6 +183,7 @@ def set_table(self, table_name, new_table_instance): class MockDb(object): def __init__(self, dbs_by_name, default_db=None): + self.dbs_by_name = dbs_by_name self.default_db = default_db @@ -306,6 +309,7 @@ def objects_from_pods(data): return MockDb(dbs_by_name, default_db) + def set_default_db(query, name): NEEDS_DB_AST = [ r_ast.TableListTL.term_type, @@ -334,6 +338,7 @@ def set_default_db(query, name): query._args = [rethinkdb.ast.DB(name)] + query._args + class MockThinkConn(object): def __init__(self, rethinkdb_mock_parent): self.rethinkdb_mock_parent = rethinkdb_mock_parent diff --git a/setup.py b/setup.py index 18f53fa..a9f3099 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name="rethinkdb_mock", zip_safe=True, - version="0.11.0-dev", + version='0.11.0-dev1', description="A pure-python in-memory mock of rethinkdb (formerly MockThink)", url="https://github.com/Inveracity/rethinkdb-mock", maintainer="Christopher Baklid",