diff --git a/git_history/utils.py b/git_history/utils.py new file mode 100644 index 0000000..29df477 --- /dev/null +++ b/git_history/utils.py @@ -0,0 +1,19 @@ +import re + +RESERVED = ("_id", "_item", "_version", "_commit", "rowid") +reserved_with_suffix_re = re.compile("^({})_*$".format("|".join(RESERVED))) + + +def fix_reserved_columns(item): + if not any(reserved_with_suffix_re.match(key) for key in item): + return item + + return {_fix_key(key): item[key] for key in item} + + +def _fix_key(key): + # Add a trailing _ if it's reserved or reserved with _ suffix + if reserved_with_suffix_re.match(key): + return key + "_" + else: + return key diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..5148c8c --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,27 @@ +from git_history.utils import fix_reserved_columns +import pytest + + +@pytest.mark.parametrize( + "column,expected", + ( + ("_id", "_id_"), + ("_item", "_item_"), + ("_version", "_version_"), + ("_commit", "_commit_"), + ("rowid", "rowid_"), + ("rowid_", "rowid__"), + ("_id__", "_id___"), + ), +) +def test_fix_reserved_columns(column, expected): + item = {column: 1} + fixed = fix_reserved_columns(item) + assert fixed == {expected: 1} + assert item is not fixed + + +def test_fix_reserved_columns_unchanged_if_no_reserved(): + item = {"id": 1, "version": "v2"} + fixed = fix_reserved_columns(item) + assert item is fixed