Skip to content

Commit

Permalink
Added utils module, ref #14
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 14, 2021
1 parent b318e5f commit 34ec48e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions git_history/utils.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 34ec48e

Please sign in to comment.