Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Inveracity committed Aug 15, 2023
2 parents 43e5ac1 + 9449821 commit 8e750fb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 70 deletions.
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"ms-python.python",
"ms-python.black-formatter"
]
}
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
151 changes: 86 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']})
Expand Down Expand Up @@ -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'}

```

Expand Down
4 changes: 2 additions & 2 deletions docker/test_live.Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
5 changes: 5 additions & 0 deletions rethinkdb_mock/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8e750fb

Please sign in to comment.