From e49feed7355327130841ab0fb29b8295090c0cfc Mon Sep 17 00:00:00 2001 From: cyyc1 <114281716+cyyc1@users.noreply.github.com> Date: Wed, 5 Oct 2022 20:43:14 -0700 Subject: [PATCH] Add more test cases --- ...after.ipynb => jupyter_case_1_after.ipynb} | 0 ...fore.ipynb => jupyter_case_1_before.ipynb} | 0 testing/resources/jupyter_case_2.ipynb | 159 ++++++++++++++++++ ...string_fixer_for_jupyter_notebooks_test.py | 17 +- 4 files changed, 172 insertions(+), 4 deletions(-) rename testing/resources/{after.ipynb => jupyter_case_1_after.ipynb} (100%) rename testing/resources/{before.ipynb => jupyter_case_1_before.ipynb} (100%) create mode 100644 testing/resources/jupyter_case_2.ipynb diff --git a/testing/resources/after.ipynb b/testing/resources/jupyter_case_1_after.ipynb similarity index 100% rename from testing/resources/after.ipynb rename to testing/resources/jupyter_case_1_after.ipynb diff --git a/testing/resources/before.ipynb b/testing/resources/jupyter_case_1_before.ipynb similarity index 100% rename from testing/resources/before.ipynb rename to testing/resources/jupyter_case_1_before.ipynb diff --git a/testing/resources/jupyter_case_2.ipynb b/testing/resources/jupyter_case_2.ipynb new file mode 100644 index 0000000..5ef9206 --- /dev/null +++ b/testing/resources/jupyter_case_2.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "1a4e6df0", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import List, Dict" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d72a909f", + "metadata": {}, + "outputs": [], + "source": [ + "def some_function(arg1: List[str], arg2: Dict[str, float]):\n", + " print(arg1)\n", + " return arg2" + ] + }, + { + "cell_type": "markdown", + "id": "0040a4f4", + "metadata": {}, + "source": [ + "## Section 1" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3a1b985c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b']\n" + ] + } + ], + "source": [ + "a = some_function(['a', 'b'], {'c': 0.2})" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2dfd7d0b", + "metadata": {}, + "outputs": [], + "source": [ + "assert 3 == 3" + ] + }, + { + "cell_type": "markdown", + "id": "de5a89c9", + "metadata": {}, + "source": [ + "Here is a math formulas:\n", + "\n", + "$$c = \\sqrt{a^2 + b^2}$$" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "4952b6de", + "metadata": {}, + "outputs": [], + "source": [ + "def some_other_function() -> bool:\n", + " \"\"\"\n", + " Some docstring\n", + " \"\"\"\n", + " return False" + ] + }, + { + "cell_type": "markdown", + "id": "bf1cb851", + "metadata": {}, + "source": [ + "There can be double quotes in non-code cells, like this: \"this\". These should not be changed to single quotes." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "01224536", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print(some_other_function())" + ] + }, + { + "cell_type": "markdown", + "id": "b54d0660", + "metadata": {}, + "source": [ + "There can also be double quotes in the cell output. Again, they should not be changed to single quotes." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "dc0edb11", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "abcd\"efg\".\n" + ] + } + ], + "source": [ + "print('abcd\"efg\".')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests/string_fixer_for_jupyter_notebooks_test.py b/tests/string_fixer_for_jupyter_notebooks_test.py index 956b71b..f86fec9 100644 --- a/tests/string_fixer_for_jupyter_notebooks_test.py +++ b/tests/string_fixer_for_jupyter_notebooks_test.py @@ -1,16 +1,25 @@ +import pytest + from pre_commit_hooks.string_fixer_for_jupyter_notebooks import main from testing.util import get_resource_path -def test_rewrite(tmpdir): - with open(get_resource_path('before.ipynb')) as f: +TESTS = ( + ('jupyter_case_1_before.ipynb', 'jupyter_case_1_after.ipynb', 1), + # the file in Case 2 is not altered, so we reload the same file + ('jupyter_case_2.ipynb', 'jupyter_case_2.ipynb', 0), +) + +@pytest.mark.parametrize(('input_file', 'output_file', 'expected_retv'), TESTS) +def test_rewrite(input_file, output_file, expected_retv, tmpdir): + with open(get_resource_path(input_file)) as f: before_contents = f.read() - with open(get_resource_path('after.ipynb')) as f: + with open(get_resource_path(output_file)) as f: after_contents = f.read() path = tmpdir.join('file.ipynb') path.write(before_contents) retval = main([str(path)]) assert path.read() == after_contents - assert retval == 1 + assert retval == expected_retv