Testing Commands in IPython

The great thing about using the below to verify that a command has been entered in an IPython shell is that unlike with a normal Python shell where commands are persisted in history only when the shell is exited, you can check that the user has entered their command in real time without them having to close their shell!
To get started, make sure that you've selected the Python or Data Science stack.
Then, you'll need to install SQLite3 in your content. You can accomplish this by adding the following to your startup script:
sudo apt install -y sqlite3
Now, in your content, create a Terminal tab and click the blue sliders to change the start command to ipython3:
Be sure to save your tabs by clicking the Save button in the top right of the screen. You may also want to select the one pane-layout, if your project will only have this terminal.
Next up, create a Custom Test with the following parameters:
(you pick!)
bash {{test}}
Test Contents
In the code below, we use SQLite to connect to IPython's history database and a simple SELECT query to see if the expected command has been entered. In this case, we are looking for 1 + 2, but you can replace this string with anything you'd like!
You can use the ILIKE keyword instead of LIKE to do a case insensitive search. You can also use % as a wildcard character in your search string. Be sure to keep your final search string in single quotes!
PASSED=$(sqlite3 /home/nt-user/.ipython/profile_default/history.sqlite "SELECT 'PASSED' FROM history WHERE source_raw LIKE '1 + 2';")
[[ "$PASSED" != "PASSED" ]] && { echo "FAILED" ; exit 1; }
exit 0
Failed Pattern
Require Success
You can then create a task and link this check to it. That's it!
If you've run a command in IPython once, it'll stay in the history database for the duration of that session, which can cause false positives (the check passing when it shouldn't).
To reset the history file, simply run this command from the terminal:
sqlite3 /home/nt-user/.ipython/profile_default/history.sqlite "DELETE FROM history;"