diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..2af0e23 --- /dev/null +++ b/.github/workflows/python-app.yml @@ -0,0 +1,32 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python application + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements-test.txt + - name: Test with pytest + run: | + pytest diff --git a/database_sanitizer/tests/test_dump_mysql.py b/database_sanitizer/tests/test_dump_mysql.py index 071e79b..5db7b63 100644 --- a/database_sanitizer/tests/test_dump_mysql.py +++ b/database_sanitizer/tests/test_dump_mysql.py @@ -35,6 +35,26 @@ --- Final line after `INSERT INTO` statement. """ +MOCK_MYSQLDUMP_OUTPUT_WITH_NEGINT = b""" +--- Fake MySQL database dump + +DROP TABLE IF EXISTS `test`; + +CREATE TABLE `test` ( +`id` int(11) NOT NULL AUTO_INCREMENT, +`created_at` date NOT NULL, +`notes` varchar(255) NOT NULL, +PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +INSERT INTO `test` (`id`, `created_at`, `notes`) VALUES \ +(-1,'2018-01-01','Test data 1'),\ +(2,'2018-01-02','Test data 2'),\ +(3,'2018-01-03','Test data 3'); + +--- Final line after `INSERT INTO` statement. +""" + MOCK_MYSQLDUMP_OUTPUT_WITH_U2028 = b""" --- Fake MySQL database dump @@ -87,6 +107,20 @@ def test_sanitize_from_stream(): (3,'2018-01-03','Sanitized');\ """ in dump_output_lines +def test_sanitize_with_negint_from_stream(): + stream = io.BytesIO(MOCK_MYSQLDUMP_OUTPUT_WITH_NEGINT) + config = Configuration() + config.sanitizers["test.notes"] = lambda value: "Sanitized" + dump_output_lines = list(sanitize_from_stream(stream, config)) + + assert "--- Fake MySQL database dump" in dump_output_lines + assert "--- Final line after `INSERT INTO` statement." in dump_output_lines + assert """INSERT INTO `test` (`id`, `created_at`, `notes`) VALUES \ +(-1,'2018-01-01','Sanitized'),\ +(2,'2018-01-02','Sanitized'),\ +(3,'2018-01-03','Sanitized');\ +""" in dump_output_lines + def test_sanitize_with_u2028_from_stream(): stream = io.BytesIO(MOCK_MYSQLDUMP_OUTPUT_WITH_U2028) config = Configuration() diff --git a/database_sanitizer/utils/mysql.py b/database_sanitizer/utils/mysql.py index 794d5e7..31ba151 100644 --- a/database_sanitizer/utils/mysql.py +++ b/database_sanitizer/utils/mysql.py @@ -64,7 +64,7 @@ def get_mysqldump_args_and_env_from_url(url): MYSQL_NULL_PATTERN = re.compile(r"^NULL$", re.IGNORECASE) MYSQL_BOOLEAN_PATTERN = re.compile(r"^(TRUE|FALSE)$", re.IGNORECASE) MYSQL_FLOAT_PATTERN = re.compile(r"^[+-]?\d*\.\d+([eE][+-]?\d+)?$") -MYSQL_INT_PATTERN = re.compile(r"^\d+$") +MYSQL_INT_PATTERN = re.compile(r"^[+-]?\d+$") MYSQL_STRING_PATTERN = re.compile(r"'(?:[^']|''|\\')*(?