Skip to content

Commit

Permalink
adds test_cli2
Browse files Browse the repository at this point in the history
  • Loading branch information
David Erb committed Jun 7, 2023
1 parent 1d62bc8 commit 0419cb3
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 34 deletions.
Binary file removed ${output_directory}/xchembku_dataface.sqlite
Binary file not shown.
3 changes: 3 additions & 0 deletions src/xchembku_api/datafaces/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ async def aenter(self):
# Open client session to the service or direct connection.
await self.interface.open_client_session()

# For convenience, return the object which is the client interface.
return self.interface

# ----------------------------------------------------------------------------------------
async def aexit(self):
""" """
Expand Down
1 change: 1 addition & 0 deletions src/xchembku_cli/subcommands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def get_multiconf(self, args_dict: dict):
"USER": os.environ.get("USER", "USER"),
"PATH": os.environ.get("PATH", "PATH"),
"PYTHONPATH": os.environ.get("PYTHONPATH", "PYTHONPATH"),
"output_directory": os.environ.get("output_directory", "output_directory"),
}

if hasattr(self._args, "visit") and self._args.visit != "VISIT":
Expand Down
18 changes: 9 additions & 9 deletions src/xchembku_lib/datafaces/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, specification=None):
specification["type_specific_tbd"]["aiohttp_specification"],
)

self.__actual_xchembku_dataface = None
self.__actual_dataface = None

# ----------------------------------------------------------------------------------------
def callsign(self):
Expand Down Expand Up @@ -85,7 +85,7 @@ async def activate_coro(self):
route_tuples = []

# Build a local xchembku_dataface for our back-end.
self.__actual_xchembku_dataface = Datafaces().build_object(
self.__actual_dataface = Datafaces().build_object(
self.specification()["type_specific_tbd"][
"actual_xchembku_dataface_specification"
]
Expand All @@ -96,7 +96,7 @@ async def activate_coro(self):
self.__transaction_lock = asyncio.Lock()

# Get the local implementation started.
await self.__actual_xchembku_dataface.start()
await self.__actual_dataface.start()

await self.activate_coro_base(route_tuples)

Expand All @@ -108,7 +108,7 @@ async def direct_shutdown(self):
""""""
try:
# Disconnect our local dataface connection, i.e. the one which holds the database connection.
await self.__actual_xchembku_dataface.disconnect()
await self.__actual_dataface.disconnect()

except Exception as exception:
raise RuntimeError(
Expand All @@ -130,7 +130,7 @@ async def __do_actually(self, function, args, kwargs):
# logger.info(describe("kwargs", kwargs))

# Get the function which the caller wants executed.
function = getattr(self.__actual_xchembku_dataface, function)
function = getattr(self.__actual_dataface, function)

# Caller wants the function wrapped in a transaction?
if "as_transaction" in kwargs:
Expand All @@ -142,16 +142,16 @@ async def __do_actually(self, function, args, kwargs):

if as_transaction:
# Make sure we have an actual connection.
await self.__actual_xchembku_dataface.establish_database_connection()
await self.__actual_dataface.establish_database_connection()

# Lock out all other requests from running their own transaction.
async with self.__transaction_lock:
try:
await self.__actual_xchembku_dataface.begin()
await self.__actual_dataface.begin()
response = await function(*args, **kwargs)
await self.__actual_xchembku_dataface.commit()
await self.__actual_dataface.commit()
except Exception:
await self.__actual_xchembku_dataface.rollback()
await self.__actual_dataface.rollback()
raise
else:
response = await function(*args, **kwargs)
Expand Down
49 changes: 24 additions & 25 deletions tests/test_cli.py → tests/test_cli1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,46 @@
from xchembku_api.datafaces.context import Context as ClientContext

# Object managing datafaces.
from xchembku_api.datafaces.datafaces import xchembku_datafaces_get_default
from xchembku_api.models.crystal_plate_filter_model import CrystalPlateFilterModel
from xchembku_api.models.crystal_plate_model import CrystalPlateModel

logger = logging.getLogger(__name__)


# ----------------------------------------------------------------------------------------
class TestCliSqlite:
class TestCli1Sqlite:
"""
Test that we can start the service (which uses sqlite) via the command line and talk to it.
"""

def test(self, constants, logging_setup, output_directory):

configuration_file = "tests/configurations/service_sqlite.yaml"
CliTester(configuration_file).main(
Cli1Tester(configuration_file).main(
constants,
configuration_file,
output_directory,
)


# ----------------------------------------------------------------------------------------
class TestCliMysql:
class TestCli1Mysql:
"""
Test that we can start the service (which uses mysql) via the command line and talk to it.
"""

def test(self, constants, logging_setup, output_directory):

configuration_file = "tests/configurations/service_mysql.yaml"
CliTester(configuration_file).main(
Cli1Tester(configuration_file).main(
constants,
configuration_file,
output_directory,
)


# ----------------------------------------------------------------------------------------
class CliTester(Base):
class Cli1Tester(Base):
"""
Class to test the dataface.
"""
Expand All @@ -64,7 +63,6 @@ def __init__(self, configuration_file):

async def _main_coroutine(self, constants, output_directory):
""" """
logger.debug("in CliTester")

# Command to run the service.
xchembku_server_cli = [
Expand All @@ -81,30 +79,28 @@ async def _main_coroutine(self, constants, output_directory):
os.environ["output_directory"] = output_directory

# Launch the service as a process.
logger.debug(f"launching {' '.join(xchembku_server_cli)}")
logger.debug(f"launching subprocess {' '.join(xchembku_server_cli)}")
process = subprocess.Popen(
xchembku_server_cli,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

try:
# Read the configuration.
# Read the same configuration which the service process reads.
multiconf_object = self.get_multiconf()
multiconf_dict = await multiconf_object.load()

# Get a client context to the server in the process we just started.
xchembku_specification = multiconf_dict["xchembku_dataface_specification"]
xchembku_client_context = ClientContext(xchembku_specification)
async with xchembku_client_context:
# Client to the dataface.
dataface = xchembku_datafaces_get_default()

# Wait for process is able to give a health report.
async with xchembku_client_context as xchembku_client:
# Wait until process is able to give a non-exceptional health report.
start_time = time.time()
max_seconds = 5.0
while True:
health = await dataface.client_report_health()
# Try to check the health.
health = await xchembku_client.client_report_health()

# Check if health report contains an exception.
exception = health.get("exception")
Expand Down Expand Up @@ -153,17 +149,17 @@ async def _main_coroutine(self, constants, output_directory):
)
)

await dataface.upsert_crystal_plates(models)
await xchembku_client.upsert_crystal_plates(models)

# Check the filtered queries.
await self.__check(
dataface,
xchembku_client,
CrystalPlateFilterModel(),
3,
"all",
)

await dataface.client_shutdown()
await xchembku_client.client_shutdown()
finally:
try:
# Wait for the process to finish and get the output.
Expand All @@ -177,13 +173,16 @@ async def _main_coroutine(self, constants, output_directory):
return_code = process.returncode
logger.debug(f"server return_code is {return_code}")

logger.debug(
f"================================== server stderr is:\n{stderr_bytes.decode()}"
)
logger.debug(
f"================================== server stdout is:\n{stdout_bytes.decode()}"
)
logger.debug("==================================")
if len(stderr_bytes) > 0:
logger.debug(
f"================================== server stderr is:\n{stderr_bytes.decode()}"
)
if len(stdout_bytes) > 0:
logger.debug(
f"================================== server stdout is:\n{stdout_bytes.decode()}"
)
if len(stderr_bytes) > 0 or len(stdout_bytes) > 0:
logger.debug("================================== end of server output")

assert return_code == 0

Expand Down
Loading

0 comments on commit 0419cb3

Please sign in to comment.