From 1d99a46fe4d30a0ddd000aa3e145af8be6d5e4b5 Mon Sep 17 00:00:00 2001 From: Inveracity Date: Wed, 26 Jul 2023 22:24:16 +0200 Subject: [PATCH 1/6] lint --- rethinkdb_mock/ast.py | 1 + rethinkdb_mock/db.py | 9 ++++++--- setup.py | 1 - 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/rethinkdb_mock/ast.py b/rethinkdb_mock/ast.py index cbc1f49..d2a479c 100644 --- a/rethinkdb_mock/ast.py +++ b/rethinkdb_mock/ast.py @@ -726,6 +726,7 @@ def __init__(self, *args, **kwargs): def run(self, arg, scope): return arg.list_dbs() + class TableListTL(RBase): def __init__(self, *args, **kwargs): pass diff --git a/rethinkdb_mock/db.py b/rethinkdb_mock/db.py index 268921f..fda454e 100644 --- a/rethinkdb_mock/db.py +++ b/rethinkdb_mock/db.py @@ -6,9 +6,10 @@ from . import rtime from . import util -from .rql_rewrite import rewrite_query, RQL_TYPE_TRANSLATIONS -from .scope import Scope from .ast_base import BinExp +from .rql_rewrite import rewrite_query +from .rql_rewrite import RQL_TYPE_TRANSLATIONS +from .scope import Scope def fill_missing_report_results(report): @@ -199,7 +200,7 @@ def set_table(self, table_name, new_table_instance): class MockDb(object): - def __init__(self, dbs_by_name, defaultDB = None): + def __init__(self, dbs_by_name, defaultDB=None): self.dbs_by_name = dbs_by_name self.defaultDB = defaultDB @@ -308,6 +309,7 @@ def objects_from_pods(data): return MockDb(dbs_by_name, defaultDB) + def set_default_db(query, name): if len(query._args) > 0: if not (query._args[0].__class__ in RQL_TYPE_TRANSLATIONS and issubclass(RQL_TYPE_TRANSLATIONS[query._args[0].__class__], BinExp)): @@ -320,6 +322,7 @@ def set_default_db(query, name): if query.__class__ in RQL_TYPE_TRANSLATIONS and issubclass(RQL_TYPE_TRANSLATIONS[query.__class__], BinExp): query._args = [rethinkdb.ast.DB(name)] + 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 6ae6beb..a345b76 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,5 @@ from setuptools import setup - setup( name="rethinkdb_mock", zip_safe=True, From 133eb8e2a141f2e5442a06d00f5a0b99b79a925c Mon Sep 17 00:00:00 2001 From: Inveracity Date: Wed, 26 Jul 2023 22:29:08 +0200 Subject: [PATCH 2/6] version v0.11.0-dev1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a345b76..ac08315 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", From 64e1c2de5cce9d59285504fd70f50051b366715e Mon Sep 17 00:00:00 2001 From: Inveracity Date: Wed, 26 Jul 2023 23:00:22 +0200 Subject: [PATCH 3/6] update vscode settings --- .vscode/extensions.json | 6 ++++++ .vscode/settings.json | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 .vscode/extensions.json 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" + } } From 2d9a893d57c9d9769d7a6673d095f8e170a65775 Mon Sep 17 00:00:00 2001 From: Inveracity Date: Wed, 26 Jul 2023 23:00:36 +0200 Subject: [PATCH 4/6] update integration tests --- docker/test_live.Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 \ From 38baa72b9f2177cedbb67fb2c558723f56f1da17 Mon Sep 17 00:00:00 2001 From: Inveracity Date: Wed, 26 Jul 2023 23:28:21 +0200 Subject: [PATCH 5/6] update readme with examples that are copy-paste ready --- README.md | 215 +++++++++++++++++++++++++++++------------------------- 1 file changed, 117 insertions(+), 98 deletions(-) diff --git a/README.md b/README.md index 199e6db..155aca8 100644 --- a/README.md +++ b/README.md @@ -25,55 +25,55 @@ 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_mock import MockThink +import rethinkdb as r + +db = MockThink({ + 'dbs': { + 'tara': { + 'tables': { + 'people': [ + {'id': 'john-id', 'name': 'John'}, + {'id': 'sam-id', 'name': 'Sam'} + ] } } - }) - - with db.connect() as conn: - result = r.db('tara').table('people').map( - lambda doc: doc.merge({'also_name': doc['name']}) - ).run(conn) - pprint(list(result)) - - # [ - # {'also_name': 'John', 'id': 'john-id', 'name': 'John'}, - # {'also_name': 'Sam', 'id': 'sam-id', 'name': 'Sam'} - # ] - - r.db('tara').table('people').update( - {'likes_fonz': True} - ).run(conn) - - result = r.db('tara').table('people').run(conn) - pprint(list(result)) - - # [ - # {'id': 'john-id', 'likes_fonz': True, 'name': 'John'}, - # {'id': 'sam-id', 'likes_fonz': True, 'name': 'Sam'} - # ] - - # data is reset at exit of context manager above - - with db.connect() as conn: - result = r.db('tara').table('people').run(conn) - pprint(list(result)) - # [ - # {'id': 'john-id', 'name': 'John'}, - # {'id': 'sam-id', 'name': 'Sam'} - # ] + } +}) + +with db.connect() as conn: + result = r.db('tara').table('people').map( + lambda doc: doc.merge({'also_name': doc['name']}) + ).run(conn) + pprint(list(result)) + + # [ + # {'also_name': 'John', 'id': 'john-id', 'name': 'John'}, + # {'also_name': 'Sam', 'id': 'sam-id', 'name': 'Sam'} + # ] + + r.db('tara').table('people').update( + {'likes_fonz': True} + ).run(conn) + + result = r.db('tara').table('people').run(conn) + pprint(list(result)) + + # [ + # {'id': 'john-id', 'likes_fonz': True, 'name': 'John'}, + # {'id': 'sam-id', 'likes_fonz': True, 'name': 'Sam'} + # ] + +# data is reset at exit of context manager above + +with db.connect() as conn: + result = r.db('tara').table('people').run(conn) + pprint(list(result)) + # [ + # {'id': 'john-id', 'name': 'John'}, + # {'id': 'sam-id', 'name': 'Sam'} + # ] ``` ### Set the default database for the connection @@ -81,78 +81,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'} ``` From 94498218dc9a65eabfc2979d50ec08c91a947719 Mon Sep 17 00:00:00 2001 From: Inveracity Date: Wed, 26 Jul 2023 23:33:03 +0200 Subject: [PATCH 6/6] update initial test in readme to be accurate --- README.md | 56 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 155aca8..4d8380a 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,10 @@ pipenv install --dev rethinkdb-mock ```python from pprint import pprint +from rethinkdb import RethinkDB from rethinkdb_mock import MockThink -import rethinkdb as r +r = RethinkDB() db = MockThink({ 'dbs': { 'tara': { @@ -42,38 +43,39 @@ db = MockThink({ } }) -with db.connect() as conn: - result = r.db('tara').table('people').map( - lambda doc: doc.merge({'also_name': doc['name']}) - ).run(conn) - pprint(list(result)) +def test_mytest(): + with db.connect() as conn: + result = r.db('tara').table('people').map( + lambda doc: doc.merge({'also_name': doc['name']}) + ).run(conn) + pprint(list(result)) - # [ - # {'also_name': 'John', 'id': 'john-id', 'name': 'John'}, - # {'also_name': 'Sam', 'id': 'sam-id', 'name': 'Sam'} - # ] + # [ + # {'also_name': 'John', 'id': 'john-id', 'name': 'John'}, + # {'also_name': 'Sam', 'id': 'sam-id', 'name': 'Sam'} + # ] - r.db('tara').table('people').update( - {'likes_fonz': True} - ).run(conn) + r.db('tara').table('people').update( + {'likes_fonz': True} + ).run(conn) - result = r.db('tara').table('people').run(conn) - pprint(list(result)) + result = r.db('tara').table('people').run(conn) + pprint(list(result)) - # [ - # {'id': 'john-id', 'likes_fonz': True, 'name': 'John'}, - # {'id': 'sam-id', 'likes_fonz': True, 'name': 'Sam'} - # ] + # [ + # {'id': 'john-id', 'likes_fonz': True, 'name': 'John'}, + # {'id': 'sam-id', 'likes_fonz': True, 'name': 'Sam'} + # ] -# data is reset at exit of context manager above + # data is reset at exit of context manager above -with db.connect() as conn: - result = r.db('tara').table('people').run(conn) - pprint(list(result)) - # [ - # {'id': 'john-id', 'name': 'John'}, - # {'id': 'sam-id', 'name': 'Sam'} - # ] + with db.connect() as conn: + result = r.db('tara').table('people').run(conn) + pprint(list(result)) + # [ + # {'id': 'john-id', 'name': 'John'}, + # {'id': 'sam-id', 'name': 'Sam'} + # ] ``` ### Set the default database for the connection