Skip to content

Commit

Permalink
Renamed SA_ENGINE constant in examples and tests into SA_ENGINE_KEY t…
Browse files Browse the repository at this point in the history
…o reduce disambiguation
  • Loading branch information
vmagamedov committed Nov 18, 2016
1 parent c3c9527 commit 5d75c9c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions docs/guide/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ way to solve this issue without using global variables (thread-locals) - by
providing query execution context.

Query execution context is a simple mapping, where you can store and read values
during query execution. In this example we are using ``SA_ENGINE`` constant
during query execution. In this example we are using ``SA_ENGINE_KEY`` constant
:sup:`[19]` as a key to access our SQLAlchemy's engine. In order to access query
context :py:func:`~hiku.engine.pass_context` decorator should be used
:sup:`[19]` and then ``to_characters_query`` function :sup:`[20]` will receive
it as a first positional argument. ``SA_ENGINE`` constant is used to get
it as a first positional argument. ``SA_ENGINE_KEY`` constant is used to get
SQLAlchemy's engine from the context :sup:`[22]` in order to execute SQL query.

:py:class:`~hiku.sources.sqlalchemy.FieldsQuery` :sup:`[8]` and
Expand Down
14 changes: 7 additions & 7 deletions docs/guide/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
from hiku.engine import pass_context
from hiku.sources import sqlalchemy as sa

SA_ENGINE = 'sa-engine'
SA_ENGINE_KEY = 'sa-engine'

character_query = sa.FieldsQuery(SA_ENGINE, character_table)
character_query = sa.FieldsQuery(SA_ENGINE_KEY, character_table)

actor_query = sa.FieldsQuery(SA_ENGINE, actor_table)
actor_query = sa.FieldsQuery(SA_ENGINE_KEY, actor_table)

character_to_actors_query = sa.LinkQuery(Sequence[TypeRef['actor']], SA_ENGINE,
character_to_actors_query = sa.LinkQuery(Sequence[TypeRef['actor']], SA_ENGINE_KEY,
from_column=actor_table.c.character_id,
to_column=actor_table.c.id)

Expand All @@ -61,12 +61,12 @@ def direct_link(ids):
@pass_context
def to_characters_query(ctx):
query = character_table.select(character_table.c.id)
return [row.id for row in ctx[SA_ENGINE].execute(query)]
return [row.id for row in ctx[SA_ENGINE_KEY].execute(query)]

@pass_context
def to_actors_query(ctx):
query = actor_table.select(actor_table.c.id)
return [row.id for row in ctx[SA_ENGINE].execute(query)]
return [row.id for row in ctx[SA_ENGINE_KEY].execute(query)]

GRAPH = Graph([
Node('character', [
Expand Down Expand Up @@ -101,7 +101,7 @@ def to_actors_query(ctx):

def execute(graph, query_string):
query = read(query_string)
result = hiku_engine.execute(graph, query, {SA_ENGINE: sa_engine})
result = hiku_engine.execute(graph, query, {SA_ENGINE_KEY: sa_engine})
return denormalize(graph, result, query)

def test_character_to_actors():
Expand Down
10 changes: 5 additions & 5 deletions docs/guide/test_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
from hiku.engine import pass_context, Nothing
from hiku.sources import sqlalchemy as sa

SA_ENGINE = 'sa-engine'
SA_ENGINE_KEY = 'sa-engine'

image_query = sa.FieldsQuery(SA_ENGINE, image_table)
image_query = sa.FieldsQuery(SA_ENGINE_KEY, image_table)

character_query = sa.FieldsQuery(SA_ENGINE, character_table)
character_query = sa.FieldsQuery(SA_ENGINE_KEY, character_table)

def direct_link(ids):
return ids
Expand All @@ -57,7 +57,7 @@ def maybe_direct_link(ids):
@pass_context
def to_characters_query(ctx):
query = character_table.select(character_table.c.id)
return [row.id for row in ctx[SA_ENGINE].execute(query)]
return [row.id for row in ctx[SA_ENGINE_KEY].execute(query)]

_GRAPH = Graph([
Node('image', [
Expand Down Expand Up @@ -89,7 +89,7 @@ def to_characters_query(ctx):
def execute(graph, query_string):
query = read(query_string)
result = hiku_engine.execute(graph, query,
{SA_ENGINE: sa_engine})
{SA_ENGINE_KEY: sa_engine})
return denormalize(graph, result, query)

def test_low_level():
Expand Down
6 changes: 3 additions & 3 deletions examples/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from hiku.console.ui import ConsoleApplication
from hiku.executors.sync import SyncExecutor

from tests.test_source_sqlalchemy import SA_ENGINE, SyncQueries, setup_db
from tests.test_source_sqlalchemy import SA_ENGINE_KEY, SyncQueries, setup_db
from tests.test_source_sqlalchemy import get_queries, get_graph


Expand All @@ -22,9 +22,9 @@
)
setup_db(sa_engine)

graph = get_graph(sa, get_queries(sa, SA_ENGINE, SyncQueries))
graph = get_graph(sa, get_queries(sa, SA_ENGINE_KEY, SyncQueries))

app = ConsoleApplication(graph, engine, {SA_ENGINE: sa_engine},
app = ConsoleApplication(graph, engine, {SA_ENGINE_KEY: sa_engine},
debug=True)
http_server = make_server('localhost', 5000, app)
http_server.serve_forever()
6 changes: 3 additions & 3 deletions tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from hiku.console.ui import ConsoleApplication
from hiku.executors.sync import SyncExecutor

from .test_source_sqlalchemy import SA_ENGINE, SyncQueries, setup_db
from .test_source_sqlalchemy import SA_ENGINE_KEY, SyncQueries, setup_db
from .test_source_sqlalchemy import get_queries, get_graph


engine = Engine(SyncExecutor())

GRAPH = get_graph(sa, get_queries(sa, SA_ENGINE, SyncQueries))
GRAPH = get_graph(sa, get_queries(sa, SA_ENGINE_KEY, SyncQueries))


def request(app, method, path_info, script_name='', payload=None):
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_query():
)
setup_db(sa_engine)

app = ConsoleApplication(GRAPH, engine, {SA_ENGINE: sa_engine},
app = ConsoleApplication(GRAPH, engine, {SA_ENGINE_KEY: sa_engine},
debug=True)
query = b'[{:bar-list [:name :type {:foo-s [:name :count]}]}]'

Expand Down
9 changes: 5 additions & 4 deletions tests/test_source_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .base import check_result


SA_ENGINE = 'sa-engine'
SA_ENGINE_KEY = 'sa-engine'

metadata = MetaData()

Expand Down Expand Up @@ -191,7 +191,7 @@ def test_types(self):

def test_same_table(self):
with pytest.raises(ValueError) as e:
sa.LinkQuery(Sequence[TypeRef['bar']], SA_ENGINE,
sa.LinkQuery(Sequence[TypeRef['bar']], SA_ENGINE_KEY,
from_column=foo_table.c.id,
to_column=bar_table.c.id)
e.match('should belong')
Expand Down Expand Up @@ -251,7 +251,7 @@ class TestSourceSQLAlchemy(SourceSQLAlchemyTestBase):

@cached_property
def queries(self):
return get_queries(sa, SA_ENGINE, SyncQueries)
return get_queries(sa, SA_ENGINE_KEY, SyncQueries)

def check(self, src, value):
sa_engine = create_engine(
Expand All @@ -262,5 +262,6 @@ def check(self, src, value):
setup_db(sa_engine)

engine = Engine(ThreadsExecutor(thread_pool))
result = engine.execute(self.graph, read(src), {SA_ENGINE: sa_engine})
result = engine.execute(self.graph, read(src),
{SA_ENGINE_KEY: sa_engine})
check_result(result, value)
6 changes: 3 additions & 3 deletions tests3/test_source_aiopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from tests.test_source_sqlalchemy import SourceSQLAlchemyTestBase


SA_ENGINE = 'sa-engine'
SA_ENGINE_KEY = 'sa-engine'


class AsyncQueries(AbstractQueries):
Expand Down Expand Up @@ -77,7 +77,7 @@ class TestSourceAIOPG(SourceSQLAlchemyTestBase):

@cached_property
def queries(self):
return get_queries(sa, SA_ENGINE, AsyncQueries)
return get_queries(sa, SA_ENGINE_KEY, AsyncQueries)

@asyncio.coroutine
def _check(self, src, value, event_loop):
Expand All @@ -86,7 +86,7 @@ def _check(self, src, value, event_loop):
try:
engine = Engine(AsyncIOExecutor(event_loop))
result = yield from engine.execute(self.graph, read(src),
{SA_ENGINE: sa_engine})
{SA_ENGINE_KEY: sa_engine})
check_result(result, value)
finally:
sa_engine.close()
Expand Down

0 comments on commit 5d75c9c

Please sign in to comment.