Skip to content

Commit

Permalink
Read logging output in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Navarro-Jonathan committed Nov 10, 2023
1 parent 2b8460e commit 25d69d1
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 154 deletions.
7 changes: 4 additions & 3 deletions src/aerie_cli/commands/scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ def delete_all_goals_for_plan(
logging.info("No goals to delete.")
return

logging.info("Deleting goals for Plan ID {plan}: ".format(plan=plan_id), nl=False)
to_print = "Deleting goals for Plan ID {plan}: ".format(plan=plan_id)
goal_ids = []
for goal in clear_goals:
goal_ids.append(goal["goal"]["id"])
logging.info(str(goal["goal"]["id"]) + " ", nl=False)

to_print += str(goal["goal"]["id"]) + " "
logging.info(to_print)

client.delete_scheduling_goals(goal_ids)
4 changes: 4 additions & 0 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# The scope is global either way, but this allows for easier debugging, type hints, and autofill
import os
import sys
import logging

from aerie_cli.aerie_client import AerieClient
from aerie_cli.aerie_host import AerieHost, AerieHostConfiguration
Expand All @@ -17,6 +18,9 @@
start_session_from_configuration,
)

GLOBAL_LOGGER = logging.getLogger(__name__)
GLOBAL_LOGGER.propagate = True # we must set this to see stdout in caplog

# in case src_path is not from aeri-cli src and from site-packages
src_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/../src")
sys.path.insert(0, src_path)
Expand Down
52 changes: 30 additions & 22 deletions tests/integration_tests/test_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from aerie_cli.__main__ import app
import pytest
import logging

from .conftest import\
HASURA_ADMIN_SECRET,\
Expand All @@ -26,20 +27,21 @@
CONFIGURATION_NAME = "localhost"
configuration_id = -1

def test_configurations_clean():
def test_configurations_clean(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(
app,
["configurations", "clean"],
input="y" + "\n",
catch_exceptions=False,
)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"

assert len(PersistentConfigurationManager.get_configurations()) == 0,\
f"CONFIGURATIONS NOT CLEARED! CONFIGURATIONS: {PersistentConfigurationManager.get_configurations()}\n"\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"

no_session_error = False
Expand Down Expand Up @@ -71,7 +73,8 @@ def test_configurations_clean():
pytest.exit("CONFIGURATION SHOULD NOT EXIST. Failed when using active configuration\n",
returncode=pytest.ExitCode.TESTS_FAILED)

def test_configurations_create():
def test_configurations_create(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(
app,
["configurations", "create"],
Expand All @@ -84,7 +87,7 @@ def test_configurations_create():
)

assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"

global configuration_id
Expand All @@ -94,7 +97,8 @@ def test_configurations_create():
configuration_id = i
assert configuration_id != -1, "CONFIGURATION NOT LOADED, is it's name localhost?"

def test_activate():
def test_activate(caplog):
caplog.set_level(logging.INFO)
before_refresh = len(PersistentConfigurationManager.get_configurations())
assert before_refresh > 0
PersistentConfigurationManager.read_configurations()
Expand All @@ -108,18 +112,18 @@ def test_activate():
)

assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert PersistentSessionManager.get_active_session().configuration_name == "localhost"

def test_deactivate():
def test_deactivate(caplog):
caplog.set_level(logging.INFO)
before_refresh = len(PersistentConfigurationManager.get_configurations())
assert before_refresh > 0
PersistentConfigurationManager.read_configurations()
assert len(PersistentConfigurationManager.get_configurations()) == before_refresh

assert PersistentSessionManager.get_active_session().configuration_name == "localhost"

result = runner.invoke(
app,
["deactivate"],
Expand All @@ -128,11 +132,11 @@ def test_deactivate():
)

assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert (
f"Deactivated session: {CONFIGURATION_NAME}"
in result.stdout
in caplog.text
)

try:
Expand All @@ -156,7 +160,8 @@ def test_deactivate():
f"CONFIGURATION SHOULD NOT BE ACTIVE. Active config: {active_config}",
returncode=pytest.ExitCode.TESTS_FAILED)

def test_configurations_delete():
def test_configurations_delete(caplog):
caplog.set_level(logging.INFO)
before_refresh = len(PersistentConfigurationManager.get_configurations())
assert before_refresh > 0
PersistentConfigurationManager.read_configurations()
Expand All @@ -177,10 +182,11 @@ def test_configurations_delete():
)

assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"

def test_configurations_load():
def test_configurations_load(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(
app,
["configurations", "load"],
Expand All @@ -189,11 +195,11 @@ def test_configurations_load():
)

assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert (
"Added configurations"
in result.stdout
in caplog.text
)

# def test_configurations_update():
Expand All @@ -214,26 +220,28 @@ def test_configurations_load():
# )

# assert result.exit_code == 0,\
# f"{result.stdout}"\
# f"{caplog.text}"\
# f"{result.stderr}"

def test_configurations_list():
def test_configurations_list(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(
app,
["configurations", "list"],
catch_exceptions=False,
)

assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert (
"Aerie Host Configurations"
in result.stdout
in result.stdout # we use Console.print for this, not log
)
# We're activating at the end to ensure that localhost is still active
# for other integration tests.
def test_last_activate():
def test_last_activate(caplog):
caplog.set_level(logging.INFO)
before_refresh = len(PersistentConfigurationManager.get_configurations())
assert before_refresh > 0
PersistentConfigurationManager.read_configurations()
Expand All @@ -249,6 +257,6 @@ def test_last_activate():
)

assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert PersistentSessionManager.get_active_session().configuration_name == "localhost"
33 changes: 19 additions & 14 deletions tests/integration_tests/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import pytest
import arrow
import logging

runner = CliRunner(mix_stderr = False)

Expand Down Expand Up @@ -50,50 +51,54 @@ def set_up_environment(request):
plan_id = client.create_activity_plan(model_id, plan_to_create)
client.simulate_plan(plan_id)

def test_constraint_upload():
def test_constraint_upload(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(app, ["constraints", "upload"],
input="Test" + "\n" + CONSTRAINT_PATH + "\n" + str(model_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert "Created constraint" in result.stdout
assert "Created constraint" in caplog.text
global constraint_id
for line in result.stdout.splitlines():
for line in caplog.text.splitlines():
if not "Created constraint: " in line:
continue
# get constraint id from the end of the line
constraint_id = int(line.split(": ")[1])
assert constraint_id != -1, "Could not find constraint ID, constraint upload may have failed"\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"

def test_constraint_update():
def test_constraint_update(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(app, ["constraints", "update"],
input=str(constraint_id) + "\n" + CONSTRAINT_PATH + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert "Updated constraint" in result.stdout
assert "Updated constraint" in caplog.text

def test_constraint_violations():
def test_constraint_violations(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(app, ["constraints", "violations"],
input=str(plan_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"

# Check that a constraint violation is returned with the open bracket and curly brace
# (The integration test constraint should report a violation)
assert "Constraint violations: [{" in result.stdout
assert "Constraint violations: [{" in caplog.text

def test_constraint_delete():
def test_constraint_delete(caplog):
caplog.set_level(logging.INFO)
result = runner.invoke(app, ["constraints", "delete"],
input=str(constraint_id) + "\n",
catch_exceptions=False,)
assert result.exit_code == 0,\
f"{result.stdout}"\
f"{caplog.text}"\
f"{result.stderr}"
assert f"Successfully deleted constraint {str(constraint_id)}" in result.stdout
assert f"Successfully deleted constraint {str(constraint_id)}" in caplog.text
Loading

0 comments on commit 25d69d1

Please sign in to comment.