Skip to content

Commit

Permalink
Tests: Speed up test suite by discriminating between layer and layer+sa
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Nov 21, 2022
1 parent d930c19 commit fc3c233
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DEVELOP.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Ignore specific test directories::
./bin/test -vvvv --ignore_dir=testing

The ``LayerTest`` test cases have quite some overhead. Omitting them will save
a few cycles (~90 seconds runtime)::
a few cycles (~70 seconds runtime)::

./bin/test -t '!LayerTest'

Expand Down
85 changes: 58 additions & 27 deletions src/crate/client/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def ensure_cratedb_layer():
return crate_layer


def setUpWithCrateLayer(test):
def setUpCrateLayerBaseline(test):
test.globs['crate_host'] = crate_host
test.globs['pprint'] = pprint
test.globs['print'] = cprint
Expand All @@ -131,16 +131,19 @@ def setUpWithCrateLayer(test):
# create blob table
cursor.execute("create blob table myfiles clustered into 1 shards " +
"with (number_of_replicas=0)")

# create users
cursor.execute("CREATE USER me WITH (password = 'my_secret_pw')")
cursor.execute("CREATE USER trusted_me")

cursor.close()


def setUpCrateLayerAndSqlAlchemy(test):
setUpWithCrateLayer(test)
import sqlalchemy as sa
def setUpCrateLayerSqlAlchemy(test):
"""
Setup tables and views needed for SQLAlchemy tests.
"""
setUpCrateLayerBaseline(test)

ddl_statements = [
"""
Expand All @@ -164,11 +167,33 @@ def setUpCrateLayerAndSqlAlchemy(test):
area GEO_SHAPE
)"""
]
_execute_statements(ddl_statements, on_error="raise")


def tearDownDropEntitiesBaseline(test):
"""
Drop all tables, views, and users created by `setUpWithCrateLayer*`.
"""
ddl_statements = [
"DROP TABLE locations",
"DROP BLOB TABLE myfiles",
"DROP USER me",
"DROP USER trusted_me",
]
_execute_statements(ddl_statements)


engine = sa.create_engine(f"crate://{crate_host}")
for ddl_statement in ddl_statements:
engine.execute(sa.text(ddl_statement))
engine.dispose()
def tearDownDropEntitiesSqlAlchemy(test):
"""
Drop all tables, views, and users created by `setUpWithCrateLayer*`.
"""
tearDownDropEntitiesBaseline(test)
ddl_statements = [
"DROP TABLE characters",
"DROP VIEW characters_view",
"DROP TABLE cities",
]
_execute_statements(ddl_statements)


class HttpsTestServerLayer:
Expand Down Expand Up @@ -269,30 +294,26 @@ def setUpWithHttps(test):
)


def _try_execute(cursor, stmt):
def _execute_statements(statements, on_error="ignore"):
with connect(crate_host) as conn:
cursor = conn.cursor()
for stmt in statements:
_execute_statement(cursor, stmt, on_error=on_error)
cursor.close()


def _execute_statement(cursor, stmt, on_error="ignore"):
try:
cursor.execute(stmt)
except Exception:
# FIXME: Why does this croak on statements like ``DROP TABLE cities``?
# Note: When needing to debug the test environment, you may want to
# enable this logger statement.
# log.exception("Executing SQL statement failed")
pass


def tearDownWithCrateLayer(test):
# clear testing data
with connect(crate_host) as conn:
cursor = conn.cursor()
for stmt in ["DROP TABLE locations",
"DROP BLOB TABLE myfiles",
"DROP TABLE characters",
"DROP VIEW characters_view",
"DROP TABLE cities",
"DROP USER me",
"DROP USER trusted_me",
]:
_try_execute(cursor, stmt)
if on_error == "ignore":
pass
elif on_error == "raise":
raise


def test_suite():
Expand Down Expand Up @@ -338,12 +359,22 @@ def test_suite():
'docs/by-example/http.rst',
'docs/by-example/client.rst',
'docs/by-example/blob.rst',
module_relative=False,
setUp=setUpCrateLayerBaseline,
tearDown=tearDownDropEntitiesBaseline,
optionflags=flags,
encoding='utf-8'
)
s.layer = ensure_cratedb_layer()
suite.addTest(s)

s = doctest.DocFileSuite(
'docs/by-example/sqlalchemy/getting-started.rst',
'docs/by-example/sqlalchemy/cru.rst',
'docs/by-example/sqlalchemy/inspection-reflection.rst',
module_relative=False,
setUp=setUpCrateLayerAndSqlAlchemy,
tearDown=tearDownWithCrateLayer,
setUp=setUpCrateLayerSqlAlchemy,
tearDown=tearDownDropEntitiesSqlAlchemy,
optionflags=flags,
encoding='utf-8'
)
Expand Down

0 comments on commit fc3c233

Please sign in to comment.