Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEST: Add repo tests (csv & sql) #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions rocketry/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from typing_extensions import Literal

from redbird.logging import RepoHandler
from redbird import BaseRepo
from rocketry._base import RedBase

if TYPE_CHECKING:
Expand Down Expand Up @@ -437,6 +438,19 @@ def get_task_loggers(self, with_adapters=True) -> Dict[str, Union['TaskAdapter',
}

# Log data
def set_repo(self, repo:BaseRepo, delete_existing=False):
handler = RepoHandler(repo=repo)
basename = self.config.task_logger_basename
logger = logging.getLogger(basename)

if delete_existing:
logger.handlers = [
hdlr
for hdlr in logger.handlers
if not hasattr(hdlr, "repo")
]
logger.handlers.insert(0, handler)

def get_task_log(self, *args, **kwargs) -> Iterable[Dict]:
"""Get task log records from all of the
readable handlers in the session.
Expand Down
64 changes: 62 additions & 2 deletions rocketry/test/session/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

from redbird.oper import in_, between
from redbird.logging import RepoHandler
from redbird.repos import MemoryRepo
from redbird.repos import MemoryRepo, SQLRepo, CSVFileRepo

from rocketry.log.log_record import LogRecord, TaskLogRecord, MinimalRecord
from rocketry.log import LogRecord, TaskLogRecord, MinimalRecord
from rocketry.pybox.time.convert import to_datetime
from rocketry.tasks import FuncTask

Expand All @@ -23,6 +23,26 @@ def create_line_to_shutdown():
with open("shut.txt", "w") as file:
file.write("line created\n")

def get_memory_repo():
return MemoryRepo(model=MinimalRecord)

def get_csv_repo():
repo = CSVFileRepo(model=MinimalRecord, filename="tasks.csv")
repo.create()
return repo

def get_sql_repo():
pytest.importorskip("sqlalchemy")
from sqlalchemy import create_engine
engine = create_engine('sqlite://')
engine.execute("""CREATE TABLE log (
id INTEGER PRIMARY KEY,
created FLOAT,
task_name TEXT,
action TEXT
)""")
return SQLRepo(model=MinimalRecord, table="log", engine=engine, id_field="id")

class CustomRecord(MinimalRecord):
timestamp: Optional[datetime.datetime]
start: Optional[datetime.datetime]
Expand All @@ -35,6 +55,46 @@ def validate_timestamp(cls, values):
values['timestamp'] = datetime.datetime.fromtimestamp(values['created'])
return values

@pytest.mark.parametrize("get_repo",
[
pytest.param(get_sql_repo, id="SQL"),
pytest.param(get_memory_repo, id="Memory"),
pytest.param(get_csv_repo, id="CSV"),
]
)
def test_repos(tmpdir, get_repo, mock_pydatetime, session):

with tmpdir.as_cwd() as old_dir:
repo = get_repo()
session.set_repo(repo, delete_existing=True)
task = FuncTask(lambda: None, name="a task", execution="main", force_run=True, session=session)

# Set log records
mock_pydatetime("2021-01-01 00:00:00.0001")
task.log_running()
mock_pydatetime("2021-01-01 00:00:00.0002")
task.log_success()

mock_pydatetime("2021-01-01 00:00:00.0003")
task.log_running()
try:
raise RuntimeError("Oops")
except:
mock_pydatetime("2021-01-01 00:00:00.0004")
task.log_failure()

records = repo.filter_by().all()
assert len(records) == 4
assert [
{"task_name": rec.task_name, "action": rec.action, "created": rec.created}
for rec in records
] == [
{"task_name": "a task", "action": "run", "created": 1609452000.0001},
{"task_name": "a task", "action": "success", "created": 1609452000.0002},
{"task_name": "a task", "action": "run", "created": 1609452000.0003},
{"task_name": "a task", "action": "fail", "created": 1609452000.0004},
]

@pytest.mark.parametrize(
"query,expected",
[
Expand Down