Skip to content

Commit

Permalink
precommit cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zsailer committed Dec 6, 2021
1 parent fe3658d commit fb4f33c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
10 changes: 6 additions & 4 deletions jupyter_server/services/sessions/sessionmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import uuid
import pathlib
import uuid

try:
import sqlite3
Expand Down Expand Up @@ -32,7 +32,7 @@ class SessionManager(LoggingConfigurable):
"(e.g. /path/to/session_database.db). By default, the session "
"database is stored in-memory (i.e. `:memory:` setting from sqlite3) "
"and does not persist when the current Jupyter Server shuts down."
)
),
).tag(config=True)

@validate("database_filepath")
Expand All @@ -44,14 +44,16 @@ def _validate_database_filepath(self, proposal):
if path.exists():
# Verify that the database path is not a directory.
if path.is_dir():
raise TraitError("`database_filepath` expected a file path, but the given path is a directory.")
raise TraitError(
"`database_filepath` expected a file path, but the given path is a directory."
)
# If the file exists, but it's empty, its a valid entry.
if os.stat(path).st_size == 0:
return value
# Verify that database path is an SQLite 3 Database by checking its header.
with open(value, "rb") as f:
header = f.read(100)
if not header.startswith(b'SQLite format 3'):
if not header.startswith(b"SQLite format 3"):
raise TraitError("The file does not look like ")
return value

Expand Down
21 changes: 10 additions & 11 deletions jupyter_server/tests/services/sessions/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import pathlib
from tornado import web
from traitlets import TraitError

Expand Down Expand Up @@ -268,18 +267,17 @@ async def test_bad_delete_session(session_manager):
await session_manager.delete_session(session_id="23424") # nonexistent



async def test_bad_database_filepath(jp_runtime_dir):
kernel_manager = DummyMKM()

# Try to write to a path that's a directory, not a file.
path_id_directory = str(jp_runtime_dir)
# Should raise an error because the path is a directory.
with pytest.raises(TraitError) as err:
SessionManager(
SessionManager(
kernel_manager=kernel_manager,
contents_manager=ContentsManager(),
database_filepath=str(path_id_directory)
database_filepath=str(path_id_directory),
)

# Try writing to file that's not a valid SQLite 3 database file.
Expand All @@ -289,10 +287,10 @@ async def test_bad_database_filepath(jp_runtime_dir):
# Should raise an error because the file doesn't
# start with an SQLite database file header.
with pytest.raises(TraitError) as err:
SessionManager(
SessionManager(
kernel_manager=kernel_manager,
contents_manager=ContentsManager(),
database_filepath=str(non_db_file)
database_filepath=str(non_db_file),
)


Expand All @@ -306,7 +304,7 @@ async def test_good_database_filepath(jp_runtime_dir):
session_manager = SessionManager(
kernel_manager=kernel_manager,
contents_manager=ContentsManager(),
database_filepath=str(empty_file)
database_filepath=str(empty_file),
)

await session_manager.create_session(
Expand All @@ -322,11 +320,12 @@ async def test_good_database_filepath(jp_runtime_dir):
session_manager = SessionManager(
kernel_manager=kernel_manager,
contents_manager=ContentsManager(),
database_filepath=str(empty_file)
database_filepath=str(empty_file),
)

assert session_manager.database_filepath == str(empty_file)


async def test_session_persistence(jp_runtime_dir):
session_db_path = jp_runtime_dir.joinpath("test-session.db")
# Kernel manager needs to persist.
Expand All @@ -337,7 +336,7 @@ async def test_session_persistence(jp_runtime_dir):
session_manager = SessionManager(
kernel_manager=kernel_manager,
contents_manager=ContentsManager(),
database_filepath=str(session_db_path)
database_filepath=str(session_db_path),
)

session = await session_manager.create_session(
Expand All @@ -350,7 +349,7 @@ async def test_session_persistence(jp_runtime_dir):
with open(session_db_path, "rb") as f:
header = f.read(100)

assert header.startswith(b'SQLite format 3')
assert header.startswith(b"SQLite format 3")

# Close the current session manager
del session_manager
Expand All @@ -359,7 +358,7 @@ async def test_session_persistence(jp_runtime_dir):
session_manager = SessionManager(
kernel_manager=kernel_manager,
contents_manager=ContentsManager(),
database_filepath=str(session_db_path)
database_filepath=str(session_db_path),
)

# Assert that the session database persists.
Expand Down

0 comments on commit fb4f33c

Please sign in to comment.