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

refactor/scoped session fix #151

Closed
wants to merge 45 commits into from
Closed

Conversation

kzidane
Copy link
Member

@kzidane kzidane commented Apr 10, 2021

Executing a SQL statement in SQL's constructor creates a db session that may not be cleanly removed in some Flask threads (e.g., when the Flask reloader is enabled). This causes a ProgrammingError exception to be raised when the Flask reloader kills the server process to reload the app:

...
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140118383687424 and this is thread id 140118412071552.

Instead of relying on flask.current_app which is only set when an app context is pushed, the SQL class now exposes init_app, which can be passed a Flask app instance to set up an teardown_appcontext listener to remove the session.

This PR also includes some refactoring to improve the readability, maintainability, and testability of the library and adds tests for the get_* functions. More tests to come.

@kzidane kzidane requested a review from dmalan April 10, 2021 00:28
Copy link
Member

@dmalan dmalan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to have the tests! Let's just comment code significantly more? Even with self-descriptive function names, much easier to onboard folks when the "why" behind blocks if code is spelled out explicitly, particularly since most of team only writes code part-time.


def _setup_logger():
# Configure default logging handler and formatter
# Prevent flask, werkzeug, etc from adding default handler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be docstring, no?

except IndexError:
pass

_logger = logging.getLogger("cs50")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not have uncommented blocks of code.

return paramstyle, placeholders


def _default_paramstyle(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring



def _bind_numeric(self, placeholders, tokens):
unused_arg_indices = set(range(len(self._args)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment

return token.ttype == sqlparse.tokens.Literal.String.Symbol


class _Paramstyle(enum.Enum):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an enum of the different paramstyle types that are supported so we don't have to hardcode "qmark", "numeric", etc and if we add or change types, their names are checked for (e.g., for misspellings).

if user_input is None:
return None

if re.search(r"^[+-]?\d+$", user_input):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment

@patch("cs50.cs50.get_string", return_value=None)
def test_get_int_eof(self, mock_get_string):
"""Returns None on EOF"""
self.assertIs(_get_int("Answer: "), None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this know to send EOF?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're just testing get_int here, we can assume that get_string is working correctly. In this particular test, we're assuming that get_string returns None on EOF, which we know should be the case. And so the patch decorator here replaces get_string with a mock implementation that just returns None. And the test is just making sure that get_int also returns None in this case.

@kzidane kzidane closed this Jul 26, 2021
@kzidane kzidane deleted the branch develop July 26, 2021 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants