Lee-W commented on code in PR #64972: URL: https://github.com/apache/airflow/pull/64972#discussion_r3167452733
########## scripts/tests/ci/prek/test_check_migration_patterns.py: ########## @@ -0,0 +1,473 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Unit tests for ``scripts/ci/prek/check_migration_patterns.py``.""" + +from __future__ import annotations + +import ast +import sys +from pathlib import Path + +import pytest +from ci.prek.check_migration_patterns import ( + MigrationFile, + _collect_ddl_table_names, + _extract_dml_table_name, + _get_noqa_codes, + _line_has_noqa, + check_mig001, + check_mig002, + check_mig003, + main, +) + + +def write_migration(tmp_path: Path, source: str) -> Path: + """Write *source* to a temp file that looks like a migration file.""" + p = tmp_path / "0099_test_migration.py" + p.write_text(source) + return p + + +def parse_migration(tmp_path: Path, source: str) -> MigrationFile: + """Write *source* to a temp file and return a parsed ``MigrationFile``.""" + return MigrationFile.from_path(write_migration(tmp_path, source)) + + +class TestGetNoqaCodes: + def test_single_code(self): + assert _get_noqa_codes("op.execute(...) # noqa: MIG001") == {"MIG001"} + + def test_multiple_codes(self): + assert _get_noqa_codes("op.execute(...) # noqa: MIG001, MIG003") == {"MIG001", "MIG003"} + + def test_code_with_reason_suffix(self): + # Reason after '--' should NOT be included in the code set. + codes = _get_noqa_codes("op.execute(...) # noqa: MIG003 -- safe in offline mode") + assert "MIG003" in codes + assert "--" not in "".join(codes) + assert "safe" not in "".join(codes) + + def test_no_noqa_comment(self): + assert _get_noqa_codes("op.execute('UPDATE foo SET x=1')") == set() + + def test_empty_string(self): + assert _get_noqa_codes("") == set() Review Comment: ```suggestion @pytest.mark.parametrize( "line, expected", [ pytest.param( "op.execute(...) # noqa: MIG001", {"MIG001"}, id="single-code", ), pytest.param( "op.execute(...) # noqa: MIG001, MIG003", {"MIG001", "MIG003"}, id="multiple-codes", ), pytest.param( "op.execute('UPDATE foo SET x=1')", set(), id="no-noqa-comment", ), pytest.param( "", set(), id="empty-string", ), ], ) def test_get_noqa_codes(self, line: str, expected: set[str]) -> None: assert _get_noqa_codes(line) == expected def test_code_with_reason_suffix(self) -> None: # Reason after '--' should NOT be included in the code set. codes = _get_noqa_codes("op.execute(...) # noqa: MIG003 -- safe in offline mode") assert "MIG003" in codes assert "--" not in "".join(codes) assert "safe" not in "".join(codes) ``` ########## scripts/tests/ci/prek/test_check_migration_patterns.py: ########## @@ -0,0 +1,473 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Unit tests for ``scripts/ci/prek/check_migration_patterns.py``.""" + +from __future__ import annotations + +import ast +import sys +from pathlib import Path + +import pytest +from ci.prek.check_migration_patterns import ( + MigrationFile, + _collect_ddl_table_names, + _extract_dml_table_name, + _get_noqa_codes, + _line_has_noqa, + check_mig001, + check_mig002, + check_mig003, + main, +) + + +def write_migration(tmp_path: Path, source: str) -> Path: + """Write *source* to a temp file that looks like a migration file.""" + p = tmp_path / "0099_test_migration.py" + p.write_text(source) + return p + + +def parse_migration(tmp_path: Path, source: str) -> MigrationFile: + """Write *source* to a temp file and return a parsed ``MigrationFile``.""" + return MigrationFile.from_path(write_migration(tmp_path, source)) + + +class TestGetNoqaCodes: + def test_single_code(self): + assert _get_noqa_codes("op.execute(...) # noqa: MIG001") == {"MIG001"} + + def test_multiple_codes(self): + assert _get_noqa_codes("op.execute(...) # noqa: MIG001, MIG003") == {"MIG001", "MIG003"} + + def test_code_with_reason_suffix(self): + # Reason after '--' should NOT be included in the code set. + codes = _get_noqa_codes("op.execute(...) # noqa: MIG003 -- safe in offline mode") + assert "MIG003" in codes + assert "--" not in "".join(codes) + assert "safe" not in "".join(codes) + + def test_no_noqa_comment(self): + assert _get_noqa_codes("op.execute('UPDATE foo SET x=1')") == set() + + def test_empty_string(self): + assert _get_noqa_codes("") == set() + + +class TestLineHasNoqa: + def test_line_has_matching_code(self): + lines = ["op.execute('UPDATE ...') # noqa: MIG001"] + assert _line_has_noqa(lines, 1, "MIG001") is True + + def test_line_has_different_code(self): + lines = ["op.execute('UPDATE ...') # noqa: MIG002"] + assert _line_has_noqa(lines, 1, "MIG001") is False + + @pytest.mark.parametrize("lineno", [0, 2], ids=["zero", "beyond_end"]) + def test_out_of_bounds(self, lineno): + assert _line_has_noqa(["some line"], lineno, "MIG001") is False Review Comment: same idea should be applied elsewhere as well ########## scripts/tests/ci/prek/test_check_migration_patterns.py: ########## @@ -0,0 +1,473 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Unit tests for ``scripts/ci/prek/check_migration_patterns.py``.""" + +from __future__ import annotations + +import ast +import sys +from pathlib import Path + +import pytest +from ci.prek.check_migration_patterns import ( + MigrationFile, + _collect_ddl_table_names, + _extract_dml_table_name, + _get_noqa_codes, + _line_has_noqa, + check_mig001, + check_mig002, + check_mig003, + main, +) + + +def write_migration(tmp_path: Path, source: str) -> Path: + """Write *source* to a temp file that looks like a migration file.""" + p = tmp_path / "0099_test_migration.py" + p.write_text(source) + return p + + +def parse_migration(tmp_path: Path, source: str) -> MigrationFile: + """Write *source* to a temp file and return a parsed ``MigrationFile``.""" + return MigrationFile.from_path(write_migration(tmp_path, source)) + + +class TestGetNoqaCodes: + def test_single_code(self): + assert _get_noqa_codes("op.execute(...) # noqa: MIG001") == {"MIG001"} + + def test_multiple_codes(self): + assert _get_noqa_codes("op.execute(...) # noqa: MIG001, MIG003") == {"MIG001", "MIG003"} + + def test_code_with_reason_suffix(self): + # Reason after '--' should NOT be included in the code set. + codes = _get_noqa_codes("op.execute(...) # noqa: MIG003 -- safe in offline mode") + assert "MIG003" in codes + assert "--" not in "".join(codes) + assert "safe" not in "".join(codes) + + def test_no_noqa_comment(self): + assert _get_noqa_codes("op.execute('UPDATE foo SET x=1')") == set() + + def test_empty_string(self): + assert _get_noqa_codes("") == set() + + +class TestLineHasNoqa: + def test_line_has_matching_code(self): + lines = ["op.execute('UPDATE ...') # noqa: MIG001"] + assert _line_has_noqa(lines, 1, "MIG001") is True + + def test_line_has_different_code(self): + lines = ["op.execute('UPDATE ...') # noqa: MIG002"] + assert _line_has_noqa(lines, 1, "MIG001") is False + + @pytest.mark.parametrize("lineno", [0, 2], ids=["zero", "beyond_end"]) + def test_out_of_bounds(self, lineno): + assert _line_has_noqa(["some line"], lineno, "MIG001") is False Review Comment: ```suggestion @pytest.mark.parametrize( "lines, lineno, code, expected", [ pytest.param( ["op.execute('UPDATE ...') # noqa: MIG001"], 1, "MIG001", True, id="matching-code", ), pytest.param( ["op.execute('UPDATE ...') # noqa: MIG002"], 1, "MIG001", False, id="different-code", ), pytest.param(["some line"], 0, "MIG001", False, id="out-of-bounds-zero"), pytest.param(["some line"], 2, "MIG001", False, id="out-of-bounds-beyond-end"), ], ) def test_line_has_noqa(lines: list[str], lineno: int, code: str, expected: bool) -> None: assert _line_has_noqa(lines, lineno, code) is expected ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
